From 582c31d19e57f26da28bf0d9ff87421f2daad81f Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 21 Jun 2018 22:14:47 +0400 Subject: [PATCH 1/4] issue #4785 New datasource: aws_codecommit_repository --- aws/data_source_aws_codecommit_repository.go | 74 ++++++++++++++++++++ aws/provider.go | 1 + 2 files changed, 75 insertions(+) create mode 100644 aws/data_source_aws_codecommit_repository.go diff --git a/aws/data_source_aws_codecommit_repository.go b/aws/data_source_aws_codecommit_repository.go new file mode 100644 index 00000000000..d804d8119ad --- /dev/null +++ b/aws/data_source_aws_codecommit_repository.go @@ -0,0 +1,74 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/codecommit" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsCodeCommitRepository() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsCodeCommitRepositoryRead, + + Schema: map[string]*schema.Schema{ + "repository_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateMaxLength(100), + }, + + "arn": { + Type: schema.TypeString, + Computed: true, + }, + + "repository_id": { + Type: schema.TypeString, + Computed: true, + }, + + "clone_url_http": { + Type: schema.TypeString, + Computed: true, + }, + + "clone_url_ssh": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsCodeCommitRepositoryRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codecommitconn + + repositoryName := d.Get("repository_name").(string) + input := &codecommit.GetRepositoryInput{ + RepositoryName: aws.String(repositoryName), + } + + out, err := conn.GetRepository(input) + if err != nil { + if isAWSErr(err, codecommit.ErrCodeRepositoryDoesNotExistException, "") { + log.Printf("[WARN] CodeCommit Repository (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } else { + return fmt.Errorf("Error reading CodeCommit Repository: %s", err.Error()) + } + } + + d.SetId(aws.StringValue(out.RepositoryMetadata.RepositoryName)) + d.Set("arn", out.RepositoryMetadata.Arn) + d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) + d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh) + d.Set("repository_name", out.RepositoryMetadata.RepositoryName) + d.Set("repository_id", out.RepositoryMetadata.RepositoryId) + + return nil +} diff --git a/aws/provider.go b/aws/provider.go index 1939f4dd146..a27aa307ed4 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -179,6 +179,7 @@ func Provider() terraform.ResourceProvider { "aws_cloudtrail_service_account": dataSourceAwsCloudTrailServiceAccount(), "aws_cloudwatch_log_group": dataSourceAwsCloudwatchLogGroup(), "aws_cognito_user_pools": dataSourceAwsCognitoUserPools(), + "aws_codecommit_repository": dataSourceAwsCodeCommitRepository(), "aws_db_instance": dataSourceAwsDbInstance(), "aws_db_snapshot": dataSourceAwsDbSnapshot(), "aws_dynamodb_table": dataSourceAwsDynamoDbTable(), From 540b68ca23cd1afbf95628bf8bb7b30e9cc4375e Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 21 Jun 2018 22:58:57 +0400 Subject: [PATCH 2/4] add acceptance test --- ...a_source_aws_codecommit_repository_test.go | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 aws/data_source_aws_codecommit_repository_test.go diff --git a/aws/data_source_aws_codecommit_repository_test.go b/aws/data_source_aws_codecommit_repository_test.go new file mode 100644 index 00000000000..0b5636729d5 --- /dev/null +++ b/aws/data_source_aws_codecommit_repository_test.go @@ -0,0 +1,45 @@ +package aws + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSCodeCommitRepositoryDataSource_basic(t *testing.T) { + rName := fmt.Sprintf("tf-acctest-%d", acctest.RandInt()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckAwsCodeCommitRepositoryDataSourceConfig(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_codecommit_repository.default", "repository_name", rName), + resource.TestMatchResourceAttr("data.aws_codecommit_repository.default", "arn", + regexp.MustCompile(fmt.Sprintf("^arn:aws:codecommit:[^:]+:\\d{12}:%s", rName))), + resource.TestMatchResourceAttr("data.aws_codecommit_repository.default", "clone_url_http", + regexp.MustCompile(fmt.Sprintf("^https://git-codecommit.[^:]+.amazonaws.com/v1/repos/%s", rName))), + resource.TestMatchResourceAttr("data.aws_codecommit_repository.default", "clone_url_ssh", + regexp.MustCompile(fmt.Sprintf("^ssh://git-codecommit.[^:]+.amazonaws.com/v1/repos/%s", rName))), + ), + }, + }, + }) +} + +func testAccCheckAwsCodeCommitRepositoryDataSourceConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_codecommit_repository" "default" { + repository_name = "%s" +} + +data "aws_codecommit_repository" "default" { + repository_name = "${aws_codecommit_repository.default.repository_name}" +} +`, rName) +} From bb44ef132e6c1dd3a58ceecce45b5743aa7e9392 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 21 Jun 2018 23:10:17 +0400 Subject: [PATCH 3/4] add documentation for datasource codecommit repository --- website/aws.erb | 3 ++ .../d/codecommit_repository.html.markdown | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 website/docs/d/codecommit_repository.html.markdown diff --git a/website/aws.erb b/website/aws.erb index 0e07c6a0f18..d1100542021 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -103,6 +103,9 @@ > aws_cloudwatch_log_group + > + aws_cloudwatch_log_group + > aws_cognito_user_pools diff --git a/website/docs/d/codecommit_repository.html.markdown b/website/docs/d/codecommit_repository.html.markdown new file mode 100644 index 00000000000..b9442dc0258 --- /dev/null +++ b/website/docs/d/codecommit_repository.html.markdown @@ -0,0 +1,34 @@ +--- +layout: "aws" +page_title: "AWS: aws_codecommit_repository" +sidebar_current: "docs-aws-datasource-codecommit-repository" +description: |- + Provides details about CodeCommit Repository. +--- + +# Data Source: aws_codecommit_repository + +The CodeCommit Repository data source allows the ARN, Repository ID, Repository URL for HTTP and Repository URL for SSH to be retrieved for an CodeCommit repository. + +## Example Usage + +```hcl +data "aws_codecommit_repository" "test" { + repository_name = "MyTestRepository" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `repository_name` - (Required) The name for the repository. This needs to be less than 100 characters. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `repository_id` - The ID of the repository +* `arn` - The ARN of the repository +* `clone_url_http` - The URL to use for cloning the repository over HTTPS. +* `clone_url_ssh` - The URL to use for cloning the repository over SSH. \ No newline at end of file From 00ba359a1e80782741f7a44836ae832ab610f7fa Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Fri, 22 Jun 2018 01:35:46 +0400 Subject: [PATCH 4/4] corrections based on feedback --- aws/data_source_aws_codecommit_repository.go | 6 +++++- aws/data_source_aws_codecommit_repository_test.go | 3 ++- website/aws.erb | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/aws/data_source_aws_codecommit_repository.go b/aws/data_source_aws_codecommit_repository.go index d804d8119ad..163a4b2e3b7 100644 --- a/aws/data_source_aws_codecommit_repository.go +++ b/aws/data_source_aws_codecommit_repository.go @@ -57,12 +57,16 @@ func dataSourceAwsCodeCommitRepositoryRead(d *schema.ResourceData, meta interfac if isAWSErr(err, codecommit.ErrCodeRepositoryDoesNotExistException, "") { log.Printf("[WARN] CodeCommit Repository (%s) not found, removing from state", d.Id()) d.SetId("") - return nil + return fmt.Errorf("Resource codecommit repository not found for %s", repositoryName) } else { return fmt.Errorf("Error reading CodeCommit Repository: %s", err.Error()) } } + if out.RepositoryMetadata == nil { + return fmt.Errorf("no matches found for repository name: %s", repositoryName) + } + d.SetId(aws.StringValue(out.RepositoryMetadata.RepositoryName)) d.Set("arn", out.RepositoryMetadata.Arn) d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) diff --git a/aws/data_source_aws_codecommit_repository_test.go b/aws/data_source_aws_codecommit_repository_test.go index 0b5636729d5..90cbdcfe8b7 100644 --- a/aws/data_source_aws_codecommit_repository_test.go +++ b/aws/data_source_aws_codecommit_repository_test.go @@ -19,7 +19,8 @@ func TestAccAWSCodeCommitRepositoryDataSource_basic(t *testing.T) { { Config: testAccCheckAwsCodeCommitRepositoryDataSourceConfig(rName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_codecommit_repository.default", "repository_name", rName), + resource.TestCheckResourceAttrPair("data.aws_codecommit_repository.default", "repository_name", + "aws_codecommit_repository.default", "repository_name"), resource.TestMatchResourceAttr("data.aws_codecommit_repository.default", "arn", regexp.MustCompile(fmt.Sprintf("^arn:aws:codecommit:[^:]+:\\d{12}:%s", rName))), resource.TestMatchResourceAttr("data.aws_codecommit_repository.default", "clone_url_http", diff --git a/website/aws.erb b/website/aws.erb index d1100542021..fe18e6d8e60 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -104,7 +104,7 @@ aws_cloudwatch_log_group > - aws_cloudwatch_log_group + aws_codecommit_repository > aws_cognito_user_pools