Class: Fog::Compute::RackspaceV2::Mock

Inherits:
Rackspace::Service show all
Includes:
Rackspace::MockData
Defined in:
lib/fog/rackspace/compute_v2.rb,
lib/fog/rackspace/requests/compute_v2/get_image.rb,
lib/fog/rackspace/requests/compute_v2/get_flavor.rb,
lib/fog/rackspace/requests/compute_v2/get_server.rb,
lib/fog/rackspace/requests/compute_v2/get_network.rb,
lib/fog/rackspace/requests/compute_v2/list_images.rb,
lib/fog/rackspace/requests/compute_v2/create_image.rb,
lib/fog/rackspace/requests/compute_v2/delete_image.rb,
lib/fog/rackspace/requests/compute_v2/list_flavors.rb,
lib/fog/rackspace/requests/compute_v2/list_servers.rb,
lib/fog/rackspace/requests/compute_v2/set_metadata.rb,
lib/fog/rackspace/requests/compute_v2/attach_volume.rb,
lib/fog/rackspace/requests/compute_v2/create_server.rb,
lib/fog/rackspace/requests/compute_v2/delete_server.rb,
lib/fog/rackspace/requests/compute_v2/list_metadata.rb,
lib/fog/rackspace/requests/compute_v2/list_networks.rb,
lib/fog/rackspace/requests/compute_v2/reboot_server.rb,
lib/fog/rackspace/requests/compute_v2/resize_server.rb,
lib/fog/rackspace/requests/compute_v2/update_server.rb,
lib/fog/rackspace/requests/compute_v2/create_network.rb,
lib/fog/rackspace/requests/compute_v2/delete_network.rb,
lib/fog/rackspace/requests/compute_v2/get_attachment.rb,
lib/fog/rackspace/requests/compute_v2/list_addresses.rb,
lib/fog/rackspace/requests/compute_v2/rebuild_server.rb,
lib/fog/rackspace/requests/compute_v2/update_metadata.rb,
lib/fog/rackspace/requests/compute_v2/list_attachments.rb,
lib/fog/rackspace/requests/compute_v2/delete_attachment.rb,
lib/fog/rackspace/requests/compute_v2/get_metadata_item.rb,
lib/fog/rackspace/requests/compute_v2/set_metadata_item.rb,
lib/fog/rackspace/requests/compute_v2/delete_metadata_item.rb,
lib/fog/rackspace/requests/compute_v2/revert_resize_server.rb,
lib/fog/rackspace/requests/compute_v2/confirm_resize_server.rb,
lib/fog/rackspace/requests/compute_v2/change_server_password.rb,
lib/fog/rackspace/requests/compute_v2/list_addresses_by_network.rb

Constant Summary collapse

RESPONSE_BODY =
{ 
  "addresses" => {
    "public"=>[{"version"=>6, "addr"=>"2001:4800:7811:0513:0fe1:75e8:ff04:760b"}, {"version"=>4, "addr"=>"166.78.18.176"}],
    "private"=>[{"version"=>4, "addr"=>"10.181.129.68"}]
  }
}

Constants included from Rackspace::MockData

Rackspace::MockData::NOT_FOUND_ID

Instance Method Summary collapse

Methods included from Rackspace::MockData

#data, ipv4_address, ipv6_address, keep, slice, uuid, zulu_time

Methods inherited from Rackspace::Service

#authenticate, #endpoint_uri, #region, #service_name

Constructor Details

#initialize(options) ⇒ Mock

Returns a new instance of Mock.



77
78
79
# File 'lib/fog/rackspace/compute_v2.rb', line 77

def initialize(options)
  @rackspace_api_key = options[:rackspace_api_key]
end

Instance Method Details

#attach_volume(server_id, volume_id, device) ⇒ Object



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

def attach_volume(server_id, volume_id, device)
  volume = self.data[:volumes][volume_id]
  server = self.data[:servers][server_id]

  if server.nil? || volume.nil?
    raise Fog::Compute::RackspaceV2::NotFound
  else
    self.data[:volume_attachments] << {
      "device"   => device,
      "serverId" => server_id,
      "volumeId" => volume_id,
      "id"       => volume_id,
    }

    volume["attachments"] << {
      "volumeId" => volume_id,
      "serverId" => server_id,
      "device"   => device,
      "id"       => volume_id,
    }

    body = {
      "volumeAttachment" => {
        "serverId" => server_id,
        "volumeId" => volume_id,
        "device"   => device,
        "id"       => volume_id,
      }
    }

    volume["status"] = "in-use"

    response(:body => body)
  end
