Class: Fog::Compute::Aliyun::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aliyun/requests/compute/stop_server.rb,
lib/fog/aliyun/compute.rb,
lib/fog/aliyun/requests/compute/list_zones.rb,
lib/fog/aliyun/requests/compute/modify_vpc.rb,
lib/fog/aliyun/requests/compute/attach_disk.rb,
lib/fog/aliyun/requests/compute/list_images.rb,
lib/fog/aliyun/requests/compute/create_image.rb,
lib/fog/aliyun/requests/compute/list_servers.rb,
lib/fog/aliyun/requests/compute/start_server.rb,
lib/fog/aliyun/requests/compute/create_server.rb,
lib/fog/aliyun/requests/compute/delete_server.rb,
lib/fog/aliyun/requests/compute/list_vrouters.rb,
lib/fog/aliyun/requests/compute/reboot_server.rb,
lib/fog/aliyun/requests/compute/list_route_tables.rb,
lib/fog/aliyun/requests/compute/join_security_group.rb,
lib/fog/aliyun/requests/compute/leave_security_group.rb,
lib/fog/aliyun/requests/compute/list_security_groups.rb,
lib/fog/aliyun/requests/compute/create_security_group.rb,
lib/fog/aliyun/requests/compute/delete_security_group.rb,
lib/fog/aliyun/requests/compute/list_security_group_rules.rb

Overview

class Real

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/fog/aliyun/compute.rb', line 192

def initialize(options = {})
  @openstack_username = options[:openstack_username]
  @openstack_user_domain = options[:openstack_user_domain] || options[:openstack_domain]
  @openstack_project_domain = options[:openstack_project_domain] || options[:openstack_domain] || 'Default'
  @openstack_auth_uri = URI.parse(options[:openstack_auth_url])

  @current_tenant = options[:openstack_tenant]
  @current_tenant_id = options[:openstack_tenant_id]

  @auth_token = Fog::Mock.random_base64(64)
  @auth_token_expiration = (Time.now.utc + 86_400).iso8601

  management_url = URI.parse(options[:openstack_auth_url])
  management_url.port = 8774
  management_url.path = '/v1.1/1'
  @openstack_management_url = management_url.to_s

  identity_public_endpoint = URI.parse(options[:openstack_auth_url])
  identity_public_endpoint.port = 5000
  @openstack_identity_public_endpoint = identity_public_endpoint.to_s
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



119
120
121
# File 'lib/fog/aliyun/compute.rb', line 119

def auth_token
  @auth_token
end

#auth_token_expirationObject (readonly)

Returns the value of attribute auth_token_expiration.



120
121
122
# File 'lib/fog/aliyun/compute.rb', line 120

def auth_token_expiration
  @auth_token_expiration
end

#current_tenantObject (readonly)

Returns the value of attribute current_tenant.



122
123
124
# File 'lib/fog/aliyun/compute.rb', line 122

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



121
122
123
# File 'lib/fog/aliyun/compute.rb', line 121

def current_user
  @current_user
end

Class Method Details

.dataObject



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
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fog/aliyun/compute.rb', line 124

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      last_modified: {
        images: {},
        servers: {},
        key_pairs: {},
        security_groups: {},
        addresses: {}
      },
      images: {
        '0e09fbd6-43c5-448a-83e9-0d3d05f9747e' => {
          'id' => '0e09fbd6-43c5-448a-83e9-0d3d05f9747e',
          'name' => 'cirros-0.3.0-x86_64-blank',
          'progress'  => 100,
          'status'    => 'ACTIVE',
          'updated'   => '',
          'minRam'    => 0,
          'minDisk'   => 0,
          'metadata'  => {},
          'links'     => [{ 'href' => 'http://nova1:8774/v1.1/admin/images/1', 'rel' => 'self' }, { 'href' => 'http://nova1:8774/admin/images/2', 'rel' => 'bookmark' }]
        }
      },
      servers: {},
      key_pairs: {},
      security_groups: {
        '0' => {
          'id'          => 0,
          'tenant_id'   => Fog::Mock.random_hex(8),
          'name'        => 'default',
          'description' => 'default',
          'rules'       => [
            { 'id'              => 0,
              'parent_group_id' => 0,
              'from_port'       => 68,
              'to_port'         => 68,
              'ip_protocol'     => 'udp',
              'ip_range'        => { 'cidr' => '0.0.0.0/0' },
              'group'           => {} }
          ]
        }
      },
      server_security_group_map: {},
      addresses: {},
      quota: {
        'security_group_rules' => 20,
        'security_groups' => 10,
        'injected_file_content_bytes' => 10_240,
        'injected_file_path_bytes' => 256,
        'injected_files' => 5,
        'metadata_items' => 128,
        'floating_ips'   => 10,
        'instances'      => 10,
        'key_pairs'      => 10,
        'gigabytes'      => 5000,
        'volumes'        => 10,
        'cores'          => 20,
        'ram'            => 51_200
      },
      volumes: {}
    }
  end
