Skip to content

Commit 7723874

Browse files
committed
chore(logme): Switch to new SDK structure
STACKITTPR-560 Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent db44fe3 commit 7723874

File tree

12 files changed

+354
-256
lines changed

12 files changed

+354
-256
lines changed

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: 20 additions & 0 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 {
@@ -147,6 +157,16 @@ func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
147157
return &listStr, nil
148158
}
149159

160+
// StringListToSlice converts basetypes.ListValue to a list of strings.
161+
// It returns nil if the value is null or unknown.
162+
func StringListToSlice(list basetypes.ListValue) ([]string, error) {
163+
ptr, err := StringListToPointer(list)
164+
if err != nil || ptr == nil {
165+
return nil, err
166+
}
167+
return *ptr, nil
168+
}
169+
150170
// StringSetToPointer converts basetypes.SetValue to a pointer to a list of strings.
151171
// It returns nil if the value is null or unknown.
152172
// Note: It sorts the resulting slice to ensure deterministic behavior.

stackit/internal/conversion/conversion_test.go

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

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: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ 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+
"github.com/stackitcloud/stackit-sdk-go/services/logme/v1api/wait"
27+
28+
logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
2729
)
2830

2931
// Ensure the implementation satisfies the expected interfaces.
@@ -40,7 +42,7 @@ type Model struct {
4042
ProjectId types.String `tfsdk:"project_id"`
4143
Host types.String `tfsdk:"host"`
4244
Password types.String `tfsdk:"password"`
43-
Port types.Int64 `tfsdk:"port"`
45+
Port types.Int32 `tfsdk:"port"`
4446
Uri types.String `tfsdk:"uri"`
4547
Username types.String `tfsdk:"username"`
4648
}
@@ -52,7 +54,7 @@ func NewCredentialResource() resource.Resource {
5254

5355
// credentialResource is the resource implementation.
5456
type credentialResource struct {
55-
client *logme.APIClient
57+
client *logmeSdk.APIClient
5658
}
5759

5860
// Metadata returns the resource type name.
@@ -137,7 +139,7 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
137139
Computed: true,
138140
Sensitive: true,
139141
},
140-
"port": schema.Int64Attribute{
142+
"port": schema.Int32Attribute{
141143
Computed: true,
142144
},
143145
"uri": schema.StringAttribute{
@@ -168,19 +170,19 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
168170
ctx = tflog.SetField(ctx, "instance_id", instanceId)
169171

170172
// Create new recordset
171-
credentialsResp, err := r.client.CreateCredentials(ctx, projectId, instanceId).Execute()
173+
credentialsResp, err := r.client.DefaultAPI.CreateCredentials(ctx, projectId, instanceId).Execute()
172174
if err != nil {
173175
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Calling API: %v", err))
174176
return
175177
}
176178

177179
ctx = core.LogResponse(ctx)
178180

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

194-
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
196+
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
195197
if err != nil {
196198
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
197199
return
@@ -229,7 +231,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
229231
ctx = tflog.SetField(ctx, "instance_id", instanceId)
230232
ctx = tflog.SetField(ctx, "credential_id", credentialId)
231233

232-
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
234+
recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
233235
if err != nil {
234236
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
235237
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -283,14 +285,14 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
283285
ctx = tflog.SetField(ctx, "credential_id", credentialId)
284286

285287
// Delete existing record set
286-
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
288+
err := r.client.DefaultAPI.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
287289
if err != nil {
288290
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
289291
}
290292

291293
ctx = core.LogResponse(ctx)
292294

293-
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
295+
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
294296
if err != nil {
295297
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
296298
return
@@ -318,7 +320,7 @@ func (r *credentialResource) ImportState(ctx context.Context, req resource.Impor
318320
tflog.Info(ctx, "LogMe credential state imported")
319321
}
320322

321-
func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error {
323+
func mapFields(credentialsResp *logmeSdk.CredentialsResponse, model *Model) error {
322324
if credentialsResp == nil {
323325
return fmt.Errorf("response input is nil")
324326
}
@@ -333,20 +335,19 @@ func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error {
333335
var credentialId string
334336
if model.CredentialId.ValueString() != "" {
335337
credentialId = model.CredentialId.ValueString()
336-
} else if credentialsResp.Id != nil {
337-
credentialId = *credentialsResp.Id
338+
} else if credentialsResp.Id != "" {
339+
credentialId = credentialsResp.Id
338340
} else {
339341
return fmt.Errorf("credentials id not present")
340342
}
341343

342344
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.InstanceId.ValueString(), credentialId)
343345
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-
}
346+
model.Host = types.StringValue(credentials.Host)
347+
model.Password = types.StringValue(credentials.Password)
348+
model.Port = types.Int32PointerValue(credentials.Port)
349+
model.Uri = types.StringPointerValue(credentials.Uri)
350+
model.Username = types.StringValue(credentials.Username)
351+
351352
return nil
352353
}

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,46 @@ import (
55

66
"github.com/google/go-cmp/cmp"
77
"github.com/hashicorp/terraform-plugin-framework/types"
8-
"github.com/stackitcloud/stackit-sdk-go/services/logme"
8+
logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
99
)
1010

1111
func TestMapFields(t *testing.T) {
1212
tests := []struct {
1313
description string
14-
input *logme.CredentialsResponse
14+
input *logmeSdk.CredentialsResponse
1515
expected Model
1616
isValid bool
1717
}{
1818
{
1919
"default_values",
20-
&logme.CredentialsResponse{
21-
Id: new("cid"),
22-
Raw: &logme.RawCredentials{},
20+
&logmeSdk.CredentialsResponse{
21+
Id: "cid",
22+
Raw: &logmeSdk.RawCredentials{},
2323
},
2424
Model{
2525
Id: types.StringValue("pid,iid,cid"),
2626
CredentialId: types.StringValue("cid"),
2727
InstanceId: types.StringValue("iid"),
2828
ProjectId: types.StringValue("pid"),
29-
Host: types.StringNull(),
30-
Password: types.StringNull(),
31-
Port: types.Int64Null(),
29+
Host: types.StringValue(""),
30+
Password: types.StringValue(""),
31+
Port: types.Int32Null(),
3232
Uri: types.StringNull(),
33-
Username: types.StringNull(),
33+
Username: types.StringValue(""),
3434
},
3535
true,
3636
},
3737
{
3838
"simple_values",
39-
&logme.CredentialsResponse{
40-
Id: new("cid"),
41-
Raw: &logme.RawCredentials{
42-
Credentials: &logme.Credentials{
43-
Host: new("host"),
44-
Password: new("password"),
45-
Port: new(int64(1234)),
39+
&logmeSdk.CredentialsResponse{
40+
Id: "cid",
41+
Raw: &logmeSdk.RawCredentials{
42+
Credentials: logmeSdk.Credentials{
43+
Host: "host",
44+
Password: "password",
45+
Port: new(int32(1234)),
4646
Uri: new("uri"),
47-
Username: new("username"),
47+
Username: "username",
4848
},
4949
},
5050
},
@@ -55,23 +55,23 @@ func TestMapFields(t *testing.T) {
5555
ProjectId: types.StringValue("pid"),
5656
Host: types.StringValue("host"),
5757
Password: types.StringValue("password"),
58-
Port: types.Int64Value(1234),
58+
Port: types.Int32Value(1234),
5959
Uri: types.StringValue("uri"),
6060
Username: types.StringValue("username"),
6161
},
6262
true,
6363
},
6464
{
6565
"null_fields_and_int_conversions",
66-
&logme.CredentialsResponse{
67-
Id: new("cid"),
68-
Raw: &logme.RawCredentials{
69-
Credentials: &logme.Credentials{
70-
Host: new(""),
71-
Password: new(""),
72-
Port: new(int64(2123456789)),
66+
&logmeSdk.CredentialsResponse{
67+
Id: "cid",
68+
Raw: &logmeSdk.RawCredentials{
69+
Credentials: logmeSdk.Credentials{
70+
Host: "",
71+
Password: "",
72+
Port: new(int32(2123456789)),
7373
Uri: nil,
74-
Username: new(""),
74+
Username: "",
7575
},
7676
},
7777
},
@@ -82,7 +82,7 @@ func TestMapFields(t *testing.T) {
8282
ProjectId: types.StringValue("pid"),
8383
Host: types.StringValue(""),
8484
Password: types.StringValue(""),
85-
Port: types.Int64Value(2123456789),
85+
Port: types.Int32Value(2123456789),
8686
Uri: types.StringNull(),
8787
Username: types.StringValue(""),
8888
},
@@ -96,14 +96,14 @@ func TestMapFields(t *testing.T) {
9696
},
9797
{
9898
"no_resource_id",
99-
&logme.CredentialsResponse{},
99+
&logmeSdk.CredentialsResponse{},
100100
Model{},
101101
false,
102102
},
103103
{
104104
"nil_raw_credential",
105-
&logme.CredentialsResponse{
106-
Id: new("cid"),
105+
&logmeSdk.CredentialsResponse{
106+
Id: "cid",
107107
},
108108
Model{},
109109
false,

0 commit comments

Comments
 (0)