From 836d568e05ded3980ae31d1a2c1aacfb12db14cc Mon Sep 17 00:00:00 2001 From: Donovan Carthew Date: Fri, 22 May 2020 16:21:17 +0200 Subject: [PATCH 1/9] Codecommit_approval_rule_assocation resource --- aws/provider.go | 1 + ...ws_codecommit_approval_rule_association.go | 152 +++++++++++++++++ ...decommit_approval_rule_association_test.go | 156 ++++++++++++++++++ ...it_approval_rule_association.html.markdown | 31 ++++ 4 files changed, 340 insertions(+) create mode 100644 aws/resource_aws_codecommit_approval_rule_association.go create mode 100644 aws/resource_aws_codecommit_approval_rule_association_test.go create mode 100644 website/docs/r/codecommit_approval_rule_association.html.markdown diff --git a/aws/provider.go b/aws/provider.go index a21e986df9e..f1d36ea04fa 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -482,6 +482,7 @@ func Provider() *schema.Provider { "aws_codedeploy_deployment_group": resourceAwsCodeDeployDeploymentGroup(), "aws_codecommit_repository": resourceAwsCodeCommitRepository(), "aws_codecommit_trigger": resourceAwsCodeCommitTrigger(), + "aws_codecommit_approval_rule_association": resourceAwsCodeCommitApprovalRuleAssociation(), "aws_codebuild_project": resourceAwsCodeBuildProject(), "aws_codebuild_report_group": resourceAwsCodeBuildReportGroup(), "aws_codebuild_source_credential": resourceAwsCodeBuildSourceCredential(), diff --git a/aws/resource_aws_codecommit_approval_rule_association.go b/aws/resource_aws_codecommit_approval_rule_association.go new file mode 100644 index 00000000000..6d8dc08b1b7 --- /dev/null +++ b/aws/resource_aws_codecommit_approval_rule_association.go @@ -0,0 +1,152 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/codecommit" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + //"github.com/hashicorp/terraform-plugin-sdk/helper/validation" +) + +func resourceAwsCodeCommitApprovalRuleAssociation() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsCodeCommitApprovalRuleAssociationCreate, + Update: resourceAwsCodeCommitApprovalRuleAssociationUpdate, + Read: resourceAwsCodeCommitApprovalRuleAssociationRead, + Delete: resourceAwsCodeCommitApprovalRuleAssociationDelete, + + Schema: map[string]*schema.Schema{ + "template_name": { + Type: schema.TypeString, + ForceNew: true, + Required: true, + }, + "repository_names": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Required: true, + }, + }, + } +} + +func resourceAwsCodeCommitApprovalRuleAssociationCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codecommitconn + + templateName := d.Get("template_name").(string) + repositoryNames := d.Get("repository_names") + + input := &codecommit.AssociateApprovalRuleTemplateWithRepositoryInput{} + for _, repositoryName := range repositoryNames.(*schema.Set).List() { + input = &codecommit.AssociateApprovalRuleTemplateWithRepositoryInput{ + ApprovalRuleTemplateName: aws.String(templateName), + RepositoryName: aws.String(repositoryName.(string)), + } + + log.Printf("[DEBUG] Associating approval rule %s with: %s", templateName, repositoryName) + _, err := conn.AssociateApprovalRuleTemplateWithRepository(input) + + if err != nil { + return fmt.Errorf("error associating approval rule (%s) with repository (%s): %s", templateName, repositoryName, err) + } + } + + d.SetId(fmt.Sprintf("%s", templateName)) + + return resourceAwsCodeCommitApprovalRuleAssociationRead(d, meta) +} + +func resourceAwsCodeCommitApprovalRuleAssociationUpdate(d *schema.ResourceData, meta interface{}) error { + if d.HasChange("repository_names") { + templateName := d.Get("template_name") + conn := meta.(*AWSClient).codecommitconn + + o, n := d.GetChange("repository_names") + oldR, newR := o.(*schema.Set).List(), n.(*schema.Set).List() + + for _, oldRepo := range oldR { + found := false + for _, newRepo := range newR { + if oldRepo == newRepo { + found = true + } + } + + if !found { + input := &codecommit.DisassociateApprovalRuleTemplateFromRepositoryInput{ + ApprovalRuleTemplateName: aws.String(templateName.(string)), + RepositoryName: aws.String(oldRepo.(string)), + } + _, err := conn.DisassociateApprovalRuleTemplateFromRepository(input) + if err != nil { + return err + } + } + } + + for _, newRepo := range newR { + found := false + for _, oldRepo := range oldR { + if newRepo == oldRepo { + found = true + } + } + + if !found { + input := &codecommit.AssociateApprovalRuleTemplateWithRepositoryInput{ + ApprovalRuleTemplateName: aws.String(templateName.(string)), + RepositoryName: aws.String(newRepo.(string)), + } + _, err := conn.AssociateApprovalRuleTemplateWithRepository(input) + if err != nil { + return err + } + } + } + } + + return resourceAwsCodeCommitApprovalRuleAssociationRead(d, meta) +} + +func resourceAwsCodeCommitApprovalRuleAssociationRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codecommitconn + + templateName := d.Get("template_name") + + input := &codecommit.ListRepositoriesForApprovalRuleTemplateInput{ + ApprovalRuleTemplateName: aws.String(templateName.(string)), + } + + _, err := conn.ListRepositoriesForApprovalRuleTemplate(input) + + if err != nil { + return err + } + + return nil +} + +func resourceAwsCodeCommitApprovalRuleAssociationDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codecommitconn + + templateName := d.Get("template_name") + repositoryNames := d.Get("repository_names") + + input := &codecommit.DisassociateApprovalRuleTemplateFromRepositoryInput{} + + for _, repositoryName := range repositoryNames.(*schema.Set).List() { + input = &codecommit.DisassociateApprovalRuleTemplateFromRepositoryInput{ + ApprovalRuleTemplateName: aws.String(templateName.(string)), + RepositoryName: aws.String(repositoryName.(string)), + } + + _, err := conn.DisassociateApprovalRuleTemplateFromRepository(input) + + if err != nil { + return err + } + } + return nil +} diff --git a/aws/resource_aws_codecommit_approval_rule_association_test.go b/aws/resource_aws_codecommit_approval_rule_association_test.go new file mode 100644 index 00000000000..963fd470a11 --- /dev/null +++ b/aws/resource_aws_codecommit_approval_rule_association_test.go @@ -0,0 +1,156 @@ +package aws + +import ( + "fmt" + "sort" + + "strings" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/codecommit" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccAWSCodeCommitApprovalRuleAssociation_basic(t *testing.T) { + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCodeCommitApprovalRuleAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCodeCommitApprovalRuleAssociation_basic(), + Check: resource.ComposeTestCheckFunc( + testAccCheckCodeCommitApprovalRuleAssociationExists("aws_codecommit_approval_rule_association.test"), + ), + }, + }, + }) +} + +func TestAccAWSCodeCommitApprovalRuleAssociation_withChanges(t *testing.T) { + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCodeCommitApprovalRuleAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCodeCommitApprovalRuleAssociation_setup(), + Check: resource.ComposeTestCheckFunc( + testAccCheckCodeCommitApprovalRuleAssociationExists("aws_codecommit_approval_rule_association.test_change"), + resource.TestCheckResourceAttr( + "aws_codecommit_approval_rule_association.test_change", "repository_names.#", "1", + ), + ), + }, + { + Config: testAccCodeCommitApprovalRuleAssociation_withChanges(), + Check: resource.ComposeTestCheckFunc( + testAccCheckCodeCommitApprovalRuleAssociationExists("aws_codecommit_approval_rule_association.test_change"), + resource.TestCheckResourceAttr( + "aws_codecommit_approval_rule_association.test_change", "repository_names.#", "2", + ), + ), + }, + }, + }) +} + +func testAccCheckCodeCommitApprovalRuleAssociationExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + codecommitconn := testAccProvider.Meta().(*AWSClient).codecommitconn + out, err := codecommitconn.ListRepositoriesForApprovalRuleTemplate(&codecommit.ListRepositoriesForApprovalRuleTemplateInput{ + ApprovalRuleTemplateName: aws.String(rs.Primary.ID), + }) + + if err != nil { + return err + } + + if strings.Join(aws.StringValueSlice(out.RepositoryNames), "") == "" { + return fmt.Errorf("No repositories associated with approval rule template: %q", rs.Primary.ID) + } + + var repoAttributes []string + + for k, v := range rs.Primary.Attributes { + if strings.Contains(k, "repository_names") && !strings.Contains(k, "repository_names.#") { + repoAttributes = append(repoAttributes, v) + } + } + + repoActual := aws.StringValueSlice(out.RepositoryNames) + + sort.Strings(repoAttributes) + sort.Strings(repoActual) + + if strings.Join(repoActual, ",") != strings.Join(repoAttributes, ",") { + return fmt.Errorf("CodeCommit Approval Rule Association mismatch - existing: %q, state: %q", + strings.Join(aws.StringValueSlice(out.RepositoryNames), ","), repoAttributes) + } + + return nil + } +} + +func testAccCheckCodeCommitApprovalRuleAssociationDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).codecommitconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_codecommit_approval_rule_association" { + continue + } + + out, err := conn.ListRepositoriesForApprovalRuleTemplate(&codecommit.ListRepositoriesForApprovalRuleTemplateInput{ + ApprovalRuleTemplateName: aws.String(rs.Primary.ID), + }) + + if len(out.RepositoryNames) == 0 { + return nil + } else { + return fmt.Errorf("Approval rule template associations still exist: %s", rs.Primary.ID) + } + + if err != nil { + return err + } + } + return nil +} + +func testAccCodeCommitApprovalRuleAssociation_basic() string { + return ` +resource "aws_codecommit_approval_rule_association" "test" { + template_name = "test-rule" + repository_names = ["test1"] +} +` +} + +func testAccCodeCommitApprovalRuleAssociation_setup() string { + return ` +resource "aws_codecommit_approval_rule_association" "test_change" { + template_name = "test2-rule" + repository_names = ["test1"] +} +` +} + +func testAccCodeCommitApprovalRuleAssociation_withChanges() string { + return ` +resource "aws_codecommit_approval_rule_association" "test_change" { + template_name = "test2-rule" + repository_names = ["test1", "test2"] +} +` +} diff --git a/website/docs/r/codecommit_approval_rule_association.html.markdown b/website/docs/r/codecommit_approval_rule_association.html.markdown new file mode 100644 index 00000000000..f43f84727fe --- /dev/null +++ b/website/docs/r/codecommit_approval_rule_association.html.markdown @@ -0,0 +1,31 @@ +--- +subcategory: "CodeCommit" +layout: "aws" +page_title: "AWS: aws_codecommit_approval_rule_association" +description: |- + Provides a CodeCommit Approval Rule Association Resource. +--- + +# Resource: aws_codecommit_approval_rule + +Provides a CodeCommit Approval Rule Association Resource. + +~> **NOTE on CodeCommit Availability**: CodeCommit is not yet rolled out +in all regions - available regions are listed +[the AWS Docs](https://docs.aws.amazon.com/general/latest/gr/rande.html#codecommit_region). + +## Example Usage + +```hcl +resource "aws_codecommit_approval_rule_association" "example" { + template_name = "my-approval-rule" + repository_names = ["repo1", "repo2"] +} +``` + +## Argument Reference + +The following arguments are supported: + +* `template_name` - (Required) The name of the approval rule template to associate with CodeCommit repositories +* `repository_names` - (Required) A list of CodeCommit repositories to associate the approval rule template with From a9749ae86eef6dbefdb2162d00ebd4c6928396da Mon Sep 17 00:00:00 2001 From: Donovan Carthew Date: Fri, 22 May 2020 16:25:28 +0200 Subject: [PATCH 2/9] Remove unused import --- aws/resource_aws_codecommit_approval_rule_association.go | 1 - 1 file changed, 1 deletion(-) diff --git a/aws/resource_aws_codecommit_approval_rule_association.go b/aws/resource_aws_codecommit_approval_rule_association.go index 6d8dc08b1b7..dede18039db 100644 --- a/aws/resource_aws_codecommit_approval_rule_association.go +++ b/aws/resource_aws_codecommit_approval_rule_association.go @@ -7,7 +7,6 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/codecommit" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - //"github.com/hashicorp/terraform-plugin-sdk/helper/validation" ) func resourceAwsCodeCommitApprovalRuleAssociation() *schema.Resource { From ea35276c96c2539b4bf901e4a1086d2e05588c13 Mon Sep 17 00:00:00 2001 From: Donovan Carthew Date: Fri, 22 May 2020 23:53:54 +0200 Subject: [PATCH 3/9] Fix linting issues --- aws/resource_aws_codecommit_approval_rule_association.go | 9 +++------ ...urce_aws_codecommit_approval_rule_association_test.go | 7 ++++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_codecommit_approval_rule_association.go b/aws/resource_aws_codecommit_approval_rule_association.go index dede18039db..4ec6fe8e99b 100644 --- a/aws/resource_aws_codecommit_approval_rule_association.go +++ b/aws/resource_aws_codecommit_approval_rule_association.go @@ -37,9 +37,8 @@ func resourceAwsCodeCommitApprovalRuleAssociationCreate(d *schema.ResourceData, templateName := d.Get("template_name").(string) repositoryNames := d.Get("repository_names") - input := &codecommit.AssociateApprovalRuleTemplateWithRepositoryInput{} for _, repositoryName := range repositoryNames.(*schema.Set).List() { - input = &codecommit.AssociateApprovalRuleTemplateWithRepositoryInput{ + input := &codecommit.AssociateApprovalRuleTemplateWithRepositoryInput{ ApprovalRuleTemplateName: aws.String(templateName), RepositoryName: aws.String(repositoryName.(string)), } @@ -52,7 +51,7 @@ func resourceAwsCodeCommitApprovalRuleAssociationCreate(d *schema.ResourceData, } } - d.SetId(fmt.Sprintf("%s", templateName)) + d.SetId(templateName) return resourceAwsCodeCommitApprovalRuleAssociationRead(d, meta) } @@ -133,10 +132,8 @@ func resourceAwsCodeCommitApprovalRuleAssociationDelete(d *schema.ResourceData, templateName := d.Get("template_name") repositoryNames := d.Get("repository_names") - input := &codecommit.DisassociateApprovalRuleTemplateFromRepositoryInput{} - for _, repositoryName := range repositoryNames.(*schema.Set).List() { - input = &codecommit.DisassociateApprovalRuleTemplateFromRepositoryInput{ + input := &codecommit.DisassociateApprovalRuleTemplateFromRepositoryInput{ ApprovalRuleTemplateName: aws.String(templateName.(string)), RepositoryName: aws.String(repositoryName.(string)), } diff --git a/aws/resource_aws_codecommit_approval_rule_association_test.go b/aws/resource_aws_codecommit_approval_rule_association_test.go index 963fd470a11..c34e7d64060 100644 --- a/aws/resource_aws_codecommit_approval_rule_association_test.go +++ b/aws/resource_aws_codecommit_approval_rule_association_test.go @@ -115,15 +115,16 @@ func testAccCheckCodeCommitApprovalRuleAssociationDestroy(s *terraform.State) er ApprovalRuleTemplateName: aws.String(rs.Primary.ID), }) + if err != nil { + return err + } + if len(out.RepositoryNames) == 0 { return nil } else { return fmt.Errorf("Approval rule template associations still exist: %s", rs.Primary.ID) } - if err != nil { - return err - } } return nil } From f0a0a4c62188f762eb8308b6e1cc1e22156a3dfa Mon Sep 17 00:00:00 2001 From: Donovan Carthew Date: Sat, 23 May 2020 00:23:09 +0200 Subject: [PATCH 4/9] Fix website linting --- .../docs/r/codecommit_approval_rule_association.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/codecommit_approval_rule_association.html.markdown b/website/docs/r/codecommit_approval_rule_association.html.markdown index f43f84727fe..7edc4401916 100644 --- a/website/docs/r/codecommit_approval_rule_association.html.markdown +++ b/website/docs/r/codecommit_approval_rule_association.html.markdown @@ -18,8 +18,8 @@ in all regions - available regions are listed ```hcl resource "aws_codecommit_approval_rule_association" "example" { - template_name = "my-approval-rule" - repository_names = ["repo1", "repo2"] + template_name = "my-approval-rule" + repository_names = ["repo1", "repo2"] } ``` From c1b9747c872c223df396f630e662a4c28db3aa0c Mon Sep 17 00:00:00 2001 From: Donovan Carthew Date: Wed, 9 Sep 2020 21:41:23 +0200 Subject: [PATCH 5/9] Refactor for v3 --- aws/resource_aws_codecommit_approval_rule_association.go | 2 +- aws/resource_aws_codecommit_approval_rule_association_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_codecommit_approval_rule_association.go b/aws/resource_aws_codecommit_approval_rule_association.go index 4ec6fe8e99b..0f918bc0c31 100644 --- a/aws/resource_aws_codecommit_approval_rule_association.go +++ b/aws/resource_aws_codecommit_approval_rule_association.go @@ -6,7 +6,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/codecommit" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func resourceAwsCodeCommitApprovalRuleAssociation() *schema.Resource { diff --git a/aws/resource_aws_codecommit_approval_rule_association_test.go b/aws/resource_aws_codecommit_approval_rule_association_test.go index c34e7d64060..2abfbd6a05a 100644 --- a/aws/resource_aws_codecommit_approval_rule_association_test.go +++ b/aws/resource_aws_codecommit_approval_rule_association_test.go @@ -9,8 +9,8 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/codecommit" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccAWSCodeCommitApprovalRuleAssociation_basic(t *testing.T) { From ff0a9b376d509cc1e4ea2e18b26a9ec89b4f5e62 Mon Sep 17 00:00:00 2001 From: Donovan Carthew Date: Thu, 10 Sep 2020 00:10:31 +0200 Subject: [PATCH 6/9] Formatting --- ...urce_aws_codecommit_approval_rule_association_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/aws/resource_aws_codecommit_approval_rule_association_test.go b/aws/resource_aws_codecommit_approval_rule_association_test.go index 2abfbd6a05a..f4644bd19ed 100644 --- a/aws/resource_aws_codecommit_approval_rule_association_test.go +++ b/aws/resource_aws_codecommit_approval_rule_association_test.go @@ -130,8 +130,7 @@ func testAccCheckCodeCommitApprovalRuleAssociationDestroy(s *terraform.State) er } func testAccCodeCommitApprovalRuleAssociation_basic() string { - return ` -resource "aws_codecommit_approval_rule_association" "test" { + return `resource "aws_codecommit_approval_rule_association" "test" { template_name = "test-rule" repository_names = ["test1"] } @@ -139,8 +138,7 @@ resource "aws_codecommit_approval_rule_association" "test" { } func testAccCodeCommitApprovalRuleAssociation_setup() string { - return ` -resource "aws_codecommit_approval_rule_association" "test_change" { + return `resource "aws_codecommit_approval_rule_association" "test_change" { template_name = "test2-rule" repository_names = ["test1"] } @@ -148,8 +146,7 @@ resource "aws_codecommit_approval_rule_association" "test_change" { } func testAccCodeCommitApprovalRuleAssociation_withChanges() string { - return ` -resource "aws_codecommit_approval_rule_association" "test_change" { + return `resource "aws_codecommit_approval_rule_association" "test_change" { template_name = "test2-rule" repository_names = ["test1", "test2"] } From 60e28f5d73847eca2ff35b4810042876115dd0b3 Mon Sep 17 00:00:00 2001 From: Donovan Carthew Date: Thu, 10 Sep 2020 00:19:54 +0200 Subject: [PATCH 7/9] Add fmt.Sprintf to satisfy terrafmt --- ...decommit_approval_rule_association_test.go | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/aws/resource_aws_codecommit_approval_rule_association_test.go b/aws/resource_aws_codecommit_approval_rule_association_test.go index f4644bd19ed..6e19291dd39 100644 --- a/aws/resource_aws_codecommit_approval_rule_association_test.go +++ b/aws/resource_aws_codecommit_approval_rule_association_test.go @@ -130,25 +130,28 @@ func testAccCheckCodeCommitApprovalRuleAssociationDestroy(s *terraform.State) er } func testAccCodeCommitApprovalRuleAssociation_basic() string { - return `resource "aws_codecommit_approval_rule_association" "test" { - template_name = "test-rule" - repository_names = ["test1"] + return fmt.Sprintf(` +resource "aws_codecommit_approval_rule_association" "test" { + template_name = "test-rule" + repository_names = ["test1"] } -` +`) } func testAccCodeCommitApprovalRuleAssociation_setup() string { - return `resource "aws_codecommit_approval_rule_association" "test_change" { - template_name = "test2-rule" - repository_names = ["test1"] + return fmt.Sprintf(` +resource "aws_codecommit_approval_rule_association" "test_change" { + template_name = "test2-rule" + repository_names = ["test1"] } -` +`) } func testAccCodeCommitApprovalRuleAssociation_withChanges() string { - return `resource "aws_codecommit_approval_rule_association" "test_change" { - template_name = "test2-rule" - repository_names = ["test1", "test2"] + return fmt.Sprintf(` +resource "aws_codecommit_approval_rule_association" "test_change" { + template_name = "test2-rule" + repository_names = ["test1", "test2"] } -` +`) } From a0e46dc5f6a36c86ed2b11e3b83917692dec1ef1 Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Wed, 1 Dec 2021 22:39:42 -0500 Subject: [PATCH 8/9] add plan-time validation; format find file --- .../approval_rule_template_association.go | 13 ++++++++++--- internal/service/codecommit/find.go | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/service/codecommit/approval_rule_template_association.go b/internal/service/codecommit/approval_rule_template_association.go index 6b059eff71b..bce04d3ebf3 100644 --- a/internal/service/codecommit/approval_rule_template_association.go +++ b/internal/service/codecommit/approval_rule_template_association.go @@ -3,12 +3,14 @@ package codecommit import ( "fmt" "log" + "regexp" "strings" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/codecommit" "github.com/hashicorp/aws-sdk-go-base/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) @@ -24,14 +26,19 @@ func ResourceApprovalRuleTemplateAssociation() *schema.Resource { Schema: map[string]*schema.Schema{ "approval_rule_template_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 100), }, "repository_name": { Type: schema.TypeString, Required: true, ForceNew: true, + ValidateFunc: validation.All( + validation.StringLenBetween(1, 100), + validation.StringMatch(regexp.MustCompile(`[\w\.-]+`), ""), + ), }, }, } diff --git a/internal/service/codecommit/find.go b/internal/service/codecommit/find.go index 4e3289588de..0924888c3bf 100644 --- a/internal/service/codecommit/find.go +++ b/internal/service/codecommit/find.go @@ -2,12 +2,14 @@ package codecommit import ( "fmt" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/codecommit" "github.com/hashicorp/aws-sdk-go-base/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) +// FindApprovalRuleTemplateAssociation validates that an approval rule template has the named associated repository func FindApprovalRuleTemplateAssociation(conn *codecommit.CodeCommit, approvalRuleTemplateName, repositoryName string) error { input := &codecommit.ListRepositoriesForApprovalRuleTemplateInput{ ApprovalRuleTemplateName: aws.String(approvalRuleTemplateName), From a65968cb79b888e6810fe505af179c5ce408be36 Mon Sep 17 00:00:00 2001 From: angie pinilla Date: Wed, 1 Dec 2021 22:58:51 -0500 Subject: [PATCH 9/9] Update internal/service/codecommit/approval_rule_template_association.go --- .../service/codecommit/approval_rule_template_association.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/codecommit/approval_rule_template_association.go b/internal/service/codecommit/approval_rule_template_association.go index bce04d3ebf3..cb726de7c11 100644 --- a/internal/service/codecommit/approval_rule_template_association.go +++ b/internal/service/codecommit/approval_rule_template_association.go @@ -84,7 +84,7 @@ func resourceApprovalRuleTemplateAssociationRead(d *schema.ResourceData, meta in } if err != nil { - return fmt.Errorf("error reading Approval Rule Template Association (%s): %w", d.Id(), err) + return fmt.Errorf("error reading CodeCommit Approval Rule Template Association (%s): %w", d.Id(), err) } d.Set("approval_rule_template_name", approvalRuleTemplateName)