Class: Fog::Compute::HPV2::Mock

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fog/hp/requests/compute_v2/attach_volume.rb,
lib/fog/hp/compute_v2.rb,
lib/fog/hp/requests/compute_v2/get_meta.rb,
lib/fog/hp/requests/compute_v2/delete_meta.rb,
lib/fog/hp/requests/compute_v2/get_address.rb,
lib/fog/hp/requests/compute_v2/list_images.rb,
lib/fog/hp/requests/compute_v2/list_limits.rb,
lib/fog/hp/requests/compute_v2/update_meta.rb,
lib/fog/hp/requests/compute_v2/create_image.rb,
lib/fog/hp/requests/compute_v2/delete_image.rb,
lib/fog/hp/requests/compute_v2/get_key_pair.rb,
lib/fog/hp/requests/compute_v2/list_flavors.rb,
lib/fog/hp/requests/compute_v2/list_servers.rb,
lib/fog/hp/requests/compute_v2/set_metadata.rb,
lib/fog/hp/requests/compute_v2/create_server.rb,
lib/fog/hp/requests/compute_v2/delete_server.rb,
lib/fog/hp/requests/compute_v2/detach_volume.rb,
lib/fog/hp/requests/compute_v2/list_metadata.rb,
lib/fog/hp/requests/compute_v2/reboot_server.rb,
lib/fog/hp/requests/compute_v2/update_server.rb,
lib/fog/hp/requests/compute_v2/list_addresses.rb,
lib/fog/hp/requests/compute_v2/list_key_pairs.rb,
lib/fog/hp/requests/compute_v2/rebuild_server.rb,
lib/fog/hp/requests/compute_v2/create_key_pair.rb,
lib/fog/hp/requests/compute_v2/delete_key_pair.rb,
lib/fog/hp/requests/compute_v2/get_vnc_console.rb,
lib/fog/hp/requests/compute_v2/release_address.rb,
lib/fog/hp/requests/compute_v2/update_metadata.rb,
lib/fog/hp/requests/compute_v2/allocate_address.rb,
lib/fog/hp/requests/compute_v2/associate_address.rb,
lib/fog/hp/requests/compute_v2/get_image_details.rb,
lib/fog/hp/requests/compute_v2/add_security_group.rb,
lib/fog/hp/requests/compute_v2/get_console_output.rb,
lib/fog/hp/requests/compute_v2/get_flavor_details.rb,
lib/fog/hp/requests/compute_v2/get_server_details.rb,
lib/fog/hp/requests/compute_v2/list_images_detail.rb,
lib/fog/hp/requests/compute_v2/list_flavors_detail.rb,
lib/fog/hp/requests/compute_v2/list_server_volumes.rb,
lib/fog/hp/requests/compute_v2/list_servers_detail.rb,
lib/fog/hp/requests/compute_v2/disassociate_address.rb,
lib/fog/hp/requests/compute_v2/get_windows_password.rb,
lib/fog/hp/requests/compute_v2/list_server_addresses.rb,
lib/fog/hp/requests/compute_v2/remove_security_group.rb,
lib/fog/hp/requests/compute_v2/list_availability_zones.rb,
lib/fog/hp/requests/compute_v2/create_persistent_server.rb,
lib/fog/hp/requests/compute_v2/get_server_volume_details.rb,
lib/fog/hp/requests/compute_v2/list_server_addresses_by_network.rb

Overview

:nodoc:all

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#decrypt_using_private_key, #encrypt_using_public_key, #extract_password_from_log

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/fog/hp/compute_v2.rb', line 207

def initialize(options={})
  # deprecate hp_account_id
  if options[:hp_account_id]
    Fog::Logger.deprecation(":hp_account_id is deprecated, please use :hp_access_key instead.")
    @hp_access_key = options.delete(:hp_account_id)
  end
  @hp_access_key = options[:hp_access_key]
  unless @hp_access_key
    raise ArgumentError.new("Missing required arguments: hp_access_key. :hp_account_id is deprecated, please use :hp_access_key instead.")
  end
end

Class Method Details

