Class: Fog::AWS::ELBV2::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/elbv2.rb,
lib/fog/aws/requests/elbv2/add_tags.rb,
lib/fog/aws/requests/elbv2/remove_tags.rb,
lib/fog/aws/requests/elbv2/describe_tags.rb,
lib/fog/aws/requests/elbv2/create_load_balancer.rb,
lib/fog/aws/requests/elbv2/describe_load_balancers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



46
47
48
49
50
51
52
53
# File 'lib/fog/aws/elbv2.rb', line 46

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

Instance Attribute Details

#regionObject (readonly)

Returns the value of attribute region.



44
45
46
# File 'lib/fog/aws/elbv2.rb', line 44

def region
  @region
end

Class Method Details

.dataObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/aws/elbv2.rb', line 24

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

.dns_name(name, region) ⇒ Object



36
37
38
# File 'lib/fog/aws/elbv2.rb', line 36

def self.dns_name(name, region)
  "#{name}-#{Fog::Mock.random_hex(8)}.#{region}.elb.amazonaws.com"
end

.resetObject



40
41
42
# File 'lib/fog/aws/elbv2.rb', line 40

def self.reset
  @data = nil
end

Instance Method Details

#add_tags(resource_arn, tags) ⇒ Object



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

def add_tags(resource_arn, tags)
  response = Excon::Response.new
  if self.data[:load_balancers_v2][resource_arn]
    self.data[:tags][resource_arn].merge! tags
    response.status = 200
    response.body = {
      "ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id }
    }
    response
  else
    raise Fog::AWS::ELBV2::NotFound.new("Elastic load balancer #{resource_arn} not found")
  end
end

#create_load_balancer(name, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fog/aws/requests/elbv2/create_load_balancer.rb', line 97

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'
  load_balancer_arn = Fog::AWS::Mock.arn('elasticloadbalancing', self.data[:owner_id], "loadbalancer/#{type[0..2]}/#{name}/#{Fog::AWS::Mock.key_id}")

  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][load_balancer_arn] = 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' => load_balancer_arn,
    'LoadBalancerName' => name
  }
  self.data[:load_balancers_v2][load_balancer_arn] = load_balancer
  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'CreateLoadBalancerResult' => {
      'LoadBalancers' => [load_balancer]
    }
  }

  response
end

#dataObject



62
63
64
# File 'lib/fog/aws/elbv2.rb', line 62

def data
  self.class.data[@region][@aws_access_key_id]
end

#describe_load_balancers(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fog/aws/requests/elbv2/describe_load_balancers.rb', line 52

def describe_load_balancers(options = {})
  unless options.is_a?(Hash)
    Fog::Logger.deprecation("describe_load_balancers with #{options.class} is deprecated, use all('LoadBalancerNames' => []) instead [light_black](#{caller.first})[/]")
    options = { 'LoadBalancerNames' => [options].flatten }
  end

  lb_names = options['LoadBalancerNames'] || []

  lb_names = [*lb_names]
  load_balancers = if lb_names.any?
    lb_names.map do |lb_name|
      lb = self.data[:load_balancers_v2].find { |name, data| name == lb_name }
      raise Fog::AWS::ELBV2::NotFound unless lb
      lb[1].dup
    end.compact
  else
    self.data[:load_balancers_v2].map { |lb, values| values.dup }
  end

  marker = options.fetch('Marker', 0).to_i
  if load_balancers.count - marker > 400
    next_marker = marker + 400
    load_balancers = load_balancers[marker...next_marker]
  else
    next_marker = nil
  end

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

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'DescribeLoadBalancersResult' => {
      'LoadBalancers' => load_balancers
    }
  }

  if next_marker
    response.body['DescribeLoadBalancersResult']['NextMarker'] = next_marker.to_s
  end

  response
end

#describe_tags(resource_arns) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/aws/requests/elbv2/describe_tags.rb', line 24

def describe_tags(resource_arns)
  response = Excon::Response.new
  resource_arns = [*resource_arns]

  tag_descriptions = resource_arns.map do |resource_arn|
    if self.data[:load_balancers_v2][resource_arn]
      {
        "Tags"=>self.data[:tags][resource_arn],
        "ResourceArn"=>resource_arn
      }
    else
      raise Fog::AWS::ELBV2::NotFound.new("Elastic load balancer #{resource_arns} not found")
    end
  end

  response.status = 200
  response.body = {
    "ResponseMetadata"=>{"RequestId"=> Fog::AWS::Mock.request_id },
    "DescribeTagsResult"=>{"TagDescriptions"=> tag_descriptions}
  }

  response
end

#remove_tags(resource_arn, keys) ⇒ Object



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

def remove_tags(resource_arn, keys)
  response = Excon::Response.new
  if self.data[:load_balancers_v2][resource_arn]
    keys.each {|key| self.data[:tags][resource_arn].delete key}
    response.status = 200
    response.body = {
      "ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id }
    }
    response
  else
    raise Fog::AWS::ELBV2::NotFound.new("Elastic load balancer #{resource_arn} not found")
  end
end

#reset_dataObject



66
67
68
# File 'lib/fog/aws/elbv2.rb', line 66

def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
end

#setup_credentials(options) ⇒ Object



55
56
57
58
59
60
# File 'lib/fog/aws/elbv2.rb', line 55

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