end

#change_server_password(server_id, password) ⇒ Object



29
30
31
# File 'lib/fog/rackspace/requests/compute_v2/change_server_password.rb', line 29

def change_server_password(server_id, password)
  response(:status => 202)
end

#confirm_resize_server(server_id) ⇒ Object



29
30
31
32
33
# File 'lib/fog/rackspace/requests/compute_v2/confirm_resize_server.rb', line 29

def confirm_resize_server(server_id)
  server = self.data[:servers][server_id]
  server["status"] = "ACTIVE"
  response(:status => 204)
end

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



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

def create_image(server_id, name, options = {})
  image_id = Fog::Rackspace::MockData.uuid
  image = {
    "OS-DCF:diskConfig" => "AUTO",
    "created"           => "2012-02-28T19:38:57Z",
    "id"                => image_id,
    "links"             => [
      {
        "href"              => "https://dfw.servers.api.rackspacecloud.com/v2/010101/images/#{image_id}",
        "rel"               => "self"
      },
      {
        "href"              => "https://dfw.servers.api.rackspacecloud.com/010101/images/#{image_id}",
        "rel"               => "bookmark"
      },
      {
        "href"              => "https://dfw.servers.api.rackspacecloud.com/010101/images/#{image_id}",
        "rel"               => "alternate",
        "type"              => "application/vnd.openstack.image"
      }
    ],
    "metadata"                              => {
      "arch"                                  => "x86-64",
      "auto_disk_config"                      => "True",
      "com.rackspace__1__build_core"          => "1",
      "com.rackspace__1__build_managed"       => "0",
      "com.rackspace__1__build_rackconnect"   => "0",
      "com.rackspace__1__options"             => "0",
      "com.rackspace__1__visible_core"        => "1",
      "com.rackspace__1__visible_managed"     => "0",
      "com.rackspace__1__visible_rackconnect" => "0",
      "image_type"                            => "base",
      "org.openstack__1__architecture"        => "x64",
      "org.openstack__1__os_distro"           => "org.ubuntu",
      "org.openstack__1__os_version"          => "11.10",
      "os_distro"                             => "ubuntu",
      "os_type"                               => "linux",
      "os_version"                            => "11.10",
      "rax_managed"                           => "false",
      "rax_options"                           => "0"
    },
    "minDisk"  => 10,
    "minRam"   => 256,
    "name"     => "Ubuntu 11.10",
    "progress" => 100,
    "status"   => "SAVING",
    "updated"  => "2012-02-28T19:39:05Z"
  }

  self.data[:images][image_id] = image

  response(
    :status => 202,
    :headers => {"Location" => "/#{image_id}"}
  )
end

#create_network(label, cidr) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/rackspace/requests/compute_v2/create_network.rb', line 23

def create_network(label, cidr)
  network_id = Fog::Rackspace::MockData.uuid

  self.data[:networks][network_id] = {
    'id' => network_id,
    'label' => label,
    'cidr' => cidr
  }
  response(:body => { 'network' => self.data[:networks][network_id] })
end

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



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

