Class: Fog::Vcloud::Compute::Real

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/fog/vcloud/compute.rb,
lib/fog/vcloud/requests/compute/login.rb,
lib/fog/vcloud/requests/compute/get_vdc.rb,
lib/fog/vcloud/requests/compute/get_task.rb,
lib/fog/vcloud/requests/compute/get_vapp.rb,
lib/fog/vcloud/requests/compute/power_on.rb,
lib/fog/vcloud/requests/compute/undeploy.rb,
lib/fog/vcloud/requests/compute/power_off.rb,
lib/fog/vcloud/requests/compute/clone_vapp.rb,
lib/fog/vcloud/requests/compute/get_server.rb,
lib/fog/vcloud/requests/compute/delete_node.rb,
lib/fog/vcloud/requests/compute/delete_vapp.rb,
lib/fog/vcloud/requests/compute/get_catalog.rb,
lib/fog/vcloud/requests/compute/get_network.rb,
lib/fog/vcloud/requests/compute/power_reset.rb,
lib/fog/vcloud/requests/compute/configure_vm.rb,
lib/fog/vcloud/requests/compute/get_vm_disks.rb,
lib/fog/vcloud/requests/compute/get_task_list.rb,
lib/fog/vcloud/requests/compute/get_vm_memory.rb,
lib/fog/vcloud/requests/compute/configure_node.rb,
lib/fog/vcloud/requests/compute/configure_vapp.rb,
lib/fog/vcloud/requests/compute/get_network_ip.rb,
lib/fog/vcloud/requests/compute/power_shutdown.rb,
lib/fog/vcloud/requests/compute/get_network_ips.rb,
lib/fog/vcloud/requests/compute/get_catalog_item.rb,
lib/fog/vcloud/requests/compute/get_organization.rb,
lib/fog/vcloud/requests/compute/configure_network.rb,
lib/fog/vcloud/requests/compute/configure_vm_cpus.rb,
lib/fog/vcloud/requests/compute/get_vapp_template.rb,
lib/fog/vcloud/requests/compute/configure_vm_disks.rb,
lib/fog/vcloud/requests/compute/configure_vm_memory.rb,
lib/fog/vcloud/requests/compute/configure_network_ip.rb,
lib/fog/vcloud/requests/compute/configure_org_network.rb,
lib/fog/vcloud/requests/compute/configure_vm_password.rb,
lib/fog/vcloud/requests/compute/get_network_extensions.rb,
lib/fog/vcloud/requests/compute/get_customization_options.rb,
lib/fog/vcloud/requests/compute/instantiate_vapp_template.rb,
lib/fog/vcloud/requests/compute/configure_vm_name_description.rb
more...

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.

[View source]

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/fog/vcloud/compute.rb', line 183

def initialize(options = {})
  require 'builder'
  require 'fog/core/parser'

  @connections = {}
  @connection_options = options[:connection_options] || {}
  @persistent = options[:persistent]

  @username  = options[:vcloud_username]
  @password  = options[:vcloud_password]
  @host      = options[:vcloud_host]
  @base_path = options[:vcloud_base_path]   || Fog::Vcloud::Compute::BASE_PATH
  @version   = options[:vcloud_version]     || Fog::Vcloud::Compute::DEFAULT_VERSION
  @path      = options[:vcloud_path]        || "#{@base_path}/v#{@version}"
  @port      = options[:vcloud_port]        || Fog::Vcloud::Compute::PORT
  @scheme    = options[:vcloud_scheme]      || Fog::Vcloud::Compute::SCHEME
  @vdc_href  = options[:vcloud_default_vdc]
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.


181
182
183
# File 'lib/fog/vcloud/compute.rb', line 181

def version
  @version
end

Class Method Details

.basic_request(*args) ⇒ Object

[View source]

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/fog/vcloud/compute.rb', line 150

def basic_request(*args)
  self.class_eval <<-EOS, __FILE__,__LINE__
    def #{args[0]}(uri)
      request(
        {
          :expects => #{args[1] || 200},
          :method  => '#{args[2] || 'GET'}',
          :headers => #{args[3] ? args[3].inspect : '{}'},
          :body => '#{args[4] ? args[4] : ''}',
          :parse => true,
          :uri     => uri
        }
      )
    end
  EOS
end

.unauthenticated_basic_request(*args) ⇒ Object

[View source]

167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/fog/vcloud/compute.rb', line 167

def unauthenticated_basic_request(*args)
  self.class_eval <<-EOS, __FILE__,__LINE__
    def #{args[0]}(uri)
      unauthenticated_request({
        :expects => #{args[1] || 200},
        :method  => '#{args[2] || 'GET'}',
        :headers => #{args[3] ? args[3].inspect : '{}'},
        :parse => true,
        :uri     => uri })
    end
  EOS
end

Instance Method Details

#base_path_urlObject

[View source]

279
280
281
# File 'lib/fog/vcloud/compute.rb', line 279