end

.resetObject



188
189
190
# File 'lib/fog/aliyun/compute.rb', line 188

def self.reset
  @data = nil
end

Instance Method Details

#add_security_group(_server_id, _group_name) ⇒ Object



32
33
34
35
36
# File 'lib/fog/aliyun/requests/compute/leave_security_group.rb', line 32

def add_security_group(_server_id, _group_name)
  response = Excon::Response.new
  response.status = 200
  response
end

#attach_volume(volume_id, server_id, device) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fog/aliyun/requests/compute/attach_disk.rb', line 63

def attach_volume(volume_id, server_id, device)
  response = Excon::Response.new
  response.status = 200
  data = {
    'id' => volume_id,
    'volumeId' => volume_id,
    'serverId' => server_id,
    'device'   => device
  }
  self.data[:volumes][volume_id]['attachments'] << data
  response.body = { 'volumeAttachment' => data }
  response
end

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fog/aliyun/requests/compute/create_image.rb', line 52

def create_image(server_id, name,  = {})
  response = Excon::Response.new
  response.status = 202

  img_id = Fog::Mock.random_numbers(6).to_s

  data = {
    'id' => img_id,
    'server' => { 'id' => '3', 'links' => [{ 'href' => "http://nova1:8774/admin/servers/#{server_id}", 'rel' => 'bookmark' }] },
    'links'     => [{ 'href' => "http://nova1:8774/v1.1/admin/images/#{img_id}", 'rel' => 'self' }, { 'href' => "http://nova1:8774/admin/images/#{img_id}", 'rel' => 'bookmark' }],
    'metadata'  =>  || {},
    'name'      => name || "server_#{rand(999)}",
    'progress'  => 0,
    'status'    => 'SAVING',
    'minDisk'   => 0,
    'minRam'    => 0,
    'updated'   => '',
    'created'   => ''
  }
  self.data[:last_modified][:images][data['id']] = Time.now
  self.data[:images][data['id']] = data
  response.body = { 'image' => data }
  response
end

#create_security_group(name, description) ⇒ Object



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/aliyun/requests/compute/create_security_group.rb', line 49

def create_security_group(name, description)
  Fog::Identity::OpenStack.new(openstack_auth_url: credentials[:openstack_auth_url])
  tenant_id = Fog::Identity::OpenStack::V2::Mock.data[current_tenant][:tenants].keys.first
  security_group_id = Fog::Mock.random_numbers(2).to_i + 1
  data[:security_groups][security_group_id.to_s] = {
    'tenant_id' => tenant_id,
    'rules'     => [],
    'id'        => security_group_id,
    'name'      => name,
    'description' => description
  }

  response = Excon::Response.new
  response.status = 200
  response.headers = {
    'X-Compute-Request-Id' => "req-#{Fog::Mock.random_hex(32)}",
    'Content-Type'   => 'application/json',
    'Content-Length' => Fog::Mock.random_numbers(3).to_s,
    'Date'           => Date.new
  }
  response.body = {
    'security_group' => data[:security_groups][security_group_id.to_s]
  }
  response
end

