Skip to content

Commit beff16a

Browse files
authored
chore(mariadb): switch to new SDK structure (#1359)
relates to STACKITTPR-562
1 parent 10c344f commit beff16a

File tree

10 files changed

+169
-178
lines changed

10 files changed

+169
-178
lines changed

stackit/internal/services/mariadb/credential/datasource.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1919
"github.com/hashicorp/terraform-plugin-framework/types"
20-
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
20+
mariadb "github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api"
2121
)
2222

2323
// Ensure the implementation satisfies the expected interfaces.
@@ -110,7 +110,7 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
110110
Computed: true,
111111
Sensitive: true,
112112
},
113-
"port": schema.Int64Attribute{
113+
"port": schema.Int32Attribute{
114114
Computed: true,
115115
},
116116
"uri": schema.StringAttribute{
@@ -142,7 +142,7 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
142142
ctx = tflog.SetField(ctx, "instance_id", instanceId)
143143
ctx = tflog.SetField(ctx, "credential_id", credentialId)
144144

145-
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
145+
recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
146146
if err != nil {
147147
utils.LogError(
148148
ctx,

stackit/internal/services/mariadb/credential/resource.go

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2222
"github.com/hashicorp/terraform-plugin-framework/types"
2323
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
24-
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
25-
"github.com/stackitcloud/stackit-sdk-go/services/mariadb/wait"
24+
mariadb "github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api"
25+
"github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api/wait"
2626
)
2727

2828
// Ensure the implementation satisfies the expected interfaces.
@@ -41,7 +41,7 @@ type Model struct {
4141
Hosts types.List `tfsdk:"hosts"`
4242
Name types.String `tfsdk:"name"`
4343
Password types.String `tfsdk:"password"`
44-
Port types.Int64 `tfsdk:"port"`
44+
Port types.Int32 `tfsdk:"port"`
4545
Uri types.String `tfsdk:"uri"`
4646
Username types.String `tfsdk:"username"`
4747
}
@@ -145,7 +145,7 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
145145
Computed: true,
146146
Sensitive: true,
147147
},
148-
"port": schema.Int64Attribute{
148+
"port": schema.Int32Attribute{
149149
Computed: true,
150150
},
151151
"uri": schema.StringAttribute{
@@ -176,19 +176,15 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
176176
ctx = tflog.SetField(ctx, "instance_id", instanceId)
177177

178178
// Create new recordset
179-
credentialsResp, err := r.client.CreateCredentials(ctx, projectId, instanceId).Execute()
179+
credentialsResp, err := r.client.DefaultAPI.CreateCredentials(ctx, projectId, instanceId).Execute()
180180
if err != nil {
181181
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Calling API: %v", err))
182182
return
183183
}
184184

185185
ctx = core.LogResponse(ctx)
186186

187-
if credentialsResp.Id == nil {
188-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
189-
return
190-
}
191-
credentialId := *credentialsResp.Id
187+
credentialId := credentialsResp.Id
192188
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
193189
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
194190
"project_id": projectId,
@@ -199,7 +195,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
199195
return
200196
}
201197

202-
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
198+
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
203199
if err != nil {
204200
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
205201
return
@@ -237,7 +233,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
237233
ctx = tflog.SetField(ctx, "instance_id", instanceId)
238234
ctx = tflog.SetField(ctx, "credential_id", credentialId)
239235

240-
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
236+
recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
241237
if err != nil {
242238
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
243239
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -291,14 +287,14 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
291287
ctx = tflog.SetField(ctx, "credential_id", credentialId)
292288

293289
// Delete existing record set
294-
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
290+
err := r.client.DefaultAPI.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
295291
if err != nil {
296292
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
297293
}
298294

299295
ctx = core.LogResponse(ctx)
300296

301-
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
297+
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
302298
if err != nil {
303299
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
304300
return
@@ -341,8 +337,8 @@ func mapFields(ctx context.Context, credentialsResp *mariadb.CredentialsResponse
341337
var credentialId string
342338
if model.CredentialId.ValueString() != "" {
343339
credentialId = model.CredentialId.ValueString()
344-
} else if credentialsResp.Id != nil {
345-
credentialId = *credentialsResp.Id
340+
} else if credentialsResp.Id != "" {
341+
credentialId = credentialsResp.Id
346342
} else {
347343
return fmt.Errorf("credentials id not present")
348344
}
@@ -360,25 +356,25 @@ func mapFields(ctx context.Context, credentialsResp *mariadb.CredentialsResponse
360356

361357
model.Hosts = types.ListNull(types.StringType)
362358
model.CredentialId = types.StringValue(credentialId)
363-
if credentials != nil {
364-
if credentials.Hosts != nil {
365-
respHosts := *credentials.Hosts
366359

367-
reconciledHosts := utils.ReconcileStringSlices(modelHosts, respHosts)
360+
if credentials.Hosts != nil {
361+
respHosts := credentials.Hosts
368362

369-
hostsTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledHosts)
370-
if diags.HasError() {
371-
return fmt.Errorf("failed to map hosts: %w", core.DiagsToError(diags))
372-
}
363+
reconciledHosts := utils.ReconcileStringSlices(modelHosts, respHosts)
373364

374-
model.Hosts = hostsTF
365+
hostsTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledHosts)
366+
if diags.HasError() {
367+
return fmt.Errorf("failed to map hosts: %w", core.DiagsToError(diags))
375368
}
376-
model.Host = types.StringPointerValue(credentials.Host)
377-
model.Name = types.StringPointerValue(credentials.Name)
378-
model.Password = types.StringPointerValue(credentials.Password)
379-
model.Port = types.Int64PointerValue(credentials.Port)
380-
model.Uri = types.StringPointerValue(credentials.Uri)
381-
model.Username = types.StringPointerValue(credentials.Username)
369+
370+
model.Hosts = hostsTF
382371
}
372+
model.Host = types.StringValue(credentials.Host)
373+
model.Name = types.StringPointerValue(credentials.Name)
374+
model.Password = types.StringValue(credentials.Password)
375+
model.Port = types.Int32PointerValue(credentials.Port)
376+
model.Uri = types.StringPointerValue(credentials.Uri)
377+
model.Username = types.StringValue(credentials.Username)
378+
383379
return nil
384380
}

stackit/internal/services/mariadb/credential/resource_test.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/google/go-cmp/cmp"
88
"github.com/hashicorp/terraform-plugin-framework/attr"
99
"github.com/hashicorp/terraform-plugin-framework/types"
10-
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
10+
mariadb "github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api"
1111
)
1212

1313
func TestMapFields(t *testing.T) {
@@ -25,21 +25,21 @@ func TestMapFields(t *testing.T) {
2525
ProjectId: types.StringValue("pid"),
2626
},
2727
&mariadb.CredentialsResponse{
28-
Id: new("cid"),
28+
Id: "cid",
2929
Raw: &mariadb.RawCredentials{},
3030
},
3131
Model{
3232
Id: types.StringValue("pid,iid,cid"),
3333
CredentialId: types.StringValue("cid"),
3434
InstanceId: types.StringValue("iid"),
3535
ProjectId: types.StringValue("pid"),
36-
Host: types.StringNull(),
36+
Host: types.StringValue(""),
3737
Hosts: types.ListNull(types.StringType),
3838
Name: types.StringNull(),
39-
Password: types.StringNull(),
40-
Port: types.Int64Null(),
39+
Password: types.StringValue(""),
40+
Port: types.Int32Null(),
4141
Uri: types.StringNull(),
42-
Username: types.StringNull(),
42+
Username: types.StringValue(""),
4343
},
4444
true,
4545
},
@@ -50,19 +50,19 @@ func TestMapFields(t *testing.T) {
5050
ProjectId: types.StringValue("pid"),
5151
},
5252
&mariadb.CredentialsResponse{
53-
Id: new("cid"),
53+
Id: "cid",
5454
Raw: &mariadb.RawCredentials{
55-
Credentials: &mariadb.Credentials{
56-
Host: new("host"),
57-
Hosts: &[]string{
55+
Credentials: mariadb.Credentials{
56+
Host: "host",
57+
Hosts: []string{
5858
"host_1",
5959
"",
6060
},
6161
Name: new("name"),
62-
Password: new("password"),
63-
Port: new(int64(1234)),
62+
Password: "password",
63+
Port: new(int32(1234)),
6464
Uri: new("uri"),
65-
Username: new("username"),
65+
Username: "username",
6666
},
6767
},
6868
},
@@ -78,7 +78,7 @@ func TestMapFields(t *testing.T) {
7878
}),
7979
Name: types.StringValue("name"),
8080
Password: types.StringValue("password"),
81-
Port: types.Int64Value(1234),
81+
Port: types.Int32Value(1234),
8282
Uri: types.StringValue("uri"),
8383
Username: types.StringValue("username"),
8484
},
@@ -96,20 +96,20 @@ func TestMapFields(t *testing.T) {
9696
}),
9797
},
9898
&mariadb.CredentialsResponse{
99-
Id: new("cid"),
99+
Id: "cid",
100100
Raw: &mariadb.RawCredentials{
101-
Credentials: &mariadb.Credentials{
102-
Host: new("host"),
103-
Hosts: &[]string{
101+
Credentials: mariadb.Credentials{
102+
Host: "host",
103+
Hosts: []string{
104104
"",
105105
"host_1",
106106
"host_2",
107107
},
108108
Name: new("name"),
109-
Password: new("password"),
110-
Port: new(int64(1234)),
109+
Password: "password",
110+
Port: new(int32(1234)),
111111
Uri: new("uri"),
112-
Username: new("username"),
112+
Username: "username",
113113
},
114114
},
115115
},
@@ -126,7 +126,7 @@ func TestMapFields(t *testing.T) {
126126
}),
127127
Name: types.StringValue("name"),
128128
Password: types.StringValue("password"),
129-
Port: types.Int64Value(1234),
129+
Port: types.Int32Value(1234),
130130
Uri: types.StringValue("uri"),
131131
Username: types.StringValue("username"),
132132
},
@@ -139,16 +139,16 @@ func TestMapFields(t *testing.T) {
139139
ProjectId: types.StringValue("pid"),
140140
},
141141
&mariadb.CredentialsResponse{
142-
Id: new("cid"),
142+
Id: "cid",
143143
Raw: &mariadb.RawCredentials{
144-
Credentials: &mariadb.Credentials{
145-
Host: new(""),
146-
Hosts: &[]string{},
144+
Credentials: mariadb.Credentials{
145+
Host: "",
146+
Hosts: []string{},
147147
Name: nil,
148-
Password: new(""),
149-
Port: new(int64(2123456789)),
148+
Password: "",
149+
Port: new(int32(2123456789)),
150150
Uri: nil,
151-
Username: new(""),
151+
Username: "",
152152
},
153153
},
154154
},
@@ -161,7 +161,7 @@ func TestMapFields(t *testing.T) {
161161
Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
162162
Name: types.StringNull(),
163163
Password: types.StringValue(""),
164-
Port: types.Int64Value(2123456789),
164+
Port: types.Int32Value(2123456789),
165165
Uri: types.StringNull(),
166166
Username: types.StringValue(""),
167167
},
@@ -194,7 +194,7 @@ func TestMapFields(t *testing.T) {
194194
ProjectId: types.StringValue("pid"),
195195
},
196196
&mariadb.CredentialsResponse{
197-
Id: new("cid"),
197+
Id: "cid",
198198
},
199199
Model{},
200200
false,

stackit/internal/services/mariadb/instance/datasource.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1818

1919
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
20-
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
20+
mariadb "github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api"
2121
)
2222

2323
// Ensure the implementation satisfies the expected interfaces.
@@ -131,11 +131,11 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
131131
Description: parametersDescriptions["graphite"],
132132
Computed: true,
133133
},
134-
"max_disk_threshold": schema.Int64Attribute{
134+
"max_disk_threshold": schema.Int32Attribute{
135135
Description: parametersDescriptions["max_disk_threshold"],
136136
Computed: true,
137137
},
138-
"metrics_frequency": schema.Int64Attribute{
138+
"metrics_frequency": schema.Int32Attribute{
139139
Description: parametersDescriptions["metrics_frequency"],
140140
Computed: true,
141141
},
@@ -190,7 +190,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
190190
ctx = tflog.SetField(ctx, "project_id", projectId)
191191
ctx = tflog.SetField(ctx, "instance_id", instanceId)
192192

193-
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute()
193+
instanceResp, err := r.client.DefaultAPI.GetInstance(ctx, projectId, instanceId).Execute()
194194
if err != nil {
195195
utils.LogError(
196196
ctx,

0 commit comments

Comments
 (0)