awspec 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c81642a5b9b297d827bead3be1bf63946948cf8
4
- data.tar.gz: ec37738f21f3047036e55876a6386406edd33220
3
+ metadata.gz: 174d933837b6f18c123157d8d39636cc8151f2a1
4
+ data.tar.gz: 80eb495b6e34d683d09122f2f3235b95e60ddf91
5
5
  SHA512:
6
- metadata.gz: 2af5ebabeece857dbf0eeb260b5541ef925448ddab272f382c74cd1234f0e3097062620ef3bac6bf71879f2f72e36e0a3fe80e3fb644193e4074b183daeb2965
7
- data.tar.gz: 6011008a79a69fe3f8fd2184374ee03670f27e550d1162e5b4f64cd1f55f124ed9c0bb1ea657c623e30129d877d6c6f0737d3f18f7630a25b892bc8802f5f7da
6
+ metadata.gz: 699c01823b05271e2ac4a1c91e9f0e83edaf3f169a16d8a5fd1a54b9fc121e1e7219c0fd29903a3bd36eb598b24c0a9fd16fa58fef4b7dbc0760f0d2596e1d53
7
+ data.tar.gz: 101535a6b96aad8949196bc35e865ca0980c019d01411a6240ffa3ca876ec8f934ba54c6cd57002975818d3d14ebb3a5dd56236e08caca9b82ae8a8f118ba45a
data/README.md CHANGED
@@ -46,11 +46,13 @@ end
46
46
  - [X] RDS DB Parameter Group (`rds_db_parameter_group`)
47
47
  - [X] Security Group (`security_group`)
48
48
  - [X] VPC (`vpc`)
49
+ - [X] S3 (`s3`)
50
+ - Route53
51
+ - [X] Route53 Hosted Zone (`route53_hosted_zone`)
49
52
 
50
53
  ### Next..
51
54
 
52
- - [ ] S3
53
- - [ ] Route53
55
+ - [ ] AutoScaling
54
56
  - ...
55
57
 
56
58
  ## Contributing
@@ -2,7 +2,8 @@ module Awspec
2
2
  module Helper
3
3
  module Type
4
4
  types = %w(
5
- base ec2 rds rds_db_parameter_group security_group vpc
5
+ base ec2 rds rds_db_parameter_group security_group
6
+ vpc s3 route53_hosted_zone
6
7
  )
7
8
 
8
9
  types.each { |type| require "awspec/type/#{type}" }
@@ -0,0 +1,32 @@
1
+ RSpec::Matchers.define :have_record_set do |name|
2
+ match do |hosted_zone|
3
+ hosted_zone.has_record_set?(name, @type, @value, @options)
4
+ end
5
+
6
+ %w(soa a txt ns cname mx ptr srv spf aaaa).each do |type|
7
+ chain type do |value|
8
+ @type = type
9
+ @value = value
10
+ @options = {} if @options.nil?
11
+ end
12
+ end
13
+
14
+ chain :alias do |dns_name, hosted_zone_id|
15
+ @type = 'a'
16
+ @options = {} if @options.nil?
17
+ @options[:alias_dns_name] = dns_name
18
+ @options[:alias_hosted_zone_id] = hosted_zone_id
19
+ end
20
+
21
+ chain :aaaa_alias do |dns_name, hosted_zone_id|
22
+ @type = 'aaaa'
23
+ @options = {} if @options.nil?
24
+ @options[:alias_dns_name] = dns_name
25
+ @options[:alias_hosted_zone_id] = hosted_zone_id
26
+ end
27
+
28
+ chain :ttl do |ttl|
29
+ @options = {} if @options.nil?
30
+ @options[:ttl] = ttl
31
+ end
32
+ end
@@ -7,3 +7,6 @@ require 'awspec/matcher/belong_to_db_subnet_group'
7
7
 
8
8
  # SecurityGroup
9
9
  require 'awspec/matcher/be_opened'
10
+
11
+ # Route53
12
+ require 'awspec/matcher/have_record_set'
@@ -4,10 +4,16 @@ module Awspec::Type
4
4
  class Base
5
5
  attr_reader :id, :ec2_client
6
6
 
7
+ # rubocop:disable all
7
8
  def initialize(id = nil)
8
- @tmp = id
9
+ @id = nil
9
10
  @ec2_client = Aws::EC2::Client.new
10
11
  end
