Class: Fog::Compute::QingCloud::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/qingcloud/compute.rb,
lib/fog/qingcloud/requests/compute/join_vxnet.rb,
lib/fog/qingcloud/requests/compute/join_router.rb,
lib/fog/qingcloud/requests/compute/leave_vxnet.rb,
lib/fog/qingcloud/requests/compute/leave_router.rb,
lib/fog/qingcloud/requests/compute/create_vxnets.rb,
lib/fog/qingcloud/requests/compute/delete_vxnets.rb,
lib/fog/qingcloud/requests/compute/routers_power.rb,
lib/fog/qingcloud/requests/compute/run_instances.rb,
lib/fog/qingcloud/requests/compute/attach_volumes.rb,
lib/fog/qingcloud/requests/compute/create_routers.rb,
lib/fog/qingcloud/requests/compute/create_volumes.rb,
lib/fog/qingcloud/requests/compute/delete_routers.rb,
lib/fog/qingcloud/requests/compute/delete_volumes.rb,
lib/fog/qingcloud/requests/compute/detach_volumes.rb,
lib/fog/qingcloud/requests/compute/stop_instances.rb,
lib/fog/qingcloud/requests/compute/update_routers.rb,
lib/fog/qingcloud/requests/compute/create_key_pair.rb,
lib/fog/qingcloud/requests/compute/describe_vxnets.rb,
lib/fog/qingcloud/requests/compute/release_address.rb,
lib/fog/qingcloud/requests/compute/reset_instances.rb,
lib/fog/qingcloud/requests/compute/start_instances.rb,
lib/fog/qingcloud/requests/compute/allocate_address.rb,
lib/fog/qingcloud/requests/compute/attach_key_pairs.rb,
lib/fog/qingcloud/requests/compute/delete_key_pairs.rb,
lib/fog/qingcloud/requests/compute/describe_routers.rb,
lib/fog/qingcloud/requests/compute/describe_volumes.rb,
lib/fog/qingcloud/requests/compute/detach_key_pairs.rb,
lib/fog/qingcloud/requests/compute/reboot_instances.rb,
lib/fog/qingcloud/requests/compute/resize_instances.rb,
lib/fog/qingcloud/requests/compute/associate_address.rb,
lib/fog/qingcloud/requests/compute/add_router_statics.rb,
lib/fog/qingcloud/requests/compute/describe_addresses.rb,
lib/fog/qingcloud/requests/compute/describe_instances.rb,
lib/fog/qingcloud/requests/compute/describe_key_pairs.rb,
lib/fog/qingcloud/requests/compute/terminate_instances.rb,
lib/fog/qingcloud/requests/compute/apply_security_group.rb,
lib/fog/qingcloud/requests/compute/disassociate_address.rb,
lib/fog/qingcloud/requests/compute/create_security_group.rb,
lib/fog/qingcloud/requests/compute/delete_router_statics.rb,
lib/fog/qingcloud/requests/compute/delete_security_groups.rb,
lib/fog/qingcloud/requests/compute/describe_router_vxnets.rb,
lib/fog/qingcloud/requests/compute/describe_router_statics.rb,
lib/fog/qingcloud/requests/compute/add_security_group_rules.rb,
lib/fog/qingcloud/requests/compute/change_address_bandwidth.rb,
lib/fog/qingcloud/requests/compute/describe_security_groups.rb,
lib/fog/qingcloud/requests/compute/describe_vxnet_instances.rb,
lib/fog/qingcloud/requests/compute/modify_router_attributes.rb,
lib/fog/qingcloud/requests/compute/modify_resource_attributes.rb,
lib/fog/qingcloud/requests/compute/upload_userdata_attachment.rb,
lib/fog/qingcloud/requests/compute/delete_security_group_rules.rb,
lib/fog/qingcloud/requests/compute/describe_security_group_rules.rb,
lib/fog/qingcloud/requests/compute/modify_security_group_rule_attributes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



143
144
145
146
# File 'lib/fog/qingcloud/compute.rb', line 143

def initialize(options={})
  setup_credentials(options)
  @zone = options[:zone] || 'pek1'
end

Instance Attribute Details

#zoneObject

Returns the value of attribute zone.



141
142
143
# File 'lib/fog/qingcloud/compute.rb', line 141

def zone
  @zone
end

Class Method Details

.dataObject



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

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :deleted_at => {},
      :modified_at => {},
      :addresses  => {},
      :images     => {},
      :instances  => {},
      :key_pairs  => {},
      :security_groups => {},
      :security_group_rule_maps => {}, #{rule_id => group_id}
      :volumes => {},
      :vxnets => {},
      :routers => {},
      :quota => {
        :addresses       => 2,
        :images          => 5,
        :volumes         => 5,
        :servers         => 5,
        :key_pairs       => 10,
        :security_groups => 10,
        :routers         => 2,
        :vxnets          => 5,
        :loadbalancers   => 2,
        :gigabytes       => 500,
        :bandwidth       => 10,
        :cores           => 10,
        :ram             => 20
      },
    }
  end
end

.resetObject



137
138
139
# File 'lib/fog/qingcloud/compute.rb', line 137

def self.reset
  @data = nil
end

Instance Method Details

#add_router_statics(rt_id, rules) ⇒ Object



14
15
# File 'lib/fog/qingcloud/requests/compute/add_router_statics.rb', line 14

def add_router_statics(rt_id, rules)
end

#add_security_group_rules(group_id, options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/qingcloud/requests/compute/add_security_group_rules.rb', line 17