.dataObject



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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/fog/hp/compute_v2.rb', line 127

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :last_modified => {
        :images  => {},
        :key_pairs => {},
        :servers => {},
        :addresses => {},
        :volume_attachments => {},
      },
      :images  => {},
      :key_pairs => {},
      :servers => {},
      :addresses => {},
      :volume_attachments => {},
      :limits => {
          "absolute" => {
            "maxImageMeta"            => 50,
            "maxPersonality"          => 5,
            "maxPersonalitySize"      => 10240,
            "maxSecurityGroupRules"   => 20,
            "maxSecurityGroups"       => 10,
            "maxTotalKeypairs"        => 100,
            "maxServerMeta"           => 50,
            "maxTotalInstances"       => 20,
            "maxTotalRAMSize"         => 102400,
            "maxTotalCores"           => -1,
            "maxTotalFloatingIps"     => 10,
            "totalRAMUsed"            => 12288,
            "totalInstancesUsed"      => 3,
            "totalFloatingIpsUsed"    => 0,
            "totalSecurityGroupsUsed" => 0,
            "totalCoresUsed"          => 8,
          },
          "rate" => [
            {
              "limit" => [
                {
                  "next-available" => "2012-10-05T03 =>47 =>15Z",
                  "remaining" => 199,
                  "unit" => "MINUTE",
                  "value" => 200,
                  "verb" => "GET"
                },
                {
                  "next-available" => "2012-10-05T03 =>47 =>15Z",
                  "remaining" => 9,
                  "unit" => "MINUTE",
                  "value" => 10,
                  "verb" => "PUT"
                },
                {
                  "next-available" => "2012-10-05T03 =>47 =>15Z",
                  "remaining" => 9,
                  "unit" => "MINUTE",
                  "value" => 10,
                  "verb" => "POST"
                },
                {
                  "next-available" => "2012-10-05T03 =>47 =>15Z",
                  "remaining" => 99,
                  "unit" => "MINUTE",
                  "value" => 100,
                  "verb" => "DELETE"
                }
              ],
              "regex" => ".*",
              "uri" => "*"
            }
          ]
      }

    }
  end
end

.resetObject



203
204
205
# File 'lib/fog/hp/compute_v2.rb', line 203

def self.reset
  @data = nil
end

Instance Method Details

#add_security_group(server_id, sg_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/hp/requests/compute_v2/add_security_group.rb', line 18

def add_security_group(server_id, sg_name)
  response = Excon::Response.new
  if server = self.data[:servers][server_id]
    data = {"name" => "#{sg_name}"}
    if server['security_groups']
      server['security_groups'] << data
    else
      server['security_groups'] = data
    end
    response.status = 202
  else
    raise Fog::Compute::HPV2::NotFound
  end
  response
end

#allocate_addressObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/hp/requests/compute_v2/allocate_address.rb', line 30

def allocate_address
  response = Excon::Response.new
  response.status = 200
  data = {
    'instance_id' => Fog::HP::Mock.uuid.to_s,
    'ip'          => Fog::HP::Mock.ip_address,
    'fixed_ip'    => Fog::HP::Mock.ip_address,
    'id'          => Fog::HP::Mock.uuid.to_s
  }
  self.data[:last_modified][:addresses][data['id']] = Time.now
  self.data[:addresses][data['id']] = data

  response.body = { 'floating_ip' => data }
  response
end

#associate_address(server_id, ip_address) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/hp/requests/compute_v2/associate_address.rb', line 22

def associate_address(server_id, ip_address)
  response = Excon::Response.new
  if server = self.data[:servers][server_id]
    data = {"version"=>4, "addr"=>"#{ip_address}"}
    if server['addresses']['custom']
      server['addresses']['custom'] << data
    else
      server['addresses']['custom'] = data
    end
    response.status = 202
  else
    #raise Fog::Compute::HPV2::NotFound
    response.status = 500
    raise(Excon::Errors.status_error({:expects => 202}, response))
  end
  response
end

#attach_volume(server_id, volume_id, device) ⇒ Object



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
# File 'lib/fog/hp/requests/compute_v2/attach_volume.rb', line 36

def attach_volume(server_id, volume_id, device)
  response = Excon::Response.new
  if server = self.data[:servers][server_id]
    # mock the case when the volume is already attached to the server
    if server['volumeAttachments'] && server['volumeAttachments'].select {|v| v['id'] == volume_id}
      response.status = 400
      response.body = '{"badRequest": {"message": "Volume status must be available", "code": 400}}'
      raise(Excon::Errors.status_error({:expects => 200}, response))
    else
      resp_data = { 'volumeAttachment' =>
                    {
                      'device'   => device,
                      'serverId' => server_id,
                      'id'       => volume_id,
                      'volumeId' => volume_id,
                    }
                  }
      response.body = resp_data
      response.status = 200

      data = {
                'device'   => device,
                'serverId' => server_id,
                'id'       => volume_id,
                'volumeId' => volume_id,
             }
      if server['volumeAttachments']
        server['volumeAttachments'] << data
      else
        server['volumeAttachments'] = [data]
      end
    end
  else
    raise Fog::Compute::HPV2::NotFound
  end
  response
end

#create_image(server_id, name, metadata = {}) ⇒ Object



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
# File 'lib/fog/hp/requests/compute_v2/create_image.rb', line 25

