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

Support CodeStar Connection data source lookup by name #19262

Prev Previous commit
Next Next commit
r/aws_codestarconnections_host: Tidy up.
Acceptance test output:

% make testacc TESTS=TestAccCodeStarConnectionsHost_basic PKG=codestarconnections
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/codestarconnections/... -v -count 1 -parallel 20 -run='TestAccCodeStarConnectionsHost_basic'  -timeout 180m
=== RUN   TestAccCodeStarConnectionsHost_basic
=== PAUSE TestAccCodeStarConnectionsHost_basic
=== CONT  TestAccCodeStarConnectionsHost_basic
--- PASS: TestAccCodeStarConnectionsHost_basic (26.92s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/codestarconnections	35.254s
  • Loading branch information
ewbankkit committed May 6, 2022
commit 304db889d252972eb29a6289258e6aa838e70491
7 changes: 4 additions & 3 deletions internal/service/codestarconnections/connection.go
Original file line number Diff line number Diff line change
@@ -86,13 +86,14 @@ func resourceConnectionCreate(d *schema.ResourceData, meta interface{}) error {
input.Tags = Tags(tags.IgnoreAWS())
}

resp, err := conn.CreateConnection(input)
log.Printf("[DEBUG] Creating CodeStar Connections Connection: %s", input)
output, err := conn.CreateConnection(input)

if err != nil {
return fmt.Errorf("creating CodeStar Connections Connection (%s): %w", name, err)
}

d.SetId(aws.StringValue(resp.ConnectionArn))
d.SetId(aws.StringValue(output.ConnectionArn))

return resourceConnectionRead(d, meta)
}
@@ -159,7 +160,7 @@ func resourceConnectionUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceConnectionDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).CodeStarConnectionsConn

log.Printf("[INFO] Deleting CodeStar Connections Connection: %s", d.Id())
log.Printf("[DEBUG] Deleting CodeStar Connections Connection: %s", d.Id())
_, err := conn.DeleteConnection(&codestarconnections.DeleteConnectionInput{
ConnectionArn: aws.String(d.Id()),
})
25 changes: 25 additions & 0 deletions internal/service/codestarconnections/find.go
Original file line number Diff line number Diff line change
@@ -32,3 +32,28 @@ func FindConnectionByARN(conn *codestarconnections.CodeStarConnections, arn stri

return output.Connection, nil
}