def add_security_group_rules(group_id, options)
  sg = self.data[:security_groups][group_id]
  raise Fog::QingCloud::Errors::NotFound, "security group #{group_id} does not exist" unless sg

  args = []
  # Convert options from {'rules.1.key' => 'xxx', 'rules.2.key' => 'yyy'} to [{'key' => 'xxx'},{'key' => 'yyy'}]
  options.each_pair do |k, v|
    n, param = k.split('.')[1..-1]
    n = n.to_i
    args[n] ||= {}
    args[n][param] = v
  end
  args.compact!

  sg['rules'] |= args.inject({}) do |ret, r| 
    r['security_group_rule_id'] = Fog::QingCloud::Mock.security_group_rule_id
    ret[r['security_group_rule_id']] = r
    self.data[:security_group_rule_maps][r['security_group_rule_id']] = group_id
    ret
  end
  sg['is_applied'] = 0

  response = Excon::Response.new
  response.body = {
    'action'   => 'AddSecurityGroupRulesResponse',
    'security_group_rules' => sg['rules'].keys,
    'ret_code' => 0
  }
  response
end

#allocate_address(bandwidth, count = 1, name = nil, need_icp = 0) ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fog/qingcloud/requests/compute/allocate_address.rb', line 30

def allocate_address(bandwidth, count = 1, name = nil, need_icp = 0)
  if (describe_addresses.body['eip_set'].size + count) > self.data[:quota][:addresses]
    raise Fog::QingCloud::Error::QuotaExceeded, "quota exceeded"
  end
  
  response = Excon::Response.new
  response.status = 200
  eips = {}
  count.times do |i|
    public_ip = Fog::QingCloud::Mock.ip_address
    eip = {
      'status' => 'available',
      'eip_id' => Fog::QingCloud::Mock.address_id,
      'description' => nil,
      'need_icp' => 0,
      'sub_code' => 0,
      'transition_status' => '',
      'icp_codes' => '',
      'eip_group' => {
        'eip_group_id' => 'eipg-00000000',
        'eip_group_name' => 'BGP multi-line'
      },
      'bandwidth' => bandwidth,
      'create_time' => Time.now,
      'status_time' => Time.now,
      'eip_name' => name,
      'resource' => {},
      'eip_addr' => public_ip
    }
    eips[eip['eip_id']] = eip
  end
  self.data[:addresses].merge! eips

  response.body = {
      'action' => 'AllocateEipsResponse',
      'eips' => eips.keys,
      'ret_code' => 0
  }
  response
end

#apply_security_group(id, server_ids = []) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/qingcloud/requests/compute/apply_security_group.rb', line 16

def apply_security_group(id, server_ids = [])
  sg = self.data[:security_groups][id]
  raise Fog::QingCloud::Errors::NotFound, "security group #{id} does not exist" unless sg
  unknown_servers = server_ids - self.data[:instances].keys
  raise Fog::QingCloud::Errors::NotFound, "servers #{unknown_servers.join(', ')} are not found" unless unknown_servers.empty?
  server_ids.each do |sid|
    self.data[:instances][sid]['security_group'] = {
      'is_default' => 0,
      'security_group_id' => id
    }
  end
  sg['is_applied'] = 1
  response = Excon::Response.new
  response.body = {
    'action'   => 'ApplySecurityGroupResponse',
    'job_id'   => Fog::QingCloud::Mock.job_id,
    'ret_code' => 0
  }
  response
end

#associate_address(id, instance_id) ⇒ Object



21
22
23
24
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
# File 'lib/fog/qingcloud/requests/compute/associate_address.rb', line 21

def associate_address(id, instance_id)
  instance = self.data[:instances][instance_id]
  eip = self.data[:addresses][id]
  unless instance
    raise Fog::QingCloud::Errors::NotFound, "The instance '#{instance_id}' does not exist."
  end

  unless eip
    raise Fog::QingCloud::Errors::NotFound, "The eip '#{id}' does not exist."
  end

  unless eip['status'] == 'available'
    raise Fog::QingCloud::Errors::PermissionDenied, "The eip can't be associated."
  end
  
  eip['resource'] = {
    'resource_name' => instance['instance_name'],
    'resource_type' => 'instance',
    'resource_id'   => instance_id
  }
  eip['status'] = 'associated'

  response = Excon::Response.new
  response.body = {
    'action'   => 'AssociateEipResponse',
    'job_id'   => Fog::QingCloud::Mock.job_id,
    'ret_code' => 0
  }
  response
end

#attach_key_pairs(key_id, instance_id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/qingcloud/requests/compute/attach_key_pairs.rb', line 21

def attach_key_pairs(key_id, instance_id)
  response = Excon::Response.new
  key_id = [*key_id]
  instance_id = [*instance_id]

  raise Fog::QingCloud::Errors::CommonClientError, "key_id count and instance_id count must be equal" if key_id.length != instance_id.length
  key_id.each_with_index do |k, i|
    self.data[:key_pairs][k]['instance_ids'] << instance_id[i]
    self.data[:instances][instance_id[i]]['keypair_ids'] << k
  end

  response.status = 200
  response.body = {
    'action' => 'AttachKeyPairsResponse',
    'job_id' => Fog::QingCloud::Mock.job_id,
    'ret_code' => 0
  }
  response
end

#attach_volumes(instance_id, volume_id) ⇒ Object



21
22
23
24
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
# File 'lib/fog/qingcloud/requests/compute/attach_volumes.rb', line 21

def attach_volumes(instance_id, volume_id)
  response = Excon::Response.new
  ids = [*volume_id]
  unless (unknown_ids = ids - self.data[:volumes].keys).empty?
      raise Fog::QingCloud::Errors::NotFound, "The volumes '#{unknown_ids.join(', ')}' are not exist."
  end
  
  instance = self.data[:instances][instance_id]
  unless instance
      raise Fog::QingCloud::Errors::NotFound, "The instance ID '#{instance_id}' does not exist."
  end

  response.status = 200
  ids.each do |vid|
    volume = self.data[:volumes][vid]['instance']
    unless volume['status'] == 'available'
      raise Fog::QingCloud::Errors::PermissionDenied, "Client.VolumeInUse => Volume #{vid} is unavailable"
    end
    volume['instance'] = {
      'instance_id' => instance_id,
      'instance_name' => instance['instance_name']
    }
    volume['status'] = 'in-use'
    instance['volume_ids'] |= [vid]
  end

  response.body = {
    'action' => 'AttachVolumesResponse',
    'job_id' => Fog::QingCloud::Mock.job_id,
    'ret_code' => 0
  }