def create_image(server_id, name,  = {})
  response = Excon::Response.new
  if list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    response.status = 202

    image_id = Fog::HP::Mock.uuid.to_s

    data = {
      'id'        => image_id,
      'server'    => {"id"=>server_id, "links"=>[{"href"=>"http://nova1:8774/v1.1/servers/#{server_id}", "rel"=>"bookmark"}]},
      'links'     => [{"href"=>"http://nova1:8774/v1.1/tenantid/images/#{image_id}", "rel"=>"self"}, {"href"=>"http://nova1:8774/tenantid/images/#{image_id}", "rel"=>"bookmark"}],
      'metadata'  =>  || {},
      'name'      => name || "image_#{rand(999)}",
      'progress'  => 0,
      'minDisk'   => 0,
      'minRam'    => 0,
      'status'    => 'SAVING',
      'updated'   => "2012-01-01T13:32:20Z",
      'created'   => "2012-01-01T13:32:20Z"
    }

    self.data[:last_modified][:images][data['id']] = Time.now
    self.data[:images][data['id']] = data
    response.headers = {'Content-Length' => '0', 'Content-Type' => 'text/html; charset=UTF-8', 'Date' => Time.now, 'Location' => "http://nova1:8774/v1.1/images/#{image_id}"}
    response.body = '' # { 'image' => data } no data is returned
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#create_key_pair(key_name, public_key = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fog/hp/requests/compute_v2/create_key_pair.rb', line 46

def create_key_pair(key_name, public_key = nil)
  response = Excon::Response.new
  response.status = 200
  private_key, new_public_key = Fog::HP::Mock.key_material
  new_public_key = public_key if public_key  # if public key was passed in
  data = {
    'public_key'   => new_public_key,
    'fingerprint'  => Fog::HP::Mock.key_fingerprint,
    'name'         => key_name
  }
  self.data[:last_modified][:key_pairs][key_name] = Time.now
  self.data[:key_pairs][key_name] = { 'keypair' => data }
  if public_key
    response.body = { 'keypair' => data.merge({'user_id' => Fog::HP::Mock.user_id,}) }
  else
    response.body = { 'keypair' => data.merge({'private_key'  => private_key, 'user_id' => Fog::HP::Mock.user_id}) }
  end
  response
end

#create_persistent_server(name, flavor_id, block_device_mapping, options = {}) ⇒ Object



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
170
171
172
173
# File 'lib/fog/hp/requests/compute_v2/create_persistent_server.rb', line 123

def create_persistent_server(name, flavor_id, block_device_mapping, options = {})
  response = Excon::Response.new

  if block_device_mapping && !block_device_mapping.empty?

    if options['security_groups']
      sec_group_name = options['security_groups'][0]
    else
      sec_group_name = 'default'
    end

    # add the default network
    addresses = {'hpcloud' => [{'version'=>4, 'addr'=>Fog::HP::Mock.ip_address}] }
    if networks = options['networks']
       networks.each do |_|
         addresses["Network #{rand(100)}"] = [{'version'=>4, 'addr'=>Fog::HP::Mock.ip_address}]
       end
    end

    id = Fog::HP::Mock.uuid.to_s
    data = {
      'addresses' => addresses,
      'flavor'    => {"id"=>"#{flavor_id}", "links"=>[{"href"=>"http://nova1:8774/admin/flavors/#{flavor_id}", "rel"=>"bookmark"}]},
      'id'        => id,
      'links'     => [{"href"=>"http://nova1:8774/v1.1/admin/servers/5", "rel"=>"self"}, {"href"=>"http://nova1:8774/admin/servers/5", "rel"=>"bookmark"}],
      'hostId'    => '123456789ABCDEF01234567890ABCDEF',
      'metadata'  => options['metadata'] || {},
      'name'      => name || "server_#{rand(999)}",
      'accessIPv4'  => options['accessIPv4'] || '',
      'accessIPv6'  => options['accessIPv6'] || '',
      'progress'  => 0,
      'status'    => 'BUILD',
      'created'   => '2012-01-01T13:32:20Z',
      'updated'   => '2012-01-01T13:32:20Z',
      'user_id'   => Fog::HP::Mock.user_id.to_s,
      'tenant_id' => Fog::Mock.random_numbers(14).to_s,
      'config_drive' => '',
      'security_groups' => [{'name'=>"#{sec_group_name}"}],
      'key_name'  => options['key_name'] || ''
    }
    self.data[:last_modified][:servers][data['id']] = Time.now
    self.data[:servers][data['id']] = data
    response.headers = {'Content-Length' => '0', 'Content-Type' => 'text/html; charset=UTF-8', 'Date' => Time.now, 'Location' => "http://nova1:8774/v1.1/servers/#{id}"}
    response.body = { 'server' => data.merge({'adminPass' => 'password'}) }
    response.status = 202
    response
  else
    response.status = 400
    raise(Excon::Errors::BadRequest, 'No boot volume or boot image specified')
  end
