Skip to content

Commit 32f5186

Browse files
committed
feat(opensearch) use versioned sdk
1 parent b66434a commit 32f5186

File tree

10 files changed

+232
-186
lines changed

10 files changed

+232
-186
lines changed

stackit/internal/conversion/conversion.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,18 @@ func BoolValueToPointer(s basetypes.BoolValue) *bool {
131131
// StringListToPointer converts basetypes.ListValue to a pointer to a list of strings.
132132
// It returns nil if the value is null or unknown.
133133
func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
134+
result, err := StringListToSlice(list)
135+
return &result, err
136+
}
137+
138+
// StringListToSlice converts basetypes.ListValue to a list of strings.
139+
// It returns nil if the value is null or unknown.
140+
func StringListToSlice(list basetypes.ListValue) ([]string, error) {
134141
if list.IsNull() || list.IsUnknown() {
135142
return nil, nil
136143
}
137144

138-
listStr := []string{}
145+
var listStr []string
139146
for i, el := range list.Elements() {
140147
elStr, ok := el.(types.String)
141148
if !ok {
@@ -144,7 +151,7 @@ func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
144151
listStr = append(listStr, elStr.ValueString())
145152
}
146153

147-
return &listStr, nil
154+
return listStr, nil
148155
}
149156

150157
// StringSetToPointer converts basetypes.SetValue to a pointer to a list of strings.

stackit/internal/conversion/conversion_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,59 @@ func TestStringSetToSlice(t *testing.T) {
449449
})
450450
}
451451
}
452+
453+
func TestStringListToSlice(t *testing.T) {
454+
t.Parallel()
455+
tests := []struct {
456+
name string
457+
in basetypes.ListValue
458+
want []string
459+
wantErr bool
460+
}{
461+
{
462+
name: "unknown",
463+
in: basetypes.NewListUnknown(types.StringType),
464+
want: nil,
465+
},
466+
{
467+
name: "null",
468+
in: basetypes.NewListNull(types.StringType),
469+
want: nil,
470+
},
471+
{
472+
name: "invalid type",
473+
in: basetypes.NewListValueMust(types.Int64Type, []attr.Value{types.Int64Value(123)}),
474+
wantErr: true,
475+
},
476+
{
477+
name: "some values",
478+
in: basetypes.NewListValueMust(
479+
types.StringType,
480+
[]attr.Value{
481+
types.StringValue("abc"),
482+
types.StringValue("xyz"),
483+
},
484+
),
485+
want: []string{
486+
"abc",
487+
"xyz",
488+
},
489+
},
490+
}
491+
492+
for _, tt := range tests {
493+
t.Run(tt.name, func(t *testing.T) {
494+
t.Parallel()
495+
got, err := StringListToSlice(tt.in)
496+
if tt.wantErr && err == nil {
497+
t.Fatal("expected error")
498+
}
499+
if !tt.wantErr && err != nil {
500+
t.Fatalf("expected no error, got: %v", err)
501+
}
502+
if d := cmp.Diff(got, tt.want); d != "" {
503+
t.Fatalf("no match, diff: %s", d)
504+
}
505+
})
506+
}
507+
}

stackit/internal/services/opensearch/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/opensearch"
20+
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api"
2121
)
2222

2323
// Ensure the implementation satisfies the expected interfaces.
@@ -107,7 +107,7 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
107107
Computed: true,
108108
Sensitive: true,
109109
},
110-
"port": schema.Int64Attribute{
110+
"port": schema.Int32Attribute{
111111
Computed: true,
112112
},
113113
"scheme": 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/opensearch/credential/resource.go

Lines changed: 25 additions & 33 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/opensearch"
25-
"github.com/stackitcloud/stackit-sdk-go/services/opensearch/wait"
24+
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api"
25+
"github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api/wait"
2626
)
2727

2828
// Ensure the implementation satisfies the expected interfaces.
@@ -40,7 +40,7 @@ type Model struct {
4040
Host types.String `tfsdk:"host"`
4141
Hosts types.List `tfsdk:"hosts"`
4242
Password types.String `tfsdk:"password"`
43-
Port types.Int64 `tfsdk:"port"`
43+
Port types.Int32 `tfsdk:"port"`
4444
Scheme types.String `tfsdk:"scheme"`
4545
Uri types.String `tfsdk:"uri"`
4646
Username types.String `tfsdk:"username"`
@@ -142,7 +142,7 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
142142
Computed: true,
143143
Sensitive: true,
144144
},
145-
"port": schema.Int64Attribute{
145+
"port": schema.Int32Attribute{
146146
Computed: true,
147147
},
148148
"scheme": schema.StringAttribute{
@@ -176,26 +176,22 @@ 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
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
193189
"project_id": projectId,
194190
"instance_id": instanceId,
195191
"credential_id": credentialId,
196192
})
197193