end

#change_address_bandwidth(id, bandwidth) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/qingcloud/requests/compute/change_address_bandwidth.rb', line 20

def change_address_bandwidth(id, bandwidth)

  response = Excon::Response.new
  response.status = 200

  unless (unknown_eips = [*id] - self.data[:addresses].keys).empty?
    raise Fog::QingCloud::Errors::NotFound, "unknown eips: #{unknown_eips.join(', ')}"
  end

  [*id].each do |x|
    self.data[:addresses][x]['bandwidth'] = bandwidth
  end

  response.body = {
    'action' => 'ChangeEipsBandwidthResponse',
    'job_id' => Fog::QingCloud::Mock.job_id,
    'ret_code' => 0
  }
  response
end

#create_key_pair(key_name = '', mode = 'system', encrypt_method = 'ssh-rsa', public_key = '') ⇒ 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/qingcloud/requests/compute/create_key_pair.rb', line 28

def create_key_pair(key_name = '', mode = 'system', encrypt_method = 'ssh-rsa', public_key = '')
  response = Excon::Response.new
  response.status = 200
  data = {
    'action'       => 'CreateKeyPairResponse',
    'keypair_id'   => Fog::QingCloud::Mock.key_id,
    'ret_code'     => 0
  }
  data['private_key'] = mode == 'user' ? '' : Fog::QingCloud::Mock.key_material

  self.data[:key_pairs][data['keypair_id']] = {
      'description' => 'test',
      'encrypt_method' => encrypt_method,
      'keypair_name'  => key_name,
      'instance_ids' => [],
      'create_time' => Time.now,
      'keypair_id'  => data['keypair_id'],
      'pub_key'  => Fog::QingCloud::Mock.public_key
  }
  response.body = data
  response
end

#create_routers(name = nil, count = 1, security_group = nil) ⇒ Object



17
18
# File 'lib/fog/qingcloud/requests/compute/create_routers.rb', line 17

def create_routers(name = nil, count = 1, security_group = nil)
end

#create_security_group(name = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/qingcloud/requests/compute/create_security_group.rb', line 19

def create_security_group(name = nil)
  response = Excon::Response.new
  if self.data[:security_groups].length < self.data[:quota][:security_groups]
    data = {
      'is_applied'          => 1,
      'description'         => nil,
      'security_group_name' => name,
      'security_group_id'   => Fog::QingCloud::Mock.security_group_id,
      'is_default'          => 0,
      'create_time'         => Time.now,
      'resources'           => [],
      'rules'               => []
    }
    self.data[:security_groups][data['security_group_id']] = data
    response.body = {
      'action'   => 'CreateSecurityGroupResponse',
      'ret_code' => 0,
      'security_group_id' => data['security_group_id']
    }
    response
  else
    raise Fog::QingCloud::Errors::QuotaExceeded, "You can create #{self.data[:quota][:security_groups]} security groups."
  end
end

#create_volumes(zone, size, options = {}) ⇒ Object



20
21
22
23
24
25
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/qingcloud/requests/compute/create_volumes.rb', line 20

def create_volumes(zone, size, options = {})
  response = Excon::Response.new
  if zone && size
    if size < 10
      raise Fog::Compute::QingCloud::Error.new("InvalidParameterValue => Volume of #{size}GiB is too small; minimum is 10GiB.")
    end

    response.status = 200
    volume_id = Fog::QingCloud::Mock.volume_id
    self.data = {
      "action" => "CreateVolumesResponse",
      "job_id" => "j-bm6ym3r8",
      "volumes" => [volume_id],
      "ret_code" => 0
    }
    response.body = self.data
  else
    response.status = 200
    response.body = {
      'ret_code' => 1100
    }
    unless zone
      response.body['message'] = 'The request must contain the parameter zone'
    else
      response.body['message'] = 'The request must contain the parameter size'
    end
  end
  response
end

#create_vxnets(options) ⇒ Object



17
18
19
20
21
22
23
24
25
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/qingcloud/requests/compute/create_vxnets.rb', line 17

def create_vxnets(options)
  Excon::Response.new.tap do |response|
    if options['vxnet_type']
      response.status = 200
      vxnet = {
        "vxnet_type" => 1,
        "vxnet_id" => Fog::QingCloud::Mock.vxnet_id,
        "instance_ids" => [
          "i-syx7qtud"
        ],
        "vxnet_name" => "test",
        "create_time" => "2013-08-27T10:02:25Z",
        "router" => {
          "router_id" => "rtr-b0u6sdj6",
          "router_name" => "demo"
        },
        "description" => nil
      }
      self.data[:vxnets].push(vxnet)
      response.body = {
        'vxnets'   => [vxnet],
        'ret_code' => 0
      }
    else
      response.status = 200
      response.body = {
        'ret_code' => 1100,
        'message'  => 'vxnet_type cannot be empty'
      }
    end
  end
end

#dataObject



148
149
150
# File 'lib/fog/qingcloud/compute.rb', line 148

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

#delete_key_pairs(key_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/qingcloud/requests/compute/delete_key_pairs.rb', line 20

def delete_key_pairs(key_id)
  response = Excon::Response.new
  key_id = [*key_id]
  unless (unknown_keys = key_id - self.data[:key_pairs].keys).empty?
    raise Fog::QingCloud::Errors::NotFound, "ResourceNotFound, resource #{unknown_keys.join(', ')} not found"
  end
  self.data[:key_pairs].delete_if {|k, v| key_id.include? k}
  response.status = 200
  response.body = {
    'action' => 'DeleteKeyPairsResponse',
    'keypairs' => [*key_id],
    'ret_code' => 0
  }
  response
end

#delete_router_statics(rule_id) ⇒ Object