#create_server(name, image_ref, flavor_ref, options = {}) ⇒ Object



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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fog/aliyun/requests/compute/create_server.rb', line 96

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

  server_id = Fog::Mock.random_numbers(6).to_s
  identity = Fog::Identity::OpenStack.new openstack_auth_url: credentials[:openstack_auth_url]
  user = identity.users.find do |u|
    u.name == @openstack_username
  end

  user_id = if user
              user.id
            else
              response = identity.create_user(@openstack_username,
                                              'password',
                                              "#{@openstack_username}@example.com")
              response.body['user']['id']
  end

  mock_data = {
    'addresses'    => { 'Private' => [{ 'addr' => Fog::Mock.random_ip }] },
    'flavor'       => { 'id' => flavor_ref, 'links' => [{ 'href' => 'http://nova1:8774/admin/flavors/1', 'rel' => 'bookmark' }] },
    'id'           => server_id,
    'image'        => { 'id' => image_ref, 'links' => [{ 'href' => "http://nova1:8774/admin/images/#{image_ref}", 'rel' => 'bookmark' }] },
    '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-09-27T00:04:18Z',
    'updated'      => '2012-09-27T00:04:27Z',
    'user_id'      => user_id,
    'config_drive' => options['config_drive'] || ''
  }

  if nics = options['nics']
    nics.each do |_nic|
      mock_data['addresses'].merge!(
        'Public' => [{ 'addr' => Fog::Mock.random_ip }]
      )
    end
  end

  response_data = {}
  if options['return_reservation_id'] == 'True'
    response_data = { 'reservation_id' => "r-#{Fog::Mock.random_numbers(6)}" }
  else
    response_data = {
      'adminPass'       => 'password',
      'id'              => server_id,
      'links'           => mock_data['links']
    }
  end

  if block_devices = options['block_device_mapping_v2']
    block_devices.each { |bd| compute.volumes.get(bd[:uuid]).attach(server_id, bd[:device_name]) }
  elsif block_device = options['block_device_mapping']
    compute.volumes.get(block_device[:volume_id]).attach(server_id, block_device[:device_name])
  end

  data[:last_modified][:servers][server_id] = Time.now
  data[:servers][server_id] = mock_data
  if options['security_groups']
    groups = Array(options['security_groups']).map do |sg|
      if sg.is_a?(Fog::Compute::OpenStack::SecurityGroup)
        sg.name
      else
        sg
      end
    end

    data[:server_security_group_map][server_id] = groups
    response_data['security_groups'] = groups
  end

  data[:last_modified][:servers][server_id] = Time.now
  data[:servers][server_id] = mock_data
  response.body = if options['return_reservation_id'] == 'True'
                    response_data
                  else
                    { 'server' => response_data }
                  end
  response
end

#credentialsObject



222
223
224
225
226
227
228
# File 'lib/fog/aliyun/compute.rb', line 222

def credentials
  { provider: 'openstack',
    openstack_auth_url: @openstack_auth_uri.to_s,
    openstack_auth_token: @auth_token,
    openstack_management_url: @openstack_management_url,
    openstack_identity_endpoint: @openstack_identity_public_endpoint }
end

#dataObject



214
215
216
# File 'lib/fog/aliyun/compute.rb', line 214

def data
  self.class.data["#{@openstack_username}-#{@current_tenant}"]
end

#delete_security_group(security_group_id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aliyun/requests/compute/delete_security_group.rb', line 35

def delete_security_group(security_group_id)
  data[:security_groups].delete security_group_id.to_s

  response = Excon::Response.new
  response.status = 202
  response.headers = {
    'Content-Type' => 'text/html; charset=UTF-8',
    'Content-Length' => '0',
    'Date' => Date.new
  }
  response.body = {}
  response
end

#delete_server(server_id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/aliyun/requests/compute/delete_server.rb', line 29

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: 204 }, response))
    else
      data[:last_modified][:servers].delete(server_id)
      data[:servers].delete(server_id)
      response.status = 204
    end
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#get_security_group(security_group_id) ⇒ Object



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

