-
Notifications
You must be signed in to change notification settings - Fork 3
/
audit_log_settings.go
169 lines (147 loc) · 5.06 KB
/
audit_log_settings.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
package datasources
import (
"context"
"encoding/json"
"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 = &AuditLogSettings{}
_ datasource.DataSourceWithConfigure = &AuditLogSettings{}
)
// AuditLogSettings is the data source implementation.
type AuditLogSettings struct {
*providerschema.Data
}
// NewAuditLogSettings is a helper function to simplify the provider implementation.
func NewAuditLogSettings() datasource.DataSource {
return &AuditLogSettings{}
}
// Metadata returns the certificates data source type name.
func (a *AuditLogSettings) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_audit_log_settings"
}
func (a *AuditLogSettings) 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,
"audit_enabled": computedBoolAttribute,
"enabled_event_ids": computedIntSetAttribute,
"disabled_users": schema.SetNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"domain": schema.StringAttribute{Computed: true},
"name": schema.StringAttribute{Computed: true},
},
},
},
},
}
}
// Read refreshes the Terraform state with the latest audit log settings.
func (a *AuditLogSettings) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state providerschema.ClusterAuditSettings
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
err := a.validate(state)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella Audit Log Settings",
"Could not read Audit Log Settings for cluster ID "+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/auditLog", a.HostURL, organizationId, projectId, clusterId)
cfg := api.EndpointCfg{Url: url, Method: http.MethodGet, SuccessStatus: http.StatusOK}
response, err := a.Client.ExecuteWithRetry(
ctx,
cfg,
nil,
a.Token,
nil,
)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella Audit Log Settings",
"Could not read audit log settings in cluster "+state.ClusterId.String()+": "+api.ParseError(err),
)
return
}
auditResponse := api.GetClusterAuditSettingsResponse{}
err = json.Unmarshal(response.Body, &auditResponse)
if err != nil {
resp.Diagnostics.AddError(
"Error reading audit log settings",
"Could not read audit log settings in cluster, unexpected error: "+err.Error(),
)
return
}
eventIDs := make([]types.Int64, len(auditResponse.EnabledEventIDs))
for i, e := range auditResponse.EnabledEventIDs {
eventIDs[i] = types.Int64Value(int64(e))
}
disabledUsers := make([]providerschema.AuditSettingsDisabledUser, len(auditResponse.DisabledUsers))
for i, u := range auditResponse.DisabledUsers {
disabledUser := providerschema.AuditSettingsDisabledUser{}
if u.Domain != nil {
disabledUser.Domain = types.StringValue(*u.Domain)
}
if u.Name != nil {
disabledUser.Name = types.StringValue(*u.Name)
}
disabledUsers[i] = disabledUser
}
state.AuditEnabled = types.BoolValue(auditResponse.AuditEnabled)
state.DisabledUsers = disabledUsers
state.EnabledEventIDs = eventIDs
// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (a *AuditLogSettings) 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
}
a.Data = data
}
func (a *AuditLogSettings) validate(state providerschema.ClusterAuditSettings) error {
if state.OrganizationId.IsNull() {
return errors.ErrOrganizationIdMissing
}
if state.ProjectId.IsNull() {
return errors.ErrProjectIdMissing
}
if state.ClusterId.IsNull() {
return errors.ErrClusterIdMissing
}
return nil
}