def create_server(name, image_id, flavor_id, min_count, max_count, options={})
  server_id   = Fog::Rackspace::MockData.uuid
  public_ip4  = Fog::Rackspace::MockData.ipv4_address
  public_ip6  = Fog::Rackspace::MockData.ipv6_address
  private_ip4 = Fog::Rackspace::MockData.ipv4_address
  private_ip6 = Fog::Rackspace::MockData.ipv6_address
  admin_pass  = Fog::Mock.random_letters(12)

  flavor = self.data[:flavors][flavor_id]
  image  = self.data[:images][image_id]

  server = {
    "OS-DCF:diskConfig"      => "AUTO",
    "OS-EXT-STS:power_state" => 1,
    "OS-EXT-STS:task_state"  => nil,
    "OS-EXT-STS:vm_state"    => "active",
    "accessIPv4" => public_ip4,
    "accessIPv6" => public_ip6,
    "addresses" => {
      "private" => [
        {
          "addr" => private_ip4,
          "version" => 4
        }
      ],
        "public" => [
          {
            "addr" => public_ip4,
            "version" => 4
          },
          {
            "addr" => public_ip6,
            "version" => 6
          }
      ]
    },
    "created" => "2012-07-28T15:32:25Z",
    "flavor" => Fog::Rackspace::MockData.keep(flavor, "id", "links"),
    "hostId" => Fog::Mock.random_hex(56),
    "id" => server_id,
    "image"  => Fog::Rackspace::MockData.keep(image, "id", "links"),
    "links" => [
      {
        "href" => "https://dfw.servers.api.rackspacecloud.com/v2/010101/servers/#{server_id}",
        "rel" => "self",
      },
      {
        "href" => "https://dfw.servers.api.rackspacecloud.com/010101/servers/#{server_id}",
        "rel" => "bookmark",
      }
    ],
    "metadata" => {},
    "name" => name,
    "progress" => 100,
    "rax-bandwidth:bandwidth" => [
      {
        "audit_period_end"   => "2012-08-16T14:12:00Z",
        "audit_period_start" => "2012-08-16T06:00:00Z",
        "bandwidth_inbound"  => 39147845,
        "bandwidth_outbound" => 13390651,
        "interface"          => "public",
      },
      {
        "audit_period_end"   => "2012-08-16T14:12:00Z",
        "audit_period_start" => "2012-08-16T06:00:00Z",
        "bandwidth_inbound"  => 24229191,
        "bandwidth_outbound" => 84,
        "interface"          => "private",
      }
    ],
    "status"             => "ACTIVE",
    "tenant_id"          => "010101",
    "updated"            => "2012-07-28T15:37:09Z",
    "user_id"            => "170454",
    :volume_ids          => [],
  }

  self.data[:servers][server_id] = server

  response = {
    "server" => {
      "OS-DCF:diskConfig" => "AUTO",
      "adminPass" => admin_pass,
      "id" => server_id,
      "links" => [
        {
          "href" => "https://dfw.servers.api.rackspacecloud.com/v2/010101/servers/#{server_id}",
          "rel"  => "self"
          },
          {
          "href" => "https://dfw.servers.api.rackspacecloud.com/010101/servers/#{server_id}",
          "rel"  => "bookmark"
        }
      ]
    }
  }

  response(:body => response)
end

#delete_attachment(server_id, volume_id) ⇒ Object



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

def delete_attachment(server_id, volume_id)
  volume     = self.data[:volumes][volume_id]
  server     = self.data[:servers][server_id]
  if volume.nil? || server.nil?
    raise Fog::Compute::RackspaceV2::NotFound
  else
    self.data[:volume_attachments].delete_if { |v| v["serverId"] == server_id && v["volumeId"] == volume_id }
    volume["attachments"].delete_if { |v| v["serverId"] == server_id && v["volumeId"] == volume_id }

    response(:status => 204)
  end
end

#delete_image(image_id) ⇒ Object



20
21
22
23
24
# File 'lib/fog/rackspace/requests/compute_v2/delete_image.rb', line 20

def delete_image(image_id)
  response = Excon::Response.new
  response.status = 202
  response.body = "" 
end

#delete_metadata_item(collection, obj_id, key) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/fog/rackspace/requests/compute_v2/delete_metadata_item.rb', line 21

def (collection, obj_id, key)
  raise Fog::Compute::RackspaceV2::NotFound if obj_id == 0
  
  response = Excon::Response.new
  response.body = ""                      
  response.status = 204
  response            
end

#delete_network(id) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/fog/rackspace/requests/compute_v2/delete_network.rb', line 11

def delete_network(id)
  unless self.data[:networks].has_key?(id)
    raise Fog::Compute::RackspaceV2::NotFound
  end

  response(:body => '', :status => 202)
end

#delete_server(server_id) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/fog/rackspace/requests/compute_v2/delete_server.rb', line 19

def delete_server(server_id)
  self.data[:servers].delete(server_id)
  volumes = self.data[:volumes].values
  volumes.each do |v|
    v["attachments"].delete_if { |a| a["serverId"] == server_id }
    v["status"] = "available" if v["attachments"].empty?
  end
  response(:status => 204)
end

#get_attachment(server_id, volume_id) ⇒ Object



27
28
29
30
31
# File 'lib/fog/rackspace/requests/compute_v2/get_attachment.rb', line 27

def get_attachment(server_id, volume_id)
  attachment = self.data[:volume_attachments].detect { |v| v["serverId"] == server_id && v["volumeId"] == volume_id }

  response(:body => {"volumeAttachment" => attachment})
