# File lib/fog/aws/compute.rb, line 145 def self.data @data ||= Hash.new do |hash, region| hash[region] = Hash.new do |region_hash, key| owner_id = Fog::AWS::Mock.owner_id security_group_id = Fog::AWS::Mock.security_group_id region_hash[key] = { :deleted_at => {}, :addresses => {}, :images => {}, :image_launch_permissions => Hash.new do |permissions_hash, image_key| permissions_hash[image_key] = { :users => [] } end, :instances => {}, :reserved_instances => {}, :key_pairs => {}, :limits => { :addresses => 5 }, :owner_id => owner_id, :security_groups => { 'default' => { 'groupDescription' => 'default group', 'groupName' => 'default', 'groupId' => security_group_id, 'ipPermissionsEgress' => [], 'ipPermissions' => [ { 'groups' => [{'groupName' => 'default', 'userId' => owner_id, 'groupId' => security_group_id }], 'fromPort' => -1, 'toPort' => -1, 'ipProtocol' => 'icmp', 'ipRanges' => [] }, { 'groups' => [{'groupName' => 'default', 'userId' => owner_id, 'groupId' => security_group_id}], 'fromPort' => 0, 'toPort' => 65535, 'ipProtocol' => 'tcp', 'ipRanges' => [] }, { 'groups' => [{'groupName' => 'default', 'userId' => owner_id, 'groupId' => security_group_id}], 'fromPort' => 0, 'toPort' => 65535, 'ipProtocol' => 'udp', 'ipRanges' => [] } ], 'ownerId' => owner_id } }, :network_interfaces => {}, :snapshots => {}, :volumes => {}, :internet_gateways => {}, :tags => {}, :tag_sets => Hash.new do |tag_set_hash, resource_id| tag_set_hash[resource_id] = {} end, :subnets => [], :vpcs => [], :dhcp_options => [], :internet_gateways => [] } end end end
# File lib/fog/aws/compute.rb, line 219 def initialize(options={}) @use_iam_profile = options[:use_iam_profile] @aws_credentials_expire_at = Time::now + 20 setup_credentials(options) @region = options[:region] || 'us-east-1' unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region) raise ArgumentError, "Unknown region: #{@region.inspect}" end end
# File lib/fog/aws/requests/compute/allocate_address.rb, line 32 def allocate_address(domain = 'standard') domain = domain == 'vpc' ? 'vpc' : 'standard' response = Excon::Response.new if describe_addresses.body['addressesSet'].size < self.data[:limits][:addresses] response.status = 200 public_ip = Fog::AWS::Mock.ip_address data = { 'instanceId' => nil, 'publicIp' => public_ip, 'domain' => domain } if domain == 'vpc' data['allocationId'] = "eipalloc-#{Fog::Mock.random_hex(8)}" end self.data[:addresses][public_ip] = data response.body = data.reject {|k, v| k == 'instanceId' }.merge('requestId' => Fog::AWS::Mock.request_id) response else response.status = 400 response.body = "<?xml version=\"1.0\"?><Response><Errors><Error><Code>AddressLimitExceeded</Code><Message>Too many addresses allocated</Message></Error></Errors><RequestID>#{Fog::AWS::Mock.request_id}</RequestID></Response>" raise(Excon::Errors.status_error({:expects => 200}, response)) end end
# File lib/fog/aws/compute.rb, line 258 def apply_tag_filters(resources, filters, resource_id_key) tag_set_fetcher = lambda {|resource| self.data[:tag_sets][resource[resource_id_key]] } # tag-key: match resources tagged with this key (any value) if filters.has_key?('tag-key') value = filters.delete('tag-key') resources = resources.select{|r| tag_set_fetcher[r].has_key?(value)} end # tag-value: match resources tagged with this value (any key) if filters.has_key?('tag-value') value = filters.delete('tag-value') resources = resources.select{|r| tag_set_fetcher[r].values.include?(value)} end # tag:key: match resources tagged with a key-value pair. Value may be an array, which is OR'd. tag_filters = {} filters.keys.each do |key| tag_filters[key.gsub('tag:', '')] = filters.delete(key) if /^tag:/ =~ key end for tag_key, tag_value in tag_filters resources = resources.select{|r| tag_value.include?(tag_set_fetcher[r][tag_key])} end resources end
# File lib/fog/aws/requests/compute/associate_address.rb, line 43 def associate_address(instance_id=nil, public_ip=nil, network_interface_id=nil, allocation_id=nil) public_ip = allocation_id.nil? ? public_ip : nil response = Excon::Response.new response.status = 200 instance = self.data[:instances][instance_id] address = public_ip.nil? ? nil : self.data[:addresses][public_ip] if ((instance && address) || (instance && !allocation_id.nil?) || (!allocation_id.nil? && !network_interface_id.nil?)) if !allocation_id.nil? allocation_ip = describe_addresses( 'allocation-id' => "#{allocation_id}").body['addressesSet'].first if !allocation_ip.nil? public_ip = allocation_ip['publicIp'] end end if !address.nil? if current_instance = self.data[:instances][address['instanceId']] current_instance['ipAddress'] = current_instance['originalIpAddress'] end address['instanceId'] = instance_id end # detach other address (if any) if self.data[:addresses][instance['ipAddress']] self.data[:addresses][instance['ipAddress']]['instanceId'] = nil end if !public_ip.nil? instance['ipAddress'] = public_ip instance['dnsName'] = Fog::AWS::Mock.dns_name_for(public_ip) end response.status = 200 if !instance_id.nil? && !public_ip.nil? response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } elsif !allocation_id.nil? response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true, 'associationId' => Fog::AWS::Mock.request_id } end response #elsif ! network_interface_id.nil? && allocation_id.nil? # raise Fog::Compute::AWS::NotFound.new("You must specify an AllocationId when specifying a NetworkInterfaceID") #elsif instance.nil? && network_interface_id.nil? # raise Fog::Compute::AWS::Error.new("You must specify either an InstanceId or a NetworkInterfaceID") #elsif !instance && !network_interface_id # raise Fog::Compute::AWS::Error.new(" 2 You must specify either an InstanceId or a NetworkInterfaceID") elsif !instance raise Fog::Compute::AWS::NotFound.new("You must specify either an InstanceId or a NetworkInterfaceID") elsif !address raise Fog::Compute::AWS::Error.new("AuthFailure => The address '#{public_ip}' does not belong to you.") end end
# File lib/fog/aws/requests/compute/associate_dhcp_options.rb, line 35 def associate_dhcp_options(dhcp_options_id, vpc_id) response = Excon::Response.new if dhcp_options_id && vpc_id response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else if !dhcp_options_id message << 'The request must contain the parameter dhcp_options_id' elsif !vpc_id message << 'The request must contain the parameter vpc_id' end raise Fog::Compute::AWS::Error.new(message) end end
# File lib/fog/aws/requests/compute/attach_internet_gateway.rb, line 34 def attach_internet_gateway(internet_gateway_id, vpc_id) response = Excon::Response.new if internet_gateway_id && vpc_id response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else if !internet_gateway_id message << 'The request must contain the parameter internet_gateway_id' elsif !vpc_id message << 'The request must contain the parameter vpc_id' end raise Fog::Compute::AWS::Error.new(message) end end
# File lib/fog/aws/requests/compute/attach_network_interface.rb, line 36 def attach_network_interface(nic_id, instance_id, device_index) response = Excon::Response.new if self.data[:network_interfaces][nic_id] attachment = self.data[:network_interfaces][nic_id]['attachment'] attachment['attachmentId'] = Fog::AWS::Mock.request_id attachment['instanceId'] = instance_id response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'attachmentId' => attachment['attachmentId'] } else raise Fog::Compute::AWS::NotFound.new("The network interface '#{nic_id}' does not exist") end response end
# File lib/fog/aws/requests/compute/attach_volume.rb, line 41 def attach_volume(instance_id, volume_id, device) response = Excon::Response.new if instance_id && volume_id && device response.status = 200 instance = self.data[:instances][instance_id] volume = self.data[:volumes][volume_id] if instance && volume unless volume['status'] == 'available' raise Fog::Compute::AWS::Error.new("Client.VolumeInUse => Volume #{volume_id} is unavailable") end 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) response elsif !instance raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_id}' does not exist.") elsif !volume raise Fog::Compute::AWS::NotFound.new("The volume '#{volume_id}' does not exist.") end else message = 'MissingParameter => ' if !instance_id message << 'The request must contain the parameter instance_id' elsif !volume_id message << 'The request must contain the parameter volume_id' else message << 'The request must contain the parameter device' end raise Fog::Compute::AWS::Error.new(message) end end
Usage
AWS.copy_snapshot("snap-1db0a957", 'us-east-1')
# File lib/fog/aws/requests/compute/copy_snapshot.rb, line 41 def copy_snapshot(source_snapshot_id, source_region, description = nil) response = Excon::Response.new response.status = 200 snapshot_id = Fog::AWS::Mock.snapshot_id data = { 'snapshotId' => snapshot_id, } self.data[:snapshots][snapshot_id] = data response.body = { 'requestId' => Fog::AWS::Mock.request_id }.merge!(data) response end
# File lib/fog/aws/requests/compute/create_dhcp_options.rb, line 43 def create_dhcp_options(dhcp_configurations = {}) params = {} params.merge!(indexed_multidimensional_params(dhcp_configurations)) Excon::Response.new.tap do |response| response.status = 200 self.data[:dhcp_options].push({ 'dhcpOptionsId' => Fog::AWS::Mock.dhcp_options_id, 'dhcpConfigurationSet' => {}, 'tagSet' => {} }) response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'dhcpOptionsSet' => self.data[:dhcp_options] } end end
Usage
AWS.create_image("i-ac65ee8c", "test", "something")
# File lib/fog/aws/requests/compute/create_image.rb, line 54 def create_image(instance_id, name, description, no_reboot = false, options = {}) params = {} block_device_mappings = options[:block_device_mappings] || [] params.merge!Fog::AWS.indexed_param('BlockDeviceMapping.%d.DeviceName', block_device_mappings.map{|mapping| mapping['DeviceName']}) params.merge!Fog::AWS.indexed_param('BlockDeviceMapping.%d.NoDevice', block_device_mappings.map{|mapping| mapping['NoDevice']}) params.merge!Fog::AWS.indexed_param('BlockDeviceMapping.%d.VirtualName', block_device_mappings.map{|mapping| mapping['VirtualName']}) params.merge!Fog::AWS.indexed_param('BlockDeviceMapping.%d.Ebs.SnapshotId', block_device_mappings.map{|mapping| mapping['Ebs.SnapshotId']}) params.merge!Fog::AWS.indexed_param('BlockDeviceMapping.%d.Ebs.DeleteOnTermination', block_device_mappings.map{|mapping| mapping['Ebs.DeleteOnTermination']}) params.merge!Fog::AWS.indexed_param('BlockDeviceMapping.%d.Ebs.VolumeType', block_device_mappings.map{|mapping| mapping['Ebs.VolumeType']}) params.merge!Fog::AWS.indexed_param('BlockDeviceMapping.%d.Ebs.Iops', block_device_mappings.map{|mapping| mapping['Ebs.Iops']}) params.reject!{|k,v| v.nil?} reserved_ebs_root_device = '/dev/sda1' block_devices = options.delete(:block_device_mappings) || [] register_image_response = register_image(name, description, reserved_ebs_root_device, block_devices, options) response = Excon::Response.new if instance_id && !name.empty? response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'imageId' => register_image_response.body['imageId'] } else response.status = 400 response.body = { 'Code' => 'InvalidParameterValue' } if name.empty? response.body['Message'] = "Invalid value '' for name. Must be specified." end end response end
# File lib/fog/aws/requests/compute/create_internet_gateway.rb, line 35 def create_internet_gateway() self.data[:internet_gateways].push({ 'internetGatewayId' => Fog::AWS::Mock.request_id, 'attachmentSet' => {}, 'tagSet' => {} }) Excon::Response.new( :status => 200, :body => { 'requestId' => Fog::AWS::Mock.request_id, 'internetGatewaySet' => self.data[:internet_gateways] } ) end
# File lib/fog/aws/requests/compute/create_key_pair.rb, line 34 def create_key_pair(key_name) response = Excon::Response.new unless self.data[:key_pairs][key_name] response.status = 200 data = { 'keyFingerprint' => Fog::AWS::Mock.key_fingerprint, 'keyMaterial' => Fog::AWS::Mock.key_material, 'keyName' => key_name } self.data[:key_pairs][key_name] = data response.body = { 'requestId' => Fog::AWS::Mock.request_id }.merge!(data) response else raise Fog::Compute::AWS::Error.new("InvalidKeyPair.Duplicate => The keypair '#{key_name}' already exists.") end end
# File lib/fog/aws/requests/compute/create_network_interface.rb, line 65 def create_network_interface(subnetId, options = {}) response = Excon::Response.new if subnetId id = Fog::AWS::Mock.network_interface_id groups = {} if options['GroupSet'] options['GroupSet'].each do |group_id| name = self.data[:security_groups].select { |k,v| v['groupId'] == group_id } .first.first if name.nil? raise Fog::Compute::AWS::Error.new("Unknown security group '#{group_id}' specified") end groups[group_id] = name end end if options['PrivateIpAddress'].nil? options['PrivateIpAddress'] = "10.0.0.2" end data = { 'networkInterfaceId' => id, 'subnetId' => subnetId, 'vpcId' => 'mock-vpc-id', 'availabilityZone' => 'mock-zone', 'description' => options['Description'], 'ownerId' => '', 'requesterManaged' => 'false', 'status' => 'available', 'macAddress' => '00:11:22:33:44:55', 'privateIpAddress' => options['PrivateIpAddress'], 'sourceDestCheck' => true, 'groupSet' => groups, 'attachment' => {}, 'association' => {}, 'tagSet' => {} } self.data[:network_interfaces][id] = data response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'networkInterface' => data } response else response.status = 400 response.body = { 'Code' => 'InvalidParameterValue', 'Message' => "Invalid value '' for subnetId" } end end
# File lib/fog/aws/requests/compute/create_security_group.rb, line 37 def create_security_group(name, description, vpc_id=nil) response = Excon::Response.new unless self.data[:security_groups][name] data = { 'groupDescription' => description, 'groupName' => name, 'groupId' => Fog::AWS::Mock.security_group_id, 'ipPermissionsEgress' => [], 'ipPermissions' => [], 'ownerId' => self.data[:owner_id], 'vpcId' => vpc_id } self.data[:security_groups][name] = data response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'groupId' => data['groupId'], 'return' => true } response else raise Fog::Compute::AWS::Error.new("InvalidGroup.Duplicate => The security group '#{name}' already exists") end end
Usage
AWS.create_snapshot("vol-f7c23423", "latest snapshot")
# File lib/fog/aws/requests/compute/create_snapshot.rb, line 43 def create_snapshot(volume_id, description = nil) response = Excon::Response.new if volume = self.data[:volumes][volume_id] response.status = 200 snapshot_id = Fog::AWS::Mock.snapshot_id data = { 'description' => description, 'ownerId' => self.data[:owner_id], 'progress' => nil, 'snapshotId' => snapshot_id, 'startTime' => Time.now, 'status' => 'pending', 'volumeId' => volume_id, 'volumeSize' => volume['size'] } self.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
# File lib/fog/aws/requests/compute/create_subnet.rb, line 44 def create_subnet(vpcId, cidrBlock, options = {}) av_zone = options['AvailabilityZone'].nil? ? 'us-east-1c' : options['AvailabilityZone'] Excon::Response.new.tap do |response| if cidrBlock && vpcId response.status = 200 self.data[:subnets].push({ 'subnetId' => Fog::AWS::Mock.request_id, 'state' => 'pending', 'vpcId' => vpcId, 'cidrBlock' => cidrBlock, 'availableIpAddressCount' => "255", 'availabilityZone' => av_zone, 'tagSet' => {} }) response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'subnetSet' => self.data[:subnets] } else response.status = 400 response.body = { 'Code' => 'InvalidParameterValue' } if cidrBlock.empty? response.body['Message'] = "Invalid value '' for cidrBlock. Must be specified." end if vpcId.empty? response.body['Message'] = "Invalid value '' for vpcId. Must be specified." end end end end
# File lib/fog/aws/requests/compute/create_volume.rb, line 49 def create_volume(availability_zone, size, options = {}) unless options.is_a?(Hash) Fog::Logger.deprecation("create_volume with a bare snapshot_id is deprecated, use create_volume(availability_zone, size, 'SnapshotId' => snapshot_id) instead [light_black](#{caller.first})[/]") options = { 'SnapshotId' => options } end response = Excon::Response.new if availability_zone && (size || options['SnapshotId']) snapshot = self.data[:snapshots][options['SnapshotId']] if options['SnapshotId'] && !snapshot raise Fog::Compute::AWS::NotFound.new("The snapshot '#{options['SnapshotId']}' does not exist.") end if snapshot && size && size < snapshot['volumeSize'] raise Fog::Compute::AWS::NotFound.new("The snapshot '#{options['SnapshotId']}' has size #{snapshot['volumeSize']} which is greater than #{size}.") elsif snapshot && !size size = snapshot['volumeSize'] end if options['VolumeType'] == 'io1' iops = options['Iops'] if !iops raise Fog::Compute::AWS::Error.new("InvalidParameterCombination => The parameter iops must be specified for io1 volumes.") end if size < 10 raise Fog::Compute::AWS::Error.new("InvalidParameterValue => Volume of #{size}GiB is too small; minimum is 10GiB.") end if (iops_to_size_ratio = iops.to_f / size.to_f) > 10.0 raise Fog::Compute::AWS::Error.new("InvalidParameterValue => Iops to volume size ratio of #{"%.1f" % iops_to_size_ratio} is too high; maximum is 10.0") end if iops < 100 raise Fog::Compute::AWS::Error.new("VolumeIOPSLimit => Volume iops of #{iops} is too low; minimum is 100.") end if iops > 2000 raise Fog::Compute::AWS::Error.new("VolumeIOPSLimit => Volume iops of #{iops} is too high; maximum is 2000.") end end response.status = 200 volume_id = Fog::AWS::Mock.volume_id data = { 'availabilityZone' => availability_zone, 'attachmentSet' => [], 'createTime' => Time.now, 'iops' => options['Iops'], 'size' => size, 'snapshotId' => options['SnapshotId'], 'status' => 'creating', 'volumeId' => volume_id, 'volumeType' => options['VolumeType'] || 'standard' } self.data[:volumes][volume_id] = data response.body = { 'requestId' => Fog::AWS::Mock.request_id }.merge!(data.reject {|key,value| !['availabilityZone','createTime','size','snapshotId','status','volumeId','volumeType'].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
# File lib/fog/aws/requests/compute/create_vpc.rb, line 42 def create_vpc(cidrBlock) Excon::Response.new.tap do |response| if cidrBlock response.status = 200 self.data[:vpcs].push({ 'vpcId' => Fog::AWS::Mock.request_id, 'state' => 'pending', 'cidrBlock' => cidrBlock, 'dhcpOptionsId' => Fog::AWS::Mock.request_id, 'tagSet' => {} }) response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'vpcSet' => self.data[:vpcs] } else response.status = 400 response.body = { 'Code' => 'InvalidParameterValue' } if cidrBlock.empty? response.body['Message'] = "Invalid value '' for cidrBlock. Must be specified." end end end end
# File lib/fog/aws/compute.rb, line 234 def data self.region_data[@aws_access_key_id] end
# File lib/fog/aws/requests/compute/delete_dhcp_options.rb, line 31 def delete_dhcp_options(dhcp_options_id) Excon::Response.new.tap do |response| if dhcp_options_id response.status = 200 self.data[:dhcp_options].reject! { |v| v['dhcpOptionsId'] == dhcp_options_id } response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } else message = 'MissingParameter => ' message << 'The request must contain the parameter dhcp_options_id' raise Fog::Compute::AWS::Error.new(message) end end end
# File lib/fog/aws/requests/compute/delete_internet_gateway.rb, line 29 def delete_internet_gateway(internet_gateway_id) Excon::Response.new.tap do |response| if internet_gateway_id response.status = 200 self.data[:internet_gateways].reject! { |v| v['internetGatewayId'] == internet_gateway_id } response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } else message = 'MissingParameter => ' message << 'The request must contain the parameter internet_gateway_id' raise Fog::Compute::AWS::Error.new(message) end end end
# File lib/fog/aws/requests/compute/delete_key_pair.rb, line 33 def delete_key_pair(key_name) response = Excon::Response.new self.data[:key_pairs].delete(key_name) response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response end
# File lib/fog/aws/requests/compute/delete_network_interface.rb, line 29 def delete_network_interface(network_interface_id) response = Excon::Response.new if self.data[:network_interfaces][network_interface_id] if self.data[:network_interfaces][network_interface_id]['attachment']['attachmentId'] raise Fog::Compute::AWS::Error.new("Interface is in use") end self.data[:network_interfaces].delete(network_interface_id) response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::NotFound.new("The network interface '#{network_interface_id}' does not exist") end end
# File lib/fog/aws/requests/compute/delete_security_group.rb, line 43 def delete_security_group(name, id = nil) if name == 'default' raise Fog::Compute::AWS::Error.new("InvalidGroup.Reserved => The security group 'default' is reserved") end if name && id raise Fog::Compute::AWS::Error.new("May not specify both group_name and group_id") end if id name = self.data[:security_groups].reject { |k,v| v['groupId'] != id } .keys.first end response = Excon::Response.new if self.data[:security_groups][name] used_by_groups = [] self.region_data.each do |access_key, key_data| key_data[:security_groups].each do |group_name, group| next if group == self.data[:security_groups][name] group['ipPermissions'].each do |group_ip_permission| group_ip_permission['groups'].each do |group_group_permission| if group_group_permission['groupName'] == name && group_group_permission['userId'] == self.data[:owner_id] used_by_groups << "#{key_data[:owner_id]}:#{group_name}" end end end end end unless used_by_groups.empty? raise Fog::Compute::AWS::Error.new("InvalidGroup.InUse => Group #{self.data[:owner_id]}:#{name} is used by groups: #{used_by_groups.uniq.join(" ")}") end self.data[:security_groups].delete(name) response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::NotFound.new("The security group '#{name}' does not exist") end end
# File lib/fog/aws/requests/compute/delete_snapshot.rb, line 33 def delete_snapshot(snapshot_id) response = Excon::Response.new if snapshot = self.data[:snapshots].delete(snapshot_id) response.status = true response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::NotFound.new("The snapshot '#{snapshot_id}' does not exist.") end end
# File lib/fog/aws/requests/compute/delete_subnet.rb, line 30 def delete_subnet(subnet_id) Excon::Response.new.tap do |response| if subnet_id self.data[:subnets].reject! { |v| v['subnetId'] == subnet_id } response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } else message = 'MissingParameter => ' message << 'The request must contain the parameter subnet_id' raise Fog::Compute::AWS::Error.new(message) end end end
# File lib/fog/aws/requests/compute/delete_volume.rb, line 33 def delete_volume(volume_id) response = Excon::Response.new if volume = self.data[:volumes][volume_id] if volume["attachmentSet"].any? attach = volume["attachmentSet"].first raise Fog::Compute::AWS::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::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::NotFound.new("The volume '#{volume_id}' does not exist.") end end
# File lib/fog/aws/requests/compute/delete_vpc.rb, line 33 def delete_vpc(vpc_id) Excon::Response.new.tap do |response| if vpc_id response.status = 200 self.data[:vpcs].reject! { |v| v['vpcId'] == vpc_id } response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } else message = 'MissingParameter => ' message << 'The request must contain the parameter vpc_id' raise Fog::Compute::AWS::Error.new(message) end end end
# File lib/fog/aws/requests/compute/deregister_image.rb, line 32 def deregister_image(image_id) response = Excon::Response.new if image_id response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => "true" } response else message = 'MissingParameter => ' if !instance_id message << 'The request must contain the parameter image_id' end raise Fog::Compute::AWS::Error.new(message) end end
# File lib/fog/aws/requests/compute/describe_addresses.rb, line 39 def describe_addresses(filters = {}) unless filters.is_a?(Hash) Fog::Logger.deprecation("describe_addresses with #{filters.class} param is deprecated, use describe_addresses('public-ip' => []) instead [light_black](#{caller.first})[/]") filters = {'public-ip' => [*filters]} end response = Excon::Response.new addresses_set = self.data[:addresses].values aliases = {'public-ip' => 'publicIp', 'instance-id' => 'instanceId'} 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 = { 'requestId' => Fog::AWS::Mock.request_id, 'addressesSet' => addresses_set } response end
# File lib/fog/aws/requests/compute/describe_availability_zones.rb, line 40 def describe_availability_zones(filters = {}) unless filters.is_a?(Hash) Fog::Logger.deprecation("describe_availability_zones with #{filters.class} param is deprecated, use describe_availability_zones('zone-name' => []) instead [light_black](#{caller.first})[/]") filters = {'public-ip' => [*filters]} end response = Excon::Response.new all_zones = [ {"messageSet" => [], "regionName" => "us-east-1", "zoneName" => "us-east-1a", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "us-east-1", "zoneName" => "us-east-1b", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "us-east-1", "zoneName" => "us-east-1c", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "us-east-1", "zoneName" => "us-east-1d", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "us-east-1", "zoneName" => "us-east-1e", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "us-west-1", "zoneName" => "us-west-1a", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "us-west-1", "zoneName" => "us-west-1b", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "us-west-1", "zoneName" => "us-west-1c", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "us-west-2", "zoneName" => "us-west-2a", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "us-west-2", "zoneName" => "us-west-2b", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "sa-east-1", "zoneName" => "sa-east-1a", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "sa-east-1", "zoneName" => "sa-east-1b", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "eu-west-1", "zoneName" => "eu-west-1a", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "eu-west-1", "zoneName" => "eu-west-1b", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "eu-west-1", "zoneName" => "eu-west-1c", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "ap-northeast-1", "zoneName" => "ap-northeast-1a", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "ap-northeast-1", "zoneName" => "ap-northeast-1b", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "ap-southeast-1", "zoneName" => "ap-southeast-1a", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "ap-southeast-1", "zoneName" => "ap-southeast-1b", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "ap-southeast-2", "zoneName" => "ap-southeast-2a", "zoneState" => "available"}, {"messageSet" => [], "regionName" => "ap-southeast-2", "zoneName" => "ap-southeast-2b", "zoneState" => "available"}, ] availability_zone_info = all_zones.select { |zoneinfo| zoneinfo["regionName"] == @region } aliases = {'region-name' => 'regionName', 'zone-name' => 'zoneName', 'state' => 'zoneState'} for filter_key, filter_value in filters aliased_key = aliases[filter_key] availability_zone_info = availability_zone_info.reject{|availability_zone| ![*filter_value].include?(availability_zone[aliased_key])} end response.status = 200 response.body = { 'availabilityZoneInfo' => availability_zone_info, 'requestId' => Fog::AWS::Mock.request_id } response end
# File lib/fog/aws/requests/compute/describe_dhcp_options.rb, line 43 def describe_dhcp_options(filters = {}) Excon::Response.new.tap do |response| response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'dhcpOptionsSet' => self.data[:dhcp_options] } end end
# File lib/fog/aws/requests/compute/describe_images.rb, line 61 def describe_images(filters = {}) unless filters.is_a?(Hash) Fog::Logger.deprecation("describe_images with #{filters.class} param is deprecated, use describe_images('image-id' => []) instead [light_black](#{caller.first})[/]") filters = {'image-id' => [*filters]} end if filters.keys.any? {|key| key =~ /^block-device/} Fog::Logger.warning("describe_images block-device-mapping filters are not yet mocked [light_black](#{caller.first})[/]") Fog::Mock.not_implemented end if owner = filters.delete('Owner') if owner == 'self' filters['owner-id'] = self.data[:owner_id] else filters['owner-alias'] = owner end end response = Excon::Response.new aliases = { 'architecture' => 'architecture', 'description' => 'description', 'hypervisor' => 'hypervisor', 'image-id' => 'imageId', 'image-type' => 'imageType', 'is-public' => 'isPublic', 'kernel-id' => 'kernelId', 'manifest-location' => 'manifestLocation', 'name' => 'name', 'owner-alias' => 'imageOwnerAlias', 'owner-id' => 'imageOwnerId', 'ramdisk-id' => 'ramdiskId', 'root-device-name' => 'rootDeviceName', 'root-device-type' => 'rootDeviceType', 'state' => 'imageState', 'virtualization-type' => 'virtualizationType' } image_set = visible_images.values image_set = apply_tag_filters(image_set, filters, 'imageId') for filter_key, filter_value in filters aliased_key = aliases[filter_key] image_set = image_set.reject{|image| ![*filter_value].include?(image[aliased_key])} end image_set = image_set.map do |image| case image['imageState'] when 'pending' if Time.now - image['registered'] >= Fog::Mock.delay image['imageState'] = 'available' end end image.reject { |key, value| ['registered'].include?(key) }.merge('tagSet' => self.data[:tag_sets][image['imageId']]) end response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'imagesSet' => image_set } response end
# File lib/fog/aws/requests/compute/describe_instance_status.rb, line 33 def describe_instance_status(filters = {}) response = Excon::Response.new response.status = 200 response.body = { 'instanceStatusSet' => [], 'requestId' => Fog::AWS::Mock.request_id } response end
# File lib/fog/aws/requests/compute/describe_instances.rb, line 83 def describe_instances(filters = {}) unless filters.is_a?(Hash) Fog::Logger.deprecation("describe_instances with #{filters.class} param is deprecated, use describe_instances('instance-id' => []) instead [light_black](#{caller.first})[/]") filters = {'instance-id' => [*filters]} end response = Excon::Response.new instance_set = self.data[:instances].values instance_set = apply_tag_filters(instance_set, filters, 'instanceId') aliases = { 'architecture' => 'architecture', 'availability-zone' => 'availabilityZone', 'client-token' => 'clientToken', 'dns-name' => 'dnsName', 'group-id' => 'groupId', 'image-id' => 'imageId', 'instance-id' => 'instanceId', 'instance-lifecycle' => 'instanceLifecycle', 'instance-type' => 'instanceType', 'ip-address' => 'ipAddress', 'kernel-id' => 'kernelId', 'key-name' => 'key-name', 'launch-index' => 'launchIndex', 'launch-time' => 'launchTime', 'monitoring-state' => 'monitoringState', 'owner-id' => 'ownerId', 'placement-group-name' => 'placementGroupName', 'platform' => 'platform', 'private-dns-name' => 'privateDnsName', 'private-ip-address' => 'privateIpAddress', 'product-code' => 'productCode', 'ramdisk-id' => 'ramdiskId', 'reason' => 'reason', 'requester-id' => 'requesterId', 'reservation-id' => 'reservationId', 'root-device-name' => 'rootDeviceName', 'root-device-type' => 'rootDeviceType', 'spot-instance-request-id' => 'spotInstanceRequestId', 'subnet-id' => 'subnetId', 'virtualization-type' => 'virtualizationType', 'vpc-id' => 'vpcId' } block_device_mapping_aliases = { 'attach-time' => 'attachTime', 'delete-on-termination' => 'deleteOnTermination', 'device-name' => 'deviceName', 'status' => 'status', 'volume-id' => 'volumeId', } instance_state_aliases = { 'code' => 'code', 'name' => 'name' } state_reason_aliases = { 'code' => 'code', 'message' => 'message' } for filter_key, filter_value in filters if block_device_mapping_key = filter_key.split('block-device-mapping.')[1] aliased_key = block_device_mapping_aliases[block_device_mapping_key] instance_set = instance_set.reject{|instance| !instance['blockDeviceMapping'].detect {|block_device_mapping| [*filter_value].include?(block_device_mapping[aliased_key])}} elsif instance_state_key = filter_key.split('instance-state-')[1] aliased_key = instance_state_aliases[instance_state_key] instance_set = instance_set.reject{|instance| ![*filter_value].include?(instance['instanceState'][aliased_key])} elsif state_reason_key = filter_key.split('state-reason-')[1] aliased_key = state_reason_aliases[state_reason_key] instance_set = instance_set.reject{|instance| ![*filter_value].include?(instance['stateReason'][aliased_key])} elsif filter_key == "group-name" instance_set = instance_set.reject {|instance| !instance['groupSet'].include?(filter_value)} else aliased_key = aliases[filter_key] instance_set = instance_set.reject {|instance| ![*filter_value].include?(instance[aliased_key])} end end brand_new_instances = instance_set.find_all do |instance| instance['instanceState']['name'] == 'pending' && Time.now - instance['launchTime'] < Fog::Mock.delay * 2 end # Error if filtering for a brand new instance directly if (filters['instance-id'] || filters['instanceId']) && !brand_new_instances.empty? raise Fog::Compute::AWS::NotFound.new("The instance ID '#{brand_new_instances.first['instanceId']}' does not exist") end # Otherwise don't include it in the list instance_set = instance_set.reject {|instance| brand_new_instances.include?(instance) } response.status = 200 reservation_set = {} instance_set.each do |instance| case instance['instanceState']['name'] when 'pending' if Time.now - instance['launchTime'] >= Fog::Mock.delay * 2 instance['ipAddress'] = Fog::AWS::Mock.ip_address instance['originalIpAddress'] = instance['ipAddress'] instance['dnsName'] = Fog::AWS::Mock.dns_name_for(instance['ipAddress']) instance['privateIpAddress'] = Fog::AWS::Mock.private_ip_address instance['privateDnsName'] = Fog::AWS::Mock.private_dns_name_for(instance['privateIpAddress']) instance['instanceState'] = { 'code' => 16, 'name' => 'running' } end when 'rebooting' instance['instanceState'] = { 'code' => 16, 'name' => 'running' } when 'stopping' instance['instanceState'] = { 'code' => 0, 'name' => 'stopping' } instance['stateReason'] = { 'code' => 0 } when 'shutting-down' if Time.now - self.data[:deleted_at][instance['instanceId']] >= Fog::Mock.delay * 2 self.data[:deleted_at].delete(instance['instanceId']) self.data[:instances].delete(instance['instanceId']) elsif Time.now - self.data[:deleted_at][instance['instanceId']] >= Fog::Mock.delay instance['instanceState'] = { 'code' => 48, 'name' => 'terminating' } end when 'terminating' if Time.now - self.data[:deleted_at][instance['instanceId']] >= Fog::Mock.delay self.data[:deleted_at].delete(instance['instanceId']) self.data[:instances].delete(instance['instanceId']) end end if self.data[:instances][instance['instanceId']] reservation_set[instance['reservationId']] ||= { 'groupSet' => instance['groupSet'], 'groupIds' => instance['groupIds'], 'instancesSet' => [], 'ownerId' => instance['ownerId'], 'reservationId' => instance['reservationId'] } reservation_set[instance['reservationId']]['instancesSet'] << instance.reject{|key,value| !['amiLaunchIndex', 'architecture', 'blockDeviceMapping', 'clientToken', 'dnsName', 'ebsOptimized', 'iamInstanceProfile', 'imageId', 'instanceId', 'instanceState', 'instanceType', 'ipAddress', 'kernelId', 'keyName', 'launchTime', 'monitoring', 'networkInterfaces', 'ownerId', 'placement', 'platform', 'privateDnsName', 'privateIpAddress', 'productCodes', 'ramdiskId', 'reason', 'rootDeviceType', 'stateReason'].include?(key)}.merge('tagSet' => self.data[:tag_sets][instance['instanceId']]) end end response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'reservationSet' => reservation_set.values } response end
# File lib/fog/aws/requests/compute/describe_internet_gateways.rb, line 42 def describe_internet_gateways(filters = {}) internet_gateways = self.data[:internet_gateways] if filters['internet-gateway-id'] internet_gateways = internet_gateways.reject {|internet_gateway| internet_gateway['internetGatewayId'] != filters['internet-gateway-id']} end Excon::Response.new( :status => 200, :body => { 'requestId' => Fog::AWS::Mock.request_id, 'internetGatewaySet' => internet_gateways } ) end
# File lib/fog/aws/requests/compute/describe_key_pairs.rb, line 39 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('key-name' => []) instead [light_black](#{caller.first})[/]") filters = {'key-name' => [*filters]} end response = Excon::Response.new key_set = self.data[:key_pairs].values aliases = {'fingerprint' => 'keyFingerprint', 'key-name' => 'keyName'} for filter_key, filter_value in filters aliased_key = aliases[filter_key] key_set = key_set.reject{|key_pair| ![*filter_value].include?(key_pair[aliased_key])} end response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'keySet' => key_set.map do |key_pair| key_pair.reject {|key,value| !['keyFingerprint', 'keyName'].include?(key)} end } response end
# File lib/fog/aws/requests/compute/describe_network_interface_attribute.rb, line 44 def describe_network_interface_attribute(network_interface_id, attribute) response = Excon::Response.new if self.data[:network_interfaces][network_interface_id] response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'networkInterfaceId' => network_interface_id } case attribute when 'description', 'groupSet', 'sourceDestCheck', 'attachment' response.body[attribute] = self.data[:network_interfaces][network_interface_id][attribute] else raise Fog::Compute::AWS::Error.new("Illegal attribute '#{attribute}' specified") end response else raise Fog::Compute::AWS::NotFound.new("The network interface '#{network_interface_id}' does not exist") end end
# File lib/fog/aws/requests/compute/describe_network_interfaces.rb, line 65 def describe_network_interfaces(filters = {}) response = Excon::Response.new network_interface_info = self.data[:network_interfaces].values for filter_key, filter_value in filters network_interface_info = network_interface_info.reject{|nic| ![*filter_value].include?(nic[filter_key])} end response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'networkInterfaceSet' => network_interface_info } response end
# File lib/fog/aws/requests/compute/describe_regions.rb, line 39 def describe_regions(filters = {}) unless filters.is_a?(Hash) Fog::Logger.deprecation("describe_regions with #{filters.class} param is deprecated, use describe_regions('region-name' => []) instead [light_black](#{caller.first})[/]") filters = {'region-name' => [*filters]} end response = Excon::Response.new region_info = [ {"regionName"=>"eu-west-1", "regionEndpoint"=>"eu-west-1.ec2.amazonaws.com"}, {"regionName"=>"us-east-1", "regionEndpoint"=>"us-east-1.ec2.amazonaws.com"} ] aliases = {'region-name' => 'regionName', 'endpoint' => 'regionEndpoint'} for filter_key, filter_value in filters aliased_key = aliases[filter_key] region_info = region_info.reject{|region| ![*filter_value].include?(region[aliased_key])} end response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'regionInfo' => region_info } response end
# File lib/fog/aws/requests/compute/describe_reserved_instances.rb, line 46 def describe_reserved_instances(filters = {}) response = Excon::Response.new response.status = 200 response.body = { 'reservedInstancesSet' => self.data[:reserved_instances].values, 'requestId' => Fog::AWS::Mock.request_id } response end
# File lib/fog/aws/requests/compute/describe_reserved_instances_offerings.rb, line 54 def describe_reserved_instances_offerings(filters = {}) response = Excon::Response.new response.status = 200 self.data[:reserved_instances_offerings] ||= [{ 'reservedInstancesOfferingId' => Fog::AWS::Mock.reserved_instances_offering_id, 'instanceType' => 'm1.small', 'availabilityZone' => 'us-east-1d', 'duration' => 31536000, 'fixedPrice' => 350.0, 'offeringType' => 'Medium Utilization', 'usagePrice' => 0.03, 'productDescription' => 'Linux/UNIX', 'instanceTenancy' => 'default', 'currencyCode' => 'USD' }] response.body = { 'reservedInstancesOfferingsSet' => self.data[:reserved_instances_offerings], 'requestId' => Fog::AWS::Mock.request_id } response end
# File lib/fog/aws/requests/compute/describe_security_groups.rb, line 50 def describe_security_groups(filters = {}) unless filters.is_a?(Hash) Fog::Logger.deprecation("describe_security_groups with #{filters.class} param is deprecated, use describe_security_groups('group-name' => []) instead [light_black](#{caller.first})[/]") filters = {'group-name' => [*filters]} end response = Excon::Response.new security_group_info = self.data[:security_groups].reject { |k,v| k['amazon-elb-sg'] }.values aliases = { 'description' => 'groupDescription', 'group-name' => 'groupName', 'group-id' => 'groupId', 'owner-id' => 'ownerId' } permission_aliases = { 'cidr' => 'cidrIp', 'from-port' => 'fromPort', 'protocol' => 'ipProtocol', 'to-port' => 'toPort' } for filter_key, filter_value in filters if permission_key = filter_key.split('ip-permission.')[1] if permission_key == 'group-name' security_group_info = security_group_info.reject{|security_group| !security_group['ipPermissions']['groups'].detect {|group| [*filter_value].include?(group['groupName'])}} elsif permission_key == 'group-id' security_group_info = security_group_info.reject{|security_group| !security_group['ipPermissions']['groups'].detect {|group| [*filter_value].include?(group['groupId'])}} elsif permission_key == 'user-id' security_group_info = security_group_info.reject{|security_group| !security_group['ipPermissions']['groups'].detect {|group| [*filter_value].include?(group['userId'])}} else aliased_key = permission_aliases[filter_key] security_group_info = security_group_info.reject{|security_group| !security_group['ipPermissions'].detect {|permission| [*filter_value].include?(permission[aliased_key])}} end else aliased_key = aliases[filter_key] security_group_info = security_group_info.reject{|security_group| ![*filter_value].include?(security_group[aliased_key])} end end response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'securityGroupInfo' => security_group_info } response end
# File lib/fog/aws/requests/compute/describe_snapshots.rb, line 55 def describe_snapshots(filters = {}, options = {}) unless filters.is_a?(Hash) Fog::Logger.deprecation("describe_snapshots with #{filters.class} param is deprecated, use describe_snapshots('snapshot-id' => []) instead [light_black](#{caller.first})[/]") filters = {'snapshot-id' => [*filters]} end unless options.empty? Fog::Logger.deprecation("describe_snapshots with a second param is deprecated, use describe_snapshots(options) instead [light_black](#{caller.first})[/]") end response = Excon::Response.new snapshot_set = self.data[:snapshots].values if filters.delete('owner-alias') Fog::Logger.warning("describe_snapshots with owner-alias is not mocked [light_black](#{caller.first})[/]") end if (restorable_by = filters.delete('RestorableBy')) && restorable_by != 'self' Fog::Logger.warning("describe_snapshots with RestorableBy other than 'self' (wanted #{restorable_by.inspect}) is not mocked [light_black](#{caller.first})[/]") end snapshot_set = apply_tag_filters(snapshot_set, filters, 'snapshotId') aliases = { 'description' => 'description', 'owner-id' => 'ownerId', 'progress' => 'progress', 'snapshot-id' => 'snapshotId', 'start-time' => 'startTime', 'status' => 'status', 'volume-id' => 'volumeId', 'volume-size' => 'volumeSize' } for filter_key, filter_value in filters aliased_key = aliases[filter_key] snapshot_set = snapshot_set.reject{|snapshot| ![*filter_value].include?(snapshot[aliased_key])} 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' else snapshot['progress'] = '0%' snapshot['status'] = 'in progress' end end end snapshot_set = snapshot_set.map {|snapshot| snapshot.merge('tagSet' => self.data[:tag_sets][snapshot['snapshotId']]) } response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'snapshotSet' => snapshot_set } response end
# File lib/fog/aws/requests/compute/describe_subnets.rb, line 46 def describe_subnets(filters = {}) subnets = self.data[:subnets] if filters['subnet-id'] subnets = subnets.reject {|subnet| subnet['subnetId'] != filters['subnet-id']} end Excon::Response.new( :status => 200, :body => { 'requestId' => Fog::AWS::Mock.request_id, 'subnetSet' => subnets } ) end
# File lib/fog/aws/requests/compute/describe_volume_status.rb, line 30 def describe_volume_status(filters = {}) response = Excon::Response.new response.status = 200 response.body = { 'volumeStatusSet' => [], 'requestId' => Fog::AWS::Mock.request_id } response end
# File lib/fog/aws/requests/compute/describe_volumes.rb, line 51 def describe_volumes(filters = {}) unless filters.is_a?(Hash) Fog::Logger.deprecation("describe_volumes with #{filters.class} param is deprecated, use describe_volumes('volume-id' => []) instead [light_black](#{caller.first})[/]") filters = {'volume-id' => [*filters]} end response = Excon::Response.new volume_set = self.data[:volumes].values volume_set = apply_tag_filters(volume_set, filters, 'volumeId') aliases = { 'availability-zone' => 'availabilityZone', 'create-time' => 'createTime', 'size' => 'size', 'snapshot-id' => 'snapshotId', 'status' => 'status', 'volume-id' => 'volumeId' } attachment_aliases = { 'attach-time' => 'attachTime', 'delete-on-termination' => 'deleteOnTermination', 'device' => 'device', 'instance-id' => 'instanceId', 'status' => 'status' } for filter_key, filter_value in filters if attachment_key = filter_key.split('attachment.')[1] aliased_key = attachment_aliases[filter_key] volume_set = volume_set.reject{|volume| !volume['attachmentSet'].detect {|attachment| [*filter_value].include?(attachment[aliased_key])}} else aliased_key = aliases[filter_key] volume_set = volume_set.reject{|volume| ![*filter_value].include?(volume[aliased_key])} end 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'] = 'in-use' volume['status'] = 'in-use' end when 'creating' if Time.now - volume['createTime'] >= Fog::Mock.delay volume['status'] = 'available' end when 'deleting' if Time.now - self.data[:deleted_at][volume['volumeId']] >= Fog::Mock.delay self.data[:deleted_at].delete(volume['volumeId']) self.data[:volumes].delete(volume['volumeId']) end end end volume_set = volume_set.reject {|volume| !self.data[:volumes][volume['volumeId']]} volume_set = volume_set.map {|volume| volume.merge('tagSet' => self.data[:tag_sets][volume['volumeId']]) } response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'volumeSet' => volume_set } response end
# File lib/fog/aws/requests/compute/describe_vpcs.rb, line 43 def describe_vpcs(filters = {}) vpcs = self.data[:vpcs] if filters['vpc-id'] vpcs = vpcs.reject {|vpc| vpc['vpcId'] != filters['vpc-id']} end Excon::Response.new( :status => 200, :body => { 'requestId' => Fog::AWS::Mock.request_id, 'vpcSet' => vpcs } ) end
# File lib/fog/aws/requests/compute/detach_internet_gateway.rb, line 34 def detach_internet_gateway(internet_gateway_id, vpc_id) response = Excon::Response.new if internet_gateway_id && vpc_id response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else if !internet_gateway_id message << 'The request must contain the parameter internet_gateway_id' elsif !vpc_id message << 'The request must contain the parameter vpc_id' end raise Fog::Compute::AWS::Error.new(message) end end
# File lib/fog/aws/requests/compute/detach_network_interface.rb, line 31 def detach_network_interface(attachment_id, force = false) response = Excon::Response.new nic_id = self.data[:network_interfaces].select { |k,v| v['attachment']['attachmentId'] == attachment_id} .first.first if nic_id self.data[:network_interfaces][nic_id]["attachment"] = {} response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::NotFound.new("The network interface '#{network_interface_id}' does not exist") end end
# File lib/fog/aws/requests/compute/detach_volume.rb, line 41 def detach_volume(volume_id, options = {}) response = Excon::Response.new response.status = 200 if (volume = self.data[:volumes][volume_id]) if !volume['attachmentSet'].empty? data = volume['attachmentSet'].pop volume['status'] = 'available' response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id }.merge!(data) response else # real response has spacing issue below raise Fog::Compute::AWS::Error.new("IncorrectState => Volume '#{volume_id}'is in the 'available' state.") end else raise Fog::Compute::AWS::NotFound.new("The volume '#{volume_id}' does not exist.") end end
# File lib/fog/aws/requests/compute/disassociate_address.rb, line 35 def disassociate_address(public_ip) response = Excon::Response.new response.status = 200 if address = self.data[:addresses][public_ip] instance_id = address['instanceId'] instance = self.data[:instances][instance_id] instance['ipAddress'] = instance['originalIpAddress'] instance['dnsName'] = Fog::AWS::Mock.dns_name_for(instance['ipAddress']) address['instanceId'] = nil response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::Error.new("AuthFailure => The address '#{public_ip}' does not belong to you.") end end
# File lib/fog/aws/requests/compute/get_console_output.rb, line 35 def get_console_output(instance_id) response = Excon::Response.new if instance = self.data[:instances][instance_id] response.status = 200 response.body = { 'instanceId' => instance_id, 'output' => (Time.now - instance['launchTime'] >= Fog::Mock.delay) ? nil : Fog::AWS::Mock.console_output, 'requestId' => Fog::AWS::Mock.request_id, 'timestamp' => Time.now } response else; raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_id}' does not exist") end end
# File lib/fog/aws/requests/compute/get_password_data.rb, line 37 def get_password_data(instance_id) response = Excon::Response.new if instance = self.data[:instances][instance_id] response.status = 200 response.body = { 'instanceId' => instance_id, 'passwordData' => nil, 'requestId' => Fog::AWS::Mock.request_id, 'timestamp' => Time.now } response else; raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_id}' does not exist") end end
# File lib/fog/aws/requests/compute/import_key_pair.rb, line 35 def import_key_pair(key_name, public_key_material) response = Excon::Response.new unless self.data[:key_pairs][key_name] response.status = 200 data = { 'keyFingerprint' => Fog::AWS::Mock.key_fingerprint, 'keyName' => key_name } self.data[:key_pairs][key_name] = data response.body = { 'requestId' => Fog::AWS::Mock.request_id }.merge!(data) response else raise Fog::Compute::AWS::Error.new("InvalidKeyPair.Duplicate => The keypair '#{key_name}' already exists.") end end
# File lib/fog/aws/requests/compute/modify_image_attribute.rb, line 43 def modify_image_attribute(image_id, attributes) raise ArgumentError.new("image_id is required") unless image_id unless self.data[:images][image_id] raise Fog::Compute::AWS::NotFound.new("The AMI ID '#{image_id}' does not exist") end (attributes['Add.UserId'] || []).each do |user_id| if image_launch_permissions = self.data[:image_launch_permissions][image_id] image_launch_permissions[:users].push(user_id) end end (attributes['Remove.UserId'] || []).each do |user_id| if image_launch_permissions = self.data[:image_launch_permissions][image_id] image_launch_permissions[:users].delete(user_id) end end response = Excon::Response.new response.status = 200 response.body = { 'return' => true, 'requestId' => Fog::AWS::Mock.request_id } response end
# File lib/fog/aws/requests/compute/modify_network_interface_attribute.rb, line 47 def modify_network_interface_attribute(network_interface_id, attribute, value) response = Excon::Response.new if self.data[:network_interfaces][network_interface_id] nic = self.data[:network_interfaces][network_interface_id] case attribute when 'description' nic['description'] = value.clone when 'groupSet' groups = {} value.each do |group_id| name = self.data[:security_groups].select { |k,v| v['groupId'] == group_id } .first.first if name.nil? raise Fog::Compute::AWS::Error.new("Unknown security group '#{group_id}' specified") end groups[group_id] = name end nic['groupSet'] = groups when 'sourceDestCheck' nic['sourceDestCheck'] = value when 'attachment' if nic['attachment'].nil? || value['attachmentId'] != nic['attachment']['attachmentId'] raise Fog::Compute::AWS::Error.new("Illegal attachment '#{value['attachmentId']}' specified") end nic['attachment']['deleteOnTermination'] = value['deleteOnTermination'] else raise Fog::Compute::AWS::Error.new("Illegal attribute '#{attribute}' specified") end response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::NotFound.new("The network interface '#{network_interface_id}' does not exist") end end
# File lib/fog/aws/requests/compute/modify_volume_attribute.rb, line 35 def modify_volume_attribute(volume_id=nil, auto_enable_io_value=false) response = Excon::Response.new if volume = self.data[:volumes][volume_id] response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::NotFound.new("The volume '#{volume_id}' does not exist.") end end
# File lib/fog/aws/requests/compute/monitor_instances.rb, line 35 def monitor_instances(instance_ids) response = Excon::Response.new response.status = 200 [*instance_ids].each do |instance_id| if instance = self.data[:instances][instance_id] instance['monitoring']['state'] = 'enabled' else raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_ids}' does not exist") end end instances_set = [*instance_ids].inject([]) { |memo, id| memo << {'instanceId' => id, 'monitoring' => 'enabled'} } response.body = {'requestId' => 'some_request_id', 'instancesSet' => instances_set} response end
# File lib/fog/aws/requests/compute/purchase_reserved_instances_offering.rb, line 34 def purchase_reserved_instances_offering(reserved_instances_offering_id, instance_count = 1) response = Excon::Response.new response.status = 200 # Need to implement filters in the mock to find this there instead of here # Also there's no information about what to do when the specified reserved_instances_offering_id doesn't exist raise unless reserved_instance_offering = describe_reserved_instances_offerings.body["reservedInstancesOfferingsSet"].find { |offering| offering["reservedInstancesOfferingId"] == reserved_instances_offering_id } reserved_instances_id = Fog::AWS::Mock.reserved_instances_id reserved_instance_offering.delete('reservedInstancesOfferingId') self.data[:reserved_instances][reserved_instances_id] = reserved_instance_offering.merge({ 'reservedInstancesId' => reserved_instances_id, 'start' => Time.now, 'instanceCount' => instance_count, 'state' => 'payment-pending', 'tagSet' => [] }) response.body = { 'reservedInstancesId' => reserved_instances_id, 'requestId' => Fog::AWS::Mock.request_id } response end
# File lib/fog/aws/requests/compute/reboot_instances.rb, line 33 def reboot_instances(instance_id = []) response = Excon::Response.new instance_id = [*instance_id] if (self.data[:instances].keys & instance_id).length == instance_id.length for instance_id in instance_id self.data[:instances][instance_id]['status'] = 'rebooting' end response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::NotFound.new("The instance ID #{instance_id.inspect} does not exist") end end
# File lib/fog/aws/compute.rb, line 230 def region_data self.class.data[@region] end
# File lib/fog/aws/requests/compute/register_image.rb, line 67 def register_image(name, description, location, block_devices=[], options={}) unless name.empty? image = { 'imageId' => Fog::AWS::Mock.image_id, 'imageLocation' => '', 'imageState' => 'pending', 'imageOwnerId' => self.data[:owner_id], 'isPublic' => false, 'productCodes' => [], 'architecture' => options['Architecture'] || 'i386', 'imageType' => 'machine', 'kernelId' => options['KernelId'] || Fog::AWS::Mock.kernel_id, 'ramdiskId' => options['RamdiskId'] || Fog::AWS::Mock.ramdisk_id, 'platform' => 'Linux', 'stateReason' => {}, 'imageOwnerAlias' => self.data[:owner_id], 'name' => name, 'description' => description, 'rootDeviceType' => '', 'rootDeviceName' => '', 'blockDeviceMapping' => [], 'virtualizationType' => 'paravirtual', 'hypervisor' => 'xen', 'registered' => Time.now } if location[/^\/dev\/sd[a-p]\d{0,2}$/] image['rootDeviceName'] = location image['rootDeviceType'] = 'ebs' else image['imageLocation'] = location end block_devices.each do |bd| block_device_mapping = { 'ebs' => {} } ["DeviceName","VirtualName"].each do |n| block_device_mapping[n] = bd[n] if bd[n] end ["SnapshotId","VolumeSize","NoDevice","DeleteOnTermination"].each do |n| block_device_mapping['ebs'][n] = bd[n] if bd[n] end image['blockDeviceMapping'] << block_device_mapping end self.data[:images][image['imageId']] = image response = Excon::Response.new response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'imageId' => image['imageId'] } response else message = 'MissingParameter => ' if name.empty? message << 'The request must contain the parameter name' end raise Fog::Compute::AWS::Error.new(message) end end
# File lib/fog/aws/requests/compute/release_address.rb, line 38 def release_address(public_ip_or_allocation_id) response = Excon::Response.new 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::AWS::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::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::Error.new("AuthFailure => The address '#{public_ip_or_allocation_id}' does not belong to you.") end end
# File lib/fog/aws/compute.rb, line 238 def reset_data self.region_data.delete(@aws_access_key_id) end
# File lib/fog/aws/requests/compute/reset_network_interface_attribute.rb, line 34 def reset_network_interface_attribute(network_interface_id, attribute) response = Excon::Response.new if self.data[:network_interfaces][network_interface_id] response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } if attribute == 'sourceDestCheck' self.data[:network_interfaces][network_interface_id]['sourceDestCheck'] = true else raise Fog::Compute::AWS::Error.new("Illegal attribute '#{attribute}' specified") end response else raise Fog::Compute::AWS::NotFound.new("The network interface '#{network_interface_id}' does not exist") end end
# File lib/fog/aws/requests/compute/revoke_security_group_ingress.rb, line 61 def revoke_security_group_ingress(group_name, options = {}) options = Fog::AWS.parse_security_group_options(group_name, options) if options.key?('GroupName') group_name = options['GroupName'] else group_name = self.data[:security_groups].reject { |k,v| v['groupId'] != options['GroupId'] } .keys.first end response = Excon::Response.new group = self.data[:security_groups][group_name] if group verify_permission_options(options, group['vpcId'] != nil) normalized_permissions = normalize_permissions(options) normalized_permissions.each do |permission| if matching_permission = find_matching_permission(group, permission) matching_permission['ipRanges'] -= permission['ipRanges'] matching_permission['groups'] -= permission['groups'] if matching_permission['ipRanges'].empty? && matching_permission['groups'].empty? group['ipPermissions'].delete(matching_permission) end end end response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } response else raise Fog::Compute::AWS::NotFound.new("The security group '#{group_name}' does not exist") end end
# File lib/fog/aws/requests/compute/run_instances.rb, line 126 def run_instances(image_id, min_count, max_count, options = {}) response = Excon::Response.new response.status = 200 group_set = [ (options['SecurityGroup'] || 'default') ].flatten instances_set = [] reservation_id = Fog::AWS::Mock.reservation_id if options['KeyName'] && describe_key_pairs('key-name' => options['KeyName']).body['keySet'].empty? raise Fog::Compute::AWS::NotFound.new("The key pair '#{options['KeyName']}' does not exist") end min_count.times do |i| instance_id = Fog::AWS::Mock.instance_id instance = { 'amiLaunchIndex' => i, 'architecture' => 'i386', 'blockDeviceMapping' => [], 'clientToken' => options['clientToken'], 'dnsName' => nil, '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 }, 'placement' => { 'availabilityZone' => options['Placement.AvailabilityZone'] || Fog::AWS::Mock.availability_zone(@region), 'groupName' => nil, 'tenancy' => 'default' }, 'privateDnsName' => nil, 'productCodes' => [], 'reason' => nil, 'rootDeviceType' => 'instance-store', 'ebsOptimized' => options['EbsOptimized'] || false } instances_set << instance self.data[:instances][instance_id] = instance.merge({ 'groupIds' => [], 'groupSet' => group_set, 'iamInstanceProfile' => {}, 'networkInterfaces' => [], 'ownerId' => self.data[:owner_id], 'privateIpAddress' => nil, 'reservationId' => reservation_id, 'stateReason' => {} }) end response.body = { 'groupSet' => group_set, 'instancesSet' => instances_set, 'ownerId' => self.data[:owner_id], 'requestId' => Fog::AWS::Mock.request_id, 'reservationId' => reservation_id } response end
# File lib/fog/aws/compute.rb, line 285 def setup_credentials(options) @aws_access_key_id = options[:aws_access_key_id] end
# File lib/fog/aws/requests/compute/start_instances.rb, line 32 def start_instances(instance_id) instance_ids = Array(instance_id) instance_set = self.data[:instances].values instance_set = apply_tag_filters(instance_set, {'instance_id' => instance_ids}, 'instanceId') instance_set = instance_set.find_all {|x| instance_ids.include?(x["instanceId"]) } if instance_set.empty? raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_ids.first}' does not exist") else response = Excon::Response.new response.status = 200 response.body = { 'instancesSet' => instance_set.inject([]) do |ia, instance| ia << {'currentState' => { 'code' => 0, 'name' => 'pending' }, 'previousState' => instance['instanceState'], 'instanceId' => instance['instanceId'] } instance['instanceState'] = {'code'=>0, 'name'=>'pending'} ia end } response end end
# File lib/fog/aws/requests/compute/stop_instances.rb, line 33 def stop_instances(instance_id, force = false) instance_ids = Array(instance_id) instance_set = self.data[:instances].values instance_set = apply_tag_filters(instance_set, {'instance_id' => instance_ids}, 'instanceId') instance_set = instance_set.find_all {|x| instance_ids.include?(x["instanceId"]) } if instance_set.empty? raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_ids.first}' does not exist") else response = Excon::Response.new response.status = 200 response.body = { 'instancesSet' => instance_set.inject([]) do |ia, instance| ia << {'currentState' => { 'code' => 0, 'name' => 'stopping' }, 'previousState' => instance['instanceState'], 'instanceId' => instance['instanceId'] } instance['instanceState'] = {'code'=>0, 'name'=>'stopping'} ia end } response end end
# File lib/fog/aws/requests/compute/terminate_instances.rb, line 40 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.body = { 'requestId' => Fog::AWS::Mock.request_id, 'instancesSet' => [] } response.status = 200 for id in instance_id instance = self.data[:instances][id] self.data[:deleted_at][id] = Time.now code = case instance['instanceState']['name'] when 'pending' 0 when 'running' 16 when 'shutting-down' 32 when 'terminated' 48 when 'stopping' 64 when 'stopped' 80 end state = { 'name' => 'shutting-down', 'code' => 32} response.body['instancesSet'] << { 'instanceId' => id, 'previousState' => instance['instanceState'], 'currentState' => state } instance['instanceState'] = state end describe_addresses.body['addressesSet'].each do |address| if instance_id.include?(address['instanceId']) disassociate_address(address['publicIp']) end end describe_volumes.body['volumeSet'].each do |volume| if volume['attachmentSet'].first && instance_id.include?(volume['attachmentSet'].first['instanceId']) detach_volume(volume['volumeId']) end end response else raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_id}' does not exist") end end
# File lib/fog/aws/requests/compute/unmonitor_instances.rb, line 35 def unmonitor_instances(instance_ids) response = Excon::Response.new response.status = 200 [*instance_ids].each do |instance_id| if instance = self.data[:instances][instance_id] instance['monitoring']['state'] = 'enabled' else raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_ids}' does not exist") end end instances_set = [*instance_ids].inject([]) { |memo, id| memo << {'instanceId' => id, 'monitoring' => 'disabled'} } response.body = {'requestId' => 'some_request_id', 'instancesSet' => instances_set} response end
# File lib/fog/aws/compute.rb, line 242 def visible_images images = self.data[:images].values.inject({}) do |h, image| h.update(image['imageId'] => image) end self.region_data.each do |aws_access_key_id, data| data[:image_launch_permissions].each do |image_id, list| if list[:users].include?(self.data[:owner_id]) images.update(image_id => data[:images][image_id]) end end end images end
Generated with the Darkfish Rdoc Generator 2.