16
17
# File 'lib/fog/qingcloud/requests/compute/delete_router_statics.rb', line 16

def delete_router_statics(rule_id)
end

#delete_routers(id) ⇒ Object



15
16
# File 'lib/fog/qingcloud/requests/compute/delete_routers.rb', line 15

def delete_routers(id)
end

#delete_security_group_rules(rule_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/qingcloud/requests/compute/delete_security_group_rules.rb', line 16

def delete_security_group_rules(rule_id)
  group_id = self.data[:security_group_rule_maps][rule_id]
  raise Fog::QingCloud::Errors::NotFound, "security group #{rule_id} does not exist" unless group_id

  self.data[:security_groups][group_id]['rules'].delete_if {|rid, r| [*rule_id].include?(rid)} 
  self.data[:security_groups][group_id]['is_applied'] = 0

  response = Excon::Response.new
  response.body = {
    'action'   => 'DeleteSecurityGroupRulesResponse',
    'security_group_rules' => [*rule_id],
    'ret_code' => 0
  }
  response
end

#delete_security_groups(id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/qingcloud/requests/compute/delete_security_groups.rb', line 18

def delete_security_groups(id)
  ids = [*id]
  if (unknown_groups = ids - self.data[:security_groups].keys).empty?
    used_sgs = []
    ids.each do |id|
      used_sgs << id if self.data[:instances].find {|server_id, s| s['security_group']['security_group_id'] == id}
      used_ids << id if self.data[:routers].find {|rid, r| r['security_group_id'] == id}
    end

    raise Fog::QingCloud::Errors::PermissionDenied, "security groups #{used_sgs.join(', ')} are in use and can't be deleted." unless used_sgs.empty?

    self.data[:security_groups].delete_if {|k, v| ids.include? k}
    
    response = Excon::Response.new
    response.status = 200
    response.body = {
      'action' => 'DeleteSecurityGroupsResponse',
      'security_groups' => ids,
      'ret_code' => 0
    }
    response
  else
    raise Fog::QingCloud::Errors::NotFound, "security group '#{unknown_groups.join(', ')}' does not exist"
  end
end

#delete_volumes(volume_id) ⇒ Object



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

def delete_volumes(volume_id)
  response = Excon::Response.new
  if volume = self.data[:volumes][volume_id]
    if volume["attachmentSet"].any?
      attach = volume["attachmentSet"].first
      raise Fog::Compute::QingCloud::Error.new("Client.VolumeInUse => Volume #{volume_id} is currently attached to #{attach["instanceId"]}")
    end
    self.data[:deleted_at][volume_id] = Time.now
    volume['status'] = 'deleting'
    response.status = 200
    response.body = {
      'requestId' => Fog::QingCloud::Mock.request_id,
      'return'    => true
    }
    response
  else
    raise Fog::Compute::QingCloud::NotFound.new("The volume '#{volume_id}' does not exist.")
  end
end

#delete_vxnets(vxnet_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/qingcloud/requests/compute/delete_vxnets.rb', line 16

def delete_vxnets(vxnet_id)
  Excon::Response.new.tap do |response|
    if vxnet_id
      self.data[:vxnets].reject! { |v| [*vxnet_id].iclude?(v['vxnet_id']) }
      response.status = 200
    
      response.body = {
          "action" => "DeleteVxnetsResponse",
          "vxnets" => [
            "vxnet-7mwzdbs",
            "vxnet-f3y0h3q"
          ],
          "ret_code" => 0
        }
    else
      message = 'MissingParameter => '
      message << 'The request must contain the parameter vxnet_id'
      raise Fog::Compute::QingCloud::Error.new(message)
    end
  end
end

#describe_addresses(filters = {}) ⇒ Object



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

def describe_addresses(filters = {})
  response = Excon::Response.new

  addresses_set = self.data[:addresses].values

  aliases = {'public-ip' => 'public_ip', 'instance-id' => 'instance_id'}
  for filter_key, filter_value in filters
    aliased_key = aliases[filter_key]
    addresses_set = addresses_set.reject{|address| ![*filter_value].include?(address[aliased_key])}
  end

  response.status = 200
  response.body = {
    'eip_set'  => addresses_set
  }
  response
end

#describe_instances(filters = {}) ⇒ Object



31
32
33
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
# File 'lib/fog/qingcloud/requests/compute/describe_instances.rb', line 31

def describe_instances(filters = {})

  response = Excon::Response.new

  instance_set = self.data[:instances].values
  aliases = {
    'instance-id'   => 'instance_id',
    'image-id'      => 'image_id',
    'instance-type' => 'instance_type',
    'state'         => 'status'
  }
  
  # Mock instance status change
  instance_set.map! do |instance|
    target_status = ''
    timestamp = instance['transition_status'] == 'creating' ? instance['create_time'] : self.data[:modified_at][instance['instance_id']]
    timestamp ||= 0
    case instance['transition_status']
    when 'creating', 'starting', 'restarting', 'resuming', 'recovering', 'reseting'
      target_status = 'running'
    when 'stopping'
      target_status = 'stopped'
    when 'suspending'
      target_status = 'suspended'
    when 'terminating'
      target_status = 'terminated'
      target_status = 'ceased' if Time.now - timestamp > Fog::Mock.delay * 2
    end
    if Time.now - timestamp > Fog::Mock.delay
      instance['status'] = target_status
      instance['transition_status'] = ''
    end
    instance
  end

  for filter_key, filter_value in filters
    if filter_key == 'search_word'
      instance_set = instance_set.select {|instance| instance['instance_name'] =~ Regexp.new(filter_value) or instance['instance_id'] =~ Regexp.new(filter_value)}
    else
      aliased_key = aliases[filter_key] || filter_key
      instance_set = instance_set.select {|instance| [*filter_value].include?(instance[aliased_key])}
    end
  end

  response.status = 200
  response.body = {
    'action'       => 'DescribeInstancesResponse',
    'instance_set' => instance_set, 
    'ret_code'     => 0,
    'total_count'  => instance_set.length
  }
  response
