From b855b32cd4639cb4c1344c8d92bcb8098c3da882 Mon Sep 17 00:00:00 2001 From: htnosm Date: Mon, 10 Dec 2018 04:25:26 +0900 Subject: [PATCH 1/2] Add autoscaling_group generate --- lib/awspec/command/generate.rb | 2 +- lib/awspec/generator.rb | 1 + .../generator/spec/autoscaling_group.rb | 52 +++++++++++++++++++ lib/awspec/helper/finder/autoscaling.rb | 19 +++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 lib/awspec/generator/spec/autoscaling_group.rb diff --git a/lib/awspec/command/generate.rb b/lib/awspec/command/generate.rb index 389e1d45d..356d938b4 100644 --- a/lib/awspec/command/generate.rb +++ b/lib/awspec/command/generate.rb @@ -9,7 +9,7 @@ class Generate < Thor types = %w( vpc ec2 rds security_group elb network_acl route_table subnet nat_gateway network_interface alb nlb - internet_gateway + internet_gateway autoscaling_group ) types.each do |type| diff --git a/lib/awspec/generator.rb b/lib/awspec/generator.rb index 79777131e..8ac06ab6c 100644 --- a/lib/awspec/generator.rb +++ b/lib/awspec/generator.rb @@ -32,6 +32,7 @@ require 'awspec/generator/spec/rds_db_parameter_group' require 'awspec/generator/spec/rds_db_cluster_parameter_group' require 'awspec/generator/spec/codebuild' +require 'awspec/generator/spec/autoscaling_group' # Doc require 'awspec/generator/doc/type' diff --git a/lib/awspec/generator/spec/autoscaling_group.rb b/lib/awspec/generator/spec/autoscaling_group.rb new file mode 100644 index 000000000..f74781f92 --- /dev/null +++ b/lib/awspec/generator/spec/autoscaling_group.rb @@ -0,0 +1,52 @@ +module Awspec::Generator + module Spec + class AutoscalingGroup + include Awspec::Helper::Finder + def generate_by_vpc_id(vpc_id) + describes = %w( + auto_scaling_group_name auto_scaling_group_arn min_size max_size desired_capacity + default_cooldown availability_zones health_check_type health_check_grace_period + vpc_zone_identifier termination_policies new_instances_protected_from_scale_in + ) + vpc = find_vpc(vpc_id) + raise 'Not Found VPC' unless vpc + @vpc_id = vpc[:vpc_id] + @vpc_tag_name = vpc.tag_name + autoscaling_groups = select_autoscaling_group_by_vpc_id(@vpc_id) + specs = autoscaling_groups.map do |autoscaling_group| + content = ERB.new(autoscaling_group_spec_template, nil, '-').result(binding).gsub(/^\n/, '') + end + specs.join("\n") + end + + def autoscaling_group_spec_template + template = <<-'EOF' +describe autoscaling_group('<%= autoscaling_group.auto_scaling_group_name %>') do + it { should exist } +<% describes.each do |describe| %> +<%- if autoscaling_group.members.include?(describe.to_sym) && !autoscaling_group[describe.to_sym].nil? -%> +<%- if autoscaling_group[describe].is_a?(String) -%> + its(:<%= describe %>) { should eq '<%= autoscaling_group[describe] %>' } +<%- else -%> + its(:<%= describe %>) { should eq <%= autoscaling_group[describe] %> } +<%- end -%> +<%- end -%> +<% end %> +<%- unless autoscaling_group.launch_configuration_name.nil? -%> + it { should have_launch_configuration('<%= autoscaling_group.launch_configuration_name %>') } +<%- else -%> + its('launch_template.launch_template_name') { should eq '<%= autoscaling_group.launch_template.launch_template_name %>' } +<%- end -%> +<% autoscaling_group[:load_balancer_names].each do |desc| %> + it { should have_elb('<%= desc %>') } +<% end %> +<% autoscaling_group[:target_group_arns].each do |desc| %> + it { should have_alb_target_group('<%= desc.sub(/^.*targetgroup\/(.[^\/]*).*$/, '\1') %>') } +<% end %> +end +EOF + template + end + end + end +end diff --git a/lib/awspec/helper/finder/autoscaling.rb b/lib/awspec/helper/finder/autoscaling.rb index ada4df515..0dfd96045 100644 --- a/lib/awspec/helper/finder/autoscaling.rb +++ b/lib/awspec/helper/finder/autoscaling.rb @@ -36,6 +36,25 @@ def find_block_device_mapping(id, device_id) end ret.single_resource(device_id) end + + def select_autoscaling_group_by_vpc_id(vpc_id) + subnets = [] + select_subnet_by_vpc_id(vpc_id).each { |subnet| subnets << subnet.subnet_id } + req = {} + selected = [] + loop do + res = autoscaling_client.describe_auto_scaling_groups(req) + res.auto_scaling_groups.select do |auto_scaling_group| + vpc_zone_identifiers = auto_scaling_group.vpc_zone_identifier.split(',') + selected.push(auto_scaling_group) if vpc_zone_identifiers.any? do |vpc_zone_identifier| + subnets.include?(vpc_zone_identifier) + end + end + break if res.next_token.nil? + req[:next_token] = res.next_token + end + selected + end end end end From a9aa1c48e440809de1bd9cdd6bda983e24da5aba Mon Sep 17 00:00:00 2001 From: htnosm Date: Mon, 10 Dec 2018 04:25:49 +0900 Subject: [PATCH 2/2] docgen --- doc/resource_types.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/resource_types.md b/doc/resource_types.md index 31ab5d21b..32b403c77 100644 --- a/doc/resource_types.md +++ b/doc/resource_types.md @@ -1763,7 +1763,7 @@ end ``` -### its(:path), its(:role_name), its(:role_id), its(:arn), its(:create_date), its(:assume_role_policy_document), its(:description), its(:tags), its(:max_session_duration), its(:permissions_boundary) +### its(:path), its(:role_name), its(:role_id), its(:arn), its(:create_date), its(:assume_role_policy_document), its(:description), its(:max_session_duration), its(:permissions_boundary), its(:tags) ### :unlock: Advanced use `iam_role` can use `Aws::IAM::Role` resource (see http://docs.aws.amazon.com/sdkforruby/api/Aws/IAM/Role.html). @@ -1852,7 +1852,7 @@ end ``` -### its(:path), its(:user_name), its(:user_id), its(:arn), its(:create_date), its(:password_last_used), its(:tags), its(:permissions_boundary) +### its(:path), its(:user_name), its(:user_id), its(:arn), its(:create_date), its(:password_last_used), its(:permissions_boundary), its(:tags) ### :unlock: Advanced use `iam_user` can use `Aws::IAM::User` resource (see http://docs.aws.amazon.com/sdkforruby/api/Aws/IAM/User.html).