-
Notifications
You must be signed in to change notification settings - Fork 3
/
clusters.go
136 lines (113 loc) · 4.2 KB
/
clusters.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
package datasources
import (
"context"
"fmt"
"net/http"
"github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/api"
clusterapi "github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/api/cluster"
"github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/errors"
providerschema "github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/schema"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &Clusters{}
_ datasource.DataSourceWithConfigure = &Clusters{}
)
// Clusters is the Clusters data source implementation.
type Clusters struct {
*providerschema.Data
}
// NewClusters is a helper function to simplify the provider implementation.
func NewClusters() datasource.DataSource {
return &Clusters{}
}
// Metadata returns the cluster data source type name.
func (d *Clusters) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_clusters"
}
// Schema defines the schema for the Clusters data source.
func (d *Clusters) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = ClusterSchema()
}
// Read refreshes the Terraform state with the latest data of clusters.
func (d *Clusters) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state providerschema.Clusters
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if state.OrganizationId.IsNull() {
resp.Diagnostics.AddError(
"Error reading cluster",
"Could not read cluster, unexpected error: organization ID cannot be empty.",
)
return
}
if state.ProjectId.IsNull() {
resp.Diagnostics.AddError(
"Error reading cluster",
"Could not read cluster, unexpected error: project ID cannot be empty.",
)
return
}
var (
organizationId = state.OrganizationId.ValueString()
projectId = state.ProjectId.ValueString()
)
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters", d.HostURL, organizationId, projectId)
cfg := api.EndpointCfg{Url: url, Method: http.MethodGet, SuccessStatus: http.StatusOK}
response, err := api.GetPaginated[[]clusterapi.GetClusterResponse](ctx, d.Client, d.Token, cfg, api.SortById)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella Clusters",
fmt.Sprintf(
"Could not read clusters in organization %s and project %s, unexpected error: %s",
organizationId, projectId, api.ParseError(err),
),
)
return
}
for i := range response {
cluster := response[i]
audit := providerschema.NewCouchbaseAuditData(cluster.Audit)
auditObj, diags := types.ObjectValueFrom(ctx, audit.AttributeTypes(), audit)
if diags.HasError() {
resp.Diagnostics.AddError(
"Error Reading Capella Clusters",
fmt.Sprintf("Could not read clusters in organization %s and project %s, unexpected error: %s", organizationId, projectId, errors.ErrUnableToConvertAuditData),
)
}
newClusterData, err := providerschema.NewClusterData(&cluster, organizationId, projectId, auditObj)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella Clusters",
fmt.Sprintf("Could not read clusters in organization %s and project %s, unexpected error: %s", organizationId, projectId, err.Error()),
)
}
state.Data = append(state.Data, *newClusterData)
}
// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Configure adds the provider configured client to the cluster data source.
func (d *Clusters) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
data, ok := req.ProviderData.(*providerschema.Data)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *ProviderSourceData, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.Data = data
}