From 494157785f5385b376c4be98f8d67b2505cd07af Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Fri, 9 Oct 2020 00:12:24 +0300 Subject: [PATCH 1/3] add codeartifact repo endpoint data source --- ...ce_aws_codeartifact_repository_endpoint.go | 74 +++++++++++++++ ...s_codeartifact_repository_endpoint_test.go | 89 +++++++++++++++++++ aws/provider.go | 1 + 3 files changed, 164 insertions(+) create mode 100644 aws/data_source_aws_codeartifact_repository_endpoint.go create mode 100644 aws/data_source_aws_codeartifact_repository_endpoint_test.go diff --git a/aws/data_source_aws_codeartifact_repository_endpoint.go b/aws/data_source_aws_codeartifact_repository_endpoint.go new file mode 100644 index 00000000000..330b0a9a34c --- /dev/null +++ b/aws/data_source_aws_codeartifact_repository_endpoint.go @@ -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 +} diff --git a/aws/data_source_aws_codeartifact_repository_endpoint_test.go b/aws/data_source_aws_codeartifact_repository_endpoint_test.go new file mode 100644 index 00000000000..906f80f671d --- /dev/null +++ b/aws/data_source_aws_codeartifact_repository_endpoint_test.go @@ -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" +} +`) +} diff --git a/aws/provider.go b/aws/provider.go index 7972e736c8c..e74e6130eee 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), From f8813ab8e5ec58f6c44533439e5d15ecdd0aee05 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Fri, 9 Oct 2020 00:30:03 +0300 Subject: [PATCH 2/3] docs --- ...artifact_repository_endpoint.html.markdown | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 website/docs/d/codeartifact_repository_endpoint.html.markdown diff --git a/website/docs/d/codeartifact_repository_endpoint.html.markdown b/website/docs/d/codeartifact_repository_endpoint.html.markdown new file mode 100644 index 00000000000..6a1e47c94f3 --- /dev/null +++ b/website/docs/d/codeartifact_repository_endpoint.html.markdown @@ -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. \ No newline at end of file From 4d6d3a834c3324a8945aa8519eefdced3c672e41 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Fri, 9 Oct 2020 00:30:57 +0300 Subject: [PATCH 3/3] lint --- aws/data_source_aws_codeartifact_repository_endpoint_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/data_source_aws_codeartifact_repository_endpoint_test.go b/aws/data_source_aws_codeartifact_repository_endpoint_test.go index 906f80f671d..e3155583f5f 100644 --- a/aws/data_source_aws_codeartifact_repository_endpoint_test.go +++ b/aws/data_source_aws_codeartifact_repository_endpoint_test.go @@ -59,8 +59,8 @@ resource "aws_codeartifact_domain" "test" { } resource "aws_codeartifact_repository" "test" { - repository = %[1]q - domain = aws_codeartifact_domain.test.domain + repository = %[1]q + domain = aws_codeartifact_domain.test.domain } `, rName) }