Skip to content

Commit

Permalink
Post PR review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-gajda committed Oct 18, 2018
1 parent c730291 commit 896adcb
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 403 deletions.
8 changes: 4 additions & 4 deletions aws/data_source_aws_cloudhsm2_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func dataSourceCloudHsm2Cluster() *schema.Resource {
},

"cluster_certificates": {
Type: schema.TypeSet,
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -113,9 +114,8 @@ func dataSourceCloudHsm2ClusterRead(d *schema.ResourceData, meta interface{}) er
d.Set("vpc_id", cluster.VpcId)
d.Set("security_group_id", cluster.SecurityGroup)
d.Set("cluster_state", cluster.State)
certs := readCloudHsm2ClusterCertificates(cluster)
if err := d.Set("cluster_certificates", certs); err != nil {
return err
if err := d.Set("cluster_certificates", readCloudHsm2ClusterCertificates(cluster)); err != nil {
return fmt.Errorf("error setting cluster_certificates: %s", err)
}

var subnets []string
Expand Down
580 changes: 290 additions & 290 deletions aws/provider.go

Large diffs are not rendered by default.

73 changes: 26 additions & 47 deletions aws/resource_aws_cloudhsm2_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"github.com/hashicorp/terraform/helper/validation"
"log"
"time"

Expand All @@ -19,7 +20,7 @@ func resourceAwsCloudHsm2Cluster() *schema.Resource {
Update: resourceAwsCloudHsm2ClusterUpdate,
Delete: resourceAwsCloudHsm2ClusterDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsCloudHsm2ClusterImport,
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Expand All @@ -29,19 +30,18 @@ func resourceAwsCloudHsm2Cluster() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"backup_identifier": {
"source_backup_identifier": {
Type: schema.TypeString,
Computed: false,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"hsm_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateCloudHsm2HsmType,
ValidateFunc: validation.StringInSlice([]string{"hsm1.medium"}, false),
},

"subnet_ids": {
Expand All @@ -63,7 +63,8 @@ func resourceAwsCloudHsm2Cluster() *schema.Resource {
},

"cluster_certificates": {
Type: schema.TypeSet,
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -106,15 +107,7 @@ func resourceAwsCloudHsm2Cluster() *schema.Resource {
}
}

func resourceAwsCloudHsm2ClusterImport(
d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
d.Set("cluster_id", d.Id())
return []*schema.ResourceData{d}, nil
}

func describeCloudHsm2Cluster(clusterId string, meta interface{}) (*cloudhsmv2.Cluster, error) {
conn := meta.(*AWSClient).cloudhsmv2conn

func describeCloudHsm2Cluster(conn *cloudhsmv2.CloudHSMV2, clusterId string) (*cloudhsmv2.Cluster, error) {
filters := []*string{&clusterId}
result := int64(1)
out, err := conn.DescribeClusters(&cloudhsmv2.DescribeClustersInput{
Expand All @@ -133,43 +126,37 @@ func describeCloudHsm2Cluster(clusterId string, meta interface{}) (*cloudhsmv2.C
for _, c := range out.Clusters {
if aws.StringValue(c.ClusterId) == clusterId {
cluster = c
break
}
}
return cluster, nil
}

func resourceAwsCloudHsm2ClusterRefreshFunc(
d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
func resourceAwsCloudHsm2ClusterRefreshFunc(conn *cloudhsmv2.CloudHSMV2, clusterId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
cluster, err := describeCloudHsm2Cluster(d.Id(), meta)
cluster, err := describeCloudHsm2Cluster(conn, clusterId)

if cluster == nil {
return 42, "destroyed", nil
}

if cluster.State != nil {
log.Printf("[DEBUG] CloudHSMv2 Cluster status (%s): %s", d.Id(), *cluster.State)
log.Printf("[DEBUG] CloudHSMv2 Cluster status (%s): %s", clusterId, *cluster.State)
}

return cluster, *cluster.State, err
return cluster, aws.StringValue(cluster.State), err
}
}

func resourceAwsCloudHsm2ClusterCreate(d *schema.ResourceData, meta interface{}) error {
cloudhsm2 := meta.(*AWSClient).cloudhsmv2conn

subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
subnetIds := make([]*string, subnetIdsSet.Len())
for i, subnetId := range subnetIdsSet.List() {
subnetIds[i] = aws.String(subnetId.(string))
}

input := &cloudhsmv2.CreateClusterInput{
HsmType: aws.String(d.Get("hsm_type").(string)),
SubnetIds: subnetIds,
SubnetIds: expandStringSet(d.Get("subnet_ids").(*schema.Set)),
}

backupId := d.Get("backup_identifier").(string)
backupId := d.Get("source_backup_identifier").(string)
if len(backupId) != 0 {
input.SourceBackupId = aws.String(backupId)
}
Expand All @@ -192,7 +179,7 @@ func resourceAwsCloudHsm2ClusterCreate(d *schema.ResourceData, meta interface{})
})

if err != nil {
return err
return fmt.Errorf("error creating CloudHSMv2 Cluster: %s", err)
}

d.SetId(aws.StringValue(output.Cluster.ClusterId))
Expand All @@ -207,7 +194,7 @@ func resourceAwsCloudHsm2ClusterCreate(d *schema.ResourceData, meta interface{})
stateConf := &resource.StateChangeConf{
Pending: []string{cloudhsmv2.ClusterStateCreateInProgress, cloudhsmv2.ClusterStateInitializeInProgress},
Target: []string{targetState},
Refresh: resourceAwsCloudHsm2ClusterRefreshFunc(d, meta),
Refresh: resourceAwsCloudHsm2ClusterRefreshFunc(cloudhsm2, d.Id()),
Timeout: d.Timeout(schema.TimeoutCreate),
MinTimeout: 30 * time.Second,
Delay: 30 * time.Second,
Expand All @@ -232,7 +219,7 @@ func resourceAwsCloudHsm2ClusterCreate(d *schema.ResourceData, meta interface{})

func resourceAwsCloudHsm2ClusterRead(d *schema.ResourceData, meta interface{}) error {

cluster, err := describeCloudHsm2Cluster(d.Id(), meta)
cluster, err := describeCloudHsm2Cluster(meta.(*AWSClient).cloudhsmv2conn, d.Id())

if cluster == nil {
log.Printf("[WARN] CloudHSMv2 Cluster (%s) not found", d.Id())
Expand All @@ -246,16 +233,18 @@ func resourceAwsCloudHsm2ClusterRead(d *schema.ResourceData, meta interface{}) e
d.Set("cluster_state", cluster.State)
d.Set("security_group_id", cluster.SecurityGroup)
d.Set("vpc_id", cluster.VpcId)
d.Set("backup_identifier", cluster.SourceBackupId)
d.Set("source_backup_identifier", cluster.SourceBackupId)
d.Set("hsm_type", cluster.HsmType)
d.Set("cluster_certificate", readCloudHsm2ClusterCertificates(cluster))
if err := d.Set("cluster_certificates", readCloudHsm2ClusterCertificates(cluster)); err != nil {
return fmt.Errorf("error setting cluster_certificates: %s", err)
}

var subnets []string
for _, sn := range cluster.SubnetMapping {
subnets = append(subnets, *sn)
subnets = append(subnets, aws.StringValue(sn))
}
if err := d.Set("subnet_ids", subnets); err != nil {
return fmt.Errorf("[DEBUG] Error saving Subnet IDs to state for CloudHSMv2 Cluster (%s): %s", d.Id(), err)
return fmt.Errorf("Error saving Subnet IDs to state for CloudHSMv2 Cluster (%s): %s", d.Id(), err)
}

return nil
Expand All @@ -274,11 +263,10 @@ func resourceAwsCloudHsm2ClusterUpdate(d *schema.ResourceData, meta interface{})
func resourceAwsCloudHsm2ClusterDelete(d *schema.ResourceData, meta interface{}) error {
cloudhsm2 := meta.(*AWSClient).cloudhsmv2conn

var output *cloudhsmv2.DeleteClusterOutput
log.Printf("[DEBUG] CloudHSMv2 Delete cluster: %s", d.Id())
err := resource.Retry(180*time.Second, func() *resource.RetryError {
var err error
output, err = cloudhsm2.DeleteCluster(&cloudhsmv2.DeleteClusterInput{
_, err = cloudhsm2.DeleteCluster(&cloudhsmv2.DeleteClusterInput{
ClusterId: aws.String(d.Id()),
})
if err != nil {
Expand All @@ -299,7 +287,7 @@ func resourceAwsCloudHsm2ClusterDelete(d *schema.ResourceData, meta interface{})
stateConf := &resource.StateChangeConf{
Pending: []string{cloudhsmv2.ClusterStateDeleteInProgress},
Target: []string{cloudhsmv2.ClusterStateDeleted},
Refresh: resourceAwsCloudHsm2ClusterRefreshFunc(d, meta),
Refresh: resourceAwsCloudHsm2ClusterRefreshFunc(cloudhsm2, d.Id()),
Timeout: d.Timeout(schema.TimeoutCreate),
MinTimeout: 30 * time.Second,
Delay: 30 * time.Second,
Expand All @@ -308,7 +296,7 @@ func resourceAwsCloudHsm2ClusterDelete(d *schema.ResourceData, meta interface{})
// Wait, catching any errors
_, errWait := stateConf.WaitForState()
if errWait != nil {
return fmt.Errorf("[WARN] Error waiting for CloudHSMv2 Cluster state to be \"DELETED\": %s", errWait)
return fmt.Errorf("Error waiting for CloudHSMv2 Cluster state to be \"DELETED\": %s", errWait)
}

return nil
Expand Down Expand Up @@ -356,15 +344,6 @@ func setTagsAwsCloudHsm2Cluster(conn *cloudhsmv2.CloudHSMV2, d *schema.ResourceD
return nil
}

func validateCloudHsm2HsmType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
hsmType := "hsm1.medium"
if value != hsmType {
errors = append(errors, fmt.Errorf("there is only %s HSM type available", hsmType))
}
return
}

func readCloudHsm2ClusterCertificates(cluster *cloudhsmv2.Cluster) []map[string]interface{} {
certs := map[string]interface{}{}
if cluster.Certificates != nil {
Expand Down
26 changes: 10 additions & 16 deletions aws/resource_aws_cloudhsm2_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ func TestAccAWSCloudHsm2Cluster_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("aws_cloudhsm_v2_cluster.cluster", "cluster_state"),
),
},
},
})
}

func TestAccAWSCloudHsm2Cluster_importBasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudHsm2ClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudHsm2Cluster(),
},
{
ResourceName: "aws_cloudhsm_v2_cluster.cluster",
ImportState: true,
Expand Down Expand Up @@ -93,14 +80,14 @@ func testAccCheckAWSCloudHsm2ClusterDestroy(s *terraform.State) error {
if rs.Type != "aws_cloudhsm_v2_cluster" {
continue
}
cluster, err := describeCloudHsm2Cluster(rs.Primary.ID, testAccProvider.Meta())
cluster, err := describeCloudHsm2Cluster(testAccProvider.Meta().(*AWSClient).cloudhsmv2conn, rs.Primary.ID)

if err != nil {
return err
}

if cluster != nil && aws.StringValue(cluster.State) != "DELETED" {
return fmt.Errorf("CloudHSM cluster still exists:\n%s", cluster)
return fmt.Errorf("CloudHSM cluster still exists %s", cluster)
}
}

Expand All @@ -109,11 +96,18 @@ func testAccCheckAWSCloudHsm2ClusterDestroy(s *terraform.State) error {

func testAccCheckAWSCloudHsm2ClusterExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
conn := testAccProvider.Meta().(*AWSClient).cloudhsmv2conn
it, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

_, err := describeCloudHsm2Cluster(conn, it.Primary.ID)

if err != nil {
return fmt.Errorf("CloudHSM cluster not found: %s", err)
}

return nil
}
}
Loading

0 comments on commit 896adcb

Please sign in to comment.