def base_path_url
  "#{@scheme}://#{@host}:#{@port}#{@base_path}"
end

#basic_request_params(uri, *args) ⇒ Object

[View source]

268
269
270
271
272
273
274
275
276
277
# File 'lib/fog/vcloud/compute.rb', line 268

def basic_request_params(uri,*args)
  {
    :expects => args[0] || 200,
    :method  => args[1] || 'GET',
    :headers => args[2] ? args[2].inspect : {},
    :body => args[3] ? args[3] : '',
    :parse => true,
    :uri     => uri
  }
end

#clone_vapp(vdc_uri, vapp_uri, options = {}) ⇒ Object

[View source]

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/vcloud/requests/compute/clone_vapp.rb', line 21

def clone_vapp(vdc_uri, vapp_uri, options = {})
  unless options.has_key?(:poweron)
    options[:poweron] = "false"
  end

  validate_clone_vapp_options(options)

  request(
    :body     => generate_clone_vapp_request(vapp_uri, options),
    :expects  => 202,
    :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.cloneVAppParams+xml'},
    :method   => 'POST',
    :uri      => vdc_uri + '/action/clonevapp',
    :parse    => true
  )
end

#configure_network(network_uri, network_data) ⇒ Object

[View source]

13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/vcloud/requests/compute/configure_network.rb', line 13

def configure_network(network_uri, network_data)
  validate_network_data(network_data)

  request(
    :body     => generate_configure_network_request(network_data),
    :expects  => 200,
    :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.networkService+xml'},
    :method   => 'PUT',
    :uri      => network_uri,
    :parse    => true
  )
end

#configure_network_ip(network_ip_uri, network_ip_data) ⇒ Object

[View source]

18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/vcloud/requests/compute/configure_network_ip.rb', line 18

def configure_network_ip(network_ip_uri, network_ip_data)
  validate_network_ip_data(network_ip_data)

  request(
    :body     => generate_configure_network_ip_request(network_ip_data),
    :expects  => 200,
    :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.ip+xml' },
    :method   => 'PUT',
    :uri      => network_ip_uri,
    :parse    => true
  )
end

#configure_node(node_uri, node_data) ⇒ Object

[View source]

22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/vcloud/requests/compute/configure_node.rb', line 22

def configure_node(node_uri, node_data)
  validate_node_data(node_data, true)

  request(
    :body     => generate_configure_node_request(node_data),
    :expects  => 200,
    :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.nodeService+xml'},
    :method   => 'PUT',
    :uri      => node_uri,
    :parse    => true
  )
end

#configure_org_network(vapp_id, vapp_network, vapp_network_uri, org_network, org_network_uri, enable_firewall = false, port_map = nil) ⇒ Object

[View source]

124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/fog/vcloud/requests/compute/configure_org_network.rb', line 124

def configure_org_network(vapp_id, vapp_network, vapp_network_uri, org_network, org_network_uri, enable_firewall=false, port_map=nil)
  body = generate_configure_org_network_request(vapp_id, vapp_network, vapp_network_uri, org_network, org_network_uri, enable_firewall, port_map)
  #puts ("Body: #{body}")

  request(
        :body     => body,
        :expects  => 202,
        :headers  => {'Content-Type' => 'Application/vnd.vmware.vcloud.networkConfigSection+xml' },
        :method   => 'PUT',
        :uri      => "#{vapp_id}/networkConfigSection",
        :parse    => true
      )
end

#configure_vapp(vapp_uri, vapp_data) ⇒ Object

[View source]

96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fog/vcloud/requests/compute/configure_vapp.rb', line 96

def configure_vapp(vapp_uri, vapp_data)
  validate_vapp_data(vapp_data)

  request(
    :body     => generate_configure_vapp_request(vapp_uri, vapp_data),
    :expects  => 202,
    :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.vApp+xml' },
    :method   => 'PUT',
    :uri      => vapp_uri,
    :parse    => true
  )
end

#configure_vm(vm_uri, vm_data) ⇒ Object

[View source]

93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fog/vcloud/requests/compute/configure_vm.rb', line 93

def configure_vm(vm_uri, vm_data)
  validate_vm_data(vm_data)

  request(
    :body     => generate_configure_vm_request(vm_uri, vm_data),
    :expects  => 202,
    :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.vm+xml' },
    :method   => 'PUT',
    :uri      => vm_uri,
    :parse    => true
  )
end

#configure_vm_cpus(vm_data) ⇒ Object

[View source]

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/vcloud/requests/compute/configure_vm_cpus.rb', line 7

