forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d000a6b
commit c1754bf
Showing
15 changed files
with
1,322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.