Skip to content

Commit 0b22580

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

File tree

10 files changed

+286
-258
lines changed

10 files changed

+286
-258
lines changed

stackit/internal/conversion/conversion.go

Lines changed: 10 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 {

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
}

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,

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

Lines changed: 12 additions & 12 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/logme"
20+
logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
2121
)
2222

2323
// Ensure the implementation satisfies the expected interfaces.
@@ -32,7 +32,7 @@ func NewInstanceDataSource() datasource.DataSource {
3232

3333
// instanceDataSource is the data source implementation.
3434
type instanceDataSource struct {
35-
client *logme.APIClient
35+
client *logmeSdk.APIClient
3636
}
3737

3838
// Metadata returns the data source type name.
@@ -133,11 +133,11 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
133133
Description: parametersDescriptions["enable_monitoring"],
134134
Computed: true,
135135
},
136-
"fluentd_tcp": schema.Int64Attribute{
136+
"fluentd_tcp": schema.Int32Attribute{
137137
Description: parametersDescriptions["fluentd_tcp"],
138138
Computed: true,
139139
},
140-
"fluentd_tls": schema.Int64Attribute{
140+
"fluentd_tls": schema.Int32Attribute{
141141
Description: parametersDescriptions["fluentd_tls"],
142142
Computed: true,
143143
},
@@ -157,7 +157,7 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
157157
Description: parametersDescriptions["fluentd_tls_version"],
158158
Computed: true,
159159
},
160-
"fluentd_udp": schema.Int64Attribute{
160+
"fluentd_udp": schema.Int32Attribute{
161161
Description: parametersDescriptions["fluentd_udp"],
162162
Computed: true,
163163
},
@@ -169,27 +169,27 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
169169
Description: parametersDescriptions["ism_deletion_after"],
170170
Computed: true,
171171
},
172-
"ism_jitter": schema.Float64Attribute{
172+
"ism_jitter": schema.Float32Attribute{
173173
Description: parametersDescriptions["ism_jitter"],
174174
Computed: true,
175175
},
176-
"ism_job_interval": schema.Int64Attribute{
176+
"ism_job_interval": schema.Int32Attribute{
177177
Description: parametersDescriptions["ism_job_interval"],
178178
Computed: true,
179179
},
180-
"java_heapspace": schema.Int64Attribute{
180+
"java_heapspace": schema.Int32Attribute{
181181
Description: parametersDescriptions["java_heapspace"],
182182
Computed: true,
183183
},
184-
"java_maxmetaspace": schema.Int64Attribute{
184+
"java_maxmetaspace": schema.Int32Attribute{
185185
Description: parametersDescriptions["java_maxmetaspace"],
186186
Computed: true,
187187
},
188-
"max_disk_threshold": schema.Int64Attribute{
188+
"max_disk_threshold": schema.Int32Attribute{
189189
Description: parametersDescriptions["max_disk_threshold"],
190190
Computed: true,
191191
},
192-
"metrics_frequency": schema.Int64Attribute{
192+
"metrics_frequency": schema.Int32Attribute{
193193
Description: parametersDescriptions["metrics_frequency"],
194194
Computed: true,
195195
},
@@ -254,7 +254,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
254254
ctx = tflog.SetField(ctx, "project_id", projectId)
255255
ctx = tflog.SetField(ctx, "instance_id", instanceId)
256256

257-
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute()
257+
instanceResp, err := r.client.DefaultAPI.GetInstance(ctx, projectId, instanceId).Execute()
258258
if err != nil {
259259
utils.LogError(
260260
ctx,

0 commit comments

Comments
 (0)