Class: Fog::AWS::EC2::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/ec2.rb,
lib/fog/aws/models/ec2/images.rb,
lib/fog/aws/models/ec2/flavors.rb,
lib/fog/aws/models/ec2/servers.rb,
lib/fog/aws/models/ec2/volumes.rb,
lib/fog/aws/models/ec2/addresses.rb,
lib/fog/aws/models/ec2/key_pairs.rb,
lib/fog/aws/models/ec2/snapshots.rb,
lib/fog/aws/models/ec2/security_groups.rb,
lib/fog/aws/requests/ec2/attach_volume.rb,
lib/fog/aws/requests/ec2/create_volume.rb,
lib/fog/aws/requests/ec2/delete_volume.rb,
lib/fog/aws/requests/ec2/detach_volume.rb,
lib/fog/aws/requests/ec2/run_instances.rb,
lib/fog/aws/requests/ec2/create_key_pair.rb,
lib/fog/aws/requests/ec2/create_snapshot.rb,
lib/fog/aws/requests/ec2/delete_key_pair.rb,
lib/fog/aws/requests/ec2/delete_snapshot.rb,
lib/fog/aws/requests/ec2/describe_images.rb,
lib/fog/aws/requests/ec2/release_address.rb,
lib/fog/aws/requests/ec2/allocate_address.rb,
lib/fog/aws/requests/ec2/describe_regions.rb,
lib/fog/aws/requests/ec2/describe_volumes.rb,
lib/fog/aws/requests/ec2/reboot_instances.rb,
lib/fog/aws/requests/ec2/associate_address.rb,
lib/fog/aws/requests/ec2/describe_addresses.rb,
lib/fog/aws/requests/ec2/describe_instances.rb,
lib/fog/aws/requests/ec2/describe_key_pairs.rb,
lib/fog/aws/requests/ec2/describe_snapshots.rb,
lib/fog/aws/requests/ec2/get_console_output.rb,
lib/fog/aws/requests/ec2/terminate_instances.rb,
lib/fog/aws/requests/ec2/disassociate_address.rb,
lib/fog/aws/requests/ec2/create_security_group.rb,
lib/fog/aws/requests/ec2/delete_security_group.rb,
lib/fog/aws/requests/ec2/describe_security_groups.rb,
lib/fog/aws/requests/ec2/describe_availability_zones.rb,
lib/fog/aws/requests/ec2/describe_reserved_instances.rb,
lib/fog/aws/requests/ec2/revoke_security_group_ingress.rb,
lib/fog/aws/requests/ec2/authorize_security_group_ingress.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



122
123
124
125
126
# File 'lib/fog/aws/ec2.rb', line 122

def initialize(options={})
  @aws_access_key_id = options[:aws_access_key_id]
  @owner_id = Fog::AWS::Mock.owner_id
  @data = self.class.data[@aws_access_key_id]
end

Class Method Details

.dataObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fog/aws/ec2.rb', line 73

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :deleted_at => {},
      :addresses => {},
      :instances => {},
      :key_pairs => {},
      :security_groups => {
        'default' => {
          'groupDescription'  => 'default group',
          'groupName'         => 'default',
          'ipPermissions'     => [
            {
              'groups'      => [{'groupName' => 'default', 'userId' => @owner_id}],
              'fromPort'    => -1,
              'toPort'      => -1,
              'ipProtocol'  => 'icmp',
              'ipRanges'    => []
            },
            {
              'groups'      => [{'groupName' => 'default', 'userId' => @owner_id}],
              'fromPort'    => 0,
              'toPort'      => 65535,
              'ipProtocol'  => 'tcp',
              'ipRanges'    => []
            },
            {
              'groups'      => [{'groupName' => 'default', 'userId' => @owner_id}],
              'fromPort'    => 0,
              'toPort'      => 65535,
              'ipProtocol'  => 'udp',
              'ipRanges'    => []
            }
          ],
          'ownerId'           => @owner_id
        }
      },
      :snapshots => {},
      :volumes => {}
    }
  end
end

.reset_data(keys = data.keys) ⇒ Object