end

#create_server(name, flavor_id, image_id, options = {}) ⇒ Object



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
# File 'lib/fog/hp/requests/compute_v2/create_server.rb', line 115

def create_server(name, flavor_id, image_id, options = {})
  response = Excon::Response.new
  response.status = 202

  if options['security_groups']
    sec_group_name = options['security_groups'][0]
  else
    sec_group_name = 'default'
  end

  # add the default network
  addresses = {'hpcloud' => [{'version'=>4, 'addr'=>Fog::HP::Mock.ip_address}] }
  if networks = options['networks']
     networks.each do |_|
       addresses["Network #{rand(100)}"] = [{'version'=>4, 'addr'=>Fog::HP::Mock.ip_address}]
     end
  end

  id = Fog::HP::Mock.uuid.to_s
  data = {
    'addresses' => addresses,
    'flavor'    => {"id"=>"#{flavor_id}", "links"=>[{"href"=>"http://nova1:8774/admin/flavors/#{flavor_id}", "rel"=>"bookmark"}]},
    'id'        => id,
    'image'     => {"id"=>"#{image_id}", "links"=>[{"href"=>"http://nova1:8774/admin/images/#{image_id}", "rel"=>"bookmark"}]},
    'links'     => [{"href"=>"http://nova1:8774/v1.1/admin/servers/#{id}", "rel"=>"self"}, {"href"=>"http://nova1:8774/admin/servers/#{id}", "rel"=>"bookmark"}],
    'hostId'    => "123456789ABCDEF01234567890ABCDEF",
    'metadata'  => options['metadata'] || {},
    'name'      => name || "server_#{rand(999)}",
    'accessIPv4'  => options['accessIPv4'] || "",
    'accessIPv6'  => options['accessIPv6'] || "",
    'progress'  => 0,
    'status'    => 'ACTIVE',
    'created'   => "2012-01-01T13:32:20Z",
    'updated'   => "2012-01-01T13:32:20Z",
    'user_id'   => Fog::HP::Mock.user_id.to_s,
    'tenant_id' => Fog::Mock.random_numbers(14).to_s,
    'config_drive' => "",
    'security_groups' => [{"name"=>"#{sec_group_name}"}],
    'key_name'  => options['key_name'] || ""
  }
  self.data[:last_modified][:servers][data['id']] = Time.now
  self.data[:servers][data['id']] = data
  response.headers = {'Content-Length' => '0', 'Content-Type' => 'text/html; charset=UTF-8', 'Date' => Time.now, 'Location' => "http://nova1:8774/v1.1/servers/#{id}"}
  response.body = { 'server' => data.merge({'adminPass' => 'password'}) }
  response
end

#dataObject



219
220
221
# File 'lib/fog/hp/compute_v2.rb', line 219

def data
  self.class.data[@hp_access_key]
end

#delete_image(image_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/hp/requests/compute_v2/delete_image.rb', line 20

def delete_image(image_id)
  response = Excon::Response.new
  if image = list_images_detail.body['images'].find {|_| _['id'] == image_id}
    if image['status'] == 'SAVING'
      response.status = 409
      raise(Excon::Errors.status_error({:expects => 202}, response))
    else
      self.data[:last_modified][:images].delete(image_id)
      self.data[:images].delete(image_id)
      response.status = 202
    end
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#delete_key_pair(key_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/hp/requests/compute_v2/delete_key_pair.rb', line 20

def delete_key_pair(key_name)
  response = Excon::Response.new
  if self.data[:key_pairs][key_name]
    self.data[:last_modified][:key_pairs].delete(key_name)
    self.data[:key_pairs].delete(key_name)
    response.status = 202
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#delete_meta(collection_name, parent_id, key) ⇒ Object



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

def delete_meta(collection_name, parent_id, key)
  if collection_name == "images" then
    if get_image_details(parent_id)
      self.data[:images][parent_id]['metadata'].delete(key)
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  if collection_name == "servers" then
    if get_server_details(parent_id)
      self.data[:servers][parent_id]['metadata'].delete(key)
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  response = Excon::Response.new
  response.status = 204
  response
end

#delete_server(server_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/hp/requests/compute_v2/delete_server.rb', line 20