end

#get_flavor(flavor_id) ⇒ Object



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

def get_flavor(flavor_id)
  flavor = self.data[:flavors][flavor_id]
  if flavor.nil?
    raise Fog::Compute::RackspaceV2::NotFound
  else
    response(:body => {"flavor" => flavor})
  end
end

#get_image(image_id) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/fog/rackspace/requests/compute_v2/get_image.rb', line 33

def get_image(image_id)
  image = self.data[:images][image_id]
  if image.nil?
    raise Fog::Compute::RackspaceV2::NotFound
  else
    response(:body => {"image" => image})
  end
end

#get_metadata_item(collection, obj_id, key) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/fog/rackspace/requests/compute_v2/get_metadata_item.rb', line 24

def (collection, obj_id, key)
  raise Fog::Compute::RackspaceV2::NotFound if obj_id == 0
  
  response = Excon::Response.new
  response.status = 202
  response.body = {"meta" => {"environment" => "dev"}}
  response            
end

#get_network(id) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/fog/rackspace/requests/compute_v2/get_network.rb', line 11

def get_network(id)
  unless self.data[:networks].has_key?(id)
    raise Fog::Compute::RackspaceV2::NotFound
  end

  response(:body => { 'network' => self.data[:networks][id] })
end

#get_server(server_id) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/rackspace/requests/compute_v2/get_server.rb', line 47

def get_server(server_id)
  server = self.data[:servers][server_id]
  if server.nil?
    raise Fog::Compute::RackspaceV2::NotFound
  else
    server_response = Fog::Rackspace::MockData.keep(server, 'id', 'name', 'hostId', 'created', 'updated', 'status', 'progress', 'user_id', 'tenant_id', 'links', 'metadata', 'accessIPv4', 'accessIPv6', 'OS-DCF:diskConfig', 'rax-bandwidth:bandwidth', 'addresses', 'flavor', 'image')
    server_response['image']['links'].map! { |l| l.delete("type"); l }
    response(:body => {"server" => server_response})
  end
end

#list_addresses(server_id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/rackspace/requests/compute_v2/list_addresses.rb', line 23

def list_addresses(server_id)
  raise Fog::Compute::RackspaceV2::NotFound.new if server_id == 0
  response        = Excon::Response.new
  response.status = 200
  response.body   = { 
    "addresses" => {
      "public"=>[{"version"=>6, "addr"=>"2001:4800:7811:0513:0fe1:75e8:ff04:760b"}, {"version"=>4, "addr"=>"166.78.18.176"}],
      "private"=>[{"version"=>4, "addr"=>"10.181.129.68"}]
    }
  }
  response
end

#list_addresses_by_network(server_id, network_id) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/fog/rackspace/requests/compute_v2/list_addresses_by_network.rb', line 37

def list_addresses_by_network(server_id, network_id)
  raise Fog::Compute::RackspaceV2::NotFound.new if server_id == 0
  response        = Excon::Response.new
  response.status = 200
  response.body   = { network_id => RESPONSE_BODY["addresses"][network_id] }
  response
end

#list_attachments(server_id) ⇒ Object



27
28
29
30
# File 'lib/fog/rackspace/requests/compute_v2/list_attachments.rb', line 27

def list_attachments(server_id)
  volumes_array = self.data[:volume_attachments].select { |va| va["serverId"] == server_id }
  response(:body => {"volumeAttachments" => volumes_array})
end

#list_flavorsObject



25
26
27
28
# File 'lib/fog/rackspace/requests/compute_v2/list_flavors.rb', line 25

def list_flavors
  flavors = self.data[:flavors].values.map { |f| Fog::Rackspace::MockData.keep(f, 'id', 'name', 'links') }
  response(:body => {"flavors" => flavors})
end

#list_imagesObject



25
26
27
28
# File 'lib/fog/rackspace/requests/compute_v2/list_images.rb', line 25

def list_images
  images = self.data[:images].values
  response(:body => {"images" => images})
end

#list_metadata(collection, obj_id) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/fog/rackspace/requests/compute_v2/list_metadata.rb', line 23

def (collection, obj_id)
  raise Fog::Compute::RackspaceV2::NotFound if obj_id == 0

  response = Excon::Response.new
  response.status = 202
  response.body = { "metadata"=>{"tag"=>"database"} }
  response