198-
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
194+
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
199195
if err != nil {
200196
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
201197
return
@@ -233,7 +229,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
233229
ctx = tflog.SetField(ctx, "instance_id", instanceId)
234230
ctx = tflog.SetField(ctx, "credential_id", credentialId)
235231

236-
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
232+
recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
237233
if err != nil {
238234
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
239235
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -287,14 +283,14 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
287283
ctx = tflog.SetField(ctx, "credential_id", credentialId)
288284

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

295291
ctx = core.LogResponse(ctx)
296292

297-
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
293+
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
298294
if err != nil {
299295
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
300296
return
@@ -337,10 +333,8 @@ func mapFields(ctx context.Context, credentialsResp *opensearch.CredentialsRespo
337333
var credentialId string
338334
if model.CredentialId.ValueString() != "" {
339335
credentialId = model.CredentialId.ValueString()
340-
} else if credentialsResp.Id != nil {
341-
credentialId = *credentialsResp.Id
342336
} else {
343-
return fmt.Errorf("credentials id not present")
337+
credentialId = credentialsResp.Id
344338
}
345339

346340
model.Id = utils.BuildInternalTerraformId(
@@ -354,24 +348,22 @@ func mapFields(ctx context.Context, credentialsResp *opensearch.CredentialsRespo
354348

355349
model.CredentialId = types.StringValue(credentialId)
356350
model.Hosts = types.ListNull(types.StringType)
357-
if credentials != nil {
358-
if credentials.Hosts != nil {
359-
respHosts := *credentials.Hosts
360-
reconciledHosts := utils.ReconcileStringSlices(modelHosts, respHosts)
351+
if credentials.Hosts != nil {
352+
respHosts := credentials.Hosts
353+
reconciledHosts := utils.ReconcileStringSlices(modelHosts, respHosts)
361354

362-
hostsTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledHosts)
363-
if diags.HasError() {
364-
return fmt.Errorf("failed to map hosts: %w", core.DiagsToError(diags))
365-
}
366-
367-
model.Hosts = hostsTF
355+
hostsTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledHosts)
356+
if diags.HasError() {
357+
return fmt.Errorf("failed to map hosts: %w", core.DiagsToError(diags))
368358
}
369-
model.Host = types.StringPointerValue(credentials.Host)
370-
model.Password = types.StringPointerValue(credentials.Password)
371-
model.Port = types.Int64PointerValue(credentials.Port)
372-
model.Scheme = types.StringPointerValue(credentials.Scheme)
373-
model.Uri = types.StringPointerValue(credentials.Uri)
374-
model.Username = types.StringPointerValue(credentials.Username)
359+
360+
model.Hosts = hostsTF
375361
}
362+
model.Host = types.StringValue(credentials.Host)
363+
model.Password = types.StringValue(credentials.Password)
364+
model.Port = types.Int32PointerValue(credentials.Port)
365+
model.Scheme = types.StringPointerValue(credentials.Scheme)
366+
model.Uri = types.StringPointerValue(credentials.Uri)
367+
model.Username = types.StringValue(credentials.Username)
376368
return nil
377369
}

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

Lines changed: 28 additions & 28 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/opensearch"
10+
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api"
1111
)
1212

1313
func TestMapFields(t *testing.T) {
@@ -25,7 +25,7 @@ func TestMapFields(t *testing.T) {
2525
ProjectId: types.StringValue("pid"),
2626
},
2727
&opensearch.CredentialsResponse{
28-
Id: new("cid"),
28+
Id: "cid",
2929
Raw: &opensearch.RawCredentials{},
3030
},
3131
Model{
@@ -36,7 +36,7 @@ func TestMapFields(t *testing.T) {
3636
Host: types.StringNull(),
3737
Hosts: types.ListNull(types.StringType),
3838
Password: types.StringNull(),
39-
Port: types.Int64Null(),
39+
Port: types.Int32Null(),
4040
Scheme: types.StringNull(),
4141
Uri: types.StringNull(),
4242
Username: types.StringNull(),
@@ -50,19 +50,19 @@ func TestMapFields(t *testing.T) {
5050
ProjectId: types.StringValue("pid"),
5151
},
5252
&opensearch.CredentialsResponse{
53-
Id: new("cid"),
53+
Id: "cid",
5454
Raw: &opensearch.RawCredentials{
55-
Credentials: &opensearch.Credentials{
56-
Host: new("host"),
57-
Hosts: &[]string{
55+
Credentials: opensearch.Credentials{
56+
Host: "host",
57+
Hosts: []string{
5858
"host_1",
5959
"",
6060
},
61-
Password: new("password"),
62-
Port: new(int64(1234)),
61+
Password: "password",
62+
Port: new(int32(1234)),
6363
Scheme: new("scheme"),
6464
Uri: new("uri"),
65-
Username: new("username"),
65+
Username: "username",
6666
},
6767
},
6868
},
@@ -77,7 +77,7 @@ func TestMapFields(t *testing.T) {
7777
types.StringValue(""),
7878
}),
7979
Password: types.StringValue("password"),
80-
Port: types.Int64Value(1234),
80+
Port: types.Int32Value(1234),
8181
Scheme: types.StringValue("scheme"),
8282
Uri: types.StringValue("uri"),
8383
Username: types.StringValue("username"),
@@ -96,20 +96,20 @@ func TestMapFields(t *testing.T) {
9696
}),
9797
},
9898
&opensearch.CredentialsResponse{
99-
Id: new("cid"),
99+
Id: "cid",
100100
Raw: &opensearch.RawCredentials{
101-
Credentials: &opensearch.Credentials{
102-
Host: new("host"),
103-
Hosts: &[]string{
101+
Credentials: opensearch.Credentials{
102+
Host: "host",
103+
Hosts: []string{
104104
"",
105105
"host_1",
106106
"host_2",
107107
},
108-
Password: new("password"),
109-
Port: new(int64(1234)),
108+
Password: "password",
109+
Port: new(int32(1234)),
110110
Scheme: new("scheme"),
111111
Uri: new("uri"),
112-
Username: new("username"),
112+
Username: "username",
113113
},
114114
},
115115
},
@@ -125,7 +125,7 @@ func TestMapFields(t *testing.T) {
125125
types.StringValue("host_1"),
126126
}),
127127
Password: types.StringValue("password"),
128-
Port: types.Int64Value(1234),
128+
Port: types.Int32Value(1234),
129129
Scheme: types.StringValue("scheme"),
130130
Uri: types.StringValue("uri"),
131131
Username: types.StringValue("username"),
@@ -139,16 +139,16 @@ func TestMapFields(t *testing.T) {
139139
ProjectId: types.StringValue("pid"),
140140
},
141141
&opensearch.CredentialsResponse{
142-
Id: new("cid"),
142+
Id: "cid",
143143
Raw: &opensearch.RawCredentials{
144-
Credentials: &opensearch.Credentials{
145-
Host: new(""),
146-
Hosts: &[]string{},
147-
Password: new(""),
148-
Port: new(int64(2123456789)),
144+
Credentials: opensearch.Credentials{
145+
Host: "",
146+
Hosts: []string{},
147+
Password: "",
148+
Port: new(int32(2123456789)),
149149
Scheme: nil,
150150
Uri: nil,
151-
Username: new(""),
151+
Username: "",
152152
},
153153
},
154154
},
@@ -160,7 +160,7 @@ func TestMapFields(t *testing.T) {
160160
Host: types.StringValue(""),
161161
Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
162162
Password: types.StringValue(""),
163-
Port: types.Int64Value(2123456789),
163+
Port: types.Int32Value(2123456789),
164164
Scheme: types.StringNull(),
165165
Uri: types.StringNull(),
166166
Username: types.StringValue(""),
@@ -194,7 +194,7 @@ func TestMapFields(t *testing.T) {
194194
ProjectId: types.StringValue("pid"),
195195
},
196196
&opensearch.CredentialsResponse{
197-
Id: new("cid"),
197+
Id: "cid",
198198
},
199199
Model{},
200200
false,

0 commit comments

Comments
 (0)