116
117
118
119
120
# File 'lib/fog/aws/ec2.rb', line 116

def self.reset_data(keys=data.keys)
  for key in [*keys]
    data.delete(key)
  end
end

Instance Method Details

#addresses(attributes = {}) ⇒ Object



9
10
11
12
13
# File 'lib/fog/aws/models/ec2/addresses.rb', line 9

def addresses(attributes = {})
  Fog::AWS::EC2::Addresses.new({
    :connection => self
  }.merge!(attributes))
end

#allocate_addressObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/aws/requests/ec2/allocate_address.rb', line 26

def allocate_address
  response = Excon::Response.new
  response.status = 200
  public_ip = Fog::AWS::Mock.ip_address
  data ={
    'instanceId' => '',
    'publicIp'   => public_ip
  }
  @data[:addresses][public_ip] = data
  response.body = {
    'publicIp'  => public_ip,
    'requestId' => Fog::AWS::Mock.request_id
  }
  response
end

#associate_address(instance_id, public_ip) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/aws/requests/ec2/associate_address.rb', line 32

def associate_address(instance_id, public_ip)
  response = Excon::Response.new
  response.status = 200
  instance = @data[:instances][instance_id]
  address = @data[:addresses][public_ip]
  if instance && address
    address['instanceId'] = instance_id
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'return'    => true
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#attach_volume(instance_id, volume_id, device) ⇒ Object



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
# File 'lib/fog/aws/requests/ec2/attach_volume.rb', line 38

def attach_volume(instance_id, volume_id, device)
  response = Excon::Response.new
  if instance_id && volume_id && device
    response.status = 200
    instance = @data[:instances][instance_id]
    volume = @data[:volumes][volume_id]
    if instance && volume
      data = {
        'attachTime'  => Time.now,
        'device'      => device,
        'instanceId'  => instance_id,
        'status'      => 'attaching',
        'volumeId'    => volume_id
      }
      volume['attachmentSet'] = [data]
      volume['status'] = 'attaching'
      response.status = 200
      response.body = {
        'requestId' => Fog::AWS::Mock.request_id
      }.merge!(data)
    else
      response.status = 400
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  else
    response.status = 400
    response.body = {
      'Code' => 'MissingParameter'
    }
    if !instance_id
      response['Message'] = 'The request must contain the parameter instance_id'
    elsif !volume_id
      response['Message'] = 'The request must contain the parameter volume_id'
    else
      response['Message'] = 'The request must contain the parameter device'
    end
  end
  response
end

#authorize_security_group_ingress(options = {}) ⇒ Object



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
# File 'lib/fog/aws/requests/ec2/authorize_security_group_ingress.rb', line 38

def authorize_security_group_ingress(options = {})
  response = Excon::Response.new
  group = @data[:security_groups][options['GroupName']]
  group['ipPermissions'] ||= []

  if options['GroupName'] && options['SourceSecurityGroupName'] && options['SourceSecurityGroupOwnerId']
    ['icmp', 'tcp', 'udp'].each do |protocol|
      group['ipPermissions'] << {
        'groups'      => [{'groupName' => options['GroupName'], 'userId' => @owner_id}],
        'fromPort'    => 1,
        'ipRanges'    => [],
        'ipProtocol'  => protocol,
        'toPort'      => 65535
      }
    end
  else
    group['ipPermissions'] << {
      'groups'      => [],
      'fromPort'    => options['FromPort'],
      'ipRanges'    => [{ 'cidrIp' => options['CidrIp'] }],
      'ipProtocol'  => options['IpProtocol'],
      'toPort'      => options['ToPort']
    }
  end
  response.status = 200
  response.body = {
    'requestId' => Fog::AWS::Mock.request_id,
    'return'    => true
  }
  response
end

#create_key_pair(key_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fog/aws/requests/ec2/create_key_pair.rb', line 32

def create_key_pair(key_name)
  response = Excon::Response.new
  unless @data[:key_pairs][key_name]
    response.status = 200
    data = {
      'keyFingerprint'  => Fog::AWS::Mock.key_fingerprint,
      'keyMaterial'     => Fog::AWS::Mock.key_material,
      'keyName'         => key_name
    }
    @data[:key_pairs][key_name] = data
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id
    }.merge!(data)
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#create_security_group(name, description) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fog/aws/requests/ec2/create_security_group.rb', line 32