def configure_vm_cpus(vm_data)
  edit_uri = vm_data.select {|k,v| k == :Link && v[:rel] == 'edit'}
  edit_uri = edit_uri.kind_of?(Array) ? edit_uri.flatten[1][:href] : edit_uri[:Link][:href]

  body = <<EOF
  <Item xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vcloud="http://www.vmware.com/vcloud/v1.5" vcloud:href="#{edit_uri}" vcloud:type="application/vnd.vmware.vcloud.rasdItem+xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://#{@host}/api/v1.5/schema/master.xsd http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2.22.0/CIM_ResourceAllocationSettingData.xsd">
    <rasd:AllocationUnits>hertz * 10^6</rasd:AllocationUnits>
    <rasd:Description>Number of Virtual CPUs</rasd:Description>
    <rasd:ElementName>#{vm_data[:'rasd:VirtualQuantity']} virtual CPU(s)</rasd:ElementName>
    <rasd:InstanceID>4</rasd:InstanceID>
    <rasd:Reservation>0</rasd:Reservation>
    <rasd:ResourceType>3</rasd:ResourceType>
    <rasd:VirtualQuantity>#{vm_data[:'rasd:VirtualQuantity']}</rasd:VirtualQuantity>
    <rasd:Weight>0</rasd:Weight>
    <Link rel="edit" type="application/vnd.vmware.vcloud.rasdItem+xml" href="#{edit_uri}"/>
</Item>
EOF
  request(
    :body     => body,
    :expects  => 202,
    :headers  => {'Content-Type' => vm_data[:"vcloud_type"] },
    :method   => 'PUT',
    :uri      => "#{edit_uri}",
    :parse    => true
  )
end

#configure_vm_disks(vm_href, disk_data) ⇒ Object

[View source]

81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fog/vcloud/requests/compute/configure_vm_disks.rb', line 81

def configure_vm_disks(vm_href, disk_data)
  disk_href = vm_href + '/virtualHardwareSection/disks'

  body = generate_configure_vm_disks_request(disk_href, disk_data)

  request(
    :body     => body,
    :expects  => 202,
    :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.rasdItem+xml' },
    :method   => 'PUT',
    :uri      => disk_href,
    :parse    => true
  )
end

#configure_vm_memory(vm_data) ⇒ Object

[View source]

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/vcloud/requests/compute/configure_vm_memory.rb', line 7

def configure_vm_memory(vm_data)
  edit_uri = vm_data.select {|k,v| k == :Link && v[:rel] == 'edit'}
  edit_uri = edit_uri.kind_of?(Array) ? edit_uri.flatten[1][:href] : edit_uri[:Link][:href]

  body = <<EOF
  <Item xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vcloud="http://www.vmware.com/vcloud/v1.5" vcloud:href="#{edit_uri}" vcloud:type="application/vnd.vmware.vcloud.rasdItem+xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://#{@host}/api/v1.5/schema/master.xsd http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2.22.0/CIM_ResourceAllocationSettingData.xsd">
    <rasd:AllocationUnits>byte * 2^20</rasd:AllocationUnits>
    <rasd:Description>Memory Size</rasd:Description>
    <rasd:ElementName>#{vm_data[:'rasd:VirtualQuantity']} MB of memory</rasd:ElementName>
    <rasd:InstanceID>5</rasd:InstanceID>
    <rasd:Reservation>0</rasd:Reservation>
    <rasd:ResourceType>4</rasd:ResourceType>
    <rasd:VirtualQuantity>#{vm_data[:'rasd:VirtualQuantity']}</rasd:VirtualQuantity>
    <rasd:Weight>0</rasd:Weight>
    <Link rel="edit" type="application/vnd.vmware.vcloud.rasdItem+xml" href="#{edit_uri}"/>
</Item>
EOF

  request(
    :body     => body,
    :expects  => 202,
    :headers  => {'Content-Type' => vm_data[:"vcloud_type"] },
    :method   => 'PUT',
    :uri      => edit_uri,
    :parse    => true
  )
end

#configure_vm_name_description(edit_href, name, description) ⇒ Object

[View source]

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/vcloud/requests/compute/configure_vm_name_description.rb', line 7

def configure_vm_name_description(edit_href, name, description)

  body = <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<VApp xmlns="http://www.vmware.com/vcloud/v1" name="#{name}" type="application/vnd.vmware.vcloud.vApp+xml">
    <Description>#{description}</Description>
</VApp>
EOF

  request(
    :body     => body,
    :expects  => 202,
    :headers  => {'Content-Type' => "application/vnd.vmware.vcloud.vApp+xml"},
    :method   => 'PUT',
    :uri      => edit_href,
    :parse    => true
  )
end

#configure_vm_password(vmdata) ⇒ Object

[View source]

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/vcloud/requests/compute/configure_vm_password.rb', line 7

def configure_vm_password(vmdata)
  edit_uri = vmdata[:href]
  body = <<EOF
  <GuestCustomizationSection xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" type="application/vnd.vmware.vcloud.guestCustomizationSection+xml" href="https://zone01.bluelock.com/api/vApp/vm-cc8e27be-f18c-4263-87c5-58a9297bac5b/guestCustomizationSection/" ovf:required="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.dmtf.org/ovf/envelope/1 http://schemas.dmtf.org/ovf/envelope/1/dsp8023_1.1.0.xsd http://www.vmware.com/vcloud/v1.5 http://zone01.bluelock.com/api/v1.5/schema/master.xsd">
