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

d/codeartifact_repository_endpoint - new data source #15566

Merged
Merged
Show file tree
Hide file tree
Changes from all 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_codeartifact_repository_endpoint.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/codeartifact"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceAwsCodeArtifactRepositoryEndpoint() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsCodeArtifactRepositoryEndpointRead,

Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
},
"repository": {
Type: schema.TypeString,
Required: true,
},
"format": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(codeartifact.PackageFormat_Values(), false),
},
"domain_owner": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateAwsAccountId,
},
"repository_endpoint": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsCodeArtifactRepositoryEndpointRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codeartifactconn
domainOwner := meta.(*AWSClient).accountid
domain := d.Get("domain").(string)
repo := d.Get("repository").(string)
format := d.Get("format").(string)
params := &codeartifact.GetRepositoryEndpointInput{
Domain: aws.String(domain),
Repository: aws.String(repo),
Format: aws.String(format),
}

if v, ok := d.GetOk("domain_owner"); ok {
params.DomainOwner = aws.String(v.(string))
domainOwner = v.(string)
}

log.Printf("[DEBUG] Getting CodeArtifact Repository Endpoint")
out, err := conn.GetRepositoryEndpoint(params)
if err != nil {
return fmt.Errorf("error getting CodeArtifact Repository Endpoint: %w", err)
}
log.Printf("[DEBUG] CodeArtifact Repository Endpoint: %#v", out)

d.SetId(fmt.Sprintf("%s:%s:%s:%s", domainOwner, domain, repo, format))
d.Set("repository_endpoint", out.RepositoryEndpoint)
d.Set("domain_owner", domainOwner)

return nil
}
89 changes: 89 additions & 0 deletions aws/data_source_aws_codeartifact_repository_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAWSCodeArtifactRepositoryEndpointDataSource_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_codeartifact_repository_endpoint.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAWSCodeArtifactRepositoryEndpointBasicConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "repository_endpoint"),
testAccCheckResourceAttrAccountID(dataSourceName, "domain_owner"),
),
},
},
})
}

func TestAccAWSCodeArtifactRepositoryEndpointDataSource_owner(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_codeartifact_repository_endpoint.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAWSCodeArtifactRepositoryEndpointOwnerConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "repository_endpoint"),
testAccCheckResourceAttrAccountID(dataSourceName, "domain_owner"),
),
},
},
})
}

func testAccCheckAWSCodeArtifactRepositoryEndpointBaseConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {
description = %[1]q
deletion_window_in_days = 7
}

resource "aws_codeartifact_domain" "test" {
domain = %[1]q
encryption_key = aws_kms_key.test.arn
}

resource "aws_codeartifact_repository" "test" {
repository = %[1]q
domain = aws_codeartifact_domain.test.domain
}
`, rName)
}

func testAccCheckAWSCodeArtifactRepositoryEndpointBasicConfig(rName string) string {
return testAccCheckAWSCodeArtifactRepositoryEndpointBaseConfig(rName) +
fmt.Sprintf(`
data "aws_codeartifact_repository_endpoint" "test" {
domain = aws_codeartifact_domain.test.domain
repository = aws_codeartifact_repository.test.repository
format = "npm"
}
`)
}

func testAccCheckAWSCodeArtifactRepositoryEndpointOwnerConfig(rName string) string {
return testAccCheckAWSCodeArtifactRepositoryEndpointBaseConfig(rName) +
fmt.Sprintf(`
data "aws_codeartifact_repository_endpoint" "test" {
domain = aws_codeartifact_domain.test.domain
repository = aws_codeartifact_repository.test.repository
domain_owner = aws_codeartifact_domain.test.owner
format = "npm"
}
`)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func Provider() *schema.Provider {
"aws_cloudtrail_service_account": dataSourceAwsCloudTrailServiceAccount(),
"aws_cloudwatch_log_group": dataSourceAwsCloudwatchLogGroup(),
"aws_codeartifact_authorization_token": dataSourceAwsCodeArtifactAuthorizationToken(),
"aws_codeartifact_repository_endpoint": dataSourceAwsCodeArtifactRepositoryEndpoint(),
"aws_cognito_user_pools": dataSourceAwsCognitoUserPools(),
"aws_codecommit_repository": dataSourceAwsCodeCommitRepository(),
"aws_cur_report_definition": dataSourceAwsCurReportDefinition(),
Expand Down
36 changes: 36 additions & 0 deletions website/docs/d/codeartifact_repository_endpoint.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
subcategory: "CodeArtifact"
layout: "aws"
page_title: "AWS: aws_codeartifact_repository_endpoint"
description: |-
Provides details about a CodeArtifact Repository Endpoint
---

# Data Source: aws_codeartifact_repository_endpoint

The CodeArtifact Repository Endpoint data source returns the endpoint of a repository for a specific package format.

## Example Usage

```hcl
data "aws_codeartifact_repository_endpoint" "test" {
domain = aws_codeartifact_domain.test.domain
repository = aws_codeartifact_repository.test.repository
format = "npm"
}
```

## Argument Reference

The following arguments are supported:

* `domain` - (Required) The name of the domain that contains the repository.
* `repository` - (Required) The name of the repository.
* `format` - (Required) Which endpoint of a repository to return. A repository has one endpoint for each package format: `npm`, `pypi`, and `maven`.
* `domain_owner` - (Optional) The account number of the AWS account that owns the domain.

## Attributes Reference

In addition to the argument above, the following attributes are exported:

* `repository_endpoint` - The URL of the returned endpoint.