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

Inherits:
Object
  • Object
show all
Defined in:
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/create_server.rb,
lib/fog/aliyun/requests/compute/delete_server.rb,
lib/fog/aliyun/requests/compute/list_vrouters.rb,
lib/fog/aliyun/requests/compute/list_route_tables.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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.

Raises:

  • (ArgumentError)


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/fog/aliyun/compute.rb', line 198

def initialize(options = {})
  @aliyun_username = options[:aliyun_username]
  @aliyun_user_domain = options[:aliyun_user_domain] || options[:aliyun_domain]
  @aliyun_project_domain = options[:aliyun_project_domain] || options[:aliyun_domain] || 'Default'

  raise ArgumentError, 'Missing required arguments: :aliyun_auth_url' unless options.key?[:aliyun_auth_url]
  @aliyun_auth_uri = URI.parse(options[:aliyun_auth_url])

  @current_tenant = options[:aliyun_tenant]
  @current_tenant_id = options[:aliyun_tenant_id]

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

  management_url = URI.parse(@aliyun_auth_url)
  management_url.port = 8774
  management_url.path = '/v1.1/1'
  @aliyun_management_url = management_url.to_s

  identity_public_endpoint = URI.parse(@aliyun_auth_url)
  identity_public_endpoint.port = 5000
  @aliyun_identity_public_endpoint = identity_public_endpoint.to_s
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



125
126
127
# File 'lib/fog/aliyun/compute.rb', line 125

def auth_token
  @auth_token
end

#auth_token_expirationObject (readonly)

Returns the value of attribute auth_token_expiration.



126
127
128
# File 'lib/fog/aliyun/compute.rb', line 126

def auth_token_expiration
  @auth_token_expiration
end

#current_tenantObject (readonly)

Returns the value of attribute current_tenant.



128
129
130
# File 'lib/fog/aliyun/compute.rb', line 128

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



127
128
129
# File 'lib/fog/aliyun/compute.rb', line 127

def current_user
  @current_user
end

Class Method Details

.dataObject



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

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



194
195
196
# File 'lib/fog/aliyun/compute.rb', line 194

def self.reset
  @data = nil
end

Instance Method Details

#attach_volume(volume_id, server_id, device) ⇒ Object



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

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



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

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



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

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



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/fog/aliyun/requests/compute/create_server.rb', line 109

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 = {}
  response_data = if options['return_reservation_id'] == 'True'
                    { 'reservation_id' => "r-#{Fog::Mock.random_numbers(6)}" }
                  else
                    {
                      '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



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

def credentials
  { provider: 'aliyun',
    aliyun_auth_url: @aliyun_auth_uri.to_s,
    aliyun_auth_token: @auth_token,
    aliyun_management_url: @aliyun_management_url,
    aliyun_identity_endpoint: @aliyun_identity_public_endpoint }
end

#dataObject



222
223
224
# File 'lib/fog/aliyun/compute.rb', line 222

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

#delete_security_group(security_group_id) ⇒ Object



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

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



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

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



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

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

#list_images(_options = {}) ⇒ Object



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

def list_images(_options = {})
  response = Excon::Response.new
  data = list_images_detail.body['images']
  images = []
  for image in data
    images << image.select { |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



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

def list_route_tables(_vrouterid, _options = {})
  response = Excon::Response.new
  data = list_images_detail.body['images']
  images = []
  for image in data
    images << image.select { |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



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

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



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

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

#list_vroutersObject



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

def list_vrouters
  response = Excon::Response.new
  data = list_images_detail.body['VRouters']
  images = []
  for image in data
    images << image.select { |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



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

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

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



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

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

#reset_dataObject



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

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