forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_aws_cloudhsm2_cluster.go
131 lines (114 loc) · 2.98 KB
/
data_source_aws_cloudhsm2_cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
}