end

#describe_key_pairs(filters = {}) ⇒ Object



31
32
33
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
# File 'lib/fog/qingcloud/requests/compute/describe_key_pairs.rb', line 31

def describe_key_pairs(filters = {})
  unless filters.is_a?(Hash)
    Fog::Logger.deprecation("describe_key_pairs with #{filters.class} param is deprecated, use describe_key_pairs('keypair-id' => []) instead [light_black](#{caller.first})[/]")
    filters = {'keypair-id' => [*filters]}
  end

  response = Excon::Response.new

  key_set = self.data[:key_pairs]
  
  args = {}
  args['instance_id'] = filters['instance-id'] || filters['instance_id']
  args['encrypt_method'] = filters['encrypt_method'] || filters['encrypt-method']
  args['search_word'] = filters['search_word'] || filters['search-word']

  if args['instance_id']
    key_set = key_set.select {|id, kp| kp['instance_ids'].include? args['instance_id']}
  end
  
  if filters['keypair-id']
    key_set = key_set.select {|id, kp| [*filters['keypair-id']].include? id}
  end

  if args['encrypt_method']
    key_set = key_set.select {|id, kp| kp['encrypt_method'] == args['encrypt_method']}
  end

  if args['search_word']
    key_set = key_set.select {|id, kp| kp['keypair_name'] =~ Regexp.new(args['search_word']) or kp['keypair_id'] =~ args['search_word']}
  end

  response.status = 200
  response.body = {
    'action'      => 'DescribeKeyPairsResponse',
    'keypair_set' => key_set.values,
    'total_count' => key_set.length,
    'ret_code'    => 0
  }
  response
end

#describe_router_statics(rt_id, rule_id = [], type = nil) ⇒ Object



17
18
# File 'lib/fog/qingcloud/requests/compute/describe_router_statics.rb', line 17

def describe_router_statics(rt_id, rule_id = [], type = nil)
end

#describe_router_vxnets(router_id = nil, vxnet_id = nil) ⇒ Object



18
19
# File 'lib/fog/qingcloud/requests/compute/describe_router_vxnets.rb', line 18

def describe_router_vxnets(router_id = nil, vxnet_id = nil)
end

#describe_routers(filters = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/qingcloud/requests/compute/describe_routers.rb', line 23

def describe_routers(filters = {})
  routers = self.data[:routers]

  # Transition from pending to available
  routers.each do |router|
    case router['state']
      when 'pending'
        router['state'] = 'available'
    end
  end

  if filters['router-id']
    routers = routers.reject {|router| router['routerId'] != filters['router-id']}
  end

  Excon::Response.new(
    :status => 200,
    :body   => {
      'router_set' => routers
    }
  )
end

#describe_security_group_rules(group_id = nil, rule_ids = nil, direction = nil) ⇒ Object



21
22
23
24
25
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/qingcloud/requests/compute/describe_security_group_rules.rb', line 21

def describe_security_group_rules(group_id = nil, rule_ids = nil, direction = nil)
  response = Excon::Response.new

  if group_id
    sg = self.data[:security_groups][group_id]
    raise Fog::QingCloud::Errors::NotFound, "security group #{group_id} does not exist" unless sg
    rule_set = sg['rules']
  else
    rule_set = self.data[:security_groups].map {|sg| sg['rules']}
  end

  if rule_ids
    rule_set = rule_set.select {|id, r| [*rule_ids].include? id}
  end

  if direction
    rule_set = rule_set.select {|id, r| r['direction'] == direction}
  end

  response.status = 200
  response.body = {
    'action'             => 'DescribeSecurityGroupRulesResponse',
    'security_group_rule_set' => rule_set.values,
    'total_count'        => rule_set.length,
    'ret_code'           => 0
  }
  response
end

#describe_security_groups(filters = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/qingcloud/requests/compute/describe_security_groups.rb', line 22

def describe_security_groups(filters = {})
  response = Excon::Response.new
  id = [*filters['group-id']]
  search_word = filters['search-word'] || filters['search_word'] || filters['group-name']

  security_group_info = self.data[:security_groups]

  security_group_info = security_group_info.select {|k, v| id.include? k}
  if search_word
    security_group_info = security_group_info.select {|k, v| k =~ Regexp.new(search_word) or v['security_group_name'] =~ Regexp.new(search_word)}
  end

  response.status = 200
  response.body = {
    'action'             => 'DescribeSecurityGroupsResponse',
    'security_group_set' => security_group_info.values,
    'total_count'        => security_group_info.length,
    'ret_code'           => 0
  }
  response
end

#describe_volumes(filters = {}) ⇒ Object



23
24
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
# File 'lib/fog/qingcloud/requests/compute/describe_volumes.rb', line 23

def describe_volumes(filters = {})

  filters['search_word'] = filters['name'] || filters['search_word']
  
  volume_set = self.data[:volumes]
  if filters['instance-id']
    volume_set = volume_set.select {|id, v| v['instance_id'] == filters['instance-id']}
  end
  if filters['volume-id']
    volume_set = volume_set.select {|id, v| [*filters['volume-id']].include? id}
  end
  if filters['status']
    volume_set = volume_set.select {|id, v| [*filters['status']].include? v['status']}
  end
  if filters['search_word']
    volume_set = volume_set.select {|id, v| v['name'] =~ Regexp.new(filters['search_word']) or id =~ Regexp.new(filters['search_word'])}
  end

  response = Excon::Response.new
  response.status = 200
  response.body = {
    'action' => 'DescribeVolumesResponse',
    'total_count' => volume_set.length,
    'volume_set' => volume_set.values,
    'ret_code' => 0
  }
  response
end

#describe_vxnet_instances(vxnet_id, filters = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/qingcloud/requests/compute/describe_vxnet_instances.rb', line 19