def create_security_group(name, description)
  response = Excon::Response.new
  unless @data[:security_groups][name]
    data = {
      'groupDescription'  => description,
      'groupName'         => name,
      'ipPermissions'     => [],
      'ownerId'           => @owner_id
    }
    @data[:security_groups][name] = data
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'return'    => true
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#create_snapshot(volume_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fog/aws/requests/ec2/create_snapshot.rb', line 34

def create_snapshot(volume_id)
  response = Excon::Response.new
  if @data[:volumes][volume_id]
    response.status = 200
    snapshot_id = Fog::AWS::Mock.snapshot_id
    data = {
      'progress'    => '',
      'snapshotId'  => snapshot_id,
      'startTime'   => Time.now,
      'status'      => 'pending',
      'volumeId'    => volume_id
    }
    @data[:snapshots][snapshot_id] = data
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id
    }.merge!(data)
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#create_volume(availability_zone, size, snapshot_id = nil) ⇒ Object



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
# File 'lib/fog/aws/requests/ec2/create_volume.rb', line 38

def create_volume(availability_zone, size, snapshot_id = nil)
  response = Excon::Response.new
  if availability_zone && size
    response.status = 200
    volume_id = Fog::AWS::Mock.volume_id
    data = {
      'availabilityZone'  => availability_zone,
      'attachmentSet'     => [],
      'createTime'        => Time.now,
      'size'              => size,
      'snapshotId'        => snapshot_id || '',
      'status'            => 'creating',
      'volumeId'          => volume_id
    }
    @data[:volumes][volume_id] = data
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id
    }.merge!(data.reject {|key,value| !['availabilityZone','createTime','size','snapshotId','status','volumeId'].include?(key) })
  else
    response.status = 400
    response.body = {
      'Code' => 'MissingParameter'
    }
    unless availability_zone
      response.body['Message'] = 'The request must contain the parameter availability_zone'
    else
      response.body['Message'] = 'The request must contain the parameter size'
    end
  end
  response
end

#delete_key_pair(key_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/aws/requests/ec2/delete_key_pair.rb', line 30

def delete_key_pair(key_name)
  response = Excon::Response.new
  @data[:key_pairs].delete(key_name)
  response.status = 200
  response.body = {
    'requestId' => Fog::AWS::Mock.request_id,
    'return'    => true
  }
  response
end

#delete_security_group(name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/aws/requests/ec2/delete_security_group.rb', line 29

def delete_security_group(name)
  response = Excon::Response.new
  if @data[:security_groups][name]
    @data[:security_groups].delete(name)
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'return'    => true
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#delete_snapshot(snapshot_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/aws/requests/ec2/delete_snapshot.rb', line 30

def delete_snapshot(snapshot_id)
  response = Excon::Response.new
  if snapshot = @data[:snapshots].delete(snapshot_id)
    response.status = true
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'return'    => true
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#delete_volume(volume_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/aws/requests/ec2/delete_volume.rb', line 30

def delete_volume(volume_id)
  response = Excon::Response.new
  if volume = @data[:volumes][volume_id]
    @data[:deleted_at][volume_id] = Time.now
    volume['status'] = 'deleting'
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'return'    => true
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#describe_addresses(public_ip = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fog/aws/requests/ec2/describe_addresses.rb', line 32