def delete_server(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    if server['status'] == 'BUILD'
      response.status = 409
      raise(Excon::Errors.status_error({:expects => 202}, response))
    else
      self.data[:last_modified][:servers].delete(server_id)
      self.data[:servers].delete(server_id)
      response.status = 204
    end
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#detach_volume(server_id, volume_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/hp/requests/compute_v2/detach_volume.rb', line 25

def detach_volume(server_id, volume_id)
  response = Excon::Response.new
  if server = self.data[:servers][server_id]
    if server['volumeAttachments'] && server['volumeAttachments'].select {|v| v['volumeId'] == volume_id}
      data = server['volumeAttachments'].reject {|v| v['volumeId'] == volume_id}
      self.data[:servers][server_id]['volumeAttachments'] = data
      response.status = 202
    else
      raise Fog::Compute::HPV2::NotFound
    end
  else
    raise Fog::Compute::HPV2::NotFound
  end
  response
end

#disassociate_address(server_id, ip_address) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/hp/requests/compute_v2/disassociate_address.rb', line 18

def disassociate_address(server_id, ip_address)
  response = Excon::Response.new
  if server = self.data[:servers][server_id]
    data = server['addresses']['custom'].reject {|a| a['addr'] == ip_address}
    self.data[:servers][server_id]['addresses']['custom'] = data

    response.status = 202
  else
    #raise Fog::Compute::HPV2::NotFound
    response.status = 500
    raise(Excon::Errors.status_error({:expects => 202}, response))
  end
  response
end

#get_address(address_id) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/hp/requests/compute_v2/get_address.rb', line 31

def get_address(address_id)
  response = Excon::Response.new
  if address = self.data[:addresses][address_id]
    response.status = 200
    response.body = { 'floating_ip' => address }
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#get_console_output(server_id, num_lines) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/hp/requests/compute_v2/get_console_output.rb', line 22

def get_console_output(server_id, num_lines)
  output = ""
  response = Excon::Response.new
  if list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    (1..num_lines).each {|i| output += "Console Output Line #{i} \r\n"}
    response.body = { 'output' => output }
    response.status = 200
  else
    raise Fog::Compute::HPV2::NotFound
  end
  response
end

#get_flavor_details(flavor_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/hp/requests/compute_v2/get_flavor_details.rb', line 30

def get_flavor_details(flavor_id)
  response = Excon::Response.new
  flavor = {
    '1' => { 'name' => 'standard.xsmall',  'ram' => 1024,    'disk' => 30,   'id' => '1', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 1, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/1", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/1", "rel"=>"bookmark"}] },
    '2' => { 'name' => 'standard.small',   'ram' => 2048,    'disk' => 60,   'id' => '2', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 2, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/2", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/2", "rel"=>"bookmark"}] },
    '3' => { 'name' => 'standard.medium',  'ram' => 4096,    'disk' => 120,  'id' => '3', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 2, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/3", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/3", "rel"=>"bookmark"}] },
    '4' => { 'name' => 'standard.large',   'ram' => 8192,    'disk' => 240,  'id' => '4', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 4, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/4", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/4", "rel"=>"bookmark"}] },
    '5' => { 'name' => 'standard.xlarge',  'ram' => 16384,   'disk' => 480,  'id' => '5', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 4, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/5", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/5", "rel"=>"bookmark"}] },
    '6' => { 'name' => 'standard.2xlarge', 'ram' => 32768,   'disk' => 960,  'id' => '6', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 8, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/6", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/6", "rel"=>"bookmark"}] }
  }[flavor_id]
  if flavor
    response.status = 200
    response.body = {
      'flavor' => flavor
    }
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#get_image_details(image_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/hp/requests/compute_v2/get_image_details.rb', line 37

def get_image_details(image_id)
  response = Excon::Response.new
  if image = list_images_detail.body['images'].find {|_| _['id'] == image_id}
    response.status = [200, 203][rand(1)]
    response.body = { 'image' => image }
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#get_key_pair(key_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/hp/requests/compute_v2/get_key_pair.rb', line 27

def get_key_pair(key_name)
  response = Excon::Response.new
  if key_pair = self.data[:key_pairs][key_name]
    response.status = 200
    response.body = { 'keypair' => key_pair['keypair'] }
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#get_meta(collection_name, parent_id, key) ⇒ Object



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

def get_meta(collection_name, parent_id, key)
  if collection_name == "images" then
    if get_image_details(parent_id)
      raise Fog::Compute::HPV2::NotFound unless midata = self.data[:images][parent_id]['metadata'].fetch(key, nil)
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  if collection_name == "servers" then
    if get_server_details(parent_id)
      raise Fog::Compute::HPV2::NotFound unless midata = self.data[:servers][parent_id]['metadata'].fetch(key, nil)
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  response = Excon::Response.new
  response.status = 200
  response.body = { 'meta' => { key => midata } }
  response
