1
0
Fork 0
mirror of https://github.com/fog/fog-aws.git synced 2022-11-09 13:50:52 -05:00

Merge pull request #3 from fcheung/classic-link

Support for VPC Classic Link
This commit is contained in:
Josh Lane 2015-01-13 08:49:56 -08:00
commit da2a4adc30
13 changed files with 537 additions and 2 deletions

View file

@ -53,6 +53,7 @@ module Fog
request :associate_dhcp_options
request :attach_network_interface
request :associate_route_table
request :attach_classic_link_vpc
request :attach_internet_gateway
request :attach_volume
request :authorize_security_group_ingress
@ -96,6 +97,7 @@ module Fog
request :describe_account_attributes
request :describe_addresses
request :describe_availability_zones
request :describe_classic_link_instances
request :describe_dhcp_options
request :describe_images
request :describe_instances
@ -121,11 +123,15 @@ module Fog
request :describe_volume_status
request :describe_vpcs
request :describe_vpc_attribute
request :describe_vpc_classic_link
request :detach_network_interface
request :detach_internet_gateway
request :detach_volume
request :detach_classic_link_vpc
request :disable_vpc_classic_link
request :disassociate_address
request :disassociate_route_table
request :enable_vpc_classic_link
request :get_console_output
request :get_password_data
request :import_key_pair
@ -454,7 +460,7 @@ module Fog
@region = options[:region] ||= 'us-east-1'
@instrumentor = options[:instrumentor]
@instrumentor_name = options[:instrumentor_name] || 'fog.aws.compute'
@version = options[:version] || '2014-06-15'
@version = options[:version] || '2014-10-01'
@use_iam_profile = options[:use_iam_profile]
setup_credentials(options)

View file

@ -0,0 +1,60 @@
module Fog
module Parsers
module Compute
module AWS
class DescribeClassicLinkInstances < Fog::Parsers::Base
def reset
@instance = { 'tagSet' => {}, 'groups' => [] }
@response = { 'instancesSet' => [] }
@tag = {}
@group = {}
end
def start_element(name, attrs = [])
super
case name
when 'groupSet'
@in_group_set = true
when 'tagSet'
@in_tag_set = true
end
end
def end_element(name)
if @in_tag_set
case name
when 'item'
@instance['tagSet'][@tag['key']] = @tag['value']
@tag = {}
when 'key', 'value'
@tag[name] = value
when 'tagSet'
@in_tag_set = false
end
elsif @in_group_set
case name
when 'item'
@instance['groups'] << @group
@group = {}
when 'groupId', 'groupName'
@group[name] = value
when 'groupSet'
@in_group_set = false
end
else
case name
when 'vpcId', 'instanceId'
@instance[name] = value
when 'item'
@response['instancesSet'] << @instance
@instance = { 'tagSet' => {}, 'groups' => [] }
when 'requestId', 'nextToken'
@response[name] = value
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,47 @@
module Fog
module Parsers
module Compute
module AWS
class DescribeVpcClassicLink < Fog::Parsers::Base
def reset
@vpc = { 'tagSet' => {} }
@response = { 'vpcSet' => [] }
@tag = {}
end
def start_element(name, attrs = [])
super
case name
when 'tagSet'
@in_tag_set = true
end
end
def end_element(name)
if @in_tag_set
case name
when 'item'
@vpc['tagSet'][@tag['key']] = @tag['value']
@tag = {}
when 'key', 'value'
@tag[name] = value
when 'tagSet'
@in_tag_set = false
end
else
case name
when 'vpcId', 'classicLinkEnabled'
@vpc[name] = value
when 'item'
@response['vpcSet'] << @vpc
@vpc = { 'tagSet' => {} }
when 'requestId'
@response[name] = value
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,67 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/basic'
# Links an EC2-Classic instance to a ClassicLink-enabled VPC through one or more of the VPC's security groups
#
# ==== Parameters
# * vpc_id<~String> - The ID of a ClassicLink-enabled VPC.
# * instance_id<~String> - The ID of an EC2-Classic instance to link to the ClassicLink-enabled VPC.
# * security_group_ids<~String> - The ID of one or more of the VPC's security groups. You cannot specify security groups from a different VPC.
# * dry_run<~Boolean> - defaults to false
#
# === Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - Whether the request succeeded
#
# (Amazon API Reference)[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_AttachClassicLinkVpc.html]
def attach_classic_link_vpc(instance_id, vpc_id, security_group_ids, dry_run=false)
request({
'Action' => 'AttachClassicLinkVpc',
'VpcId' => vpc_id,
'InstanceId'=> instance_id,
'DryRun' => dry_run,
:parser => Fog::Parsers::Compute::AWS::Basic.new
}.merge(Fog::AWS.indexed_param('SecurityGroupId', security_group_ids)))
end
end
class Mock
def attach_classic_link_vpc(instance_id, vpc_id, security_group_ids, dry_run=false)
response = Excon::Response.new
vpc = self.data[:vpcs].find{ |v| v['vpcId'] == vpc_id }
instance = self.data[:instances][instance_id]
if vpc && instance
if instance['instanceState']['name'] != 'running' || instance['vpcId']
raise Fog::Compute::AWS::Error.new("Client.InvalidInstanceID.NotLinkable => Instance #{instance_id} is unlinkable")
end
if instance['classicLinkVpcId']
raise Fog::Compute::AWS::Error.new("Client.InvalidInstanceID.InstanceAlreadyLinked => Instance #{instance_id} is already linked")
end
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
unless dry_run
instance['classicLinkSecurityGroups'] = security_group_ids
instance['classicLinkVpcId'] = vpc_id
end
response
elsif !instance
raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_id}' does not exist.")
elsif !vpc
raise Fog::Compute::AWS::NotFound.new("The VPC '#{vpc_id}' does not exist.")
end
end
end
end
end
end

