From 6082b18da42aca306e678e52831f67560ad56509 Mon Sep 17 00:00:00 2001 From: alpineriveredge Date: Sat, 1 Jun 2024 02:34:49 +0900 Subject: [PATCH] Add codepipeline resource --- doc/_resource_types/codedeploy.md | 6 + doc/_resource_types/codepipeline.md | 7 ++ doc/resource_types.md | 22 +++- lib/awspec/generator.rb | 1 + lib/awspec/generator/doc/type/codepipeline.rb | 19 ++++ lib/awspec/generator/spec/codepipeline.rb | 29 +++++ lib/awspec/helper/finder.rb | 5 +- lib/awspec/helper/finder/codepipeline.rb | 25 +++++ lib/awspec/helper/type.rb | 2 +- lib/awspec/stub/cloudfront_distribution.rb | 2 - lib/awspec/stub/codepipeline.rb | 104 ++++++++++++++++++ lib/awspec/type/codepipeline.rb | 13 +++ spec/generator/spec/codepipeline_spec.rb | 31 ++++++ spec/type/codepipeline_spec.rb | 15 +++ 14 files changed, 276 insertions(+), 5 deletions(-) create mode 100644 doc/_resource_types/codepipeline.md create mode 100644 lib/awspec/generator/doc/type/codepipeline.rb create mode 100644 lib/awspec/generator/spec/codepipeline.rb create mode 100644 lib/awspec/helper/finder/codepipeline.rb create mode 100644 lib/awspec/stub/codepipeline.rb create mode 100644 lib/awspec/type/codepipeline.rb create mode 100644 spec/generator/spec/codepipeline_spec.rb create mode 100644 spec/type/codepipeline_spec.rb diff --git a/doc/_resource_types/codedeploy.md b/doc/_resource_types/codedeploy.md index 3a2e77657..7019d2932 100644 --- a/doc/_resource_types/codedeploy.md +++ b/doc/_resource_types/codedeploy.md @@ -1 +1,7 @@ ### exist + +```ruby +describe codedeploy('my-codedeploy') do + it { should exist } +end +``` diff --git a/doc/_resource_types/codepipeline.md b/doc/_resource_types/codepipeline.md new file mode 100644 index 000000000..fba83a472 --- /dev/null +++ b/doc/_resource_types/codepipeline.md @@ -0,0 +1,7 @@ +### exist + +```ruby +describe codepipeline('my-codepipeline') do + it { should exist } +end +``` diff --git a/doc/resource_types.md b/doc/resource_types.md index 718ffccea..567991b99 100644 --- a/doc/resource_types.md +++ b/doc/resource_types.md @@ -19,6 +19,7 @@ | [codebuild](#codebuild) | [codedeploy](#codedeploy) | [codedeploy_deployment_group](#codedeploy_deployment_group) +| [codepipeline](#codepipeline) | [cognito_identity_pool](#cognito_identity_pool) | [cognito_user_pool](#cognito_user_pool) | [customer_gateway](#customer_gateway) @@ -759,6 +760,12 @@ Codedeploy resource type. ### exist +```ruby +describe codedeploy('my-codedeploy') do + it { should exist } +end +``` + ### its(:application_id), its(:application_name), its(:create_time), its(:linked_to_git_hub), its(:git_hub_account_name), its(:compute_platform) ## codedeploy_deployment_group @@ -784,6 +791,19 @@ end ``` ### its(:application_name), its(:deployment_group_id), its(:deployment_group_name), its(:deployment_config_name), its(:on_premises_instance_tag_filters), its(:service_role_arn), its(:target_revision), its(:trigger_configurations), its(:alarm_configuration), its(:deployment_style), its(:outdated_instances_strategy), its(:load_balancer_info), its(:last_successful_deployment), its(:last_attempted_deployment), its(:ec2_tag_set), its(:on_premises_tag_set), its(:compute_platform), its(:ecs_services), its(:termination_hook_enabled) +## codepipeline + +Codepipeline resource type. + +### exist + +```ruby +describe codepipeline('my-codepipeline') do + it { should exist } +end +``` + +### its(:name), its(:role_arn), its(:artifact_stores), its(:version), its(:execution_mode), its(:pipeline_type), its(:variables), its(:triggers) ## cognito_identity_pool CognitoIdentityPool resource type. @@ -839,7 +859,7 @@ end ``` -### its(:bgp_asn), its(:customer_gateway_id), its(:ip_address), its(:certificate_arn), its(:state), its(:type), its(:device_name), its(:tags) +### its(:bgp_asn), its(:customer_gateway_id), its(:ip_address), its(:certificate_arn), its(:state), its(:type), its(:device_name), its(:tags), its(:bgp_asn_extended) ## directconnect_virtual_interface DirectconnectVirtualInterface resource type. diff --git a/lib/awspec/generator.rb b/lib/awspec/generator.rb index 134a59e05..b8e0327ee 100644 --- a/lib/awspec/generator.rb +++ b/lib/awspec/generator.rb @@ -44,6 +44,7 @@ require 'awspec/generator/spec/rds_db_cluster' require 'awspec/generator/spec/rds_global_cluster' require 'awspec/generator/spec/managed_prefix_list' +require 'awspec/generator/spec/codepipeline' # Doc require 'awspec/generator/doc/type' diff --git a/lib/awspec/generator/doc/type/codepipeline.rb b/lib/awspec/generator/doc/type/codepipeline.rb new file mode 100644 index 000000000..2c701283f --- /dev/null +++ b/lib/awspec/generator/doc/type/codepipeline.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Awspec::Generator + module Doc + module Type + class Codepipeline < Base + def initialize + super + @type_name = 'Codepipeline' + @type = Awspec::Type::Codepipeline.new('my-codepipeline') + @ret = @type.resource_via_client + @matchers = [] + @ignore_matchers = [] + @describes = [] + end + end + end + end +end diff --git a/lib/awspec/generator/spec/codepipeline.rb b/lib/awspec/generator/spec/codepipeline.rb new file mode 100644 index 000000000..89bad34c8 --- /dev/null +++ b/lib/awspec/generator/spec/codepipeline.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Awspec::Generator + module Spec + class Codepipeline + include Awspec::Helper::Finder + def generate_all + pipelines = select_all_codepipelines + raise 'Not Found CodePipeline' if pipelines.empty? + + ERB.new(codepipeline_spec_template, nil, '-').result(binding).chomp + end + + def codepipeline_spec_template + <<-'EOF' +<% pipelines.each do |pipeline| %> +describe codepipeline('<%= pipeline.name %>') do + it { should exist } + its(:name) { should eq '<%= pipeline.name %>' } + its(:version) { should eq <%= pipeline.version %> } + its(:pipeline_type) { should eq '<%= pipeline.pipeline_type %>' } + its(:execution_mode) { should eq '<%= pipeline.execution_mode %>' } +end +<% end %> +EOF + end + end + end +end diff --git a/lib/awspec/helper/finder.rb b/lib/awspec/helper/finder.rb index 8106307ec..843005558 100644 --- a/lib/awspec/helper/finder.rb +++ b/lib/awspec/helper/finder.rb @@ -55,6 +55,7 @@ require 'awspec/helper/finder/msk' require 'awspec/helper/finder/cognito_identity_pool' require 'awspec/helper/finder/transfer' +require 'awspec/helper/finder/codepipeline' require 'awspec/helper/finder/account_attributes' @@ -117,6 +118,7 @@ module Finder include Awspec::Helper::Finder::Msk include Awspec::Helper::Finder::CognitoIdentityPool include Awspec::Helper::Finder::Transfer + include Awspec::Helper::Finder::Codepipeline CLIENTS = { ec2_client: Aws::EC2::Client, @@ -165,7 +167,8 @@ module Finder msk_client: Aws::Kafka::Client, cognito_identity_client: Aws::CognitoIdentity::Client, cognito_identity_provider_client: Aws::CognitoIdentityProvider::Client, - transfer_client: Aws::Transfer::Client + transfer_client: Aws::Transfer::Client, + codepipeline_client: Aws::CodePipeline::Client } CLIENT_OPTIONS = { diff --git a/lib/awspec/helper/finder/codepipeline.rb b/lib/awspec/helper/finder/codepipeline.rb new file mode 100644 index 000000000..c9a0a01af --- /dev/null +++ b/lib/awspec/helper/finder/codepipeline.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Awspec::Helper + module Finder + module Codepipeline + def find_codepipeline(id) + res = codepipeline_client.get_pipeline({ name: id }) + res.pipeline + end + + def select_all_codepipelines + req = {} + pipelines = [] + loop do + res = codepipeline_client.list_pipelines(req) + pipelines.push(*res.pipelines) + break if res.next_token.nil? + + req[:next_token] = res.next_token + end + pipelines + end + end + end +end diff --git a/lib/awspec/helper/type.rb b/lib/awspec/helper/type.rb index 7df4c52fb..30dc9de06 100644 --- a/lib/awspec/helper/type.rb +++ b/lib/awspec/helper/type.rb @@ -24,7 +24,7 @@ module Type internet_gateway acm cloudwatch_logs dynamodb_table eip sqs ssm_parameter cloudformation_stack codebuild sns_topic redshift redshift_cluster_parameter_group codedeploy codedeploy_deployment_group secretsmanager msk transit_gateway cognito_identity_pool cognito_user_pool vpc_endpoints - transfer_server managed_prefix_list + transfer_server managed_prefix_list codepipeline ] ACCOUNT_ATTRIBUTES = %w[ diff --git a/lib/awspec/stub/cloudfront_distribution.rb b/lib/awspec/stub/cloudfront_distribution.rb index 07996e1e3..6695270ef 100644 --- a/lib/awspec/stub/cloudfront_distribution.rb +++ b/lib/awspec/stub/cloudfront_distribution.rb @@ -17,7 +17,6 @@ status: 'Deployed', last_modified_time: Time.new(2015, 1, 2, 10, 00, 00, '+00:00'), domain_name: 'abcdefghijklmn.cloudfront.net', - staging: false, aliases: { quantity: 0, items: [] @@ -117,7 +116,6 @@ status: 'Deployed', last_modified_time: Time.new(2016, 3, 2, 10, 00, 00, '+00:00'), domain_name: '123456789zyxw.cloudfront.net', - staging: false, aliases: { quantity: 1, items: ['cf-s3-origin-hosting.dev.example.com'] diff --git a/lib/awspec/stub/codepipeline.rb b/lib/awspec/stub/codepipeline.rb new file mode 100644 index 000000000..bdb4d951e --- /dev/null +++ b/lib/awspec/stub/codepipeline.rb @@ -0,0 +1,104 @@ +# frozen_string_literal: true + +Aws.config[:codepipeline] = { + stub_responses: { + get_pipeline: { + pipeline: { + name: 'my-codepipeline', + role_arn: 'arn:aws:iam::123456789012:role/service-role/AWSCodePipelineServiceRole-my-codepipeline', + artifact_store: { + type: 'S3', + location: 'codepipeline-ap-northeast-1-12345678901' + }, + stages: [ + { + name: 'Source', + actions: [ + { + name: 'Source', + action_type_id: { + category: 'Source', + owner: 'AWS', + provider: 'S3', + version: '1' + }, + run_order: 1, + configuration: { + 'PollForSourceChanges' => 'false', + 'S3Bucket' => 'example-bucket', + 'S3ObjectKey' => 'test.zip' + }, + output_artifacts: [ + { + name: 'SourceArtifact' + } + ], + input_artifacts: [], + region: 'ap-northeast-1', + namespace: 'SourceVariables' + } + ] + }, + { + name: 'Build', + actions: [ + { + name: 'Build', + action_type_id: { + category: 'Build', + owner: 'AWS', + provider: 'CodeBuild', + version: '1' + }, + run_order: 1, + configuration: { + 'ProjectName' => 'my-codebuild1' + }, + output_artifacts: [ + { + name: 'BuildArtifact' + } + ], + input_artifacts: [ + { + name: 'SourceArtifact' + } + ], + region: 'ap-northeast-1', + namespace: 'BuildVariables' + } + ] + } + ], + version: 1, + execution_mode: 'QUEUED', + pipeline_type: 'V2' + }, + metadata: { + pipeline_arn: 'arn:aws:codepipeline:ap-northeast-1:123456789012:my-codepipeline', + created: Time.local(2024), + updated: Time.local(2024) + } + }, + list_pipelines: { + pipelines: [ + { + name: 'my-codepipeline1', + version: 1, + pipeline_type: 'V2', + execution_mode: 'QUEUED', + created: Time.local(2024), + updated: Time.local(2024) + }, + { + name: 'my-codepipeline2', + version: 1, + pipeline_type: 'V2', + execution_mode: 'QUEUED', + created: Time.local(2024), + updated: Time.local(2024) + } + ] + } + } +} diff --git a/lib/awspec/type/codepipeline.rb b/lib/awspec/type/codepipeline.rb new file mode 100644 index 000000000..8f6afc4a0 --- /dev/null +++ b/lib/awspec/type/codepipeline.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Awspec::Type + class Codepipeline < ResourceBase + def resource_via_client + @resource_via_client ||= find_codepipeline(@display_name) + end + + def id + @id ||= resource_via_client if resource_via_client.name + end + end +end diff --git a/spec/generator/spec/codepipeline_spec.rb b/spec/generator/spec/codepipeline_spec.rb new file mode 100644 index 000000000..e76c836bc --- /dev/null +++ b/spec/generator/spec/codepipeline_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'Awspec::Generator::Spec::Codepipeline' do + before do + Awspec::Stub.load 'codepipeline' + end + let(:codepipeline) { Awspec::Generator::Spec::Codepipeline.new } + it 'generate_all generate spec' do + spec = <<-'EOF' + +describe codepipeline('my-codepipeline1') do + it { should exist } + its(:name) { should eq 'my-codepipeline1' } + its(:version) { should eq 1 } + its(:pipeline_type) { should eq 'V2' } + its(:execution_mode) { should eq 'QUEUED' } +end + +describe codepipeline('my-codepipeline2') do + it { should exist } + its(:name) { should eq 'my-codepipeline2' } + its(:version) { should eq 1 } + its(:pipeline_type) { should eq 'V2' } + its(:execution_mode) { should eq 'QUEUED' } +end +EOF + expect(codepipeline.generate_all.to_s.gsub(/\n/, "\n")).to eq spec + end +end diff --git a/spec/type/codepipeline_spec.rb b/spec/type/codepipeline_spec.rb new file mode 100644 index 000000000..22498eb81 --- /dev/null +++ b/spec/type/codepipeline_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'spec_helper' +Awspec::Stub.load 'codepipeline' + +describe codepipeline('my-codepipeline') do + it { should exist } + its(:name) { should eq 'my-codepipeline' } + its(:role_arn) { should eq 'arn:aws:iam::123456789012:role/service-role/AWSCodePipelineServiceRole-my-codepipeline' } + its('artifact_store.type') { should eq 'S3' } + its('artifact_store.location') { should eq 'codepipeline-ap-northeast-1-12345678901' } + its(:version) { should eq 1 } + its(:execution_mode) { should eq 'QUEUED' } + its(:pipeline_type) { should eq 'V2' } +end