generated from hashicorp/terraform-provider-scaffolding
/
data_source_private_link_endpoints.go
140 lines (119 loc) · 3.97 KB
/
data_source_private_link_endpoints.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
package provider
import (
"context"
"fmt"
"github.com/datastax/astra-client-go/v2/astra"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourcePrivateLinkEndpoints() *schema.Resource {
return &schema.Resource{
Description: "`astra_private_link_endpoints` provides a datasource that lists the private link endpoints for an Astra database.",
ReadContext: dataSourcePrivateLinkEndpointsRead,
Schema: map[string]*schema.Schema{
// Required
"database_id": {
Description: "The ID of the Astra database.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},
"datacenter_id": {
Description: "The Datacenter ID of the Astra database.",
Type: schema.TypeString,
Required: true,
},
"endpoint_id": {
Description: "Endpoint ID.",
Type: schema.TypeString,
Required: true,
},
// Computed
"results": {
Type: schema.TypeList,
Description: "The list of private links endpoint details that match the search criteria.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"endpoint_id": {
Description: "The private link endpoint id",
Type: schema.TypeString,
Computed: true,
},
"description": {
Description: "Description for private link endpoint",
Type: schema.TypeString,
Computed: true,
},
"status": {
Description: "Private link endpoint status",
Type: schema.TypeString,
Computed: true,
},
"create_time": {
Description: "Crate time for private link endpoint",
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourcePrivateLinkEndpointsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
fmt.Printf("testing")
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
databaseID := d.Get("database_id").(string)
datacenterID := d.Get("datacenter_id").(string)
endpointID := d.Get("endpoint_id").(string)
privateLinks, err := listPrivateLinkEndpoints(ctx, client, databaseID, datacenterID, endpointID)
if err != nil {
return diag.FromErr(err)
}
if privateLinks == nil {
return nil
}
d.SetId(id.UniqueId())
if err := d.Set("results", privateLinkEndpointsToMap(privateLinks)); err != nil {
fmt.Printf("testing")
return diag.FromErr(err)
}
return nil
}
func listPrivateLinkEndpoints(ctx context.Context, client *astra.ClientWithResponses, databaseID string, datacenterID string, endpointID string) (*astra.PrivateLinkEndpoint, error) {
resp, err := client.GetDatabaseWithResponse(ctx, astra.DatabaseIdParam(databaseID))
if err != nil {
return nil, err
}
db := resp.JSON200
if db == nil {
return nil, fmt.Errorf("error fetching database: %s", string(resp.Body))
}
// If the database is terminated then the private links have been deleted.
if db.Status == astra.TERMINATING || db.Status == astra.TERMINATED {
return nil, nil
}
plResponse, err := client.GetPrivateLinkEndpointWithResponse(ctx, databaseID, datacenterID, endpointID)
if err != nil {
return nil, err
}
privateLinkEndpointOutput := plResponse.JSON200
return privateLinkEndpointOutput, err
}
func privateLinkEndpointsToMap(privateLinkEndpoints *astra.PrivateLinkEndpoint) []map[string]interface{} {
endpointID := privateLinkEndpoints.EndpointID
description := privateLinkEndpoints.Description
status := privateLinkEndpoints.Status
createTime := privateLinkEndpoints.CreatedDateTime
results := make([]map[string]interface{}, 0, 1)
results = append(results, map[string]interface{}{
"endpoint_id": endpointID,
"description": description,
"status": status,
"create_time": createTime,
})
return results
}