View file

@ -50,7 +50,8 @@ module Fog
'tagSet' => {},
'enableDnsSupport' => true,
'enableDnsHostnames' => false,
'mapPublicIpOnLaunch'=> false
'mapPublicIpOnLaunch'=> false,
'classicLinkEnabled' => false
}
self.data[:vpcs].push(vpc)

View file

@ -0,0 +1,73 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/describe_classic_link_instances'
# Describes one or more of your linked EC2-Classic instances. This request only returns information about EC2-Classic instances linked to a VPC through ClassicLink; you cannot use this request to return information about other instances.
#
# ==== Parameters
# * options<~Hash>
# * instance_ids<~Array> - An array of instance ids to restruct the results to
# * filters<~Hash> - Filters to restrict the results to. Recognises vpc-id, group-id, instance-id in addition
# to tag-key, tag-value and tag:key
# * max_results
# * next_token
# === Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'instancesSet'<~Array> - array of ClassicLinkInstance
# * 'vpcId'<~String>
# * 'instanceId'<~String>
# * 'tagSet'<~Hash>
# * 'groups'<~Array>
# * groupId <~String>
# * groupName <~String>
# (Amazon API Reference)[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeClassicLinkInstances.html
def describe_classic_link_instances(options={})
params = {}
params['MaxResults'] = options[:max_results] if options[:max_results]
params['NextToken'] = options[:next_token] if options[:next_token]
params.merge!(Fog::AWS.indexed_param('InstanceId', options[:instance_ids])) if options[:instance_ids]
params.merge!(Fog::AWS.indexed_filters(options[:filters])) if options[:filters]
request({
'Action' => 'DescribeClassicLinkInstances',
:parser => Fog::Parsers::Compute::AWS::DescribeClassicLinkInstances.new
}.merge(params))
end
end
class Mock
def describe_classic_link_instances(options={})
response = Excon::Response.new
instances = self.data[:instances].values.select {|instance| instance['classicLinkVpcId']}
if options[:filters]
instances = apply_tag_filters(instances, options[:filters], 'instanceId')
instances = instances.select {|instance| instance['classicLinkVpcId'] == options[:filters]['vpc-id']} if options[:filters]['vpc-id']
instances = instances.select {|instance| instance['instanceId'] == options[:filters]['instance-id']} if options[:filters]['instance-id']
instances = instances.select {|instance| instance['classicLinkSecurityGroups'].include?(options[:filters]['group-id'])} if options[:filters]['group-id']
end
instances = instances.select {|instance| options[:instance_ids].include?(instance['instanceId'])} if options[:instance_ids]
response.status = 200
instance_data = instances.collect do |instance|
groups = self.data[:security_groups].values.select {|data| instance['classicLinkSecurityGroups'].include?(data['groupId'])}
{
'instanceId' => instance['instanceId'],
'vpcId' => instance['classicLinkVpcId'],
'groups' => groups.collect {|group| {'groupId' => group['groupId'], 'groupName' => group['groupName']}},
'tagSet' => self.data[:tag_sets][instance['instanceId']] || {}
}
end
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'instancesSet' => instance_data
}
response
end
end
end
end
end

View file