<ovf:Info>Specifies Guest OS Customization Settings</ovf:Info>
<Enabled>true</Enabled>
<ChangeSid>false</ChangeSid>
<VirtualMachineId>#{vmdata[:VirtualMachineId]}</VirtualMachineId>
<JoinDomainEnabled>false</JoinDomainEnabled>
<UseOrgSettings>false</UseOrgSettings>
<AdminPasswordEnabled>true</AdminPasswordEnabled>
<AdminPasswordAuto>false</AdminPasswordAuto>
<AdminPassword>#{vmdata[:AdminPassword]}</AdminPassword>
<ResetPasswordRequired>false</ResetPasswordRequired>
<ComputerName>#{vmdata[:ComputerName]}</ComputerName>
<Link rel="edit" type="application/vnd.vmware.vcloud.guestCustomizationSection+xml" href="#{edit_uri}"/>
</GuestCustomizationSection>
EOF
  request(
    :body     => body,
    :expects  => 202,
    :headers  => {'Content-Type' => vmdata[:type] },
    :method   => 'PUT',
    :uri      => "#{edit_uri}",
    :parse    => true
  )
end

#default_organization_uriObject

[View source]

206
207
208
209
# File 'lib/fog/vcloud/compute.rb', line 206

def default_organization_uri
  @default_organization_uri ||= organizations.first.href
  @default_organization_uri
end

#default_vdc_hrefObject

[View source]

211
212
213
214
215
216
217
218
219
220
221
# File 'lib/fog/vcloud/compute.rb', line 211

def default_vdc_href
  if @vdc_href.nil?
    unless @login_results
      
    end
    org = organizations.first
    vdc = get_organization(org.href).links.find { |item| item[:type] == 'application/vnd.vmware.vcloud.vdc+xml'}
    @vdc_href = vdc[:href]
  end
  @vdc_href
end

#do_loginObject

login handles the auth, but we just need the Set-Cookie header from that call.

[View source]

225
226
227
228
# File 'lib/fog/vcloud/compute.rb', line 225

def 
  @login_results = 
  @cookie = @login_results.headers['Set-Cookie']
end

#ensure_unparsed(uri) ⇒ Object

[View source]

230
231
232
233
234
235
236
# File 'lib/fog/vcloud/compute.rb', line 230

def ensure_unparsed(uri)
  if uri.is_a?(String)
    uri
  else
    uri.to_s
  end
end

#generate_clone_vapp_request(uri, options) ⇒ Object

[View source]

13
14
15
16
17
18
19
# File 'lib/fog/vcloud/requests/compute/clone_vapp.rb', line 13

def generate_clone_vapp_request(uri, options)
  xml = Builder::XmlMarkup.new
  xml.CloneVAppParams(xmlns.merge!(:name => options[:name], :deploy => "true", :powerOn => options[:poweron])) {
    xml.VApp( :href => uri, :type => "application/vnd.vmware.vcloud.vApp+xml",
              :xmlns => "http://www.vmware.com/vcloud/v0.8")
  }
end

#generate_configure_org_network_request(vapp_id, vapp_network, vapp_network_uri, org_network, org_network_uri, enable_firewall = false, portmap = nil) ⇒ Object

[View source]

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fog/vcloud/requests/compute/configure_org_network.rb', line 72

def generate_configure_org_network_request(vapp_id, vapp_network, vapp_network_uri, org_network, org_network_uri, enable_firewall=false, portmap=nil)

  firewall_body = ""
  if not enable_firewall
    firewall_body = "<ns0:IsEnabled>false</ns0:IsEnabled>"

  else
    firewall_rules = generate_outbound_rule + generate_tcp_rules(portmap["TCP"]) + generate_udp_rules(portmap["UDP"])
            firewall_body = <<EOF
                    <ns0:IsEnabled>true</ns0:IsEnabled>
                    <ns0:DefaultAction>drop</ns0:DefaultAction>
                    <ns0:LogDefaultAction>false</ns0:LogDefaultAction>
                    #{firewall_rules}
EOF
  end
body = <<EOF
  <ns0:NetworkConfigSection xmlns:ns0="http://www.vmware.com/vcloud/v1.5" xmlns:ns1="http://schemas.dmtf.org/ovf/envelope/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" href="#{vapp_id}/networkConfigSection/" type="application/vnd.vmware.vcloud.networkConfigSection+xml" ns1:required="false" xsi:schemaLocation="http://schemas.dmtf.org/ovf/envelope/1 http://schemas.dmtf.org/ovf/envelope/1/dsp8023_1.1.0.xsd http://www.vmware.com/vcloud/v1.5 http://zone01.bluelock.com/api/v1.5/schema/master.xsd">
<ns1:Info>The configuration parameters for logical networks</ns1:Info>
<ns0:Link href="#{vapp_id}/networkConfigSection/" rel="edit" type="application/vnd.vmware.vcloud.networkConfigSection+xml" />
<ns0:NetworkConfig networkName="#{vapp_network}">
<ns0:Link href="#{vapp_network_uri}" rel="repair" />
<ns0:Description />
  <ns0:Configuration>
