Class: MiqVimDataStore

Inherits:
Object
  • Object
show all
Includes:
VMwareWebService::Logging
Defined in:
lib/VMwareWebService/MiqVimDataStore.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VMwareWebService::Logging

#logger

Constructor Details

#initialize(invObj, dsh) ⇒ MiqVimDataStore

Returns a new instance of MiqVimDataStore.



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

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.



8
9
10
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 8

def accessible
  @accessible
end

#capacityBytesObject (readonly)

Returns the value of attribute capacityBytes.



8
9
10
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 8

def capacityBytes
  @capacityBytes
end

#dsTypeObject (readonly)

Returns the value of attribute dsType.



8
9
10
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 8

def dsType
  @dsType
end

#freeBytesObject (readonly)

Returns the value of attribute freeBytes.



8
9
10
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 8

def freeBytes
  @freeBytes
end

#invObjObject (readonly)

Returns the value of attribute invObj.



8
9
10
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 8

def invObj
  @invObj
end

#multipleHostAccessObject (readonly)

Returns the value of attribute multipleHostAccess.



8
9
10
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 8

def multipleHostAccess
  @multipleHostAccess
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 8

def name
  @name
end

#uncommittedObject (readonly)

Returns the value of attribute uncommitted.



8
9
10
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 8

def uncommitted
  @uncommitted
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 8

def url
  @url
end

Instance Method Details

#browserObject

Public accessor



66
67
68
69
70
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 66

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

#browser_lockedObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 48

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



104
105
106
107
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 104

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



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

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

#dsFolderFileList(path = nil) ⇒ Object



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

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



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

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

#dshObject



44
45
46
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 44

def dsh
  (@dsh)
end

#dsHash(refresh = false) ⇒ Object

Public accessor



262
263
264
265
266
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 262

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

#dsHash_locked(refresh = false) ⇒ Object



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
255
256
257
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 189

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

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

    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



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

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

#dsMorObject



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

def dsMor
  (@dsMor)
end

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



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
167
168
169
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 109

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
    logger.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsSearch: calling searchDatastoreSubFolders_Task"
    taskMor = @invObj.searchDatastoreSubFolders_Task(browserMor, dsPath, searchSpec)
    logger.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsSearch: returned from searchDatastoreSubFolders_Task"
  else
    logger.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsSearch: calling searchDatastore_Task"
    taskMor = @invObj.searchDatastore_Task(browserMor, dsPath, searchSpec)
    logger.info "MiqVimDataStore(#{@invObj.server}, #{@invObj.username}).dsSearch: returned from searchDatastore_Task"
  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



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

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



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

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



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

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



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

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



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

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

#dumpPropsObject



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

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

#dupObj(obj) ⇒ Object



302
303
304
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 302

def dupObj(obj)
  obj
end

#get_file_content(filepath) ⇒ Object



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

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

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



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

def release
  # @invObj.releaseObj(self)
end

#waitForTask(tmor) ⇒ Object



274
275
276
# File 'lib/VMwareWebService/MiqVimDataStore.rb', line 274

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