def describe_vxnet_instances(vxnet_id, filters = {})
  instances = self.data[:instances]
  instances = instances.select {|i| i['vxnets'].map {|v| v['vxnet_id']}.include?(vxnet_id) }

  Excon::Response.new(
    :status => 200,
    :body   => {
      'instance_set' => instances,
      'action'       => 'DescribeVxnetInstancesResponse',
      'ret_code'     => 0,
      'total_count'  => instances.length
    }
  )
end

#describe_vxnets(filters = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/qingcloud/requests/compute/describe_vxnets.rb', line 19

def describe_vxnets(filters = {})
  vxnets = self.data[:vxnets]

  # Transition from pending to available
  vxnets.each do |vxnet|
    case vxnet['state']
      when 'pending'
        vxnet['state'] = 'available'
    end
  end

  if filters['vxnet-id']
    vxnets = vxnets.reject {|vxnet| vxnet['vxnet_id'] != filters['vxnet-id']}
  end

  Excon::Response.new(
    :status => 200,
    :body   => {
      'vxnet_set' => vxnets
    }
  )
end

#detach_key_pairs(key_id, instance_id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/qingcloud/requests/compute/detach_key_pairs.rb', line 21

def detach_key_pairs(key_id, instance_id)
  response = Excon::Response.new
  key_id = [*key_id]
  instance_id = [*instance_id]

  raise Fog::QingCloud::Errors::CommonClientError, "key_id count and instance_id count must be equal" if key_id.length != instance_id.length
  key_id.each_with_index do |k, i|
    self.data[:key_pairs][k]['instance_ids'].delete instance_id[i]
    self.data[:instances][instance_id[i]]['keypair_ids'].delete k
  end

  response.status = 200
  response.body = {
    'action' => 'DetachKeyPairsResponse',
    'job_id' => Fog::QingCloud::Mock.job_id,
    'ret_code' => 0
  }
  response
end

#detach_volumes(server_id, volume_id) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/qingcloud/requests/compute/detach_volumes.rb', line 22

def detach_volumes(server_id, volume_id)
  response = Excon::Response.new
  response.status = 200

  raise Fog::QingCloud::Errors::NotFound, "server #{server_id} not found" unless self.data[:instances][server_id]
  
  [*volume_id].each do |vid|
     self.data[:instances][server_id]['volume_ids'].delete(vid)
     self.data[:volumes][vid]['instance'] = {}
  end
  response.body = {
    'action' => 'DetachVolumesResponse',
    'job_id' => Fog::QingCloud::Mock.job_id,
    'ret_code' => 0
  }
end

#disassociate_address(id) ⇒ Object



18
19
20
21
22
23
24
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
# File 'lib/fog/qingcloud/requests/compute/disassociate_address.rb', line 18

def disassociate_address(id)
  ids = [*id]
  unless (unknown_ids = ids - self.data[:addresses].keys).empty?
    raise Fog::QingCloud::Errors::NotFound, "eips #{ids.join(', ')} not found"
  end

  response = Excon::Response.new
  response.status = 200
  ids.each do |id|
    address = self.data[:addresses][id]
    unless address['resource'].empty?
      case address['resource_type']
      when 'router'
        self.data[:routers].map! do |r| 
          if r['eip']['eip_id'] == id
            r['eip'] = {}
          end
          r
        end
      end
      address['resource'] = {}
    end
  end
  response.status = 200
  response.body = {
    'action' => 'DissociateEipsResponse',
    'owner'  => Fog::Mock.random_letters(12),
    'job_id' => Fog::Mock.job_id,
    'ret_code' => 0
  }
  response
end

#join_router(rt_id, vxnet_id, ip_network, features = 1) ⇒ Object



18
19
# File 'lib/fog/qingcloud/requests/compute/join_router.rb', line 18

def join_router(rt_id, vxnet_id, ip_network, features = 1)
end

#join_vxnet(vxnet_id, server_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/qingcloud/requests/compute/join_vxnet.rb', line 16

def join_vxnet(vxnet_id, server_id)
  vxnets = self.data[:vxnets]

  # Transition from pending to available
  vxnets.each do |vxnet|
    case vxnet['state']
      when 'pending'
        vxnet['state'] = 'available'
    end
  end

  if filters['vxnet-id']
    vxnets = vxnets.reject {|vxnet| vxnet['vxnetId'] != filters['vxnet-id']}
  end

  Excon::Response.new(
    :status => 200,
    :body   => {
      'requestId' => Fog::QingCloud::Mock.request_id,
      'vxnetSet' => vxnets
    }
  )
end

#leave_router(rt_id, vxnet_id) ⇒ Object



16
17
# File 'lib/fog/qingcloud/requests/compute/leave_router.rb', line 16

def leave_router(rt_id, vxnet_id)
end

#leave_vxnet(vxnet_id, server_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/qingcloud/requests/compute/leave_vxnet.rb', line 16

def leave_vxnet(vxnet_id, server_id)
  vxnets = self.data[:vxnets]

  # Transition from pending to available
  vxnets.each do |vxnet|
    case vxnet['state']
      when 'pending'
        vxnet['state'] = 'available'
    end
  end

  if filters['vxnet-id']
    vxnets = vxnets.reject {|vxnet| vxnet['vxnetId'] != filters['vxnet-id']}
  end

  Excon::Response.new(
    :status => 200,
    :body   => {
      'requestId' => Fog::QingCloud::Mock.request_id,
      'vxnetSet' => vxnets
    }
  )
end

#modify_resource_attributes(id, resource_type, name = nil, description = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fog/qingcloud/requests/compute/modify_resource_attributes.rb', line 38