end

#get_server_details(server_id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/fog/hp/requests/compute_v2/get_server_details.rb', line 55

def get_server_details(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    response.status = 200
    response.body = { 'server' => server }
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#get_server_volume_details(server_id, volume_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/hp/requests/compute_v2/get_server_volume_details.rb', line 25

def get_server_volume_details(server_id, volume_id)
  response = Excon::Response.new
  if server = self.data[:servers][server_id]
    if server['volumeAttachments'] && server['volumeAttachments'].select {|v| v['volumeId'] == volume_id}
      data = server['volumeAttachments'].select {|v| v['volumeId'] == volume_id}
      response.body = { 'volumeAttachment' => data[0] }
      response.status = 200
    else
      raise Fog::Compute::HPV2::NotFound
    end
  else
    raise Fog::Compute::HPV2::NotFound
  end
  response
end

#get_vnc_console(server_id, type = 'novnc') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/hp/requests/compute_v2/get_vnc_console.rb', line 24

def get_vnc_console(server_id, type='novnc')
  output = {
      'type' => type,
      'url'  => 'https://region.compute.hpcloud.com/vnc_auto.html?token=123ABX234'
  }
  response = Excon::Response.new
  if list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    response.body = { 'console' => output }
    response.status = 200
  else
    raise Fog::Compute::HPV2::NotFound
  end
  response
end

#get_windows_password(server_id) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/hp/requests/compute_v2/get_windows_password.rb', line 22

def get_windows_password(server_id)
  # need to mock out the private key as well
  private_key = OpenSSL::PKey::RSA.generate(1024)
  public_key = private_key.public_key
  ### The original password is Passw0rd
  encoded_password = encrypt_using_public_key("Passw0rd", public_key)

  if list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    # mock output for this call get_console_output(server_id, 400).body['output']
    log_output = "start junk [cloud-init] Encrypt random password\n-----BEGIN BASE64-ENCODED ENCRYPTED PASSWORD-----\n#{encoded_password}-----END BASE64-ENCODED ENCRYPTED PASSWORD-----\nend junk [cloud-init] Done\n"
    encrypted_password = extract_password_from_log(log_output)
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#list_addressesObject



28
29
30
31
32
33
34
35
36
# File 'lib/fog/hp/requests/compute_v2/list_addresses.rb', line 28

def list_addresses
  response = Excon::Response.new
  addresses = []
  addresses = self.data[:addresses].values unless self.data[:addresses].nil?

  response.status = 200
  response.body = { 'floating_ips' => addresses }
  response
end

#list_availability_zonesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/hp/requests/compute_v2/list_availability_zones.rb', line 25

def list_availability_zones
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'availabilityZoneInfo' => [
        {'zoneState' => {'available' => true},
         'hosts' => nil,
         'zoneName' => 'az1'},
        {'zoneState' => {'available' => true},
         'hosts' => nil,
         'zoneName' => 'az2'}
    ]
  }
  response
end

#list_flavorsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/hp/requests/compute_v2/list_flavors.rb', line 31

def list_flavors
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'flavors' => [
      { 'name' => 'standard.xsmall',   'id' => '1', 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/1", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/1", "rel"=>"bookmark"}] },
      { 'name' => 'standard.small',    'id' => '2', 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/2", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/2", "rel"=>"bookmark"}] },
      { 'name' => 'standard.medium',   'id' => '3', 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/3", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/3", "rel"=>"bookmark"}] },
      { 'name' => 'standard.large',    'id' => '4', 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/4", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/4", "rel"=>"bookmark"}] },
      { 'name' => 'standard.xlarge',   'id' => '5', 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/5", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/5", "rel"=>"bookmark"}] },
      { 'name' => 'standard.2xlarge',  'id' => '6', 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/6", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/6", "rel"=>"bookmark"}] }
    ]
  }
  response
end

#list_flavors_detail(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/hp/requests/compute_v2/list_flavors_detail.rb', line 31

