Skip to content

Commit 047903b

Browse files
Merge branch 'main' into chore/NO-ISSUE_goimports-local-prefixes
2 parents 28e7b19 + f30f3f1 commit 047903b

File tree

14 files changed

+270
-218
lines changed

14 files changed

+270
-218
lines changed

docs/data-sources/opensearch_instance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ Read-Only:
5959
- `sgw_acl` (String) Comma separated list of IP networks in CIDR notation which are allowed to access this instance.
6060
- `syslog` (List of String) List of syslog servers to send logs to.
6161
- `tls_ciphers` (List of String) List of TLS ciphers to use.
62-
- `tls_protocols` (String) The TLS protocol to use.
62+
- `tls_protocols` (List of String) List of TLS protocols to use.

golang-ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ linters:
4040
- typeDefFirst
4141
- ifElseChain
4242
- dupImport # https://github.com/go-critic/go-critic/issues/845
43+
- rangeValCopy
4344
enabled-tags:
4445
- performance
4546
- style

stackit/internal/conversion/conversion.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,21 @@ func BoolValueToPointer(s basetypes.BoolValue) *bool {
132132
// StringListToPointer converts basetypes.ListValue to a pointer to a list of strings.
133133
// It returns nil if the value is null or unknown.
134134
func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
135+
result, err := StringListToSlice(list)
136+
if result == nil {
137+
return nil, err
138+
}
139+
return &result, err
140+
}
141+
142+
// StringListToSlice converts basetypes.ListValue to a list of strings.
143+
// It returns nil if the value is null or unknown.
144+
func StringListToSlice(list basetypes.ListValue) ([]string, error) {
135145
if list.IsNull() || list.IsUnknown() {
136146
return nil, nil
137147
}
138148

139-
listStr := []string{}
149+
var listStr []string
140150
for i, el := range list.Elements() {
141151
elStr, ok := el.(types.String)
142152
if !ok {
@@ -145,7 +155,7 @@ func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
145155
listStr = append(listStr, elStr.ValueString())
146156
}
147157

148-
return &listStr, nil
158+
return listStr, nil
149159
}
150160

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

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

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

1919
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
2020
"github.com/hashicorp/terraform-plugin-framework/types"
21-
"github.com/stackitcloud/stackit-sdk-go/services/opensearch"
21+
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api"
2222
)
2323

2424
// Ensure the implementation satisfies the expected interfaces.
@@ -108,7 +108,7 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
108108
Computed: true,
109109
Sensitive: true,
110110
},
111-
"port": schema.Int64Attribute{
111+
"port": schema.Int32Attribute{
112112
Computed: true,
113113
},
114114
"scheme": schema.StringAttribute{
@@ -143,7 +143,7 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
143143
ctx = tflog.SetField(ctx, "instance_id", instanceId)
144144
ctx = tflog.SetField(ctx, "credential_id", credentialId)
145145

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

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

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2323
"github.com/hashicorp/terraform-plugin-framework/types"
2424
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
25-
"github.com/stackitcloud/stackit-sdk-go/services/opensearch"
26-
"github.com/stackitcloud/stackit-sdk-go/services/opensearch/wait"
25+
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api"
26+
"github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api/wait"
2727
)
2828

2929
// Ensure the implementation satisfies the expected interfaces.
@@ -41,7 +41,7 @@ type Model struct {
4141
Host types.String `tfsdk:"host"`
4242
Hosts types.List `tfsdk:"hosts"`
4343
Password types.String `tfsdk:"password"`
44-
Port types.Int64 `tfsdk:"port"`
44+
Port types.Int32 `tfsdk:"port"`
4545
Scheme types.String `tfsdk:"scheme"`
4646
Uri types.String `tfsdk:"uri"`
4747
Username types.String `tfsdk:"username"`
@@ -143,7 +143,7 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
143143
Computed: true,
144144
Sensitive: true,
145145
},
146-
"port": schema.Int64Attribute{
146+
"port": schema.Int32Attribute{
147147
Computed: true,
148148
},
149149
"scheme": schema.StringAttribute{
@@ -177,26 +177,22 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
177177
ctx = tflog.SetField(ctx, "instance_id", instanceId)
178178

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

186186
ctx = core.LogResponse(ctx)
187187

188-
if credentialsResp.Id == nil {
189-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
190-
return
191-
}
192-
credentialId := *credentialsResp.Id
188+
credentialId := credentialsResp.Id
193189
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
194190
"project_id": projectId,
195191
"instance_id": instanceId,
196192
"credential_id": credentialId,
197193
})
198194

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

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

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

296292
ctx = core.LogResponse(ctx)
297293

298-
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
294+
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
299295
if err != nil {
300296
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
301297
return
@@ -338,10 +334,8 @@ func mapFields(ctx context.Context, credentialsResp *opensearch.CredentialsRespo
338334
var credentialId string
339335
if model.CredentialId.ValueString() != "" {
340336
credentialId = model.CredentialId.ValueString()
341-
} else if credentialsResp.Id != nil {
342-
credentialId = *credentialsResp.Id
343337
} else {
344-
return fmt.Errorf("credentials id not present")
338+
credentialId = credentialsResp.Id
345339
}
346340

347341
model.Id = utils.BuildInternalTerraformId(
@@ -355,24 +349,22 @@ func mapFields(ctx context.Context, credentialsResp *opensearch.CredentialsRespo
355349

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

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

0 commit comments

Comments
 (0)