Skip to content

Commit

Permalink
CloudHSM v2 resource and datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-gajda committed Apr 24, 2018
1 parent d000a6b commit c1754bf
Show file tree
Hide file tree
Showing 15 changed files with 1,322 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 @@ -149,6 +150,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 @@ -431,6 +433,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
128 changes: 128 additions & 0 deletions aws/data_source_aws_cloudhsm2_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
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.TypeSet,
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 CloudHSMv2 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 err
}

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

if cluster != nil {
d.SetId(clusterId)
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
}
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 CloudHSMv2 Cluster (%s): %s", d.Id(), err)
}
} else {
return fmt.Errorf("cluster with id %s not found", clusterId)
}
return nil
}
65 changes: 65 additions & 0 deletions aws/data_source_aws_cloudhsm2_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package aws

import (
"fmt"
"testing"

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

func TestAccDataSourceCloudHsm2Cluster_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudHsm2ClusterDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_cloudhsm_v2_cluster.default", "cluster_state", "UNINITIALIZED"),
),
},
},
})
}

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 @@ -173,6 +173,7 @@ func Provider() terraform.ResourceProvider {
"aws_caller_identity": dataSourceAwsCallerIdentity(),
"aws_canonical_user_id": dataSourceAwsCanonicalUserId(),
"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 @@ -316,6 +317,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_cloudwatch_metric_alarm": resourceAwsCloudWatchMetricAlarm(),
"aws_cloudwatch_dashboard": resourceAwsCloudWatchDashboard(),
"aws_codedeploy_app": resourceAwsCodeDeployApp(),
Expand Down
Loading

0 comments on commit c1754bf

Please sign in to comment.