def list_flavors_detail(options = {})
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'flavors' => [
      { 'name' => 'standard.xsmall',  'ram' => 1024,    'disk' => 30,   'id' => '1', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 1, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/1", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/1", "rel"=>"bookmark"}] },
      { 'name' => 'standard.small',   'ram' => 2048,    'disk' => 60,   'id' => '2', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 2, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/2", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/2", "rel"=>"bookmark"}] },
      { 'name' => 'standard.medium',  'ram' => 4096,    'disk' => 120,  'id' => '3', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 2, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/3", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/3", "rel"=>"bookmark"}] },
      { 'name' => 'standard.large',   'ram' => 8192,    'disk' => 240,  'id' => '4', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 4, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/4", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/4", "rel"=>"bookmark"}] },
      { 'name' => 'standard.xlarge',  'ram' => 16384,   'disk' => 480,  'id' => '5', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 4, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/5", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/5", "rel"=>"bookmark"}] },
      { 'name' => 'standard.2xlarge', 'ram' => 32768,   'disk' => 960,  'id' => '6', 'OS-FLV-EXT-DATA:ephemeral' => 20, 'vcpus' => 8, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/flavors/6", "rel"=>"self"}, {"href"=>"http://nova1:8774admin/flavors/6", "rel"=>"bookmark"}] }
    ]
  }
  response
end

#list_images(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/hp/requests/compute_v2/list_images.rb', line 34

def list_images(options = {})
  response = Excon::Response.new
  data = list_images_detail.body['images']
  images = []
  for image in data
    images << image.reject { |key, _| !['id', 'name', 'links'].include?(key) }
  end
  response.status = [200, 203][rand(1)]
  response.body = { 'images' => images }
  response
end

#list_images_detail(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fog/hp/requests/compute_v2/list_images_detail.rb', line 44

def list_images_detail(options = {})
  response = Excon::Response.new

  images = self.data[:images].values
  for image in images
    case image['status']
    when 'SAVING'
      image['status'] = 'ACTIVE'
    end
  end

  response.status = [200, 203][rand(1)]
  response.body = { 'images' => images }
  response
end

#list_key_pairsObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/hp/requests/compute_v2/list_key_pairs.rb', line 25

def list_key_pairs
  response = Excon::Response.new

  key_pairs = []
  key_pairs = self.data[:key_pairs].values unless self.data[:key_pairs].nil?

  response.status = [200, 203][rand(1)]
  response.body = { 'keypairs' => key_pairs }
  response
end

#list_limitsObject



47
48
49
50
51
52
53
54
# File 'lib/fog/hp/requests/compute_v2/list_limits.rb', line 47

def list_limits
  response = Excon::Response.new
  limits = self.data[:limits].values

  response.status = 200
  response.body = { 'limits' => limits }
  response
end

#list_metadata(collection_name, parent_id) ⇒ Object



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

def (collection_name, parent_id)
  mdata = {}
  if collection_name == "images" then
    if get_image_details(parent_id)
      mdata = self.data[:images][parent_id]['metadata']
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  if collection_name == "servers" then
    if get_server_details(parent_id)
      mdata = self.data[:servers][parent_id]['metadata']
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  response = Excon::Response.new
  response.status = 200
  response.body = {'metadata' => mdata}
  response
end

#list_server_addresses(server_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/hp/requests/compute_v2/list_server_addresses.rb', line 25

def list_server_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    response.status = 200
    response.body = { 'addresses' => server['addresses'] }
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#list_server_addresses_by_network(server_id, network_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/hp/requests/compute_v2/list_server_addresses_by_network.rb', line 27

def list_server_addresses_by_network(server_id, network_name)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    response.status = 200
    # get the addresses for the network, which is 'custom' in case of mocks
    address = server['addresses'].select { |key, _| key == network_name}
    response.body = address
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#list_server_volumes(server_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/hp/requests/compute_v2/list_server_volumes.rb', line 30

def list_server_volumes(server_id)
  response = Excon::Response.new
  volumes = []
  if server = self.data[:servers][server_id]
    volumes = server['volumeAttachments']
    response.status = 200
    response.body = { 'volumeAttachments' => volumes }
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#list_servers(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/hp/requests/compute_v2/list_servers.rb', line 35

def list_servers(options = {})
  response = Excon::Response.new
  data = list_servers_detail.body['servers']
  servers = []
  for server in data
    servers << server.reject { |key, value| !['id', 'name', 'links'].include?(key) }
  end
  response.status = 200
  response.body = { 'servers' => servers }
  response
end

#list_servers_detail(options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fog/hp/requests/compute_v2/list_servers_detail.rb', line 56

def list_servers_detail(options = {})
  response = Excon::Response.new

  servers = self.data[:servers].values
  for server in servers
    case server['status']
    when 'BUILD'
      if Time.now - self.data[:last_modified][:servers][server['id']] > Fog::Mock.delay * 2
        server['status'] = 'ACTIVE'
      end
    end
  end

  response.status = 200
  response.body = { 'servers' => servers }
  response
end

#reboot_server(server_id, type = 'SOFT') ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/hp/requests/compute_v2/reboot_server.rb', line 17

def reboot_server(server_id, type = 'SOFT')
  response = Excon::Response.new
  if list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    self.data[:servers][server_id]['status'] = (type == 'SOFT') ? 'REBOOT' : 'HARD_REBOOT'
    response.status = 202
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end