<ns0:ParentNetwork href="#{org_network_uri}" name="#{org_network}" type="application/vnd.vmware.vcloud.network+xml" />
<ns0:FenceMode>natRouted</ns0:FenceMode>
<ns0:RetainNetInfoAcrossDeployments>true</ns0:RetainNetInfoAcrossDeployments>
<ns0:Features>
<ns0:FirewallService>
#{firewall_body}
</ns0:FirewallService>
<ns0:NatService>
<ns0:IsEnabled>true</ns0:IsEnabled>
<ns0:NatType>ipTranslation</ns0:NatType>
<ns0:Policy>allowTraffic</ns0:Policy>
</ns0:NatService>
</ns0:Features>
</ns0:Configuration>
<ns0:IsDeployed>false</ns0:IsDeployed>
</ns0:NetworkConfig>
<ns0:NetworkConfig networkName="#{org_network}"><ns0:Link href="#{org_network_uri}" rel="repair" />
<ns0:Description />
<ns0:Configuration>
<ns0:ParentNetwork href="#{org_network_uri}" name="#{org_network}" type="application/vnd.vmware.vcloud.network+xml" />
<ns0:FenceMode>bridged</ns0:FenceMode>
<ns0:RetainNetInfoAcrossDeployments>true</ns0:RetainNetInfoAcrossDeployments>
<ns0:SyslogServerSettings />
</ns0:Configuration>
</ns0:NetworkConfig>
</ns0:NetworkConfigSection>
EOF
end

#generate_configure_vapp_request(vapp_uri, vapp_data) ⇒ Object

[View source]

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fog/vcloud/requests/compute/configure_vapp.rb', line 18

