Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #4785: New Datasource aws_codecommit_repository #4934

Merged
merged 4 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions aws/data_source_aws_codecommit_repository.go
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, data sources should return a direct error instead of silently failing returning no results and likely returning a resource data.XXX.XXX not found for error later on. 😄

} else {
return fmt.Errorf("Error reading CodeCommit Repository: %s", err.Error())
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent potential panics, we should perform a nil check for out.RepositoryMetadata before referencing it below.

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
}
45 changes: 45 additions & 0 deletions aws/data_source_aws_codecommit_repository_test.go
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we switch these to use resource.TestCheckResourceAttrPair() where possible? This helps us ensure that the data source definitely matched the correct resource. Thanks! 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: I will update the rest of the checks to resource.TestCheckResourceAttrPair() post merge.

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)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
<li<%= sidebar_current("docs-aws-datasource-cloudwatch-log-group") %>>
<a href="/docs/providers/aws/d/cloudwatch_log_group.html">aws_cloudwatch_log_group</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-codecommit-repository") %>>
<a href="/docs/providers/aws/d/codecommit_repository.html">aws_cloudwatch_log_group</a>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copypasta typo 🍝 here 😉

</li>
<li<%= sidebar_current("docs-aws-datasource-cognito-user-pools") %>>
<a href="/docs/providers/aws/d/cognito_user_pools.html">aws_cognito_user_pools</a>
</li>
Expand Down
34 changes: 34 additions & 0 deletions website/docs/d/codecommit_repository.html.markdown
Original file line number Diff line number Diff line change
@@ -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.