#rebuild_server(server_id, image_id, name, options = {}) ⇒ Object



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
# File 'lib/fog/hp/requests/compute_v2/rebuild_server.rb', line 43

def rebuild_server(server_id, image_id, name, options={})
  if image = list_images_detail.body['images'].find {|_| _['id'] == image_id}
    if response = get_server_details(server_id)
      response.body['server']['name'] = name
      response.body['server']['image']['id'] = image_id
      response.body['server']['image']['name'] = image['name']
      response.body['server']['image']['links'] = image['links']
      response.body['server']['status'] = 'REBUILD'
      l_options = ['metadata', 'accessIPv4', 'accessIPv6']
      l_options.select{|o| options[o]}.each do |key|
        response.body['server'][key] = options[key] || ''
      end
      # personality files are wiped off while rebuilding
      response.body['server']['personality'] = []
      if options['personality']
        for file in options['personality']
          response.body['server']['personality'] << {
            'contents'  => Base64.encode64(file['contents']),
            'path'      => file['path']
          }
        end
      end
      response.status = 202
      response
    else
      raise Fog::Compute::HPV2::NotFound
    end
  else
    raise Fog::Compute::HPV2::NotFound.new("Image with image_id #{image_id} not found.")
  end
end

#release_address(address_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/hp/requests/compute_v2/release_address.rb', line 20

def release_address(address_id)
  response = Excon::Response.new
  if self.data[:addresses][address_id]
    self.data[:last_modified][:addresses].delete(address_id)
    self.data[:addresses].delete(address_id)
    response.status = 202
  else
    raise Fog::Compute::HPV2::NotFound
  end
  response
end

#remove_security_group(server_id, sg_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/hp/requests/compute_v2/remove_security_group.rb', line 18

def remove_security_group(server_id, sg_name)
  response = Excon::Response.new
  if server = self.data[:servers][server_id]
    data = server['security_groups'].reject {|sg| sg['name'] == sg_name}
    self.data[:servers][server_id]['security_groups'] = data

    response.status = 202
  else
    raise Fog::Compute::HPV2::NotFound
  end
  response
end

#reset_dataObject



223
224
225
# File 'lib/fog/hp/compute_v2.rb', line 223

def reset_data
  self.class.data.delete(@hp_access_key)
end

#set_metadata(collection_name, parent_id, metadata = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/hp/requests/compute_v2/set_metadata.rb', line 28

def (collection_name, parent_id,  = {})
  if collection_name == "images" then
    if get_image_details(parent_id)
      self.data[:images][parent_id]['metadata'] = 
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  if collection_name == "servers" then
    if get_server_details(parent_id)
      self.data[:servers][parent_id]['metadata'] = 
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  response = Excon::Response.new
  response.body = { "metadata" =>  }
  response.status = 200
  response
end

#update_meta(collection_name, parent_id, key, value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fog/hp/requests/compute_v2/update_meta.rb', line 29

def update_meta(collection_name, parent_id, key, value)
  if collection_name == "images" then
    if get_image_details(parent_id)
      self.data[:images][parent_id]['metadata'][key] = value
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  if collection_name == "servers" then
    if get_server_details(parent_id)
      self.data[:servers][parent_id]['metadata'][key] = value
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  response = Excon::Response.new
  response.body = { "meta" => { key => value } }
  response.status = 200
  response
end

#update_metadata(collection_name, parent_id, metadata = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/hp/requests/compute_v2/update_metadata.rb', line 28

def (collection_name, parent_id,  = {})
  if collection_name == "images" then
    if get_image_details(parent_id)
       = self.data[:images][parent_id]['metadata'].merge!()
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  if collection_name == "servers" then
    if get_server_details(parent_id)
       = self.data[:servers][parent_id]['metadata'].merge!()
    else
      raise Fog::Compute::HPV2::NotFound
    end
  end

  response = Excon::Response.new
  response.body = { "metadata" =>  }
  response.status = 200
  response
end

#update_server(server_id, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fog/hp/requests/compute_v2/update_server.rb', line 51

def update_server(server_id, options)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    if options['name']
      server['name'] = options['name']
    end
    # remove these keys from the response
    server = server.reject { |key, value| ['key_name', 'security_groups', 'created', 'config_drive', 'OS-EXT-AZ:availability_zone', 'OS-EXT-STS:power_state', 'OS-EXT-STS:task_state', 'OS-EXT-STS:vm_state'].include?(key) }
    response.status = 200
    response.body = { 'server' => server }
    response
  else
    raise Fog::Compute::HPV2::NotFound
  end
end