@ -0,0 +1,63 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/describe_vpc_classic_link'
# Describes the ClassicLink status of one or more VPCs.
#
# ==== Parameters
# * options<~Hash>
# * vpc_ids<~Array> - An array of vpc ids to restruct the results to
# * filters<~Hash> - Filters to restrict the results to. Recognises is-classic-link-enabled in addition
# to tag-key, tag-value and tag:key
# === Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'vpcSet'<~Array> - array of VpcClassicLink
# * 'vpcId'<~String>
# * 'classicLinkEnabled'<~Boolean>
# * 'tagSet'<~Hash>
#
# (Amazon API Reference)[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcClassicLink.html
def describe_vpc_classic_link(options={})
params = {}
params.merge!(Fog::AWS.indexed_param('VpcId', options[:vpc_ids])) if options[:vpc_ids]
params.merge!(Fog::AWS.indexed_filters(options[:filters])) if options[:filters]
request({
'Action' => 'DescribeVpcClassicLink',
:parser => Fog::Parsers::Compute::AWS::DescribeVpcClassicLink.new
}.merge(params))
end
end
class Mock
def describe_vpc_classic_link(options={})
response = Excon::Response.new
vpcs = self.data[:vpcs]
if vpc_ids = options[:vpc_ids]
vpcs = vpc_ids.collect do |vpc_id|
vpc = vpcs.find{ |v| v['vpcId'] == vpc_id }
raise Fog::Compute::AWS::NotFound.new("The VPC '#{vpc_id}' does not exist") unless vpc
vpc
end
end
vpcs = apply_tag_filters(vpcs, options[:filters], 'vpcId') if options[:filters]
response.status = 200
vpc_data = vpcs.collect do |vpc|
{
'vpcId' => vpc['vpcId'],
'classicLinkEnabled' => vpc['classicLinkEnabled'],
'tagSet' => self.data[:tag_sets][vpc['vpcId']] || {}
}
end
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'vpcSet' => vpc_data
}
end
end
end
end
end

View file

@ -0,0 +1,60 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/basic'
# Links an EC2-Classic instance to a ClassicLink-enabled VPC through one or more of the VPC's security groups
#
# ==== Parameters
# * vpc_id<~String> - The ID of the vpc to which the instance is linkced.
# * instance_id<~String> - The ID of an EC2-Classic instance to unlink from the vpc.
# * security_group_ids<~String> - The ID of one or more of the VPC's security groups. You cannot specify security groups from a different VPC.
# * dry_run<~Boolean> - defaults to false
#
# === Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - Whether the request succeeded
#
# (Amazon API Reference)[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DetachClassicLinkVpc.html]
def detach_classic_link_vpc(instance_id, vpc_id, dry_run=false)
request(
'Action' => 'DetachClassicLinkVpc',
'VpcId' => vpc_id,
'InstanceId'=> instance_id,
'DryRun' => dry_run,
:parser => Fog::Parsers::Compute::AWS::Basic.new
)
end
end
class Mock
def detach_classic_link_vpc(instance_id, vpc_id, dry_run=false)
response = Excon::Response.new
vpc = self.data[:vpcs].find{ |v| v['vpcId'] == vpc_id }
instance = self.data[:instances][instance_id]
if vpc && instance
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
unless dry_run
instance['classicLinkSecurityGroups'] = nil
instance['classicLinkVpcId'] = nil
end
response
elsif !instance
raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_id}' does not exist.")
elsif !vpc
raise Fog::Compute::AWS::NotFound.new("The VPC '#{vpc_id}' does not exist.")
end
end
end
end
end
end

View file

@ -0,0 +1,47 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/basic'
# disavbles classic link for a vpc
#
# ==== Parameters
# * vpc_id<~String> - The ID of the VPC you want to describe an attribute of
# * dry_run<~Boolean> - defaults to false
#
# === Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - Whether the request succeeded
#
# (Amazon API Reference)[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DisableVpcClassicLink.html]
def disable_vpc_classic_link(vpc_id, dry_run=false)
request(
'Action' => 'DisableVpcClassicLink',
'VpcId' => vpc_id,
'DryRun' => dry_run,
:parser => Fog::Parsers::Compute::AWS::Basic.new
)
end
end
class Mock
def disable_vpc_classic_link(vpc_id, dry_run=false)
response = Excon::Response.new
if vpc = self.data[:vpcs].find{ |v| v['vpcId'] == vpc_id }
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
vpc['classicLinkEnabled'] = false unless dry_run
response
else
raise Fog::Compute::AWS::NotFound.new("The VPC '#{vpc_id}' does not exist")
end
end
end
end
end
end

