-
Notifications
You must be signed in to change notification settings - Fork 3
/
certificate.go
143 lines (122 loc) · 4.02 KB
/
certificate.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
package datasources
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/api"
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 = &Certificate{}
_ datasource.DataSourceWithConfigure = &Certificate{}
)
// Certificate is the certificate data source implementation.
type Certificate struct {
*providerschema.Data
}
// NewCertificate is a helper function to simplify the provider implementation.
func NewCertificate() datasource.DataSource {
return &Certificate{}
}
// Metadata returns the certificates data source type name.
func (c *Certificate) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_certificate"
}
// Schema defines the schema for the allowlist data source.
func (c *Certificate) 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{
"certificate": schema.StringAttribute{
Computed: true,
},
},
},
},
},
}
}
// Read refreshes the Terraform state with the latest data of projects.
func (c *Certificate) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state providerschema.Certificate
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Validate state is not empty
err := state.Validate()
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella Certificate",
"Could not read certificate in cluster "+state.ClusterId.String()+": "+err.Error(),
)
return
}
var (
organizationId = state.OrganizationId.ValueString()
projectId = state.ProjectId.ValueString()
clusterId = state.ClusterId.ValueString()
)
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters/%s/certificates", c.HostURL, organizationId, projectId, clusterId)
cfg := api.EndpointCfg{Url: url, Method: http.MethodGet, SuccessStatus: http.StatusOK}
response, err := c.Client.ExecuteWithRetry(
ctx,
cfg,
nil,
c.Token,
nil,
)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella Certificate",
"Could not read certificate in cluster "+state.ClusterId.String()+": "+api.ParseError(err),
)
return
}
certResp := api.GetCertificateResponse{}
err = json.Unmarshal(response.Body, &certResp)
if err != nil {
resp.Diagnostics.AddError(
"Error reading certificate",
"Could not read certificate in cluster, unexpected error: "+err.Error(),
)
return
}
certState := providerschema.OneCertificate{
Certificate: types.StringValue(certResp.Certificate),
}
state.Data = append(state.Data, certState)
// 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 project data source.
func (c *Certificate) 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
}
c.Data = data
}