Class: MiqVimDataStore

Inherits:
Object
  • Object
show all
Includes:
DMiqVimSync, DRb::DRbUndumped, MiqBrokerObjRegistry, MiqBrokerVimConnectionCheck
Defined in:
lib/VMwareWebService/MiqVimDataStore.rb,
lib/VMwareWebService/MiqVimBrokerMods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MiqBrokerVimConnectionCheck

#connectionRemoved?

Methods included from MiqBrokerObjRegistry

#holdBrokerObj, #registerBrokerObj, #unregisterBrokerObj

Constructor Details

#initialize(invObj, dsh) ⇒ MiqVimDataStore

Returns a new instance of MiqVimDataStore.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 7

def initialize(invObj, dsh)
  @invObj                 = invObj
  @sic                    = invObj.sic
  @dsh                    = dsh

  @accessible             = (@dsh['summary']['accessible'] == 'true')
  @multipleHostAccess     = (@dsh['summary']['multipleHostAccess'] == 'true')
  @name                   = @dsh['summary']['name']
  @dsType                 = @dsh['summary']['type']
  @url                    = @dsh['summary']['url']
  @dsMor                  = @dsh['summary']['datastore']
  @freeBytes              = @dsh['summary']['freeSpace'].to_i
  @capacityBytes          = @dsh['summary']['capacity'].to_i
  @uncommitted      = @dsh['summary']['uncommitted'].to_i if @invObj.apiVersion[0, 1].to_i <= 4 && @dsh['summary']['uncommitted']

  @browser                = nil
  @dsHash                 = nil

  @cacheLock              = Sync.new
end

Instance Attribute Details

#accessibleObject (readonly)

Returns the value of attribute accessible.



5
6
7
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 5

def accessible
  @accessible
end

#capacityBytesObject (readonly)

Returns the value of attribute capacityBytes.



5
6
7
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 5

def capacityBytes
  @capacityBytes
end

#dsTypeObject (readonly)

Returns the value of attribute dsType.



5
6
7
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 5

def dsType
  @dsType
end

#freeBytesObject (readonly)

Returns the value of attribute freeBytes.



5
6
7
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 5

def freeBytes
  @freeBytes
end

#invObjObject (readonly)

Returns the value of attribute invObj.



5
6
7
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 5

def invObj
  @invObj
end

#multipleHostAccessObject (readonly)

Returns the value of attribute multipleHostAccess.



5
6
7
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 5

def multipleHostAccess
  @multipleHostAccess
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 5

def name
  @name
end

#uncommittedObject (readonly)

Returns the value of attribute uncommitted.



5
6
7
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 5

def uncommitted
  @uncommitted
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 5

def url
  @url
end

Instance Method Details

#browserObject

Public accessor



63
64
65
66
67
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 63

def browser
  @cacheLock.synchronize(:SH) do
    return(browser_locked)
  end
end

#browser_lockedObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 45

def browser_locked
  raise "browser_locked: cache lock not held" unless @cacheLock.sync_locked?
  return @browser if @browser

  begin
    @cacheLock.sync_lock(:EX) if (unlock = @cacheLock.sync_shared?)

    props = @invObj.getMoProp_local(@dsMor, "browser")
    @browser = props["browser"] if props
  ensure
    @cacheLock.sync_unlock if unlock
  end
  @browser
end

#dsFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



101
102
103
104
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 101

def dsFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true)
  return(dsSearch(nil, pattern, path, pathOnly, recurse)) if @invObj.v20
  (dsSearch('FileQuery', pattern, path, pathOnly, recurse))
end

#dsFloppyImageFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



69
70
71
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 69

def dsFloppyImageFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true)
  dsSearch('FloppyImageFileQuery', pattern, path, pathOnly, recurse)
end

#dsFolderFileList(path = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 168

def dsFolderFileList(path = nil)
  dirs = dsFolderFileSearch(nil, path, true, false).inject({}) { |h, d| h[d] = nil; h }

  children = VimArray.new
  ret = dsFileSearch(nil, path, false, false)

  ret.each do |f|
    full_path = f['fullPath']
    if dirs.key?(full_path)
      f['fileType'] = 'FolderFileInfo'
      full_path =~ /\] (.+)$/
      children.concat(dsFolderFileList($1))
    end
  end

  ret.concat(children)
end

#dsFolderFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



73
74
75
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 73

def dsFolderFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true)
  dsSearch('FolderFileQuery', pattern, path, pathOnly, recurse)
