Class: Reyes::FakeAws

Inherits:
Object
  • Object
show all
Includes:
Chalk::Log
Defined in:
lib/reyes/fake_aws.rb

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ FakeAws

Returns a new instance of FakeAws.

Parameters:

  • data (Hash)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :assert_version (Boolean) — default: true
  • :check_not_after (Boolean) — default: true


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/reyes/fake_aws.rb', line 13

def initialize(data, options={})
  options = {assert_version: true, check_not_after: true}.merge(options)

  @data = data
  log.info("Initialized FakeAws with metadata: #{.inspect}")

  version = ['format_version']
  if version != Reyes::JSON_FORMAT_VERSION
    msg = "JSON format_version #{version.inspect} " \
          "differs from our version #{Reyes::JSON_FORMAT_VERSION}"
    log.error('WARNING: ' + msg)
    if options.fetch(:assert_version)
      raise Error.new(msg)
    end
  end

  if options.fetch(:check_not_after)
    not_after = Time.at(.fetch('not_after_stamp')).utc
    if Time.now.utc > not_after
      log.error('JSON data has expired')
      log.error("Current time: #{Time.now.utc}")
      log.error("JSON not_after: #{not_after}")
      raise Error.new("JSON data expired at #{not_after}")
    end
  end
end

Instance Method Details

#addresses_for_security_group(region, security_group_id) ⇒ Object



66
67
68
69
70
# File 'lib/reyes/fake_aws.rb', line 66

def addresses_for_security_group(region, security_group_id)
  security_group_members(region, security_group_id).map do |instance_id|
    instance(region, instance_id).fetch('private_ip_address')
  end
end

#ec2_classic_cidr_blocksObject

We could actually calculate this data, but it makes assigning security group rules a little bit trickier, so these will be generated statically from config.



126
127
128
# File 'lib/reyes/fake_aws.rb', line 126

def ec2_classic_cidr_blocks
  @data.fetch('classic_cidr_blocks')
end

#excluded_group_namesObject



145
146
147
# File 'lib/reyes/fake_aws.rb', line 145

def excluded_group_names
  @data.fetch('excluded_group_names')
end

#foreign_groups_by_name(group_name, vpc_id) ⇒ Hash<Hash>

Look up remote security group data. If ‘vpc_id` is nil, all VPC security groups will be returned. If `vpc_id` is given, all EC2 classic and all VPC security groups that aren’t in the given VPC will be returned.

Parameters:

  • group_name (String)

    The name of the groups to fetch.

  • vpc_id (String, nil)

    The String VPC ID or nil if EC2 classic of the current cluster.

Returns:

  • (Hash<Hash>)

    A mapping of group_id => group_data for relevant security groups.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/reyes/fake_aws.rb', line 87

def foreign_groups_by_name(group_name, vpc_id)
  vpc_set = vpc_ids.to_set
  groups = {}
  @data.fetch('security_groups_by_name').fetch(group_name).each do |g|

    group_vpc_id = g.fetch('vpc')

    # skip local groups that are not foreign
    # (groups with same VPC ID / VPCness)
    next if group_vpc_id == vpc_id

    # skip VPC groups that are not listed in our config
    next if group_vpc_id && !vpc_set.include?(group_vpc_id)

    groups[g.fetch('group_id')] = security_group(g.fetch('region'),
                                                 g.fetch('group_id'))
  end

  groups
end

#instance(region, instance_id) ⇒ Object



44
45
46
# File 'lib/reyes/fake_aws.rb', line 44

def instance(region, instance_id)
  region_data(region).fetch('instances').fetch(instance_id)
end

#metadataObject



141
142
143
# File 'lib/reyes/fake_aws.rb', line 141

def 
  @data.fetch('metadata')
end

#region_data(region) ⇒ Object



40
41
42
# File 'lib/reyes/fake_aws.rb', line 40

def region_data(region)
  @data.fetch('regions').fetch(region)
end

#remote_cidr_blocks(self_vpc_id) ⇒ Array<String>

Return all EC2 classic and VPC CIDR blocks that are not in ‘self_vpc_id`.

Parameters:

  • self_vpc_id (String)

Returns:

  • (Array<String>)

    A list of CIDR block strings.



136
137
138
139
# File 'lib/reyes/fake_aws.rb', line 136

def remote_cidr_blocks(self_vpc_id)
  nets = vpcs_except(self_vpc_id).map {|v, data| data.fetch('cidr_block') }
  return nets + ec2_classic_cidr_blocks
end

#security_group(region, security_group_id) ⇒ Object



48
49
50
# File 'lib/reyes/fake_aws.rb', line 48

def security_group(region, security_group_id)
  region_data(region).fetch('security_groups').fetch(security_group_id)
end

#security_group_ids_for_instance(region, instance_id) ⇒ Object



52
53
54
# File 'lib/reyes/fake_aws.rb', line 52

def security_group_ids_for_instance(region, instance_id)
  instance(region, instance_id).fetch('security_groups')
end

#security_group_members(region, security_group_id) ⇒ Object



72
73
74
# File 'lib/reyes/fake_aws.rb', line 72

def security_group_members(region, security_group_id)
  security_group(region, security_group_id).fetch('instances')
end

#security_groups_for_instance(region, instance_id) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/reyes/fake_aws.rb', line 56

def security_groups_for_instance(region, instance_id)
  data = {}

  security_group_ids_for_instance(region, instance_id).each do |sg_id|
    data[sg_id] = security_group(region, sg_id)
  end

  data
end

#vpc_idsObject



119
120
121
# File 'lib/reyes/fake_aws.rb', line 119

def vpc_ids
  @data.fetch('vpcs').keys
end

#vpcsObject



108
109
110
# File 'lib/reyes/fake_aws.rb', line 108

def vpcs
  @data.fetch('vpcs')
end

#vpcs_except(vpc_id) ⇒ Object



112
113
114
115
116
117
# File 'lib/reyes/fake_aws.rb', line 112

def vpcs_except(vpc_id)
  unless vpc_id.is_a?(String)
    raise ArgumentError.new("#{vpc_id.inspect} must be a String")
  end
  vpcs.find_all {|v, _| v != vpc_id }
end