def generate_configure_vapp_request(vapp_uri, vapp_data)
  rasd_xmlns = { "xmlns" => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" }

  xml = Nokogiri::XML(request( :uri => vapp_uri).body)
  xml.root['name'] = vapp_data[:name]

  #cpu
  xml.at("//xmlns:ResourceType[.='3']/..", rasd_xmlns).at('.//xmlns:VirtualQuantity', rasd_xmlns).content = vapp_data[:cpus]

  #memory
  xml.at("//xmlns:ResourceType[.='4']/..", rasd_xmlns).at('.//xmlns:VirtualQuantity', rasd_xmlns).content = vapp_data[:memory]

  #disks
  real_disks = xml.xpath("//xmlns:ResourceType[ .='17']/..", rasd_xmlns)
  real_disk_numbers = real_disks.map { |disk| disk.at('.//xmlns:AddressOnParent', rasd_xmlns).content }
  disk_numbers = vapp_data[:disks].map { |vdisk| vdisk[:number].to_s }

  if vapp_data[:disks].length < real_disks.length
    #Assume we're removing a disk
    remove_disk_numbers = real_disk_numbers - disk_numbers
    remove_disk_numbers.each do |number|
      if result = xml.at("//xmlns:ResourceType[ .='17']/../xmlns:AddressOnParent[.='#{number}']/..", rasd_xmlns)
        result.remove
      end
    end
  elsif vapp_data[:disks].length > real_disks.length
    add_disk_numbers = disk_numbers - real_disk_numbers

    add_disk_numbers.each do |number|
      new_disk = real_disks.first.dup
      new_disk.at('.//xmlns:AddressOnParent', rasd_xmlns).content = -1
      new_disk.at('.//xmlns:VirtualQuantity', rasd_xmlns).content = vapp_data[:disks].detect { |disk| disk[:number].to_s == number.to_s }[:size]
      real_disks.first.parent << new_disk
    end
  end

  #puts xml.root.to_s
  xml.root.to_s

  #builder = Builder::XmlMarkup.new
  #builder.Vapp(:href => vapp_uri.to_s,
  #             :type => 'application/vnd.vmware.vcloud.vApp+xml',
  #             :name => vapp_data[:name],
  #             :status => 2,
  #             :size => 0,
  #             :xmlns => 'http://www.vmware.com/vcloud/v0.8',
  #             :"xmlns:xsi" => 'http://www.w3.org/2001/XMLSchema-instance',
  #             :"xmlns:xsd" => 'http://www.w3.org/2001/XMLSchema') {
  #  #builder.VirtualHardwareSection(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') {
  #  builder.Section(:"xsi:type" => "q2:VirtualHardwareSection_Type", :xmlns => "http://schemas.dmtf.org/ovf/envelope/1", :"xmlns:q2" => "http://www.vmware.com/vcloud/v0.8") {
  #    builder.Info('Virtual Hardware')
  #    builder.Item(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') {
  #    #builder.Item {
  #      builder.InstanceID(1, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #      builder.ResourceType(3, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #      builder.VirtualQuantity(vapp_data[:cpus], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #    }
  #    builder.Item(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') {
  #    #builder.Item {
  #      builder.InstanceID(2, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #      builder.ResourceType(4, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #      builder.VirtualQuantity(vapp_data[:memory], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #    }
  #    vapp_data[:disks].each do |disk_data|
  #      #builder.Item(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') {
  #      builder.Item {
  #        builder.AddressOnParent(disk_data[:number], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #        builder.HostResource(disk_data[:resource], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #        builder.InstanceID(9, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #        builder.ResourceType(17, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #        builder.VirtualQuantity(disk_data[:size], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
  #      }
  #    end
  #
  #  }
  #}
end

#generate_configure_vm_disks_request(href, disk_data) ⇒ Object

[View source]

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/vcloud/requests/compute/configure_vm_disks.rb', line 40

def generate_configure_vm_disks_request(href, disk_data)
  xmlns = {
    "xmlns:rasd" => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
    "xmlns" => "http://www.vmware.com/vcloud/v1"
  }
  # Get the XML from the API, parse it.
  xml = Nokogiri::XML(request( :uri => href).body)

  #xml.root['name'] = vapp_data[:name]

  #disks
  real_disks = xml.xpath("//rasd:ResourceType[ .='17']/..", xmlns)
  real_disk_numbers = real_disks.map { |disk| disk.at('.//rasd:AddressOnParent', xmlns).content }
  disk_numbers = disk_data.map { |vdisk| vdisk[:"rasd:AddressOnParent"].to_s }

  if disk_data.length < real_disks.length
    #Assume we're removing a disk
    remove_disk_numbers = real_disk_numbers - disk_numbers
    remove_disk_numbers.each do |number|
      if result = xml.at("//rasd:ResourceType[ .='17']/../rasd:AddressOnParent[.='#{number}']/..", xmlns)
        result.remove
      end
    end
  elsif disk_data.length > real_disks.length
    add_disk_numbers = disk_numbers - real_disk_numbers

    add_disk_numbers.each do |number|
      new_disk = real_disks.first.dup
      new_disk.at('.//rasd:AddressOnParent', xmlns).content = number.to_i #-1
      new_disk.at('.//rasd:HostResource', xmlns)["vcloud:capacity"] = disk_data.detect { |disk| disk[:'rasd:AddressOnParent'].to_s == number.to_s }[:'rasd:HostResource'][:vcloud_capacity].to_s
      # nokogiri bug? shouldn't need to add this explicitly.
      new_disk.at('.//rasd:HostResource', xmlns)["xmlns:vcloud"] = xmlns['xmlns']
      new_disk.at('.//rasd:InstanceID', xmlns).content = (2000 + number.to_i).to_s
      new_disk.at('.//rasd:ElementName', xmlns).content = "Hard disk #{number.to_i + 1}"
      real_disks.first.parent << new_disk
    end

  end
  xml.to_s
end

#generate_configure_vm_request(vm_data) ⇒ Object

[View source]

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fog/vcloud/requests/compute/configure_vm.rb', line 18

def generate_configure_vm_request(vm_data)
  xmlns = 'http://schemas.dmtf.org/ovf/envelope/1'
  xmlns_vcloud = 'http://www.vmware.com/vcloud/v1'
  xmlns_rasd = 'http://schemas.dmtf.org/wbem/wscim/1/cim‐schema/2/CIM_ResourceAllocationSettingData'
  xmlns_vssd = 'http://schemas.dmtf.org/wbem/wscim/1/cim‐schema/2/CIM_VirtualSystemSettingData'

  builder = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2) # TODO - remove params
  builder.VirtualHardwareSection(
                                 :"vcloud:href" => vm_data[:"vcloud_href"],
                                 :"vcloud:type" => vm_data[:"vcloud_type"],
                                 :name => vm_data[:name],
                                 :status => 2,
                                 :size => 0,
                                 :xmlns => xmlns,
                                 :"xmlns:vcloud" => xmlns_vcloud,
                                 :"xmlns:rasd" => xmlns_rasd,
                                 :"xmlns:vssd" => xmlns_vssd) {
    #builder.VirtualHardwareSection(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') {

    builder.Info(vm_data[:"ovf:Info"])
    if system = vm_data[:"ovf:System"]
      builder.System {
        builder.ElementName(system[:"vssd:ElementName"], :xmlns => xmlns_vssd) if system[:"vssd:ElementName"]
        builder.InstanceID(system[:"vssd:InstanceID"], :xmlns => xmlns_vssd) if system[:"vssd:InstanceID"]
        builder.VirtualSystemIdentifier(system[:"vssd:VirtualSystemIdentifier"], :xmlns => xmlns_vssd) if system[:"vssd:VirtualSystemIdentifier"]
        builder.VirtualSystemType(system[:"vssd:VirtualSystemType"], :xmlns => xmlns_vssd) if system[:"vssd:VirtualSystemType"]
      }
    end

    vm_data[:'ovf:Item'].each do |oi|
      builder.Item {
        builder.Address(oi[:'rasd:Address'], :xmlns => xmlns_rasd) if oi[:'rasd:Address']
        builder.AddressOnParent(oi[:'rasd:AddressOnParent'], :xmlns => xmlns_rasd) if oi[:'rasd:AddressOnParent']
        builder.AutomaticAllocation(oi[:'rasd:AutomaticAllocation'], :xmlns => xmlns_rasd) if oi[:'rasd:AutomaticAllocation']
        builder.Connection(oi[:'rasd:Connection'], :xmlns => xmlns_rasd) if oi[:'rasd:Connection']
        builder.Description(oi[:'rasd:Description'], :xmlns => xmlns_rasd) if oi[:'rasd:Description']
        builder.ElementName(oi[:'rasd:ElementName'], :xmlns => xmlns_rasd) if oi[:'rasd:ElementName']
        builder.InstanceID(oi[:'rasd:InstanceID'], :xmlns => xmlns_rasd) if oi[:'rasd:InstanceID']
        builder.ResourceSubType(oi[:'rasd:ResourceSubType'], :xmlns => xmlns_rasd) if oi[:'rasd:ResourceSubType']
        builder.ResourceType(oi[:'rasd:ResourceType'], :xmlns => xmlns_rasd) if oi[:'rasd:ResourceType']
        if hr = oi[:'rasd:HostResource']
          attrs = {}
          attrs[]
        end
      }
    end



     # builder.Item(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') {
     # #builder.Item {
     #   builder.InstanceID(1, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     #   builder.ResourceType(3, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     #   builder.VirtualQuantity(vm_data[:cpus], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     # }
     # builder.Item(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') {
     # #builder.Item {
     #   builder.InstanceID(2, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     #   builder.ResourceType(4, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     #   builder.VirtualQuantity(vm_data[:memory], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     # }
     # vm_data[:disks].each do |disk_data|
     #   #builder.Item(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') {
     #   builder.Item {
     #     builder.AddressOnParent(disk_data[:number], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     #     builder.HostResource(disk_data[:resource], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     #     builder.InstanceID(9, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     #     builder.ResourceType(17, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     #     builder.VirtualQuantity(disk_data[:size], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData')
     #   }
     # end

   }