end

#dshObject



41
42
43
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 41

def dsh
  (@dsh)
end

#dsHash(refresh = false) ⇒ Object

Public accessor



259
260
261
262
263
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 259

def dsHash(refresh = false)
  @cacheLock.synchronize(:SH) do
    return(dupObj(dsHash_locked(refresh)))
  end
end

#dsHash_locked(refresh = false) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 186

def dsHash_locked(refresh = false)
  raise "dsHash_locked: cache lock not held" unless @cacheLock.sync_locked?
  return(@dsHash) if @dsHash && !refresh

  begin
    @cacheLock.sync_lock(:EX) if (unlock = @cacheLock.sync_shared?)

    searchSpec = VimHash.new("HostDatastoreBrowserSearchSpec") do |hdbs|
      hdbs.details = VimHash.new("FileQueryFlags") do |fqf|
        fqf.fileSize    = "true"
        fqf.fileType    = "true"
        fqf.modification  = "true"
        fqf.fileOwner   = "true"
      end
      hdbs.sortFoldersFirst = "true"
    end

    $vim_log.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsHash_locked: calling searchDatastoreSubFolders_Task" if $vim_log
    taskMor = @invObj.searchDatastoreSubFolders_Task(browser_locked, "[#{@name}]", searchSpec)
    $vim_log.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsHash_locked: returned from searchDatastoreSubFolders_Task" if $vim_log

    retObj = waitForTask(taskMor)
    retObj = [retObj] unless retObj.kind_of?(Array)

    @dsHash = {}

    retObj.each do |dsbr|
      # puts "dsbr.class: #{dsbr.class.to_s}"
      # @invObj.dumpObj(dsbr)
      # puts "****** dsbr end"
      # puts "Folder Path: #{dsbr.folderPath}"
      dsl = (dsbr["folderPath"][-1, 1] == ']')

      if dsl
        rInfo = {}
        rInfo['fileType'] = "FolderFileInfo"
        @dsHash[dsbr["folderPath"]] = rInfo
      else
        dsbr["folderPath"] = dsbr["folderPath"][0..-2] if dsbr["folderPath"][-1, 1] == "/"
      end

      raise "[BUG] Parent directory '#{dsbr["folderPath"]}' not defined" unless (parentDir = @dsHash[dsbr["folderPath"]])
      parentDir['dirEntries'] = [] unless parentDir['dirEntries']
      dirEntries = parentDir['dirEntries']

      dsbr["file"] = dsbr["file"] || []
      dsbr["file"] = [dsbr["file"]] unless dsbr["file"].kind_of? Array

      dsbr["file"].each do |fInfo|
        # puts "***** fInfo.class: #{fInfo.class.to_s}"
        fInfoHash = fInfo
        # fInfoHash = @invObj.unMarshalSoapMappingObject(fInfo)
        fInfoHash['fileType'] = fInfoHash.xsiType
        # puts "\tType: #{fInfoHash['xmlattr_type']}, Path: #{fInfoHash["path"]}"
        if dsl
          path = dsbr["folderPath"] + " " + fInfoHash["path"]
        else
          path = File.join(dsbr["folderPath"], fInfoHash["path"])
        end
        fInfoHash['dirEntries'] = [] if fInfoHash['fileType'] == "FolderFileInfo" && !fInfoHash['dirEntries']
        dirEntries << fInfoHash["path"]
        @dsHash[path] = fInfoHash
      end
    end
  ensure
    @cacheLock.sync_unlock if unlock
  end
  @dsHash
end

#dsIsoImageFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



77
78
79
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 77

def dsIsoImageFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true)
  dsSearch('IsoImageFileQuery', pattern, path, pathOnly, recurse)
end

#dsMorObject



37
38
39
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 37

def dsMor
  (@dsMor)
end

#dsSearch(typeClass, pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 106

