-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Changes from 3 commits
582c31d
540b68c
bb44ef1
00ba359
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} else { | ||
return fmt.Errorf("Error reading CodeCommit Repository: %s", err.Error()) | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent potential panics, we should perform a |
||
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 | ||
} |
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we switch these to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nitpick: I will update the rest of the checks to |
||
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
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. |
There was a problem hiding this comment.
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. 😄