Module: PuppetX::IntechWIFI::AwsCmds

Defined in:
lib/puppet_x/intechwifi/awscmds.rb

Class Method Summary collapse

Class Method Details

.clear_vpc_tag_cache(name) ⇒ Object



30
31
32
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 30

def AwsCmds.clear_vpc_tag_cache(name)
  @vpc_tag_cache = { :key => nil, :value => nil} if @vpc_tag_cache[:key] == name
end

.find_autoscaling_by_name(regions, name, &aws_command) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 94

def AwsCmds.find_autoscaling_by_name( regions, name, &aws_command)
  result = regions.map{ |r|
    { :region => r, :data => JSON.parse(aws_command.call('autoscaling', 'describe-auto-scaling-groups', '--region', r, "--auto-scaling-group-names", name))["AutoScalingGroups"]}
  }.select{ |a| a[:data].length != 0 }.flatten

  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name if result.length == 0
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result.length > 1  #  matches in more than one region.
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result[0][:data].length > 1  #  More than one match in the region.

  {
      :region => result[0][:region],
      :data   => result[0][:data][0],
  }
end

.find_elb_target_by_name(name, region, &aws_command) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 124

def AwsCmds.find_elb_target_by_name(name, region, &aws_command)
  args = [
      'elbv2', 'describe-target-groups',
      '--region', region,
      '--names', name
  ]
  JSON.parse(aws_command.call(args.flatten))["TargetGroups"][0]["TargetGroupArn"]

rescue Puppet::ExecutionFailure => e
  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name
end

.find_iam_instance_profile_by_name(name, &aws_command) ⇒ Object



109
110
111
112
113
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 109

def AwsCmds.find_iam_instance_profile_by_name(name, &aws_command)
  JSON.parse(aws_command.call('iam', 'get-instance-profile', "--instance-profile-name", name))["InstanceProfile"]
rescue Puppet::ExecutionFailure => e
  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name
end

.find_iam_profile_by_name(name, scope, &aws_command) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 115

def AwsCmds.find_iam_profile_by_name(name, scope, &aws_command)
  result = JSON.parse(aws_command.call('iam', 'list-policies', "--scope", scope))["Policies"].select{|p| p["PolicyName"] == name}

  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name if result.length == 0
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result.length > 1  #  Multiple matches

  result[0]
end

.find_iam_profile_policy(arn, &aws_command) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 138

def AwsCmds.find_iam_profile_policy(arn, &aws_command)
  version_id = JSON.parse(aws_command.call('iam', 'list-policy-versions', "--policy-arn", arn))["Versions"].select{|p|
    p["IsDefaultVersion"]
  }.map{|p| p["VersionId"] }

  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, arn if version_id.length == 0
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, arn if version_id.length > 1  #  Multiple matches

  JSON.parse(aws_command.call('iam', 'get-policy-version', "--policy-arn", arn, "--version-id", version_id[0]))["PolicyVersion"]
end

.find_iam_role_by_name(name, &aws_command) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 149

def AwsCmds.find_iam_role_by_name(name, &aws_command)
  result = JSON.parse(aws_command.call('iam', 'list-roles'))["Roles"].select{|p| p["RoleName"] == name}

  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name if result.length == 0
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result.length > 1  #  Multiple matches

  result[0]
end

.find_id_by_name(region, resource_type, id, &aws_command) ⇒ Object



61
62
63
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 61

def AwsCmds.find_id_by_name(region, resource_type, id, &aws_command)
  AwsCmds.find_tag([region], resource_type, "Name", "value", id, &aws_command)[:tag]["ResourceId"]
end

.find_launch_configuration_by_name(regions, name, &aws_command) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 80

def AwsCmds.find_launch_configuration_by_name( regions, name, &aws_command)
  lcs = []
  region = nil
  regions.each{ |r|
    output = JSON.parse(aws_command.call('autoscaling', 'describe-launch-configurations', '--region', r))
    lcs << output["LaunchConfigurations"].select{|l| PuppetX::IntechWIFI::Autoscaling_Rules.is_valid_lc_name?(name, l['LaunchConfigurationName'] )}.reduce([]){|memo, lc| memo << lc }
  }
  result = lcs.flatten
  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name if result.length == 0
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if lcs.length > 1  #  matches in more than one region.

  result.max { |a, b| a["LaunchConfigurationName"]  <=> b["LaunchConfigurationName"]}
end