func FindHostByARN(conn *codestarconnections.CodeStarConnections, arn string) (*codestarconnections.GetHostOutput, error) {
input := &codestarconnections.GetHostInput{
HostArn: aws.String(arn),
}

output, err := conn.GetHost(input)

if tfawserr.ErrCodeEquals(err, codestarconnections.ErrCodeResourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}
115 changes: 81 additions & 34 deletions internal/service/codestarconnections/host.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package codestarconnections

import (
"errors"
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/codestarconnections"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func ResourceHost() *schema.Resource {
@@ -19,10 +23,17 @@ func ResourceHost() *schema.Resource {
Read: resourceHostRead,
Update: resourceHostUpdate,
Delete: resourceHostDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
@@ -83,23 +94,25 @@ func ResourceHost() *schema.Resource {
func resourceHostCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).CodeStarConnectionsConn

params := &codestarconnections.CreateHostInput{
Name: aws.String(d.Get("name").(string)),
name := d.Get("name").(string)
input := &codestarconnections.CreateHostInput{
Name: aws.String(name),
ProviderEndpoint: aws.String(d.Get("provider_endpoint").(string)),
ProviderType: aws.String(d.Get("provider_type").(string)),
VpcConfiguration: expandHostVPCConfiguration(d.Get("vpc_configuration").([]interface{})),
}

resp, err := conn.CreateHost(params)
log.Printf("[DEBUG] Creating CodeStar Connections Host: %s", input)
output, err := conn.CreateHost(input)

if err != nil {
return fmt.Errorf("error creating CodeStar Connections Host: %w", err)
return fmt.Errorf("creating CodeStar Connections Host (%s): %w", name, err)
}

d.SetId(aws.StringValue(resp.HostArn))
d.SetId(aws.StringValue(output.HostArn))

if _, err := waitHostPendingOrAvailable(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for CodeStar Connections Host (%s) creation: %w", d.Id(), err)
if _, err := waitHostPendingOrAvailable(conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return fmt.Errorf("waiting for CodeStar Connections Host (%s) create: %w", d.Id(), err)
}

return resourceHostRead(d, meta)
@@ -108,32 +121,24 @@ func resourceHostCreate(d *schema.ResourceData, meta interface{}) error {
func resourceHostRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).CodeStarConnectionsConn

input := &codestarconnections.GetHostInput{
HostArn: aws.String(d.Id()),
}

resp, err := conn.GetHost(input)
output, err := FindHostByARN(conn, d.Id())

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, codestarconnections.ErrCodeResourceNotFoundException) {
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] CodeStar Connections Host (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading CodeStar Connections Host (%s): %w", d.Id(), err)
}

if resp == nil {
return fmt.Errorf("error reading CodeStar Connections Host (%s): empty response", d.Id())
return fmt.Errorf("reading CodeStar Connections Host (%s): %w", d.Id(), err)
}

d.Set("arn", d.Id())
d.Set("name", resp.Name)
d.Set("provider_endpoint", resp.ProviderEndpoint)
d.Set("provider_type", resp.ProviderType)
d.Set("status", resp.Status)
d.Set("vpc_configuration", flattenHostVPCConfiguration(resp.VpcConfiguration))
d.Set("name", output.Name)
d.Set("provider_endpoint", output.ProviderEndpoint)
d.Set("provider_type", output.ProviderType)
d.Set("status", output.Status)
d.Set("vpc_configuration", flattenHostVPCConfiguration(output.VpcConfiguration))

return nil
}
@@ -142,20 +147,20 @@ func resourceHostUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).CodeStarConnectionsConn

if d.HasChanges("provider_endpoint", "vpc_configuration") {
input := codestarconnections.UpdateHostInput{
HostArn: aws.String(d.Get("name").(string)),
input := &codestarconnections.UpdateHostInput{
HostArn: aws.String(d.Id()),
ProviderEndpoint: aws.String(d.Get("provider_endpoint").(string)),
VpcConfiguration: expandHostVPCConfiguration(d.Get("vpc_configuration").([]interface{})),
}

_, err := conn.UpdateHost(&input)
_, err := conn.UpdateHost(input)

if err != nil {
return fmt.Errorf("error updating CodeStar Connections Host (%s): %w", d.Id(), err)
return fmt.Errorf("updating CodeStar Connections Host (%s): %w", d.Id(), err)
}

if _, err := waitHostPendingOrAvailable(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for CodeStar Connections Host (%s) update: %w", d.Id(), err)
if _, err := waitHostPendingOrAvailable(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("waiting for CodeStar Connections Host (%s) update: %w", d.Id(), err)
}
}

@@ -165,18 +170,17 @@ func resourceHostUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceHostDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).CodeStarConnectionsConn

input := &codestarconnections.DeleteHostInput{
log.Printf("[DEBUG] Deleting CodeStar Connections Host: %s", d.Id())
_, err := conn.DeleteHost(&codestarconnections.DeleteHostInput{
HostArn: aws.String(d.Id()),
}

_, err := conn.DeleteHost(input)
})

if tfawserr.ErrCodeEquals(err, codestarconnections.ErrCodeResourceNotFoundException) {
return nil
}

if err != nil {
return fmt.Errorf("error deleting CodeStar Connections Host (%s): %w", d.Id(), err)
return fmt.Errorf("deleting CodeStar Connections Host (%s): %w", d.Id(), err)
}

return nil
@@ -219,3 +223,46 @@ func flattenHostVPCConfiguration(vpcConfig *codestarconnections.VpcConfiguration

return []interface{}{m}
}

func statusHost(conn *codestarconnections.CodeStarConnections, arn string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindHostByARN(conn, arn)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, aws.StringValue(output.Status), nil
}
}

const (
hostStatusAvailable = "AVAILABLE"
hostStatusPending = "PENDING"
hostStatusVPCConfigDeleting = "VPC_CONFIG_DELETING"
hostStatusVPCConfigFailedInitialization = "VPC_CONFIG_FAILED_INITIALIZATION"
hostStatusVPCConfigInitializing = "VPC_CONFIG_INITIALIZING"
)

func waitHostPendingOrAvailable(conn *codestarconnections.CodeStarConnections, arn string, timeout time.Duration) (*codestarconnections.Host, error) { //nolint:unparam
stateConf := &resource.StateChangeConf{
Pending: []string{hostStatusVPCConfigInitializing},
Target: []string{hostStatusAvailable, hostStatusPending},
Refresh: statusHost(conn, arn),
Timeout: timeout,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*codestarconnections.Host); ok {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.StatusMessage)))

return output, err
}

return nil, err
}
35 changes: 19 additions & 16 deletions internal/service/codestarconnections/host_test.go
Original file line number Diff line number Diff line change
@@ -6,15 +6,14 @@ import (
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/codestarconnections"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfcodestarconnections "github.com/hashicorp/terraform-provider-aws/internal/service/codestarconnections"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func TestAccCodeStarConnectionsHost_basic(t *testing.T) {
@@ -115,19 +114,18 @@ func testAccCheckHostExists(n string, v *codestarconnections.GetHostOutput) reso
}

if rs.Primary.ID == "" {
return errors.New("No CodeStar host ID is set")
return errors.New("No CodeStar Connections Host ID is set")
}

conn := acctest.Provider.Meta().(*conns.AWSClient).CodeStarConnectionsConn

resp, err := conn.GetHost(&codestarconnections.GetHostInput{
HostArn: aws.String(rs.Primary.ID),
})
output, err := tfcodestarconnections.FindHostByARN(conn, rs.Primary.ID)

if err != nil {
return err
}

*v = *resp
*v = *output

return nil
}
@@ -137,16 +135,21 @@ func testAccCheckHostDestroy(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).CodeStarConnectionsConn

for _, rs := range s.RootModule().Resources {
switch rs.Type {
case "aws_codestarconnections_host":
_, err := conn.DeleteHost(&codestarconnections.DeleteHostInput{
HostArn: aws.String(rs.Primary.ID),
})

if err != nil && !tfawserr.ErrCodeEquals(err, codestarconnections.ErrCodeResourceNotFoundException) {
return err
}
if rs.Type != "aws_codestarconnections_host" {
continue
}

_, err := tfcodestarconnections.FindHostByARN(conn, rs.Primary.ID)

if tfresource.NotFound(err) {
continue
}

if err != nil {
return err
}

return fmt.Errorf("CodeStar Connections Host %s still exists", rs.Primary.ID)
}

return nil
28 changes: 0 additions & 28 deletions internal/service/codestarconnections/status.go

This file was deleted.

Loading