View file

@ -0,0 +1,47 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/basic'
# enables classic link for a vpc
#
# ==== Parameters
# * vpc_id<~String> - The ID of the VPC you want to describe an attribute of
# * dry_run<~Boolean> - defaults to false
#
# === Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - Whether the request succeeded
#
# (Amazon API Reference)[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_EnableVpcClassicLink.html]
def enable_vpc_classic_link(vpc_id, dry_run=false)
request(
'Action' => 'EnableVpcClassicLink',
'VpcId' => vpc_id,
'DryRun' => dry_run,
:parser => Fog::Parsers::Compute::AWS::Basic.new
)
end
end
class Mock
def enable_vpc_classic_link(vpc_id, dry_run=false)
response = Excon::Response.new
if vpc = self.data[:vpcs].find{ |v| v['vpcId'] == vpc_id }
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
vpc['classicLinkEnabled'] = true unless dry_run
response
else
raise Fog::Compute::AWS::NotFound.new("The VPC '#{vpc_id}' does not exist")
end
end
end
end
end
end

View file

@ -43,6 +43,8 @@ module Fog
response.body = {
'instancesSet' => instance_set.reduce([]) do |ia, instance|
instance['classicLinkSecurityGroups'] = nil
instance['classicLinkVpcId'] = nil
ia << {'currentState' => { 'code' => 0, 'name' => 'stopping' },
'previousState' => instance['instanceState'],
'instanceId' => instance['instanceId'] }

View file

@ -45,6 +45,8 @@ module Fog
response.status = 200
for id in instance_id
instance = self.data[:instances][id]
instance['classicLinkSecurityGroups'] = nil
instance['classicLinkVpcId'] = nil
self.data[:deleted_at][id] = Time.now
code = case instance['instanceState']['name']
when 'pending'

View file

@ -11,6 +11,25 @@ Shindo.tests('Fog::Compute[:aws] | vpc requests', ['aws']) do
'requestId' => String
}
@describe_vpcs_classic_link_format = {
'vpcSet' => [{
'vpcId' => String,
'tagSet' => Hash,
'classicLinkEnabled' => Fog::Boolean
}],
'requestId' => String
}
@describe_classic_link_instances = {
'instancesSet' => [{
'vpcId' => String,
'tagSet' => Hash,
'instanceId' => String,
'groups' => [{'groupId' => String, 'groupName' => String}]
}],
'requestId' => String,
'NextToken' => Fog::Nullable::String
}
@describe_vpcs_format = {
'vpcSet' => [{
'vpcId' => String,
@ -99,6 +118,47 @@ Shindo.tests('Fog::Compute[:aws] | vpc requests', ['aws']) do
body
end
tests("describe_vpc_classic_link(:filters => {'tag-key' => 'foo'}").formats(@describe_vpcs_classic_link_format) do
body = Fog::Compute[:aws].describe_vpc_classic_link(:filters => {'tag-key' => 'foo'})
tests("returns 1 vpc").returns(1) { body['vpcSet'].size }
body
end
tests("enable_vpc_classic_link").returns(true) do
Fog::Compute[:aws].enable_vpc_classic_link @vpc_id
body = Fog::Compute[:aws].describe_vpc_classic_link(:vpc_ids => [@vpc_id])
body['vpcSet'].first['classicLinkEnabled']
end
@server = Fog::Compute[:aws].servers.create
@server.wait_for {ready?}
@group = Fog::Compute[:aws].security_groups.create :name => 'test-group', :description => 'vpc security group'
tests("attach_classic_link_vpc") do
Fog::Compute[:aws].attach_classic_link_vpc(@server.id, @vpc_id, [@group])
end
tests('describe_classic_link_instances').formats(@describe_classic_link_instances) do
Fog::Compute[:aws].describe_classic_link_instances().body
end
tests("detach_classic_link_vpc").returns([]) do
Fog::Compute[:aws].detach_classic_link_vpc(@server.id, @vpc_id)
Fog::Compute[:aws].describe_classic_link_instances().body['instancesSet']
end
if !Fog.mocking?
@server.destroy
@server.wait_for {state == 'terminated'}
end
tests("disable_vpc_classic_link").returns(false) do
Fog::Compute[:aws].disable_vpc_classic_link @vpc_id
body = Fog::Compute[:aws].describe_vpc_classic_link(:vpc_ids => [@vpc_id])
body['vpcSet'].first['classicLinkEnabled']
end
tests("#delete_vpc('#{@vpc_id}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].delete_vpc(@vpc_id).body
end