12
+ # rubocop:enable all
13
+
14
+ def exists?
15
+ @id
16
+ end
11
17
 
12
18
  def find_vpc(id)
13
19
  res = @ec2_client.describe_vpcs({
@@ -59,5 +65,37 @@ module Awspec::Type
59
65
 
60
66
  return res[:security_groups][0] if res[:security_groups].count == 1
61
67
  end
68
+
69
+ def find_ec2(id)
70
+ if id.is_a?(Array)
71
+ # Aws::EC2::Client.describe_instances native filters format
72
+ res = @client.describe_instances({
73
+ filters: id
74
+ })
75
+ elsif id.is_a?(Hash)
76
+ # syntax sugar
77
+ filters = []
78
+ id.each do |k, v|
79
+ filters.push({ name: k, values: Array(v) })
80
+ end
81
+ res = @client.describe_instances({
82
+ filters: filters
83
+ })
84
+ else
85
+ # instance_id or tag:Name
86
+ begin
87
+ res = @client.describe_instances({
88
+ instance_ids: [id]
89
+ })
90
+ rescue
91
+ # Aws::EC2::Errors::InvalidInstanceIDMalformed
92
+ # Aws::EC2::Errors::InvalidInstanceIDNotFound
93
+ res = @client.describe_instances({
94
+ filters: [{ name: 'tag:Name', values: [id] }]
95
+ })
96
+ end
97
+ end
98
+ return res[:reservations][0][:instances][0] if res[:reservations][0][:instances].count == 1
99
+ end
62
100
  end
63
101
  end
@@ -5,36 +5,8 @@ module Awspec::Type
5
5
  def initialize(id)
6
6
  super
7
7
  @client = @ec2_client
8
- if id.is_a?(Array)
9
- # Aws::EC2::Client.describe_instances native filters format
10
- res = @client.describe_instances({
11
- filters: id
12
- })
13
- elsif id.is_a?(Hash)
14
- # syntax sugar
15
- filters = []
16
- id.each do |k, v|
17
- filters.push({ name: k, values: Array(v) })
18
- end
19
- res = @client.describe_instances({
20
- filters: filters
21
- })
22
- else
23
- # instance_id or tag:Name
24
- begin
25
- res = @client.describe_instances({
26
- instance_ids: [id]
27
- })
28
- rescue
29
- # Aws::EC2::Errors::InvalidInstanceIDMalformed
30
- # Aws::EC2::Errors::InvalidInstanceIDNotFound
31
- res = @client.describe_instances({
32
- filters: [{ name: 'tag:Name', values: [id] }]
33
- })
34
- end
35
- end
36
- @id = res[:reservations][0][:instances][0][:instance_id]
37
- @instance = res[:reservations][0][:instances][0]
8
+ @instance = find_ec2(id)
9
+ @id = @instance[:instance_id] if @instance
38
10
  end
39
11
 
40
12
  states = %w(
@@ -9,8 +9,8 @@ module Awspec::Type
9
9
  res = @client.describe_db_instances({
10
10
  db_instance_identifier: id
11
11
  })
12
- @id = res[:db_instances][0][:db_instance_identifier]
13
- @instance = res[:db_instances][0]
12
+ @instance = res[:db_instances][0] if res[:db_instances].count == 1
13
+ @id = @instance[:db_instance_identifier] if @instance
14
14
  end
15
15
 
16
16
  states = %w(
@@ -3,6 +3,7 @@ module Awspec::Type
3
3
  attr_reader :parameters
4
4
 
5
5
  def initialize(name)
6
+ super
6
7
  @client = Aws::RDS::Client.new
7
8
  @parameters = {}
8
9
 
@@ -17,6 +18,7 @@ module Awspec::Type
17
18
  @parameters[param.parameter_name] = param.parameter_value
18
19
  end
19
20
  end
21
+ @id = name unless @parameters.empty?
20
22
  end
21
23
 
22
24
  def method_missing(name)
@@ -0,0 +1,64 @@
1
+ module Awspec::Type
2
+ class Route53HostedZone < Base
3
+ attr_reader :hosted_zone, :resource_record_sets
4
+
5
+ def initialize(id)
6
+ super
7
+ @client = Aws::Route53::Client.new
8
+ @hosted_zone = find_hosted_zone(id)
9
+ @id = @hosted_zone[:id] if @hosted_zone
10
+ return unless @id
11
+ res = @client.list_resource_record_sets({
12
+ hosted_zone_id: @id
13
+ })
14
+ @resource_record_sets = res.resource_record_sets
15
+ end
16
+
17
+ def has_record_set?(name, type, value, options = {})
18
+ ret = @resource_record_sets.find do |record_set|
19
+ next if record_set.type != type.upcase
20
+ options[:ttl] = record_set.ttl unless options[:ttl]
21
+ if !record_set.resource_records.empty?
22
+ v = record_set.resource_records.map { |r| r.value }.join("\n")
23
+ record_set.name == name && \
24
+ value == v && \
25
+ record_set.ttl == options[:ttl]
26
+ else
27
+ # ALIAS
28
+ record_set.name == name && \
29
+ record_set.alias_target.dns_name == options[:alias_dns_name] && \
30
+ record_set.alias_target.hosted_zone_id == options[:alias_hosted_zone_id]
31
+ end
32
+ end
33
+ end
34
+
35
+ def find_hosted_zone(id)
36
+ hosted_zones = {}
37
+ marker = nil
38
+ loop do
39
+ res = @client.list_hosted_zones({
40
+ marker: marker
41
+ })
42
+ marker = res.marker
43
+ break if res.hosted_zones.empty?
44
+ res.hosted_zones.each do |hosted_zone|
45
+ hosted_zones[hosted_zone[:name]] = hosted_zones
46
+ if hosted_zone[:name] == id || hosted_zone[:id] == '/hostedzone/' + id || hosted_zone[:id] == id
47
+ return hosted_zone
48
+ end
49
+ end
50
+
51
+ break if marker.nil?
52
+ end
53
+ end
54
+
55
+ def method_missing(name)
56
+ describe = name.to_s
57
+ if @hosted_zone.key?(describe)
58
+ @hosted_zone[describe]
59
+ else
60
+ super
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,26 @@
1
+ module Awspec::Type
2
+ class S3 < Base
3
+ attr_reader :client, :bucket
4
+
5
+ def initialize(id)
6
+ super
7
+ @client = Aws::S3::Client.new
8
+ res = @client.list_buckets
9
+ ret = @client.list_buckets[:buckets].find do |bucket|
10
+ bucket.name == id
11
+ end
12
+ @id = id if ret
13
+ @bucket = ret
14
+ end
15
+
16
+ def has_object?(key)
17
+ res = @client.head_object({
18
+ bucket: @id,
19
+ key: key.sub(%r(\A/), '')
20
+ })
21
+ res
22
+ rescue
23
+ false
24
+ end
25
+ end
26
+ end
@@ -7,7 +7,7 @@ module Awspec::Type
7
7
  @client = @ec2_client
8
8
  @inbound = true
9
9
  @sg = find_security_group(id)
10
- @id = @sg[:group_id]
10
+ @id = @sg[:group_id] if @sg
11
11
  end
12
12
 
13
13
  def method_missing(name)
@@ -6,7 +6,7 @@ module Awspec::Type
6
6
  super
7
7
  @client = @ec2_client
8
8
  @vpc = find_vpc(id)
9
- @id = @vpc[:vpc_id]
9
+ @id = @vpc[:vpc_id] if @vpc
10
10
  end
11
11
 
12
12
  states = %w(
@@ -1,3 +1,3 @@
1
1
  module Awspec
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - k1LoW
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-29 00:00:00.000000000 Z
11
+ date: 2015-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -135,11 +135,14 @@ files:
135
135
  - lib/awspec/matcher/belong_to_db_subnet_group.rb
136
136
  - lib/awspec/matcher/belong_to_subnet.rb
137
137
  - lib/awspec/matcher/belong_to_vpc.rb
138
+ - lib/awspec/matcher/have_record_set.rb
138
139
  - lib/awspec/setup.rb
139
140
  - lib/awspec/type/base.rb
140
141
  - lib/awspec/type/ec2.rb
141
142
  - lib/awspec/type/rds.rb
142
143
  - lib/awspec/type/rds_db_parameter_group.rb
144
+ - lib/awspec/type/route53_hosted_zone.rb
145
+ - lib/awspec/type/s3.rb
143
146
  - lib/awspec/type/security_group.rb
144
147
  - lib/awspec/type/vpc.rb
145
148
  - lib/awspec/version.rb