.find_load_balancer_by_name(regions, name, &aws_command) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 195

def AwsCmds.find_load_balancer_by_name(regions, name, &aws_command)
  result = regions.map{ |r|
    {
        :region => r,
        :data => JSON.parse(aws_command.call('elbv2', 'describe-load-balancers', '--region', r, '--names', name))["LoadBalancers"]
    }
  }.select{ |a| a[:data].length != 0}.flatten

  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name if result.length == 0
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result.length > 1  #  Multiple matches
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result[0][:data].length > 1  #  More than one match in the region.

  result[0]

rescue Puppet::ExecutionFailure => e
  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name
end

.find_name_by_id(region, resource_type, id, &aws_command) ⇒ Object



57
58
59
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 57

def AwsCmds.find_name_by_id(region, resource_type, id, &aws_command)
  AwsCmds.find_tag([region], resource_type, "Name", "resource-id", id, &aws_command)[:tag]["Value"]
end

.find_name_or_id_by_id(region, resource_type, id, &aws_command) ⇒ Object



50
51
52
53
54
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 50

def AwsCmds.find_name_or_id_by_id(region, resource_type, id, &aws_command)
  AwsCmds.find_tag([region], resource_type, "Name", "resource-id", id, &aws_command)[:tag]["Value"]
rescue PuppetX::IntechWIFI::Exceptions::NotFoundError => e
  id
end

.find_rds_by_name(regions, name, &aws_command) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 158

def AwsCmds.find_rds_by_name(regions, name, &aws_command)
  result = regions.map{ |r|
    {
        :region => r,
        :data => JSON.parse(aws_command.call('rds', 'describe-db-instances', '--region', r))["DBInstances"].select{ |db|
          db["DBInstanceIdentifier"] == name
        }
    }
  }.select{ |a| a[:data].length != 0}.flatten



  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name if result.length == 0
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result.length > 1  #  Multiple matches
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result[0][:data].length > 1  #  More than one match in the region.

  result[0]
end

.find_rds_subnet_group_by_name(regions, name, &aws_command) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 177

def AwsCmds.find_rds_subnet_group_by_name(regions, name, &aws_command)
  result = regions.map{ |r|
    {
        :region => r,
        :data => JSON.parse(aws_command.call('rds', 'describe-db-subnet-groups', '--region', r, '--db-subnet-group-name', name))["DBSubnetGroups"]
    }
  }.select{ |a| a[:data].length != 0}.flatten

  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name if result.length == 0
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result.length > 1  #  Multiple matches
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, name if result[0][:data].length > 1  #  More than one match in the region.

  result[0]

rescue Puppet::ExecutionFailure => e
  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, name
end

.find_tag(regions, resource_type, key, filter, value, &aws_command) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 66

def AwsCmds.find_tag(regions, resource_type, key, filter, value, &aws_command)
  tags = []
  region = nil
  regions.each{ |r|
    output = aws_command.call('ec2', 'describe-tags', '--filters', "Name=resource-type,Values=#{resource_type}", "Name=key,Values=#{key}", "Name=#{filter},Values=#{value}", '--region', r)
    JSON.parse(output)["Tags"].each{|t| tags << t; region = r }
  }

  raise PuppetX::IntechWIFI::Exceptions::NotFoundError, value if tags.length == 0
  raise PuppetX::IntechWIFI::Exceptions::MultipleMatchesError, value if tags.length > 1

  {:tag => tags[0], :region => region }
end

.find_tag_from_list(tag_list, name) ⇒ Object



25
26
27
28
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 25

def AwsCmds.find_tag_from_list(tag_list, name)
  tags = tag_list.select{|x| x["Key"] == name}.map{|x| x["Value"]}
  tags.length == 1 ? tags[0] : nil
end

.find_vpc_tag(regions, name, &aws_command) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet_x/intechwifi/awscmds.rb', line 34

def AwsCmds.find_vpc_tag(regions, name, &aws_command)
  #  Typically, a puppet run will only be dealing with the one VPC, but many components
  #  will need to obtain the vpcid from vpc name.  As an optimisation, we cache the last answer.
  #

  result = nil

  result = @vpc_tag_cache[:value] unless @vpc_tag_cache[:key] != name

  if result == nil
    result = AwsCmds.find_tag(regions, "vpc", "Name", "value", name, &aws_command)
    @vpc_tag_cache = { :key => name, :value => result}
  end
  result
end