def dsSearch(typeClass, pattern = nil, path = nil, pathOnly = true, recurse = true)
  searchSpec = VimHash.new("HostDatastoreBrowserSearchSpec") do |hdbs|
    hdbs.details = VimHash.new("FileQueryFlags") do |fqf|
      fqf.fileSize    = "true"
      fqf.fileType    = "true"
      fqf.modification  = "true"
      fqf.fileOwner   = "true"
    end
    hdbs.query = VimArray.new("ArrayOfFileQuery") do |fqa|
      fqa << VimHash.new(typeClass)
    end if typeClass
    hdbs.matchPattern = pattern if pattern
    hdbs.sortFoldersFirst = "true"
  end

  browserMor = nil
  @cacheLock.synchronize(:SH) do
    browserMor = browser_locked
  end
  dsPath = "[#{@name}]"
  dsPath = "#{dsPath} #{path}" if path

  taskMor = nil
  if recurse
    $vim_log.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsSearch: calling searchDatastoreSubFolders_Task" if $vim_log
    taskMor = @invObj.searchDatastoreSubFolders_Task(browserMor, dsPath, searchSpec)
    $vim_log.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsSearch: returned from searchDatastoreSubFolders_Task" if $vim_log
  else
    $vim_log.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsSearch: calling searchDatastore_Task" if $vim_log
    taskMor = @invObj.searchDatastore_Task(browserMor, dsPath, searchSpec)
    $vim_log.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsSearch: returned from searchDatastore_Task" if $vim_log
  end

  retObj = waitForTask(taskMor)
  retObj = [retObj] unless retObj.kind_of?(Array)
  # @invObj.dumpObj(retObj)

  ra = VimArray.new
  retObj.each do |dsbr|
    dsl = (dsbr["folderPath"][-1, 1] == ']')

    dsbr["file"] = dsbr["file"] || []
    # dsbr["file"] = [dsbr["file"]] if !dsbr["file"].kind_of? Array

    dsbr["file"].each do |fInfo|
      path = nil
      if dsl
        path = dsbr["folderPath"] + " " + fInfo["path"]
      else
        path = File.join(dsbr["folderPath"], fInfo["path"])
      end
      fInfo["fullPath"] = path
      if pathOnly
        ra << path
      else
        ra << fInfo
      end
    end
  end
  (ra)
end

#dsVmConfigFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



97
98
99
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 97

def dsVmConfigFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true)
  dsSearch('VmConfigFileQuery', pattern, path, pathOnly, recurse)
end

#dsVmDiskFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



81
82
83
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 81

def dsVmDiskFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true)
  dsSearch('VmDiskFileQuery', pattern, path, pathOnly, recurse)
end

#dsVmLogFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



85
86
87
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 85

def dsVmLogFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true)
  dsSearch('VmLogFileQuery', pattern, path, pathOnly, recurse)
end

#dsVmNvramFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



89
90
91
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 89

def dsVmNvramFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true)
  dsSearch('VmNvramFileQuery', pattern, path, pathOnly, recurse)
end

#dsVmSnapshotFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true) ⇒ Object



93
94
95
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 93

def dsVmSnapshotFileSearch(pattern = nil, path = nil, pathOnly = true, recurse = true)
  dsSearch('VmSnapshotFileQuery', pattern, path, pathOnly, recurse)
end

#dumpPropsObject



266
267
268
269
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 266

def dumpProps
  props = @invObj.getMoProp_local(@dsMor, "browser")["browser"]
  @invObj.dumpObj(props)
end

#dupObj(obj) ⇒ Object



299
300
301
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 299

def dupObj(obj)
  obj
end

#get_file_content(filepath) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 275

def get_file_content(filepath)
  # Sample URL required to read file
  # agentURI = "https://192.168.254.247/folder/BH_9336_1/BH_9336_1.vmsd?dcPath=ha-datacenter&dsName=DCRaid2"
  # Note: dcPath is only required if going through vCenter.
  raise "get_file_content not supported through Virtual Center" if @invObj.isVirtualCenter?
  /(\d*)\.(\d*)/ =~ @invObj.about['version']
  raise "get_file_content not supported on [#{@invObj.about['fullName']}]" if $1.to_i < 3 || ($1.to_i == 3 && $2.to_i < 5)

  fileUrl = encode_datastore_url(filepath, @name)

  options = {
    :http_basic_authentication => [@invObj.username, @invObj.password],
    :ssl_verify_mode           => OpenSSL::SSL::VERIFY_NONE
  }

  if block_given?
    open(fileUrl, options) { |ret| yield(ret) }
  else
    data = nil
    open(fileUrl, options) { |ret| data = ret.read }
    return data
  end
end

#releaseObject Also known as: release_orig

Called when client is finished using this MiqVimVm object. The server will delete its reference to the object, so the server-side object csn be GC’d



33
34
35
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 33

def release
  # @invObj.releaseObj(self)
end

#waitForTask(tmor) ⇒ Object



271
272
273
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 271

def waitForTask(tmor)
  @invObj.waitForTask(tmor, self.class.to_s)
end