def modify_resource_attributes(id, resource_type, name = nil, description = nil)
  resource_type = 'eip' if resource_type == 'address'
  resource_type = 'instance' if resource_type == 'server'
  
  supported_types = ['instance', 'volume', 'vxnet', 'eip', 
    'security_group', 'keypair', 'image']
  raise Fog::QingCloud::Errors::CommonClientError, "unsupported resource type #{resource_type}" unless supported_types.include?(resource_type)
  action = "Modify#{Fog::QingCloud.camelcase_string(resource_type)}AttributesResponse"
  name_key = "#{resource_type}_name"

  response = Excon::Response.new
  response.status = 200
  key = "#{resource_type}s".to_sym
  self.data[key][id][name_key] = name if name
  self.data[key][id]['description'] = description if description 

  response.body = {
    'action'   => action,
    'ret_code' => 0
  }
  response
end

#modify_router_attributes(id, attributes) ⇒ Object



20
21
# File 'lib/fog/qingcloud/requests/compute/modify_router_attributes.rb', line 20

def modify_router_attributes(id, attributes)
end

#modify_security_group_rule_attributes(rule_id, priority, name = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/qingcloud/requests/compute/modify_security_group_rule_attributes.rb', line 18

def modify_security_group_rule_attributes(rule_id, priority, name = nil)
  group_id = self.data[:security_group_rule_maps][rule_id]
  raise Fog::QingCloud::Errors::NotFound, "security group #{rule_id} does not exist" unless group_id

  rule = self.data[:security_groups][group_id]['rules'][rule_id]
  rule['security_group_rule_name'] = name if name
  rule['priority'] = priority
  self.data[:security_groups][group_id]['is_applied'] = 0

  response = Excon::Response.new
  response.body = {
    'action'   => 'AddSecurityGroupRulesResponse',
    'security_group_rule_id' => rule['security_group_rule_id'],
    'ret_code' => 0
  }
  response
end

#reboot_instances(instance_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/qingcloud/requests/compute/reboot_instances.rb', line 20

def reboot_instances(instance_id)
  instance_ids = Array(instance_id)
  instance_set = self.data[:instances].select {|id, s| instance_ids.include?(id) }

  if instance_set.empty?
    raise Fog::QingCloud::Errors::NotFound, "The instance ID '#{instance_ids.join(', ')}' does not exist"
  else
    instance_set.map! do |x| 
      x['status'] = 'pending'
      x['transition_status'] = 'restarting'
      self.data[:modified_at][x['instance_id']] = Time.now
      x
    end
    response = Excon::Response.new
    response.status = 200
    response.body = {
      'action' => 'RestartInstancesResponse',
      'job_id' => Fog::QingCloud::Mock.job_id,
      'ret_code' => 0
    }
    response
  end

end

#release_address(id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/qingcloud/requests/compute/release_address.rb', line 18

def release_address(id)
  response = Excon::Response.new
  ids = [*id]

  address = self.data[:addresses][public_ip_or_allocation_id] || self.data[:addresses].values.detect {|a| a['allocationId'] == public_ip_or_allocation_id }

  if address
    if address['allocationId'] && public_ip_or_allocation_id == address['publicIp']
      raise Fog::Compute::QingCloud::Error, "InvalidParameterValue => You must specify an allocation id when releasing a VPC elastic IP address"
    end

    self.data[:addresses].delete(address['publicIp'])
    response.status = 200
    response.body = {
      'requestId' => Fog::QingCloud::Mock.request_id,
      'return'    => true
    }
    response
  else
    raise Fog::Compute::QingCloud::Error.new("AuthFailure => The address '#{public_ip_or_allocation_id}' does not belong to you.")
  end
end

#reset_dataObject



152
153
154
# File 'lib/fog/qingcloud/compute.rb', line 152

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

#reset_instances(instance_id) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/qingcloud/requests/compute/reset_instances.rb', line 19

def reset_instances(instance_id)
  instance_ids = Array(instance_id)
  instance_set = self.data[:instances].select {|id, s| instance_ids.include?(id) }

  if instance_set.empty?
    raise Fog::QingCloud::Errors::NotFound, "The instance ID '#{instance_ids.join(', ')}' does not exist"
  else
    instance_set.each do |x|
      unless ['running', 'stopped'].include? x['status']
        raise Fog::QingCloud::Errors::PermissionDenied, "instance must be in 'running' or 'stopped' status"
      end
    end

    instance_set.map! do |x| 
      x['status'] = 'pending'
      x['transition_status'] = 'reseting'
      self.data[:modified_at][x['instance_id']] = Time.now
      x
    end
    response = Excon::Response.new
    response.status = 200
    response.body = {
      'action' => 'ResetInstancesResponse',
      'job_id' => Fog::QingCloud::Mock.job_id,
      'ret_code' => 0
    }
    response
  end
end

#resize_instances(instance_id, target_instance_type = nil, cpu = nil, memory = nil) ⇒ Object



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
55
56
57
58
59
60
# File 'lib/fog/qingcloud/requests/compute/resize_instances.rb', line 30

def resize_instances(instance_id, target_instance_type = nil, cpu = nil, memory = nil)
  instance_ids = Array(instance_id)
  instance_set = self.data[:instances].select {|id, s| instance_ids.include?(id) }

  if instance_set.empty?
    raise Fog::QingCloud::Errors::NotFound, "The instance ID '#{instance_ids.join(', ')}' does not exist"
  else
    instance_set.map! do |x| 
      x['status'] = 'pending'
      x['transition_status'] = 'resizeping'
      self.data[:modified_at][x['instance_id']] = Time.now
      if target_instance_type
        x['instance_type'] = target_instance_type
        # TODO: assign vcpus_current and memory_current
      else
        x['cpu'] = cpu
        x['memory'] = memory
        x['instance_type'] = '' #?? 
      end
      x
    end
    response = Excon::Response.new
    response.status = 200
    response.body = {
      'action' => 'ResizeInstancesResponse',
      'job_id' => Fog::QingCloud::Mock.job_id,
      'ret_code' => 0
    }
    response
  end
end

#routers_power(id, action) ⇒ Object



17
18
# File 'lib/fog/qingcloud/requests/compute/routers_power.rb', line 17

def routers_power(id, action)
end

#run_instances(image_id, count, options = {}) ⇒ Object



