diff --git a/aws/data_source_aws_codecommit_repository.go b/aws/data_source_aws_codecommit_repository.go new file mode 100644 index 00000000000..163a4b2e3b7 --- /dev/null +++ b/aws/data_source_aws_codecommit_repository.go @@ -0,0 +1,78 @@ +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 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) + 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/data_source_aws_codecommit_repository_test.go b/aws/data_source_aws_codecommit_repository_test.go new file mode 100644 index 00000000000..90cbdcfe8b7 --- /dev/null +++ b/aws/data_source_aws_codecommit_repository_test.go @@ -0,0 +1,46 @@ +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.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", + 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) +} 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(), diff --git a/website/aws.erb b/website/aws.erb index 0e07c6a0f18..fe18e6d8e60 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -103,6 +103,9 @@