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

CloudHSM v2 resource & datasource #4125

Merged
merged 8 commits into from
Oct 18, 2018
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
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/aws/aws-sdk-go/service/cloud9"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/cloudhsmv2"
"github.com/aws/aws-sdk-go/service/cloudtrail"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
Expand Down Expand Up @@ -160,6 +161,7 @@ type AWSClient struct {
cfconn *cloudformation.CloudFormation
cloud9conn *cloud9.Cloud9
cloudfrontconn *cloudfront.CloudFront
cloudhsmv2conn *cloudhsmv2.CloudHSMV2
cloudtrailconn *cloudtrail.CloudTrail
cloudwatchconn *cloudwatch.CloudWatch
cloudwatchlogsconn *cloudwatchlogs.CloudWatchLogs
Expand Down Expand Up @@ -499,6 +501,7 @@ func (c *Config) Client() (interface{}, error) {
client.cloud9conn = cloud9.New(sess)
client.cfconn = cloudformation.New(awsCfSess)
client.cloudfrontconn = cloudfront.New(sess)
client.cloudhsmv2conn = cloudhsmv2.New(sess)
client.cloudtrailconn = cloudtrail.New(sess)
client.cloudwatchconn = cloudwatch.New(awsCwSess)
client.cloudwatcheventsconn = cloudwatchevents.New(awsCweSess)
Expand Down
131 changes: 131 additions & 0 deletions aws/data_source_aws_cloudhsm2_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudhsmv2"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceCloudHsm2Cluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudHsm2ClusterRead,

Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
},

"cluster_state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"vpc_id": {
Type: schema.TypeString,
Computed: true,
},

"security_group_id": {
Type: schema.TypeString,
Computed: true,
},

"cluster_certificates": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cluster_certificate": {
Type: schema.TypeString,
Computed: true,
},
"cluster_csr": {
Type: schema.TypeString,
Computed: true,
},
"aws_hardware_certificate": {
Type: schema.TypeString,
Computed: true,
},
"hsm_certificate": {
Type: schema.TypeString,
Computed: true,
},
"manufacturer_hardware_certificate": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"subnet_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

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

clusterId := d.Get("cluster_id").(string)
filters := []*string{&clusterId}
log.Printf("[DEBUG] Reading CloudHSM v2 Cluster %s", clusterId)
result := int64(1)
input := &cloudhsmv2.DescribeClustersInput{
Filters: map[string][]*string{
"clusterIds": filters,
},
MaxResults: &result,
}
state := d.Get("cluster_state").(string)
states := []*string{&state}
if len(state) > 0 {
input.Filters["states"] = states
}
out, err := conn.DescribeClusters(input)

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

var cluster *cloudhsmv2.Cluster
for _, c := range out.Clusters {
if aws.StringValue(c.ClusterId) == clusterId {
cluster = c
break
}
}

if cluster == nil {
return fmt.Errorf("cluster with id %s not found", clusterId)
}

d.SetId(clusterId)
d.Set("vpc_id", cluster.VpcId)
d.Set("security_group_id", cluster.SecurityGroup)
d.Set("cluster_state", cluster.State)
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)
}

if err := d.Set("subnet_ids", subnets); err != nil {
return fmt.Errorf("[DEBUG] Error saving Subnet IDs to state for CloudHSM v2 Cluster (%s): %s", d.Id(), err)
}

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

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceCloudHsm2Cluster_basic(t *testing.T) {
resourceName := "aws_cloudhsm_v2_cluster.cluster"
dataSourceName := "data.aws_cloudhsm_v2_cluster.default"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudHsm2ClusterDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "cluster_state", "UNINITIALIZED"),
resource.TestCheckResourceAttrPair(dataSourceName, "cluster_id", resourceName, "cluster_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "cluster_state", resourceName, "cluster_state"),
resource.TestCheckResourceAttrPair(dataSourceName, "security_group_id", resourceName, "security_group_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "subnet_ids.#", resourceName, "subnet_ids.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "vpc_id", resourceName, "vpc_id"),
),
},
},
})
}

var testAccCheckCloudHsm2ClusterDataSourceConfig = fmt.Sprintf(`
variable "subnets" {
default = ["10.0.1.0/24", "10.0.2.0/24"]
type = "list"
}

data "aws_availability_zones" "available" {}

resource "aws_vpc" "cloudhsm2_test_vpc" {
cidr_block = "10.0.0.0/16"

tags {
Name = "terraform-testacc-aws_cloudhsm_v2_cluster-data-source-basic"
}
}

resource "aws_subnet" "cloudhsm2_test_subnets" {
count = 2
vpc_id = "${aws_vpc.cloudhsm2_test_vpc.id}"
cidr_block = "${element(var.subnets, count.index)}"
map_public_ip_on_launch = false
availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}"

tags {
Name = "tf-acc-aws_cloudhsm_v2_cluster-data-source-basic"
}
}

resource "aws_cloudhsm_v2_cluster" "cluster" {
hsm_type = "hsm1.medium"
subnet_ids = ["${aws_subnet.cloudhsm2_test_subnets.*.id}"]
tags {
Name = "tf-acc-aws_cloudhsm_v2_cluster-data-source-basic-%d"
}
}

data "aws_cloudhsm_v2_cluster" "default" {
cluster_id = "${aws_cloudhsm_v2_cluster.cluster.cluster_id}"
}
`, acctest.RandInt())
3 changes: 3 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func Provider() terraform.ResourceProvider {
"aws_canonical_user_id": dataSourceAwsCanonicalUserId(),
"aws_cloudformation_export": dataSourceAwsCloudFormationExport(),
"aws_cloudformation_stack": dataSourceAwsCloudFormationStack(),
"aws_cloudhsm_v2_cluster": dataSourceCloudHsm2Cluster(),
"aws_cloudtrail_service_account": dataSourceAwsCloudTrailServiceAccount(),
"aws_cloudwatch_log_group": dataSourceAwsCloudwatchLogGroup(),
"aws_cognito_user_pools": dataSourceAwsCognitoUserPools(),
Expand Down Expand Up @@ -353,6 +354,8 @@ func Provider() terraform.ResourceProvider {
"aws_cognito_user_pool": resourceAwsCognitoUserPool(),
"aws_cognito_user_pool_client": resourceAwsCognitoUserPoolClient(),
"aws_cognito_user_pool_domain": resourceAwsCognitoUserPoolDomain(),
"aws_cloudhsm_v2_cluster": resourceAwsCloudHsm2Cluster(),
"aws_cloudhsm_v2_hsm": resourceAwsCloudHsm2Hsm(),
"aws_cognito_resource_server": resourceAwsCognitoResourceServer(),
"aws_cloudwatch_metric_alarm": resourceAwsCloudWatchMetricAlarm(),
"aws_cloudwatch_dashboard": resourceAwsCloudWatchDashboard(),
Expand Down
Loading