end

#generate_outbound_ruleObject

[View source]

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/vcloud/requests/compute/configure_org_network.rb', line 7

def generate_outbound_rule()
  outbound_rule = <<EOF 
    <ns0:FirewallRule>
    <ns0:IsEnabled>true</ns0:IsEnabled>
    <ns0:Description>OUTGOING</ns0:Description>
    <ns0:Policy>allow</ns0:Policy>
    <ns0:Protocols>
        <ns0:Any>true</ns0:Any>
    </ns0:Protocols>
    <ns0:Port>-1</ns0:Port>
    <ns0:DestinationIp>Any</ns0:DestinationIp>
    <ns0:SourcePort>-1</ns0:SourcePort>
    <ns0:SourceIp>Any</ns0:SourceIp>
    <ns0:Direction>out</ns0:Direction>
    <ns0:EnableLogging>false</ns0:EnableLogging>
</ns0:FirewallRule>
EOF
outbound_rule
end

#generate_tcp_rules(tcp_ports) ⇒ Object

[View source]

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/vcloud/requests/compute/configure_org_network.rb', line 26

def generate_tcp_rules(tcp_ports)
  firewall_rules = ""
  tcp_ports.each do |port|
    firewall_rules << <<EOF 
            <ns0:FirewallRule>
            <ns0:IsEnabled>true</ns0:IsEnabled>
            <ns0:Description>#{port}</ns0:Description>
            <ns0:Policy>allow</ns0:Policy>
            <ns0:Protocols>
            <ns0:Tcp>true</ns0:Tcp>
            </ns0:Protocols>
            <ns0:Port>#{port}</ns0:Port>
            <ns0:DestinationIp>Any</ns0:DestinationIp>
            <ns0:SourcePort>-1</ns0:SourcePort>
            <ns0:SourceIp>Any</ns0:SourceIp>
            <ns0:Direction>in</ns0:Direction>
            <ns0:EnableLogging>false</ns0:EnableLogging>
            </ns0:FirewallRule>
EOF
  end
  firewall_rules
end

#generate_udp_rules(udp_ports) ⇒ Object

[View source]

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fog/vcloud/requests/compute/configure_org_network.rb', line 49

def generate_udp_rules(udp_ports)
  firewall_rules = ""
  udp_ports.each do |port|
      firewall_rules << <<EOF 
            <ns0:FirewallRule>
            <ns0:IsEnabled>true</ns0:IsEnabled>
            <ns0:Description>#{port}</ns0:Description>
            <ns0:Policy>allow</ns0:Policy>
            <ns0:Protocols>
            <ns0:Udp>true</ns0:Udp>
            </ns0:Protocols>
            <ns0:Port>#{port}</ns0:Port>
            <ns0:DestinationIp>Any</ns0:DestinationIp>
            <ns0:SourcePort>-1</ns0:SourcePort>
            <ns0:SourceIp>Any</ns0:SourceIp>
            <ns0:Direction>in</ns0:Direction>
            <ns0:EnableLogging>false</ns0:EnableLogging>
            </ns0:FirewallRule>
