Skip to content

Commit 6668515

Browse files
committed
Merge branch 'chore/loadbalancer-multi-api-support-sdk' of https://github.com/stackitcloud/terraform-provider-stackit into chore/loadbalancer-multi-api-support-sdk
2 parents 2071411 + 3d85793 commit 6668515

File tree

23 files changed

+556
-477
lines changed

23 files changed

+556
-477
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: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ func Int64ValueToPointer(s basetypes.Int64Value) *int64 {
108108
return &value
109109
}
110110

111+
// Float32ValueToPointer converts basetypes.Float32Value to a pointer to float32.
112+
// It returns nil if the value is null or unknown.
113+
func Float32ValueToPointer(s basetypes.Float32Value) *float32 {
114+
if s.IsNull() || s.IsUnknown() {
115+
return nil
116+
}
117+
value := s.ValueFloat32()
118+
return &value
119+
}
120+
111121
// Float64ValueToPointer converts basetypes.Float64Value to a pointer to float64.
112122
// It returns nil if the value is null or unknown.
113123
func Float64ValueToPointer(s basetypes.Float64Value) *float64 {
@@ -131,11 +141,21 @@ func BoolValueToPointer(s basetypes.BoolValue) *bool {
131141
// StringListToPointer converts basetypes.ListValue to a pointer to a list of strings.
132142
// It returns nil if the value is null or unknown.
133143
func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
144+
result, err := StringListToSlice(list)
145+
if result == nil {
146+
return nil, err
147+
}
148+
return &result, err
149+
}
150+
151+
// StringListToSlice converts basetypes.ListValue to a list of strings.
152+
// It returns nil if the value is null or unknown.
153+
func StringListToSlice(list basetypes.ListValue) ([]string, error) {
134154
if list.IsNull() || list.IsUnknown() {
135155
return nil, nil
136156
}
137157

138-
listStr := []string{}
158+
var listStr []string
139159
for i, el := range list.Elements() {
140160
elStr, ok := el.(types.String)
141161
if !ok {
@@ -144,7 +164,7 @@ func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
144164
listStr = append(listStr, elStr.ValueString())
145165
}
146166

147-
return &listStr, nil
167+
return listStr, nil
148168
}
149169

150170
// 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/logme/credential/datasource.go

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

1818
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
19-
"github.com/stackitcloud/stackit-sdk-go/services/logme"
19+
logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
2020
)
2121

2222
// Ensure the implementation satisfies the expected interfaces.
@@ -31,7 +31,7 @@ func NewCredentialDataSource() datasource.DataSource {
3131

3232
// credentialDataSource is the data source implementation.
3333
type credentialDataSource struct {
34-
client *logme.APIClient
34+
client *logmeSdk.APIClient
3535
}
3636

3737
// Metadata returns the data source type name.
@@ -102,7 +102,7 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
102102
Computed: true,
103103
Sensitive: true,
104104
},
105-
"port": schema.Int64Attribute{
105+
"port": schema.Int32Attribute{
106106
Computed: true,
107107
},
108108
"uri": schema.StringAttribute{
@@ -134,7 +134,7 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
134134
ctx = tflog.SetField(ctx, "instance_id", instanceId)
135135
ctx = tflog.SetField(ctx, "credential_id", credentialId)
136136

137-
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
137+
recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
138138
if err != nil {
139139
utils.LogError(
140140
ctx,

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ 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/logme"
26-
"github.com/stackitcloud/stackit-sdk-go/services/logme/wait"
25+
26+
logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
27+
"github.com/stackitcloud/stackit-sdk-go/services/logme/v1api/wait"
2728
)
2829

2930
// Ensure the implementation satisfies the expected interfaces.
@@ -40,7 +41,7 @@ type Model struct {
4041
ProjectId types.String `tfsdk:"project_id"`
4142
Host types.String `tfsdk:"host"`
4243
Password types.String `tfsdk:"password"`
43-
Port types.Int64 `tfsdk:"port"`
44+
Port types.Int32 `tfsdk:"port"`
4445
Uri types.String `tfsdk:"uri"`
4546
Username types.String `tfsdk:"username"`
4647
}
@@ -52,7 +53,7 @@ func NewCredentialResource() resource.Resource {
5253

5354
// credentialResource is the resource implementation.
5455
type credentialResource struct {
55-
client *logme.APIClient
56+
client *logmeSdk.APIClient
5657
}
5758

5859
// Metadata returns the resource type name.
@@ -137,7 +138,7 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
137138
Computed: true,
138139
Sensitive: true,
139140
},
140-
"port": schema.Int64Attribute{
141+
"port": schema.Int32Attribute{
141142
Computed: true,
142143
},
143144
"uri": schema.StringAttribute{
@@ -168,19 +169,15 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
168169
ctx = tflog.SetField(ctx, "instance_id", instanceId)
169170

170171
// Create new recordset
171-
credentialsResp, err := r.client.CreateCredentials(ctx, projectId, instanceId).Execute()
172+
credentialsResp, err := r.client.DefaultAPI.CreateCredentials(ctx, projectId, instanceId).Execute()
172173
if err != nil {
173174
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Calling API: %v", err))
174175
return
175176
}
176177

177178
ctx = core.LogResponse(ctx)
178179

179-
if credentialsResp.Id == nil {
180-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
181-
return
182-
}
183-
credentialId := *credentialsResp.Id
180+
credentialId := credentialsResp.Id
184181
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
185182
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
186183
"project_id": projectId,
@@ -191,7 +188,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
191188
return
192189
}
193190

194-
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
191+
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
195192
if err != nil {
196193
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
197194
return
@@ -229,7 +226,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
229226
ctx = tflog.SetField(ctx, "instance_id", instanceId)
230227
ctx = tflog.SetField(ctx, "credential_id", credentialId)
231228

232-
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
229+
recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
233230
if err != nil {
234231
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
235232
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -283,14 +280,14 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
283280
ctx = tflog.SetField(ctx, "credential_id", credentialId)
284281

285282
// Delete existing record set
286-
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
283+
err := r.client.DefaultAPI.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
287284
if err != nil {
288285
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
289286
}
290287

291288
ctx = core.LogResponse(ctx)
292289

293-
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
290+
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
294291
if err != nil {
295292
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
296293
return
@@ -318,7 +315,7 @@ func (r *credentialResource) ImportState(ctx context.Context, req resource.Impor
318315
tflog.Info(ctx, "LogMe credential state imported")
319316
}
320317

321-
func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error {
318+
func mapFields(credentialsResp *logmeSdk.CredentialsResponse, model *Model) error {
322319
if credentialsResp == nil {
323320
return fmt.Errorf("response input is nil")
324321
}
@@ -333,20 +330,19 @@ func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error {
333330
var credentialId string
334331
if model.CredentialId.ValueString() != "" {
335332
credentialId = model.CredentialId.ValueString()
336-
} else if credentialsResp.Id != nil {
337-
credentialId = *credentialsResp.Id
333+
} else if credentialsResp.Id != "" {
334+
credentialId = credentialsResp.Id
338335
} else {
339336
return fmt.Errorf("credentials id not present")
340337
}
341338

342339
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.InstanceId.ValueString(), credentialId)
343340
model.CredentialId = types.StringValue(credentialId)
344-
if credentials != nil {
345-
model.Host = types.StringPointerValue(credentials.Host)
346-
model.Password = types.StringPointerValue(credentials.Password)
347-
model.Port = types.Int64PointerValue(credentials.Port)
348-
model.Uri = types.StringPointerValue(credentials.Uri)
349-
model.Username = types.StringPointerValue(credentials.Username)
350-
}
341+
model.Host = types.StringValue(credentials.Host)
342+
model.Password = types.StringValue(credentials.Password)
343+
model.Port = types.Int32PointerValue(credentials.Port)
344+
model.Uri = types.StringPointerValue(credentials.Uri)
345+
model.Username = types.StringValue(credentials.Username)
346+
351347
return nil
352348
}

0 commit comments

Comments
 (0)