end

#list_networksObject



11
12
13
14
# File 'lib/fog/rackspace/requests/compute_v2/list_networks.rb', line 11

def list_networks
  networks = self.data[:networks].values
  response(:body => { 'networks' => networks })
end

#list_serversObject



46
47
48
49
# File 'lib/fog/rackspace/requests/compute_v2/list_servers.rb', line 46

def list_servers
  servers = self.data[:servers].values.map { |s| Fog::Rackspace::MockData.keep(s, 'id', 'name', 'hostId', 'created', 'updated', 'status', 'progress', 'user_id', 'tenant_id', 'links', 'metadata') }
  response(:body => {"servers" => servers})
end

#reboot_server(server_id, type) ⇒ Object



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

def reboot_server(server_id, type)
  body = {
    "reboot" => {
      "type" => type.upcase
    }
  }
  response(
    :body => body,
    :status => 202
  )
end

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



58
59
60
61
62
63
64
# File 'lib/fog/rackspace/requests/compute_v2/rebuild_server.rb', line 58

def rebuild_server(server_id, image_id, options={})
  server = self.data[:servers][server_id]
  response(
    :body => {"server" => server},
    :status => 202
  )
end

#request(params) ⇒ Object



81
82
83
# File 'lib/fog/rackspace/compute_v2.rb', line 81

def request(params)
  Fog::Mock.not_implemented
end

#resize_server(server_id, flavor_id) ⇒ Object



32
33
34
35
36
# File 'lib/fog/rackspace/requests/compute_v2/resize_server.rb', line 32

def resize_server(server_id, flavor_id)
  server = self.data[:servers][server_id]
  server["status"] = "VERIFY_RESIZE"
  response(:status => 202)
end

#response(params = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fog/rackspace/compute_v2.rb', line 85

def response(params={})
  body    = params[:body] || {}
  status  = params[:status] || 200
  headers = params[:headers] || {}

  response = Excon::Response.new(:body => body, :headers => headers, :status => status)
  if params.has_key?(:expects) && ![*params[:expects]].include?(response.status)
    raise(Excon::Errors.status_error(params, response))
  else response
  end
end

#revert_resize_server(server_id) ⇒ Object



30
31
32
33
34
# File 'lib/fog/rackspace/requests/compute_v2/revert_resize_server.rb', line 30

def revert_resize_server(server_id)
  server = self.data[:servers][server_id]
  server["status"] = "ACTIVE"
  response(:status => 202)
end

#set_metadata(collection, obj_id, metadata = {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/fog/rackspace/requests/compute_v2/set_metadata.rb', line 26

def (collection, obj_id,  = {})
  raise Fog::Compute::RackspaceV2::NotFound if obj_id == 0
          
  response = Excon::Response.new
  response.status = 202
  response.body = {"metadata"=>{"environment"=>"dev"}}
  response
end

#set_metadata_item(collection, obj_id, key, value) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/fog/rackspace/requests/compute_v2/set_metadata_item.rb', line 26

def (collection, obj_id, key, value)
  raise Fog::Compute::RackspaceV2::NotFound if obj_id == 0
  
  response = Excon::Response.new
  response.status = 202
  response.body = {"meta" => {key => value}}
  response            
end

#update_metadata(collection, obj_id, metadata = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/fog/rackspace/requests/compute_v2/update_metadata.rb', line 25

def (collection, obj_id,  = {})
  raise Fog::Compute::RackspaceV2::NotFound if obj_id == 0
  
  response = Excon::Response.new
  response.status = 202
  response.body = {"metadata" => {"environment" => "dev", "tag" => "database"}}
  response            
end

#update_server(server_id, name) ⇒ Object



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

def update_server(server_id, name)
  server          = self.data[:servers][server_id]
  name.each_pair {|k,v| server[k] = v } if name.is_a?(Hash)
  server['name']  = name if name.is_a?(String)
  server_response = Fog::Rackspace::MockData.keep(server, 'id', 'name', 'hostId', 'created', 'updated', 'status', 'progress', 'user_id', 'tenant_id', 'links', 'metadata', 'accessIPv4', 'accessIPv6', 'OS-DCF:diskConfig', 'rax-bandwidth:bandwidth', 'addresses', 'flavor', 'links', 'image')

  response(
    :status => 200,
    :body   => {'server' => server_response}
  )
end