EOF
  end
  firewall_rules
end

#get_vm_disks(href) ⇒ Object

[View source]

6
7
8
9
10
11
12
# File 'lib/fog/vcloud/requests/compute/get_vm_disks.rb', line 6

def get_vm_disks(href)
  request(
          :expects  => 200,
          :uri      => href,
          :parse    => true#false#true
          )
end

#get_vm_memory(href, parse = true) ⇒ Object

[View source]

6
7
8
9
10
11
12
# File 'lib/fog/vcloud/requests/compute/get_vm_memory.rb', line 6

def get_vm_memory(href, parse = true)
  request(
          :expects  => 200,
          :uri      => href,
          :parse    => parse
          )
end

#instantiate_vapp_template(options = {}) ⇒ Object

[View source]

68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/vcloud/requests/compute/instantiate_vapp_template.rb', line 68

def instantiate_vapp_template options = {}
  validate_instantiate_vapp_template_options options

  request(
    :body     => generate_instantiate_vapp_template_request(options),
    :expects  => 201,
    :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml'},
    :method   => 'POST',
    :uri      => options[:vdc_uri] + '/action/instantiateVAppTemplate',
    :parse    => true
  )
end

#loginObject

[View source]

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fog/vcloud/requests/compute/login.rb', line 8

def 
  headers = { 'Authorization' => authorization_header }
  uri = if version == '1.0'
    "#{base_url}/login"
  else
    "#{base_path_url}/sessions"
  end
  unauthenticated_request({
    :expects  => 200,
    :headers  => headers,
    :method   => 'POST',
    :parse    => true,
    :uri      => uri
  })
end

#reloadObject

[View source]

202
203
204
# File 'lib/fog/vcloud/compute.rb', line 202

def reload
  @connections.each_value { |k,v| v.reset if v }
end

#request(params) ⇒ Object

If the cookie isn’t set, do a get_organizations call to set it and try the request. If we get an Unauthorized error, we assume the token expired, re-auth and try again

[View source]

255
256
257
258
259
260
261
262
263
264
265
# File 'lib/fog/vcloud/compute.rb', line 255

def request(params)
  unless @cookie
    
  end
  begin
    do_request(params)
  rescue Excon::Errors::Unauthorized
    
    do_request(params)
  end
end

#undeploy(vapp_uri, save_state = false) ⇒ Object

[View source]

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/vcloud/requests/compute/undeploy.rb', line 5

def undeploy(vapp_uri, save_state = false)
  # builder = Builder::XmlMarkup.new
  # builder.UndeployVAppParams(:xmlns => 'http://www.vmware.com/vcloud/v1',
  #                            :saveState => save_state) {}
  builder = if version =='1.0' 
                "<UndeployVAppParams saveState=\"#{save_state.to_s}\" xmlns=\"http://www.vmware.com/vcloud/v1\"/>"
            else
               <<EOF 
                <UndeployVAppParams xmlns="http://www.vmware.com/vcloud/v1.5">
                <UndeployPowerAction>shutdown</UndeployPowerAction>
                </UndeployVAppParams>

EOF
            end
  request(
          :body     => builder,
          :expects  => 202,
          :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.undeployVAppParams+xml' },
          :method   => 'POST',
          :uri      => vapp_uri + '/action/undeploy',
          :parse    => true
          )
end

#validate_clone_vapp_options(options) ⇒ Object

[View source]

6
7
8
9
10
11
# File 'lib/fog/vcloud/requests/compute/clone_vapp.rb', line 6

def validate_clone_vapp_options(options)
  valid_opts = [:name, :poweron]
  unless valid_opts.all? { |opt| options.has_key?(opt) }
    raise ArgumentError.new("Required data missing: #{(valid_opts - options.keys).map(&:inspect).join(", ")}")
  end
end

#validate_network_data(network_data, configure = false) ⇒ Object

[View source]

6
7
8
9
10
11
# File 'lib/fog/vcloud/requests/compute/configure_network.rb', line 6

def validate_network_data(network_data, configure=false)
  valid_opts = [:id, :href, :name, :address, :broadcast, :gateway]
  unless valid_opts.all? { |opt| network_data.has_key?(opt) }
    raise ArgumentError.new("Required data missing: #{(valid_opts - network_data.keys).map(&:inspect).join(", ")}")
  end
end

#xmlnsObject

[View source]

238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/fog/vcloud/compute.rb', line 238

def xmlns
  if version == '1.0'
    { "xmlns" => "http://www.vmware.com/vcloud/v1",
      "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1",
      "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
      "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema" }
  else
    { 'xmlns' => "http://www.vmware.com/vcloud/v1.5",
      "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1",
      "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
      "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema" }
  end
end