generated from hashicorp/terraform-provider-scaffolding
/
data_source_database.go
159 lines (148 loc) · 4.34 KB
/
data_source_database.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
package provider
import (
"context"
"net/http"
"github.com/datastax/astra-client-go/v2/astra"
"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"
)
func dataSourceDatabase() *schema.Resource {
return &schema.Resource{
Description: "`astra_database` provides a datasource for Astra an Astra database. This can be used to select an existing database within your Astra Organization.",
ReadContext: dataSourceDatabaseRead,
Schema: map[string]*schema.Schema{
// Required inputs
"database_id": {
Description: "Astra Database ID (system generated)",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},
// computed outputs
"name": {
Description: "Database name (user provided)",
Type: schema.TypeString,
Computed: true,
},
"owner_id": {
Description: "Owner id (system generated)",
Type: schema.TypeString,
Computed: true,
},
"organization_id": {
Description: "Ordg id (system generated)",
Type: schema.TypeString,
Computed: true,
},
"cloud_provider": {
Description: "Cloud provider (AWS, GCP, AZURE)",
Type: schema.TypeString,
Computed: true,
},
"regions": {
Description: "Cloud provider region. Get list of supported regions from regions data-source",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"status": {
Description: "Database status",
Type: schema.TypeString,
Computed: true,
},
"cqlsh_url": {
Description: "URL for cqlsh web",
Type: schema.TypeString,
Computed: true,
},
"grafana_url": {
Description: "URL for the grafana dashboard for this database",
Type: schema.TypeString,
Computed: true,
},
"data_endpoint_url": {
Description: "REST API URL",
Type: schema.TypeString,
Computed: true,
},
"graphql_url": {
Description: "Graphql URL",
Type: schema.TypeString,
Computed: true,
},
"keyspace": {
Description: "Initial keyspace",
Type: schema.TypeString,
Computed: true,
},
"node_count": {
Description: "Node count (not relevant for serverless databases)",
Type: schema.TypeInt,
Computed: true,
},
"replication_factor": {
Description: "Replication Factor (not relevant for serverless databases)",
Type: schema.TypeInt,
Computed: true,
},
"total_storage": {
Description: "Storage Capacity (not relevant for serverelss databases)",
Type: schema.TypeInt,
Computed: true,
},
"additional_keyspaces": {
Description: "Additional keyspaces",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"datacenters": {
Description: "Map of Datacenter IDs. The map key is \"cloud_provider.region\". Example: \"GCP.us-east4\".",
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"db_type": {
Description: "Database Type. Only 'vector' is supported. For serverless, this field will be omitted",
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceDatabaseRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
databaseID := d.Get("database_id").(string)
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
db, err := getDatabase(ctx, d, client, databaseID)
if err != nil {
diag.Errorf("error fetching database: %s", err.Error())
return nil
}
if err := setDatabaseResourceData(d, db); err != nil {
return diag.FromErr(err)
}
return nil
}
func getDatabase(ctx context.Context, d *schema.ResourceData, client *astra.ClientWithResponses, databaseID string) (*astra.Database, error) {
resp, err := client.GetDatabaseWithResponse(ctx, astra.DatabaseIdParam(databaseID))
if err != nil {
return nil, err
}
if resp.StatusCode() == http.StatusNotFound {
d.SetId("")
return nil, err
}
db := resp.JSON200
if db == nil {
diag.Errorf("error fetching database: %s", string(resp.Body))
return nil, err
}
return db, nil
}