def describe_addresses(public_ip = [])
  response = Excon::Response.new
  public_ip = [*public_ip]
  if public_ip != []
    addresses_set = @data[:addresses].reject {|key, value| !public_ip.include?(key)}.values
  else
    addresses_set = @data[:addresses].values
  end
  if public_ip.length == 0 || public_ip.length == addresses_set.length
    response.status = 200
    response.body = {
      'requestId'     => Fog::AWS::Mock.request_id,
      'addressesSet'  => addresses_set
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#describe_availability_zones(zone_name = []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/aws/requests/ec2/describe_availability_zones.rb', line 33

def describe_availability_zones(zone_name = [])
  response = Excon::Response.new
  zone_name = [*zone_name]
  zones = {
    'us-east-1a' => {"zoneName"=>"us-east-1a", "regionName"=>"us-east-1", "zoneState"=>"available"}, 
    'us-east-1b' => {"zoneName"=>"us-east-1b", "regionName"=>"us-east-1", "zoneState"=>"available"}, 
    'us-east-1c' => {"zoneName"=>"us-east-1c", "regionName"=>"us-east-1", "zoneState"=>"available"}, 
    'us-east-1d' => {"zoneName"=>"us-east-1d", "regionName"=>"us-east-1", "zoneState"=>"available"}
  }
  if zone_name != []
    availability_zone_info = zones.reject {|key, value| !zone_name.include?(key)}.values
  else
    availability_zone_info = zones.values
  end

  response.status = 200
  response.body = {
    'requestId'             => Fog::AWS::Mock.request_id,
    'availabilityZoneInfo'  => availability_zone_info
  }
  response
end

#describe_images(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/aws/requests/ec2/describe_images.rb', line 47

def describe_images(options = {})
  response = Excon::Response.new
  images = []

  (rand(101 + 100)).times do
    images << Fog::AWS::Mock.image
  end

  response.status = 200
  response.body = {
    'requestId' => Fog::AWS::Mock.request_id,
    'imagesSet' => images
  }
  response
end

#describe_instances(instance_id = []) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fog/aws/requests/ec2/describe_instances.rb', line 65

def describe_instances(instance_id = [])
  response = Excon::Response.new
  instance_id = [*instance_id]
  if instance_id != []
    instance_set = @data[:instances].reject {|key,value| !instance_id.include?(key)}.values
  else
    instance_set = @data[:instances].values
  end

  instance_set.each do |instance|
    case instance['instanceState']['name']
    when 'pending'
      if Time.now - instance['launchTime'] > Fog::Mock::DELAY
        instance['instanceState'] = { 'code' => 16, 'name' => 'running' }
      end
    when 'rebooting'
      instance['instanceState'] = { 'code' => 16, 'name' => 'running' }
    when 'shutting-down'
      if Time.now - @data[:deleted_at][instance['instanceId']] > Fog::Mock::DELAY * 2
        @data[:deleted_at].delete(instance['instanceId'])
        @data[:instances].delete(instance['instanceId'])
      elsif Time.now - @data[:deleted_at][instance['instanceId']] > Fog::Mock::DELAY
        instance['instanceState'] = { 'code' => 16, 'name' => 'terminating' }
      end
    when 'terminating'
      if Time.now - @data[:deleted_at][instance['instanceId']] > Fog::Mock::DELAY
        @data[:deleted_at].delete(instance['instanceId'])
        @data[:instances].delete(instance['instanceId'])
      end
    end
  end

  if instance_id.length == 0 || instance_id.length == instance_set.length
    instance_set = instance_set.reject {|instance| !@data[:instances][instance['instanceId']]}
    response.status = 200
    reservation_set = {}
    instance_set.each do |instance|
      reservation_set[instance['reservationId']] ||= {
        'groupSet'      => instance['groupSet'],
        'instancesSet'  => [],
        'ownerId'       => instance['ownerId'],
        'reservationId' => instance['reservationId']
      }
      reservation_set[instance['reservationId']]['instancesSet'] << instance.reject{|key,value| !['amiLaunchIndex', 'blockDeviceMapping', 'dnsName', 'imageId', 'instanceId', 'instanceState', 'instanceType', 'kernelId', 'keyName', 'launchTime', 'monitoring', 'placement', 'privateDnsName', 'productCodes', 'ramdiskId', 'reason', 'rootDeviceType'].include?(key)}
    end

    response.body = {
      'requestId'       => Fog::AWS::Mock.request_id,
      'reservationSet' => reservation_set.values
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#describe_key_pairs(key_name = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/aws/requests/ec2/describe_key_pairs.rb', line 32

def describe_key_pairs(key_name = [])
  response = Excon::Response.new
  key_name = [*key_name]
  if key_name != []
    key_set = @data[:key_pairs].reject {|key, value| !key_name.include?(key)}.values
  else
    key_set = @data[:key_pairs].values
  end
  if key_name.length == 0 || key_name.length == key_set.length
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'keySet'    => key_set.map do |key|
        key.reject {|key,value| !['keyFingerprint', 'keyName'].include?(key)}
      end
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#describe_regions(region_name = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fog/aws/requests/ec2/describe_regions.rb', line 32

def describe_regions(region_name = [])
  response = Excon::Response.new
  region_name = [*region_name]
  regions = {
    'eu-west-1' => {"regionName"=>"eu-west-1", "regionEndpoint"=>"eu-west-1.ec2.amazonaws.com"},
    'us-east-1' => {"regionName"=>"us-east-1", "regionEndpoint"=>"us-east-1.ec2.amazonaws.com"}
  }
  if region_name != []
    region_info = regions.reject {|key, value| !region_name.include?(key)}.values
  else
    region_info = regions.values
  end

  response.status = 200
  response.body = {
    'requestId'   => Fog::AWS::Mock.request_id,
    'regionInfo'  => region_info
  }
  response
end

#describe_reserved_instances(reserved_instances_id = {}) ⇒ Object

Raises:



40
41
42
# File 'lib/fog/aws/requests/ec2/describe_reserved_instances.rb', line 40

def describe_reserved_instances(reserved_instances_id = {})
  raise MockNotImplemented.new("Contributions welcome!")
end

#describe_security_groups(group_name = []) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/aws/requests/ec2/describe_security_groups.rb', line 42

def describe_security_groups(group_name = [])
  response = Excon::Response.new
  group_name = [*group_name]
  if group_name != []
    security_group_info = @data[:security_groups].reject {|key, value| !group_name.include?(key)}.values
  else
    security_group_info = @data[:security_groups].values
  end
  if group_name.length == 0 || group_name.length == security_group_info.length
    response.status = 200
    response.body = {
      'requestId'         => Fog::AWS::Mock.request_id,
      'securityGroupInfo' => security_group_info
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#describe_snapshots(snapshot_id = []) ⇒ Object



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
# File 'lib/fog/aws/requests/ec2/describe_snapshots.rb', line 35

def describe_snapshots(snapshot_id = [])
  response = Excon::Response.new
  snapshot_id = [*snapshot_id]
  if snapshot_id != []
    snapshot_set = @data[:snapshots].reject {|key,value| !snapshot_id.include?(key)}.values
  else
    snapshot_set = @data[:snapshots].values
  end

  snapshot_set.each do |snapshot|
    case snapshot['status']
    when 'in progress', 'pending'
      if Time.now - snapshot['startTime'] > Fog::Mock::DELAY * 2
        snapshot['progress']  = '100%'
        snapshot['status']    = 'completed'
      elsif Time.now - snapshot['startTime'] > Fog::Mock::DELAY
        snapshot['progress']  = '50%'
        snapshot['status']    = 'in progress'
      end
    end
  end

  if snapshot_id.length == 0 || snapshot_id.length == snapshot_set.length
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'snapshotSet' => snapshot_set
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#describe_volumes(volume_id = []) ⇒ Object



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
# File 'lib/fog/aws/requests/ec2/describe_volumes.rb', line 41

def describe_volumes(volume_id = [])
  response = Excon::Response.new
  volume_id = [*volume_id]
  if volume_id != []
    volume_set = @data[:volumes].reject {|key,value| !volume_id.include?(key)}.values
  else
    volume_set = @data[:volumes].values
  end

  volume_set.each do |volume|
    case volume['status']
    when 'attaching'
      if Time.now - volume['attachmentSet'].first['attachTime'] > Fog::Mock::DELAY
        volume['attachmentSet'].first['status'] = 'attached'
        volume['status'] = 'attached'
      end
    when 'creating'
      if Time.now - volume['createTime'] > Fog::Mock::DELAY
        volume['status'] = 'available'
      end
    when 'deleting'
      if Time.now - @data[:deleted_at][volume['volumeId']] > Fog::Mock::DELAY
        @data[:deleted_at].delete(volume['volumeId'])
        @data[:volumes].delete(volume['volumeId'])
      end
    end
  end

  if volume_id.length == 0 || volume_id.length == volume_set.length
    volume_set = volume_set.reject {|volume| !@data[:volumes][volume['volumeId']]}
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'volumeSet' => volume_set
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#detach_volume(volume_id, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/aws/requests/ec2/detach_volume.rb', line 38

def detach_volume(volume_id, options = {})
  response = Excon::Response.new
  response.status = 200
  if volume = @data[:volumes][volume_id]
    data = volume['attachmentSet'].pop
    @data[:volumes][volume_id]['status'] = 'available'
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id
    }.merge!(data)
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#disassociate_address(public_ip) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/aws/requests/ec2/disassociate_address.rb', line 30

def disassociate_address(public_ip)
  response = Excon::Response.new
  response.status = 200
  if address = @data[:addresses][public_ip]
    address['instanceId'] = ''
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'return'    => true
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#flavorsObject



9
10
11
# File 'lib/fog/aws/models/ec2/flavors.rb', line 9

def flavors
  Fog::AWS::EC2::Flavors.new(:connection => self)
end

#get_console_output(instance_id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aws/requests/ec2/get_console_output.rb', line 32

def get_console_output(instance_id)
  response = Excon::Response.new
  if instance = @data[:instances][instance_id]
    response.status = 200
    response.body = {
      'instanceId'    => instance_id,
      'output'        => Fog::AWS::Mock.console_output,
      'requestId'     => Fog::AWS::Mock.request_id,
      'timestamp'     => Time.now
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#imagesObject



9
10
11
# File 'lib/fog/aws/models/ec2/images.rb', line 9

def images
  Fog::AWS::EC2::Images.new(:connection => self)
end

#key_pairsObject



9
10
11
# File 'lib/fog/aws/models/ec2/key_pairs.rb', line 9

def key_pairs
  Fog::AWS::EC2::KeyPairs.new(:connection => self)
end

#reboot_instances(instance_id = []) ⇒ Object



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

def reboot_instances(instance_id = [])
  response = Excon::Response.new
  instance_id = [*instance_id]
  if (@data[:instances].keys & instance_id).length == instance_id.length
    for instance_id in instance_id
      @data[:instances][instance_id]['status'] = 'rebooting'
    end
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'return'    => true
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#release_address(public_ip) ⇒ Object



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

def release_address(public_ip)
  response = Excon::Response.new
  if (address = @data[:addresses].delete(public_ip))
    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'return'    => true
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#revoke_security_group_ingress(options = {}) ⇒ Object

TODO: handle the GroupName/Source/Source case



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fog/aws/requests/ec2/revoke_security_group_ingress.rb', line 39

def revoke_security_group_ingress(options = {})
  if options['GroupName'] && options['SourceSecurityGroupName'] && options['SourceSecurityGroupOwnerId']
    raise MockNotImplemented.new("Contributions welcome!")
  else
    response = Excon::Response.new
    group = @data[:security_groups][options['GroupName']]

    ingress = group['ipPermissions'].select {|permission|
      permission['fromPort']    == options['FromPort'] &&
      permission['ipProtocol']  == options['IpProtocol'] &&
      permission['toPort']      == options['ToPort'] &&
      permission['ipRanges'].first['cidrIp'] == options['CidrIp']
    }.first

    group['ipPermissions'].delete(ingress)

    response.status = 200
    response.body = {
      'requestId' => Fog::AWS::Mock.request_id,
      'return'    => true
    }
    response
  end
end

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

TODO: allow for block device mapping in mocks TODO: allow for rootDeviceType specification



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
# File 'lib/fog/aws/requests/ec2/run_instances.rb', line 99

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

  group_set = [ (options['GroupId'] || 'default') ]
  instances_set = []
  reservation_id = Fog::AWS::Mock.reservation_id

  min_count.times do |i|
    instance_id = Fog::AWS::Mock.instance_id
    data = {
      'amiLaunchIndex'      => i,
      'blockDeviceMapping'  => [],
      'dnsName'             => '',
      'groupSet'            => group_set,
      'imageId'             => image_id,
      'instanceId'          => instance_id,
      'instanceState'       => { 'code' => 0, 'name' => 'pending' },
      'instanceType'        => options['InstanceType'] || 'm1.small',
      'kernelId'            => options['KernelId'] || Fog::AWS::Mock.kernel_id,
      'keyName'             => options['KeyName'] || '',
      'launchTime'          => Time.now,
      'monitoring'          => { 'state' => options['Monitoring.Enabled'] || false },
      'ownerId'             => @owner_id,
      'placement'           => { 'availabilityZone' => options['Placement.AvailabilityZone'] || Fog::AWS::Mock.availability_zone },
      'privateDnsName'      => '',
      'productCodes'        => [],
      'ramdiskId'           => options['RamdiskId'] || Fog::AWS::Mock.ramdisk_id,
      'reason'              => '',
      'reservationId'       => reservation_id,
      'rootDeviceType'      => 'instance-store'
    }
    @data[:instances][instance_id] = data
    instances_set << data.reject{|key,value| !['amiLaunchIndex', 'blockDeviceMapping', 'dnsName', 'imageId', 'instanceId', 'instanceState', 'instanceType', 'kernelId', 'keyName', 'launchTime', 'monitoring', 'placement', 'privateDnsName', 'productCodes', 'ramdiskId', 'reason', 'rootDeviceType'].include?(key)}
  end
  response.body = {
    'groupSet'      => group_set,
    'instancesSet'  => instances_set,
    'ownerId'       => @owner_id,
    'requestId'     => Fog::AWS::Mock.request_id,
    'reservationId' => reservation_id
  }
  response
end

#security_groupsObject



9
10
11
# File 'lib/fog/aws/models/ec2/security_groups.rb', line 9

def security_groups
  Fog::AWS::EC2::SecurityGroups.new(:connection => self)
end

#serversObject



9
10
11
# File 'lib/fog/aws/models/ec2/servers.rb', line 9

def servers
  Fog::AWS::EC2::Servers.new(:connection => self)
end

#snapshots(attributes = {}) ⇒ Object



9
10
11
12
13
# File 'lib/fog/aws/models/ec2/snapshots.rb', line 9

def snapshots(attributes = {})
  Fog::AWS::EC2::Snapshots.new({
    :connection => self
  }.merge!(attributes))
end

#terminate_instances(instance_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fog/aws/requests/ec2/terminate_instances.rb', line 37

def terminate_instances(instance_id)
  response = Excon::Response.new
  instance_id = [*instance_id]
  if (@data[:instances].keys & instance_id).length == instance_id.length
    for instance_id in instance_id
      response.body = {
        'requestId'     => Fog::AWS::Mock.request_id,
        'instancesSet'  => []
      }
      instance = @data[:instances][instance_id]
      @data[:deleted_at][instance_id] = Time.now
      response.status = 200
      # TODO: the codes are mostly educated guessing, not certainty
      code = case instance['state']
      when 'pending'
        0
      when 'running'
        16
      when 'shutting-down'
        32
      when 'terminated'
        64
      when 'rebooting'
        128
      end
      state = { 'name' => 'shutting-down', 'code' => 32}
      response.body['instancesSet'] << {
        'instanceId'    => instance_id,
        'previousState' => instance['instanceState'],
        'currentState'  => state
      }
      instance['instanceState'] = state

      describe_addresses.body['addressesSet'].each do |address|
        if address['instanceId'] == instance_id
          disassociate_address(address['publicIp'])
        end
      end

      describe_volumes.body['volumeSet'].each do |volume|
        if volume['attachmentSet'].first && volume['attachmentSet'].first['instanceId'] == instance_id
          detach_volume(volume['volumeId'])
        end
      end
    end
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#volumes(attributes = {}) ⇒ Object



9
10
11
12
13
# File 'lib/fog/aws/models/ec2/volumes.rb', line 9

def volumes(attributes = {})
  Fog::AWS::EC2::Volumes.new({
    :connection => self
  }.merge!(attributes))
end