18
19
20
21
22
23
24
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fog/qingcloud/requests/compute/run_instances.rb', line 18

def run_instances(image_id, count, options = {})
  response = Excon::Response.new
  response.status = 200

  sg = options['security_group'] || 'default'
  instances_set = []

  if options['login_keypair'] && describe_key_pairs('keypair-id' => options['login_keypair']).body['keypair_set'].empty?
    raise Fog::QingCloud::Errors::NotFound, "The key pair '#{options['login_keypair']}' does not exist"
  end
  
  options['vxnets'] ||= []
  vxnets = options['vxnets'].map do |vxnet_id|
    vxnet = describe_vxnets('vxnet-id' => vxnet_id).body['vxnet_set'].first
    
    {
      'vxnet_name' => vxnet['vxnet_name'],
      'vxnet_type' => vxnet['vxnet_type'],
      'vxnet_id'   => vxnet_id,
      'nic_id'     => Fog::QingCloud::Mock.mac,
      'private_ip' => Fog::QingCloud::Mock.private_ip_address
    }
  end
  count.times do
    instance = {
      'vcpus_current' => 1,
      'instance_id' => Fog::QingCloud::Mock.instance_id,
      'volume_ids' => [],
      'vxnets' => vxnets,
      'memory_current' => 1024,
      'sub_code' => 0,
      'transition_status' => 'creating',
      'instance_name' => options['instance_name'],
      'instance_type' => options['instance_type'],
      'create_time' => Time.now,
      'status' => 'pending',
      'description' => nil,
      'security_group' => {'is_default' => 0, 'security_group_id' => options['security_group']},
      'status_time' => Time.now,
      'image' => {
        'processor_type' => '64bit',
        'platform' => 'linux',
        'image_size' => 20,
        'image_name' => 'CentOS 6.4 64bit',
        'image_id' => image_id,
        'os_family' => 'centos',
        'provider' => 'system'},
      'keypair_ids' => [options['login_keypair']]
    }
    
    instances_set << instance
    self.data[:instances][instance['instance_id']] = instance
  end
  response.body = {
    'action'     => 'RunInstancesResponse',
    'instances'  => instances_set.map { |x| x['instance_id'] },
    'job_id'     => Fog::QingCloud::Mock.job_id,
    'ret_code'   => 0
  }
  response
end

#setup_credentials(options) ⇒ Object



156
157
158
# File 'lib/fog/qingcloud/compute.rb', line 156

def setup_credentials(options)
  @qingcloud_access_key_id = options[:qingcloud_access_key_id]
end

#start_instances(instance_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/qingcloud/requests/compute/start_instances.rb', line 16

def start_instances(instance_id)
  instance_ids = Array(instance_id)

  instance_set = self.data[:instances].select {|id, s| instance_ids.include?(id) }

  if instance_set.empty?
    raise Fog::QingCloud::Errors::NotFound, "The instance ID '#{instance_ids.join(', ')}' does not exist"
  else
    instance_set.map! do |x| 
      x['status'] = 'pending'
      x['transition_status'] = 'starting'
      self.data[:modified_at][x['instance_id']] = Time.now
      x
    end
    response = Excon::Response.new
    response.status = 200
    response.body = {
      'action' => 'StartInstancesResponse',
      'job_id' => Fog::QingCloud::Mock.job_id,
      'ret_code' => 0
    }
    response
  end
end

#stop_instances(instance_id, force = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/qingcloud/requests/compute/stop_instances.rb', line 19

def stop_instances(instance_id, force = false)
  instance_ids = Array(instance_id)
  instance_set = self.data[:instances].select {|id, s| instance_ids.include?(id) }

  if instance_set.empty?
    raise Fog::QingCloud::Errors::NotFound, "The instance ID '#{instance_ids.join(', ')}' does not exist"
  else
    instance_set.map! do |x| 
      x['status'] = 'pending'
      x['transition_status'] = 'stopping'
      self.data[:modified_at][x['instance_id']] = Time.now
      x
    end
    response = Excon::Response.new
    response.status = 200
    response.body = {
      'action' => 'StopInstancesResponse',
      'job_id' => Fog::QingCloud::Mock.job_id,
      'ret_code' => 0
    }
    response
  end
end

#terminate_instances(instance_id) ⇒ Object



18
19
20
21
22
23
24
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
# File 'lib/fog/qingcloud/requests/compute/terminate_instances.rb', line 18

def terminate_instances(instance_id)
  response = Excon::Response.new
  instance_id = [*instance_id]
  if (self.data[:instances].keys & instance_id).length == instance_id.length
    response.status = 200
    for id in instance_id
      self.data[:instances].delete(id)
    end

    describe_addresses('instance-id' => instance_id).body['eip_set'].each do |address|
      if address['resource'].has_key?('resource_id') and instance_id.include?(address['resource']['resource_id'])
        disassociate_address(address['eip_id'])
      end
    end

    describe_volumes('verbose' => 1,'instance-id' => instance_id).body['volume_set'].each do |volume|
      if !volume['instance'].empty? and instance_id.include?(volume['instance']['instance_id'])
        detach_volume(volume['volume_id'])
      end
    end
    
    response.body = {
      'action'    => 'TerminateInstancesResponse',
      'job_id'    => Fog::QingCloud::Mock.job_id,
      'ret_code'  => 0
    }

    response
  else
    raise Fog::QingCloud::Errors::NotFound, "The instance ID '#{instance_id}' does not exist"
  end
end

#update_routers(id) ⇒ Object



15
16
# File 'lib/fog/qingcloud/requests/compute/update_routers.rb', line 15

def update_routers(id)
end

#upload_userdata_attachment(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/qingcloud/requests/compute/upload_userdata_attachment.rb', line 16

def upload_userdata_attachment(options)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'action'   => 'UploadUserDataAttachment',
    'attachment_id' => 'uda-' + Fog::Mock.random_hex(8),
    'ret_code' => 0
  }
  response
end