def get_security_group(security_group_id)
  security_group = data[:security_groups][security_group_id.to_s]
  response = Excon::Response.new
  if security_group
    response.status = 200
    response.headers = {
      'X-Compute-Request-Id' => 'req-63a90344-7c4d-42e2-936c-fd748bced1b3',
      'Content-Type' => 'application/json',
      'Content-Length' => '167',
      'Date' => Date.new
    }
    response.body = {
      'security_group' => security_group
    }
  else
    raise Fog::Compute::OpenStack::NotFound, "Security group #{security_group_id} does not exist"
  end
  response
end

#join_security_group(_server_id, _group_id) ⇒ Object



32
33
34
35
36
# File 'lib/fog/aliyun/requests/compute/join_security_group.rb', line 32

def join_security_group(_server_id, _group_id)
  response = Excon::Response.new
  response.status = 200
  response
end

#list_images(_options = {}) ⇒ Object



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

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

#list_route_tables(_vrouterid, _options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fog/aliyun/requests/compute/list_route_tables.rb', line 50

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

#list_security_groups(server_id = nil) ⇒ Object



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
# File 'lib/fog/aliyun/requests/compute/list_security_groups.rb', line 49

def list_security_groups(server_id = nil)
  security_groups = data[:security_groups].values

  groups = if server_id
             server_group_names =
               Array(data[:server_security_group_map][server_id])

             server_group_names.map do |name|
               security_groups.find do |sg|
                 sg['name'] == name
               end
             end.compact
           else
             security_groups
           end

  Excon::Response.new(
    body: { 'security_groups' => groups },
    headers: {
      'X-Compute-Request-Id' => "req-#{Fog::Mock.random_base64(36)}",
      'Content-Type' => 'application/json',
      'Date' => Date.new
    },
    status: 200
  )
end

#list_servers(_options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/aliyun/requests/compute/list_servers.rb', line 57

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

#list_vroutersObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/aliyun/requests/compute/list_vrouters.rb', line 47

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

#list_zones(*_args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/aliyun/requests/compute/list_zones.rb', line 27

def list_zones(*_args)
  Excon::Response.new(
    body: { 'availabilityZoneInfo' => [
      {
        'zoneState' => {
          'available' => true
        },
        'hosts' => nil,
        'zoneName' => 'nova'
      }
    ] },
    status: 200
  )
end

#modify_vpc(_vpcId, _options = {}) ⇒ Object



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
# File 'lib/fog/aliyun/requests/compute/modify_vpc.rb', line 45

def modify_vpc(_vpcId, _options = {})
  Fog::Identity::OpenStack.new(openstack_auth_url: credentials[:openstack_auth_url])
  tenant_id = Fog::Identity::OpenStack::V2::Mock.data[current_tenant][:tenants].keys.first
  security_group_id = Fog::Mock.random_numbers(2).to_i + 1
  data[:security_groups][security_group_id.to_s] = {
    'tenant_id' => tenant_id,
    'rules'     => [],
    'id'        => security_group_id,
    'name'      => name,
    'description' => description
  }

  response = Excon::Response.new
  response.status = 200
  response.headers = {
    'X-Compute-Request-Id' => "req-#{Fog::Mock.random_hex(32)}",
    'Content-Type'   => 'application/json',
    'Content-Length' => Fog::Mock.random_numbers(3).to_s,
    'Date'           => Date.new
  }
  response.body = {
    'security_group' => data[:security_groups][security_group_id.to_s]
  }
  response
end

#reboot_server(_server_id, _type = 'SOFT') ⇒ Object



35
36
37
38
39
# File 'lib/fog/aliyun/requests/compute/reboot_server.rb', line 35

def reboot_server(_server_id, _type = 'SOFT')
  response = Excon::Response.new
  response.status = 202
  response
end

#reset_dataObject



218
219
220
# File 'lib/fog/aliyun/compute.rb', line 218

def reset_data
  self.class.data.delete("#{@openstack_username}-#{@current_tenant}")
end

#start_server(_server_id) ⇒ Object



35
36
37
# File 'lib/fog/aliyun/requests/compute/start_server.rb', line 35

def start_server(_server_id)
  true
end

#stop_server(_server_id) ⇒ Object



35
36
37
# File 'lib/fog/aliyun/requests/compute/stop_server.rb', line 35

def stop_server(_server_id)
  true
end