mirror of
https://github.com/fog/fog-aws.git
synced 2022-11-09 13:50:52 -05:00
Implement ELBV2 creation request
This commit is contained in:
parent
aaad43a616
commit
ac0826f7e0
3 changed files with 226 additions and 0 deletions
|
@ -1,7 +1,11 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class ELBV2 < ELB
|
||||
requires :aws_access_key_id, :aws_secret_access_key
|
||||
recognizes :region, :host, :path, :port, :scheme, :persistent, :use_iam_profile, :aws_session_token, :aws_credentials_expire_at, :version, :instrumentor, :instrumentor_name
|
||||
|
||||
request_path 'fog/aws/requests/elbv2'
|
||||
request :create_load_balancer
|
||||
request :describe_load_balancers
|
||||
request :describe_listeners
|
||||
|
||||
|
@ -12,6 +16,54 @@ module Fog
|
|||
super(options)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, region|
|
||||
owner_id = Fog::AWS::Mock.owner_id
|
||||
hash[region] = Hash.new do |region_hash, key|
|
||||
region_hash[key] = {
|
||||
:owner_id => owner_id,
|
||||
:load_balancers_v2 => {}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.dns_name(name, region)
|
||||
"#{name}-#{Fog::Mock.random_hex(8)}.#{region}.elb.amazonaws.com"
|
||||
end
|
||||
|
||||
def self.reset
|
||||
@data = nil
|
||||
end
|
||||
|
||||
attr_reader :region
|
||||
|
||||
def initialize(options={})
|
||||
@use_iam_profile = options[:use_iam_profile]
|
||||
|
||||
@region = options[:region] || 'us-east-1'
|
||||
setup_credentials(options)
|
||||
|
||||
Fog::AWS.validate_region!(@region)
|
||||
end
|
||||
|
||||
def setup_credentials(options)
|
||||
@aws_access_key_id = options[:aws_access_key_id]
|
||||
@aws_secret_access_key = options[:aws_secret_access_key]
|
||||
|
||||
@signer = Fog::AWS::SignatureV4.new(@aws_access_key_id, @aws_secret_access_key,@region,'elasticloadbalancing')
|
||||
end
|
||||
|
||||
def data
|
||||
self.class.data[@region][@aws_access_key_id]
|
||||
end
|
||||
|
||||
def reset_data
|
||||
self.class.data[@region].delete(@aws_access_key_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
159
lib/fog/aws/requests/elbv2/create_load_balancer.rb
Normal file
159
lib/fog/aws/requests/elbv2/create_load_balancer.rb
Normal file
|
@ -0,0 +1,159 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class ELBV2
|
||||
class Real
|
||||
require 'fog/aws/parsers/elbv2/create_load_balancer'
|
||||
|
||||
# Create a new Elastic Load Balancer
|
||||
#
|
||||
# ==== Parameters
|
||||
# * name<~String> - The name of the load balancer.
|
||||
# This name must be unique per region per account, can have a maximum of 32 characters, must contain only alphanumeric characters or hyphens,
|
||||
# must not begin or end with a hyphen, and must not begin with "internal-".
|
||||
# - Required: Yes
|
||||
# * options<~Hash>:
|
||||
# * ip_address_type<~String> - [Application Load Balancers] The type of IP addresses used by the subnets for your load balancer.
|
||||
# The possible values are ipv4 (for IPv4 addresses) and dualstack (for IPv4 and IPv6 addresses).
|
||||
# Internal load balancers must use ipv4.
|
||||
# - Required: No
|
||||
# * scheme<~String> - The default is an Internet-facing load balancer. Valid Values: internet-facing | internal
|
||||
# - Required: No
|
||||
# * security_groups<~Array> - The IDs of the security groups for the load balancer.
|
||||
# - Required: No
|
||||
# * subnet_mappings<~Array> - The IDs of the public subnets. You can specify only one subnet per Availability Zone. You must specify either subnets or subnet mappings.
|
||||
# - [Application Load Balancers] You must specify subnets from at least two Availability Zones.
|
||||
# You cannot specify Elastic IP addresses for your subnets.
|
||||
# - [Network Load Balancers] You can specify subnets from one or more Availability Zones.
|
||||
# You can specify one Elastic IP address per subnet if you need static IP addresses for your internet-facing load balancer.
|
||||
# For internal load balancers, you can specify one private IP address per subnet from the IPv4 range of the subnet.
|
||||
# - Required: No
|
||||
# * subnets<~Array> - The IDs of the public subnets. You can specify only one subnet per Availability Zone. You must specify either subnets or subnet mappings.
|
||||
# - [Application Load Balancers] You must specify subnets from at least two Availability Zones.
|
||||
# - [Network Load Balancers] You can specify subnets from one or more Availability Zones.
|
||||
# - Required: No
|
||||
# * tags<~Hash> - One or more tags to assign to the load balancer.
|
||||
# - Required: No
|
||||
# * type<~String> - The type of load balancer. The default is application. Valid Values: application | network
|
||||
# - Required: No
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Hash>:
|
||||
# * 'ResponseMetadata'<~Hash>:
|
||||
# * 'RequestId'<~String> - Id of request
|
||||
# * 'CreateLoadBalancerResult'<~Hash>:
|
||||
# * 'LoadBalancers'<~Array>
|
||||
# * 'AvailabilityZones'<~Array>:
|
||||
# * 'SubnetId'<~String> - ID of the subnet
|
||||
# * 'ZoneName'<~String> - Name of the Availability Zone
|
||||
# * 'LoadBalancerAddresses'<~Array>:
|
||||
# * 'IpAddress'<~String> - IP address
|
||||
# * 'AllocationId'<~String> - ID of the AWS allocation
|
||||
# * 'CanonicalHostedZoneName'<~String> - name of the Route 53 hosted zone associated with the load balancer
|
||||
# * 'CanonicalHostedZoneNameID'<~String> - ID of the Route 53 hosted zone associated with the load balancer
|
||||
# * 'CreatedTime'<~Time> - time load balancer was created
|
||||
# * 'DNSName'<~String> - external DNS name of load balancer
|
||||
# * 'LoadBalancerName'<~String> - name of load balancer
|
||||
# * 'SecurityGroups'<~Array> - array of security group id
|
||||
def create_load_balancer(name, options = {})
|
||||
params = {}
|
||||
params.merge!(Fog::AWS.indexed_param('Subnets.member.%d', options[:subnets]))
|
||||
params.merge!(Fog::AWS.indexed_param('SecurityGroups.member.%d', options[:security_groups]))
|
||||
params.merge!(Fog::AWS.serialize_keys('Scheme', options[:scheme]))
|
||||
params.merge!(Fog::AWS.serialize_keys('Type', options[:type]))
|
||||
params.merge!(Fog::AWS.serialize_keys('IpAddressType', options[:ip_address_type]))
|
||||
|
||||
|
||||
unless options[:tags].nil?
|
||||
tag_keys = options[:tags].keys.sort
|
||||
tag_values = tag_keys.map { |key| options[:tags][key] }
|
||||
params.merge!(Fog::AWS.indexed_param('Tags.member.%d.Key', tag_keys))
|
||||
params.merge!(Fog::AWS.indexed_param('Tags.member.%d.Value', tag_values))
|
||||
end
|
||||
|
||||
unless options[:subnet_mappings].nil?
|
||||
subnet_ids = []
|
||||
allocation_ids = []
|
||||
private_ipv4_address = []
|
||||
options[:subnet_mappings].each do |subnet_mapping|
|
||||
subnet_ids.push(subnet_mapping[:subnet_id])
|
||||
allocation_ids.push(subnet_mapping[:allocation_id])
|
||||
private_ipv4_address.push(subnet_mapping[:private_ipv4_address])
|
||||
end
|
||||
params.merge!(Fog::AWS.indexed_param('SubnetMappings.member.%d.SubnetId', subnet_ids))
|
||||
params.merge!(Fog::AWS.indexed_param('SubnetMappings.member.%d.AllocationId', allocation_ids))
|
||||
params.merge!(Fog::AWS.indexed_param('SubnetMappings.member.%d.PrivateIPv4Address', private_ipv4_address))
|
||||
end
|
||||
|
||||
|
||||
request({
|
||||
'Action' => 'CreateLoadBalancer',
|
||||
'Name' => name,
|
||||
:parser => Fog::Parsers::AWS::ELBV2::CreateLoadBalancer.new
|
||||
}.merge!(params))
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def create_load_balancer(name, options = {})
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
raise Fog::AWS::ELBV2::IdentifierTaken if self.data[:load_balancers_v2].key? name
|
||||
|
||||
dns_name = Fog::AWS::ELBV2::Mock.dns_name(name, @region)
|
||||
type = options[:type] || 'application'
|
||||
|
||||
subnet_ids = options[:subnets] || []
|
||||
region = if subnet_ids.any?
|
||||
# using Hash here for Rubt 1.8.7 support.
|
||||
Hash[
|
||||
Fog::AWS::Compute::Mock.data.select do |_, region_data|
|
||||
unless region_data[@aws_access_key_id].nil?
|
||||
region_data[@aws_access_key_id][:subnets].any? do |region_subnets|
|
||||
subnet_ids.include? region_subnets['subnetId']
|
||||
end
|
||||
end
|
||||
end
|
||||
].keys[0]
|
||||
else
|
||||
'us-east-1'
|
||||
end
|
||||
|
||||
subnets = Fog::AWS::Compute::Mock.data[region][@aws_access_key_id][:subnets].select {|e| subnet_ids.include?(e["subnetId"]) }
|
||||
availability_zones = subnets.map do |subnet|
|
||||
{ "LoadBalancerAddresses"=>[], "SubnetId"=>subnet["subnetId"], "ZoneName"=>subnet["availabilityZone"]}
|
||||
end
|
||||
vpc_id = subnets.first['vpcId']
|
||||
|
||||
self.data[:tags] ||= {}
|
||||
self.data[:tags][name] = options[:tags] || {}
|
||||
|
||||
load_balancer = {
|
||||
'AvailabilityZones' => availability_zones || [],
|
||||
'Scheme' => options[:scheme] || 'internet-facing',
|
||||
'SecurityGroups' => options[:security_groups] || [],
|
||||
'CanonicalHostedZoneId' => '',
|
||||
'CreatedTime' => Time.now,
|
||||
'DNSName' => dns_name,
|
||||
'VpcId' => vpc_id,
|
||||
'Type' => type,
|
||||
'State' => {'Code' => 'provisioning'},
|
||||
'LoadBalancerArn' => Fog::AWS::Mock.arn('elasticloadbalancing', self.data[:owner_id], "loadbalancer/#{type[0..2]}/#{name}/#{Fog::AWS::Mock.key_id}"),
|
||||
'LoadBalancerName' => name
|
||||
}
|
||||
self.data[:load_balancers_v2][name] = load_balancer
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
},
|
||||
'CreateLoadBalancerResult' => {
|
||||
'LoadBalancers' => [load_balancer]
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
tests/requests/elbv2/load_balancer_tests.rb
Normal file
15
tests/requests/elbv2/load_balancer_tests.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
Shindo.tests('AWS::ELBV2 | load_balancer_tests', ['aws', 'elb']) do
|
||||
@load_balancer_id = 'fog-test-elb'
|
||||
@key_name = 'fog-test'
|
||||
vpc = Fog::Compute[:aws].create_vpc('10.255.254.64/28').body['vpcSet'].first
|
||||
@subnet_id = Fog::Compute[:aws].create_subnet(vpc['vpcId'], vpc['cidrBlock']).body['subnet']['subnetId']
|
||||
|
||||
tests('success') do
|
||||
tests("#create_load_balancer").formats(AWS::ELBV2::Formats::CREATE_LOAD_BALANCER) do
|
||||
options = {
|
||||
subnets: [@subnet_id]
|
||||
}
|
||||
Fog::AWS[:elbv2].create_load_balancer(@load_balancer_id, options).body
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue