generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 43
/
data_source_consul_cluster.go
367 lines (325 loc) · 11.4 KB
/
data_source_consul_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package providersdkv2
import (
"context"
"fmt"
"log"
consulmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-service/stable/2021-02-04/models"
sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-hcp/internal/clients"
)
func dataSourceConsulCluster() *schema.Resource {
return &schema.Resource{
Description: "The cluster data source provides information about an existing HCP Consul cluster.",
ReadContext: dataSourceConsulClusterRead,
Timeouts: &schema.ResourceTimeout{
Default: &defaultConsulClusterTimeout,
},
Schema: map[string]*schema.Schema{
// Required inputs
"cluster_id": {
Description: "The ID of the HCP Consul cluster.",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateSlugID,
},
// Optional fields
"project_id": {
Description: `
The ID of the HCP project where the HCP Consul cluster is located.
If not specified, the project specified in the HCP Provider config block will be used, if configured.
If a project is not configured in the HCP Provider config block, the oldest project in the organization will be used.`,
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: validation.IsUUID,
},
// computed outputs
"organization_id": {
Description: "The ID of the organization the project for this HCP Consul cluster is located.",
Type: schema.TypeString,
Computed: true,
},
"hvn_id": {
Description: "The ID of the HVN this HCP Consul cluster is associated to.",
Type: schema.TypeString,
Computed: true,
},
"cloud_provider": {
Description: "The provider where the HCP Consul cluster is located. Only 'aws' is available at this time.",
Type: schema.TypeString,
Computed: true,
},
"region": {
Description: "The region where the HCP Consul cluster is located.",
Type: schema.TypeString,
Computed: true,
},
"public_endpoint": {
Description: "Denotes that the cluster has a public endpoint for the Consul UI. Defaults to false.",
Type: schema.TypeBool,
Computed: true,
},
"datacenter": {
Description: "The Consul data center name of the cluster. If not specified, it is defaulted to the value of `cluster_id`.",
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the HCP Consul cluster.",
Type: schema.TypeString,
Computed: true,
},
"connect_enabled": {
Description: "Denotes the Consul connect feature should be enabled for this cluster. Default to true.",
Type: schema.TypeBool,
Computed: true,
},
"consul_automatic_upgrades": {
Description: "Denotes that automatic Consul upgrades are enabled.",
Type: schema.TypeBool,
Computed: true,
},
"consul_snapshot_interval": {
Description: "The Consul snapshot interval.",
Type: schema.TypeString,
Computed: true,
},
"consul_snapshot_retention": {
Description: "The retention policy for Consul snapshots.",
Type: schema.TypeString,
Computed: true,
},
"consul_config_file": {
Description: "The cluster config encoded as a Base64 string.",
Type: schema.TypeString,
Computed: true,
},
"consul_ca_file": {
Description: "The cluster CA file encoded as a Base64 string.",
Type: schema.TypeString,
Computed: true,
},
"consul_version": {
Description: "The Consul version of the cluster.",
Type: schema.TypeString,
Computed: true,
},
"consul_public_endpoint_url": {
Description: "The public URL for the Consul UI. This will be empty if `public_endpoint` is `false`.",
Type: schema.TypeString,
Computed: true,
},
"consul_private_endpoint_url": {
Description: "The private URL for the Consul UI.",
Type: schema.TypeString,
Computed: true,
},
"scale": {
Description: "The the number of Consul server nodes in the cluster.",
Type: schema.TypeInt,
Computed: true,
},
"tier": {
Description: "The tier that the HCP Consul cluster will be provisioned as. Only `development`, `standard`, `plus`, and `premium` are available at this time.",
Type: schema.TypeString,
Computed: true,
},
"size": {
Description: "The t-shirt size representation of each server VM that this Consul cluster is provisioned with. Valid option for development tier - `x_small`. Valid options for other tiers - `small`, `medium`, `large`. For more details - https://cloud.hashicorp.com/pricing/consul",
Type: schema.TypeString,
Computed: true,
},
"self_link": {
Description: "A unique URL identifying the HCP Consul cluster.",
Type: schema.TypeString,
Computed: true,
},
"primary_link": {
Description: "The `self_link` of the HCP Consul cluster which is the primary in the federation setup with this HCP Consul cluster. If not specified, it is a standalone cluster.",
Type: schema.TypeString,
Computed: true,
},
"auto_hvn_to_hvn_peering": {
Description: "Enables automatic HVN to HVN peering when creating a secondary cluster in a federation.",
Type: schema.TypeBool,
Computed: true,
},
"ip_allowlist": {
Description: "Allowed IPV4 address ranges (CIDRs) for inbound traffic. Each entry must be a unique CIDR. Maximum 3 CIDRS supported at this time.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": {
Description: "IP address range in CIDR notation.",
Type: schema.TypeString,
Computed: true,
},
"description": {
Description: "Description to help identify source (maximum 255 chars).",
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceConsulClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
clusterID := d.Get("cluster_id").(string)
client := meta.(*clients.Client)
projectID, err := GetProjectID(d.Get("project_id").(string), client.Config.ProjectID)
if err != nil {
return diag.Errorf("unable to retrieve project ID: %v", err)
}
loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: client.Config.OrganizationID,
ProjectID: projectID,
}
log.Printf("[INFO] Reading Consul cluster (%s) [project_id=%s, organization_id=%s]", clusterID, loc.ProjectID, loc.OrganizationID)
cluster, err := clients.GetConsulClusterByID(ctx, client, loc, clusterID)
if err != nil {
return diag.FromErr(err)
}
// build the id for this Consul cluster
link := newLink(loc, ConsulClusterResourceType, clusterID)
url, err := linkURL(link)
if err != nil {
return diag.FromErr(err)
}
d.SetId(url)
// cluster found, set data source attributes
if err := setConsulClusterDataSourceAttributes(d, cluster); err != nil {
return diag.FromErr(err)
}
// get the cluster's Consul client config files
clientConfigFiles, err := clients.GetConsulClientConfigFiles(ctx, client, loc, clusterID)
if err != nil {
log.Printf("[WARN] unable to retrieve Consul cluster (%s) client config files: %v", clusterID, err)
return nil
}
// client config found, set data source attributes
if err := setConsulClusterClientConfigDataSourceAttributes(d, clientConfigFiles); err != nil {
return diag.FromErr(err)
}
return nil
}
// setConsulClusterDataSourceAttributes sets all data source attributes from the cluster
func setConsulClusterDataSourceAttributes(
d *schema.ResourceData,
cluster *consulmodels.HashicorpCloudConsul20210204Cluster,
) error {
if err := d.Set("cluster_id", cluster.ID); err != nil {
return err
}
if err := d.Set("hvn_id", cluster.Config.NetworkConfig.Network.ID); err != nil {
return err
}
if err := d.Set("organization_id", cluster.Location.OrganizationID); err != nil {
return err
}
if err := d.Set("project_id", cluster.Location.ProjectID); err != nil {
return err
}
if err := d.Set("cloud_provider", cluster.Location.Region.Provider); err != nil {
return err
}
if err := d.Set("region", cluster.Location.Region.Region); err != nil {
return err
}
publicEndpoint := !cluster.Config.NetworkConfig.Private
if err := d.Set("public_endpoint", publicEndpoint); err != nil {
return err
}
if err := d.Set("datacenter", cluster.Config.ConsulConfig.Datacenter); err != nil {
return err
}
if err := d.Set("state", cluster.State); err != nil {
return err
}
if err := d.Set("scale", cluster.Config.CapacityConfig.Scale); err != nil {
return err
}
if err := d.Set("tier", cluster.Config.Tier); err != nil {
return err
}
if err := d.Set("size", cluster.Config.CapacityConfig.Size); err != nil {
return err
}
if err := d.Set("consul_snapshot_interval", "24h"); err != nil {
return err
}
if err := d.Set("consul_snapshot_retention", "30d"); err != nil {
return err
}
if err := d.Set("connect_enabled", cluster.Config.ConsulConfig.ConnectEnabled); err != nil {
return err
}
if err := d.Set("consul_version", cluster.ConsulVersion); err != nil {
return err
}
if err := d.Set("auto_hvn_to_hvn_peering", cluster.Config.AutoHvnToHvnPeering); err != nil {
return err
}
if publicEndpoint {
// No port needed to communicate with HCP Consul via HTTPS
if err := d.Set("consul_public_endpoint_url", fmt.Sprintf("https://%s", cluster.DNSNames.Public)); err != nil {
return err
}
}
// No port needed to communicate with HCP Consul via HTTPS
if err := d.Set("consul_private_endpoint_url", fmt.Sprintf("https://%s", cluster.DNSNames.Private)); err != nil {
return err
}
link := newLink(cluster.Location, ConsulClusterResourceType, cluster.ID)
selfLink, err := linkURL(link)
if err != nil {
return err
}
if err := d.Set("self_link", selfLink); err != nil {
return err
}
if cluster.Config.ConsulConfig.Primary != nil {
link := newLink(cluster.Config.ConsulConfig.Primary.Location, ConsulClusterResourceType, cluster.Config.ConsulConfig.Primary.ID)
primaryLink, err := linkURL(link)
if err != nil {
return err
}
if err := d.Set("primary_link", primaryLink); err != nil {
return err
}
}
if cluster.Config.NetworkConfig != nil {
ipAllowlist := make([]map[string]interface{}, len(cluster.Config.NetworkConfig.IPAllowlist))
for i, cidrRange := range cluster.Config.NetworkConfig.IPAllowlist {
cidr := map[string]interface{}{
"description": cidrRange.Description,
"address": cidrRange.Address,
}
ipAllowlist[i] = cidr
}
if err := d.Set("ip_allowlist", ipAllowlist); err != nil {
return err
}
}
return nil
}
// setConsulClusterClientConfigDataSourceAttributes sets all resource data that's derived from client config meta
func setConsulClusterClientConfigDataSourceAttributes(
d *schema.ResourceData,
clientConfigFiles *consulmodels.HashicorpCloudConsul20210204GetClientConfigResponse,
) error {
if err := d.Set("consul_config_file", clientConfigFiles.ConsulConfigFile.String()); err != nil {
return err
}
if err := d.Set("consul_ca_file", clientConfigFiles.CaFile.String()); err != nil {
return err
}
return nil
}