-
Notifications
You must be signed in to change notification settings - Fork 3
/
allowlists.go
195 lines (172 loc) · 6.33 KB
/
allowlists.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
package datasources
import (
"context"
"fmt"
"net/http"
"github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/api"
"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/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &AllowLists{}
_ datasource.DataSourceWithConfigure = &AllowLists{}
)
// AllowLists is the allow list data source implementation.
type AllowLists struct {
*providerschema.Data
}
// NewAllowLists is a helper function to simplify the provider implementation.
func NewAllowLists() datasource.DataSource {
return &AllowLists{}
}
// Metadata returns the allow list data source type name.
func (d *AllowLists) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_allowlists"
}
// Schema defines the schema for the allowlist data source.
func (d *AllowLists) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"organization_id": requiredStringAttribute,
"project_id": requiredStringAttribute,
"cluster_id": requiredStringAttribute,
"data": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": computedStringAttribute,
"organization_id": computedStringAttribute,
"project_id": computedStringAttribute,
"cluster_id": computedStringAttribute,
"cidr": computedStringAttribute,
"comment": computedStringAttribute,
"expires_at": computedStringAttribute,
"audit": computedAuditAttribute,
},
},
},
},
}
}
// Read refreshes the Terraform state with the latest data of allowlists.
func (d *AllowLists) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state providerschema.AllowLists
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Validate state is not empty
err := d.validate(state)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella AllowLists",
"Could not read allow lists in cluster "+state.ClusterId.String()+": "+err.Error(),
)
return
}
var (
organizationId = state.OrganizationId.ValueString()
projectId = state.ProjectId.ValueString()
clusterId = state.ClusterId.ValueString()
)
allowLists, err := d.listAllowLists(ctx, organizationId, projectId, clusterId)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella AllowLists",
"Could not read allow lists in cluster "+state.ClusterId.String()+": "+api.ParseError(err),
)
return
}
state = d.mapResponseBody(allowLists, &state)
if err != nil {
resp.Diagnostics.AddError(
"Error reading allowlist",
"Could not read allowlist, unexpected error: "+err.Error(),
)
return
}
// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// listAllowLists executes calls to the list allowlist endpoint. It handles pagination and
// returns a slice of individual allowlists responses retrieved from multiple pages.
func (d *AllowLists) listAllowLists(ctx context.Context, organizationId, projectId, clusterId string) ([]api.GetAllowListResponse, error) {
url := fmt.Sprintf(
"%s/v4/organizations/%s/projects/%s/clusters/%s/allowedcidrs",
d.HostURL,
organizationId,
projectId,
clusterId,
)
cfg := api.EndpointCfg{Url: url, Method: http.MethodGet, SuccessStatus: http.StatusOK}
return api.GetPaginated[[]api.GetAllowListResponse](ctx, d.Client, d.Token, cfg, api.SortById)
}
// Configure adds the provider configured client to the allowlist data source.
func (d *AllowLists) 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
}
// mapResponseBody is used to map the response body from a call to
// listAllowlists to the allowlists schema that will be used by terraform.
func (d *AllowLists) mapResponseBody(
allowLists []api.GetAllowListResponse,
state *providerschema.AllowLists,
) providerschema.AllowLists {
for _, allowList := range allowLists {
allowListState := providerschema.OneAllowList{
Id: types.StringValue(allowList.Id.String()),
OrganizationId: types.StringValue(state.OrganizationId.ValueString()),
ProjectId: types.StringValue(state.ProjectId.ValueString()),
ClusterId: types.StringValue(state.ClusterId.ValueString()),
Cidr: types.StringValue(allowList.Cidr),
Audit: providerschema.CouchbaseAuditData{
CreatedAt: types.StringValue(allowList.Audit.CreatedAt.String()),
CreatedBy: types.StringValue(allowList.Audit.CreatedBy),
ModifiedAt: types.StringValue(allowList.Audit.ModifiedAt.String()),
ModifiedBy: types.StringValue(allowList.Audit.ModifiedBy),
Version: types.Int64Value(int64(allowList.Audit.Version)),
},
}
if allowList.Comment != nil {
allowListState.Comment = types.StringValue(*allowList.Comment)
}
if allowList.ExpiresAt != nil {
allowListState.ExpiresAt = types.StringValue(*allowList.ExpiresAt)
}
state.Data = append(state.Data, allowListState)
}
return *state
}
// validate is used to verify that all the fields in the datasource
// have been populated.
func (d *AllowLists) validate(state providerschema.AllowLists) error {
if state.OrganizationId.IsNull() {
return errors.ErrOrganizationIdMissing
}
if state.ProjectId.IsNull() {
return errors.ErrProjectIdMissing
}
if state.ClusterId.IsNull() {
return errors.ErrClusterIdMissing
}
return nil
}