class Fog::Compute::Aliyun::Mock

Attributes

auth_token[R]
auth_token_expiration[R]
current_tenant[R]
current_user[R]

Public Class Methods

data() click to toggle source
# File lib/fog/aliyun/compute.rb, line 128
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' => 10240,
        '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'            => 51200
      },
      :volumes => {}
    }
  end
end
new(options={}) click to toggle source
# File lib/fog/aliyun/compute.rb, line 196
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 + 86400).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
reset() click to toggle source
# File lib/fog/aliyun/compute.rb, line 192
def self.reset
  @data = nil
end

Public Instance Methods

add_security_group(server_id, group_name) click to toggle source
# File lib/fog/aliyun/requests/compute/leave_security_group.rb, line 33
def add_security_group(server_id, group_name)
  response = Excon::Response.new
  response.status = 200
  response
end
allocate_address(pool = nil) click to toggle source
# File lib/fog/aliyun/requests/compute/allocate_eip_address.rb, line 57
def allocate_address(pool = nil)
  response = Excon::Response.new
  response.status = 200
  response.headers = {
    "X-Compute-Request-Id" => "req-d4a21158-a86c-44a6-983a-e25645907f26",
    "Content-Type" => "application/json",
    "Content-Length" => "105",
    "Date"=> Date.new
  }
  response.body = {
    "floating_ip" => {
      "instance_id" => nil,
      "ip" => "192.168.27.132",
      "fixed_ip" => nil,
      "id" => 4,
      "pool"=>"nova"
    }
  }
  response
end
attach_volume(volume_id, server_id, device) click to toggle source
# File lib/fog/aliyun/requests/compute/attach_disk.rb, line 66
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={}) click to toggle source
# File lib/fog/aliyun/requests/compute/create_image.rb, line 52
def create_image(server_id, name, metadata={})
  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'  => 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) click to toggle source
# File lib/fog/aliyun/requests/compute/create_disk.rb, line 140
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
  self.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' => self.data[:security_groups][security_group_id.to_s]
  }
  response
end
create_security_group_rule(parent_group_id, ip_protocol, from_port, to_port, cidr, group_id=nil) click to toggle source
# File lib/fog/aliyun/requests/compute/create_security_group_egress_ip_rule.rb, line 74
def create_security_group_rule(parent_group_id, ip_protocol, from_port, to_port, cidr, group_id=nil)
  parent_group_id = parent_group_id.to_i
  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
  }
  rule = {
    'id' => Fog::Mock.random_numbers(2).to_i,
    'from_port'   => from_port,
    'group'       => group_id || {},
    'ip_protocol' => ip_protocol,
    'to_port'     => to_port,
    'parent_group_id' => parent_group_id,
    'ip_range' => {
      'cidr'   => cidr
    }
  }
  self.data[:security_groups][parent_group_id.to_s]['rules'].push(rule)
  response.body = {
    'security_group_rule' => rule
  }
  response
end
create_server(name, image_ref, flavor_ref, options = {}) click to toggle source
# File lib/fog/aliyun/requests/compute/create_server.rb, line 98
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 { |u|
    u.name == @openstack_username
  }

  user_id = if user then
              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'      => @openstack_username,
    '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' then
    response_data = { 'reservation_id' => "r-#{Fog::Mock.random_numbers(6).to_s}" }
  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

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

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

  self.data[:last_modified][:servers][server_id] = Time.now
  self.data[:servers][server_id] = mock_data
  if options['return_reservation_id'] == 'True' then
    response.body = response_data
  else
    response.body = { 'server' => response_data }
  end
  response
end
credentials() click to toggle source
# File lib/fog/aliyun/compute.rb, line 226
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
data() click to toggle source
# File lib/fog/aliyun/compute.rb, line 218
def data
  self.class.data["#{@openstack_username}-#{@current_tenant}"]
end
delete_security_group(security_group_id) click to toggle source
# File lib/fog/aliyun/requests/compute/delete_security_group.rb, line 35
def delete_security_group(security_group_id)
  self.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) click to toggle source
# 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
      self.data[:last_modified][:servers].delete(server_id)
      self.data[:servers].delete(server_id)
      response.status = 204
    end
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end
get_security_group(security_group_id) click to toggle source
# File lib/fog/aliyun/requests/compute/list_security_group_rules.rb, line 52
def get_security_group(security_group_id)
  security_group = self.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) click to toggle source
# File lib/fog/aliyun/requests/compute/join_security_group.rb, line 33
def join_security_group(server_id, group_id)
  response = Excon::Response.new
  response.status = 200
  response
end
list_images() click to toggle source
# File lib/fog/aliyun/requests/compute/create_snapshot.rb, line 46
def list_images
  response = Excon::Response.new
  data = list_images_detail.body['images']
  images = []
  for image in data
    images << image.reject { |key, value| !['id', 'name', 'links'].include?(key) }
  end
  response.status = [200, 203][rand(1)]
  response.body = { 'images' => images }
  response
end
list_route_tables(vrouterid, options={}) click to toggle source
# File lib/fog/aliyun/requests/compute/list_route_tables.rb, line 53
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| !['id', 'name', 'links'].include?(key) }
  end
  response.status = [200, 203][rand(1)]
  response.body = { 'images' => images }
  response
end
list_security_groups(server_id = nil) click to toggle source
# File lib/fog/aliyun/requests/compute/list_security_groups.rb, line 52
def list_security_groups(server_id = nil)
  security_groups = self.data[:security_groups].values

  groups = if server_id then
             server_group_names =
               Array(self.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 = {}) click to toggle source
# File lib/fog/aliyun/requests/compute/list_servers.rb, line 60
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, 203][rand(1)]
  response.body = { 'servers' => servers }
  response
end
list_vrouters() click to toggle source
# File lib/fog/aliyun/requests/compute/list_vrouters.rb, line 50
def list_vrouters
  response = Excon::Response.new
  data = list_images_detail.body['VRouters']
  images = []
  for image in data
    images << image.reject { |key, value| !['id', 'name', 'links'].include?(key) }
  end
  response.status = [200, 203][rand(1)]
  response.body = { 'VRouter' => images }
  response
end
list_zones(*args) click to toggle source
# File lib/fog/aliyun/requests/compute/list_disks.rb, line 152
def list_zones(*args)
  Excon::Response.new(
    :body   => { "availabilityZoneInfo" => [
          {
              "zoneState" => {
                  "available" => true
              },
              "hosts" => nil,
              "zoneName" => "nova"
          }
      ] },
    :status => 200
  )
end
modify_vpc(vpcId, options={}) click to toggle source
# File lib/fog/aliyun/requests/compute/modify_vpc.rb, line 46
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
  self.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' => self.data[:security_groups][security_group_id.to_s]
  }
  response
end
reboot_server(server_id, type = 'SOFT') click to toggle source
# File lib/fog/aliyun/requests/compute/reboot_server.rb, line 36
def reboot_server(server_id, type = 'SOFT')
  response = Excon::Response.new
  response.status = 202
  response
end
reset_data() click to toggle source
# File lib/fog/aliyun/compute.rb, line 222
def reset_data
  self.class.data.delete("#{@openstack_username}-#{@current_tenant}")
end
start_server(server_id) click to toggle source
# File lib/fog/aliyun/requests/compute/start_server.rb, line 36
def start_server(server_id)
  true
end
stop_server(server_id) click to toggle source
# File lib/fog/aliyun/requests/compute/stop_server.rb, line 36
def stop_server(server_id)
  true
end