Skip to content

Commit 3158d9e

Browse files
s-interrubenhoenle
andauthored
chore(serviceaccount): migrate to new SDK structure (#1401)
* chore(serviceaccount): migrate to new SDK structure * chore(serviceaccount): replace utils.Contains with slices.Contains Co-authored-by: Ruben Hönle <Ruben.Hoenle@stackit.cloud> * chore(serviceaccount): remove unnecessary checks * chore(serviceaccount): remove redundant test cases --------- Co-authored-by: Ruben Hönle <Ruben.Hoenle@stackit.cloud>
1 parent b4822fb commit 3158d9e

12 files changed

Lines changed: 58 additions & 134 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/stackitcloud/stackit-sdk-go/services/secretsmanager v0.18.0
3939
github.com/stackitcloud/stackit-sdk-go/services/serverbackup v1.6.0
4040
github.com/stackitcloud/stackit-sdk-go/services/serverupdate v1.5.0
41-
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.12.0
41+
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.18.1
4242
github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v1.4.1
4343
github.com/stackitcloud/stackit-sdk-go/services/sfs v0.6.3
4444
github.com/stackitcloud/stackit-sdk-go/services/ske v1.12.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ github.com/stackitcloud/stackit-sdk-go/services/serverbackup v1.6.0 h1:OGAaEbuox
722722
github.com/stackitcloud/stackit-sdk-go/services/serverbackup v1.6.0/go.mod h1:h2fhcXRiSFP9yJXY8eb37e+2PhMW11g1GB8LL/EQ1aU=
723723
github.com/stackitcloud/stackit-sdk-go/services/serverupdate v1.5.0 h1:4MYNb3VQjVnVPfJ9xhDbSQgoSkxQZJ0tsv9N7O43/RI=
724724
github.com/stackitcloud/stackit-sdk-go/services/serverupdate v1.5.0/go.mod h1:iVCh5xZW/DHBMnJW4Zrw8KMhQIBRAETcnTPi5bbcQIE=
725-
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.12.0 h1:l1EDIlXce2C8JcbBDHVa6nZ4SjPTqmnALTgrhms+NKI=
726-
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.12.0/go.mod h1:EXq8/J7t9p8zPmdIq+atuxyAbnQwxrQT18fI+Qpv98k=
725+
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.18.1 h1:T/5murYRgKIuieEY4PvLM0WipaSD2MsyR1VuQoKt+MQ=
726+
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.18.1/go.mod h1:D8P9ZhWfXRE8gNQKUae9pyZGpskS949ezR7QQIGAMp8=
727727
github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v1.4.1 h1:HZnZju8yqpvRIs71PEk54Jov6p+jiKIIlN+J+4tvcL0=
728728
github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v1.4.1/go.mod h1:wBxlGcNeQPIh1aS4xYqJuN2z6haSHRwzne6drN5ROfM=
729729
github.com/stackitcloud/stackit-sdk-go/services/sfs v0.6.3 h1:LEdPJ6f9pbrft+HlIIzRcCQog58b7UKVm4ObiOH8H4o=

stackit/internal/services/serviceaccount/account/datasource.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1313
"github.com/hashicorp/terraform-plugin-framework/types"
1414
"github.com/hashicorp/terraform-plugin-log/tflog"
15-
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
15+
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"
1616

1717
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1818
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
@@ -115,7 +115,7 @@ func (r *serviceAccountDataSource) Read(ctx context.Context, req datasource.Read
115115
projectId := model.ProjectId.ValueString()
116116

117117
// Call the API to list service accounts in the specified project
118-
listSaResp, err := r.client.ListServiceAccounts(ctx, projectId).Execute()
118+
listSaResp, err := r.client.DefaultAPI.ListServiceAccounts(ctx, projectId).Execute()
119119
if err != nil {
120120
utils.LogError(
121121
ctx,
@@ -132,10 +132,10 @@ func (r *serviceAccountDataSource) Read(ctx context.Context, req datasource.Read
132132
ctx = core.LogResponse(ctx)
133133

134134
// Iterate over the service accounts returned by the API to find the one matching the email
135-
serviceAccounts := *listSaResp.Items
135+
serviceAccounts := listSaResp.Items
136136
for i := range serviceAccounts {
137137
// Skip if the service account email does not match
138-
if *serviceAccounts[i].Email != model.Email.ValueString() {
138+
if serviceAccounts[i].Email != model.Email.ValueString() {
139139
continue
140140
}
141141

stackit/internal/services/serviceaccount/account/resource.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2121
"github.com/hashicorp/terraform-plugin-framework/types"
2222
"github.com/hashicorp/terraform-plugin-log/tflog"
23-
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
23+
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"
2424

2525
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2626
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
@@ -154,7 +154,7 @@ func (r *serviceAccountResource) Create(ctx context.Context, req resource.Create
154154
}
155155

156156
// Create the new service account via the API client.
157-
serviceAccountResp, err := r.client.CreateServiceAccount(ctx, projectId).CreateServiceAccountPayload(*payload).Execute()
157+
serviceAccountResp, err := r.client.DefaultAPI.CreateServiceAccount(ctx, projectId).CreateServiceAccountPayload(*payload).Execute()
158158
if err != nil {
159159
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating service account", fmt.Sprintf("Calling API: %v", err))
160160
return
@@ -198,7 +198,7 @@ func (r *serviceAccountResource) Read(ctx context.Context, req resource.ReadRequ
198198
projectId := model.ProjectId.ValueString()
199199

200200
// Fetch the list of service accounts from the API.
201-
listSaResp, err := r.client.ListServiceAccounts(ctx, projectId).Execute()
201+
listSaResp, err := r.client.DefaultAPI.ListServiceAccounts(ctx, projectId).Execute()
202202
if err != nil {
203203
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading service account", fmt.Sprintf("Error calling API: %v", err))
204204
return
@@ -207,9 +207,9 @@ func (r *serviceAccountResource) Read(ctx context.Context, req resource.ReadRequ
207207
ctx = core.LogResponse(ctx)
208208

209209
// Iterate over the list of service accounts to find the one that matches the email from the state.
210-
serviceAccounts := *listSaResp.Items
210+
serviceAccounts := listSaResp.Items
211211
for i := range serviceAccounts {
212-
if *serviceAccounts[i].Email != model.Email.ValueString() {
212+
if serviceAccounts[i].Email != model.Email.ValueString() {
213213
continue
214214
}
215215

@@ -255,7 +255,7 @@ func (r *serviceAccountResource) Delete(ctx context.Context, req resource.Delete
255255
ctx = tflog.SetField(ctx, "service_account_name", serviceAccountName)
256256

257257
// Call API to delete the existing service account.
258-
err := r.client.DeleteServiceAccount(ctx, projectId, serviceAccountEmail).Execute()
258+
err := r.client.DefaultAPI.DeleteServiceAccount(ctx, projectId, serviceAccountEmail).Execute()
259259
if err != nil {
260260
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting service account", fmt.Sprintf("Calling API: %v", err))
261261
return
@@ -303,7 +303,7 @@ func toCreatePayload(model *Model) (*serviceaccount.CreateServiceAccountPayload,
303303
}
304304

305305
return &serviceaccount.CreateServiceAccountPayload{
306-
Name: conversion.StringValueToPointer(model.Name),
306+
Name: model.Name.ValueString(),
307307
}, nil
308308
}
309309

@@ -316,19 +316,11 @@ func mapFields(resp *serviceaccount.ServiceAccount, model *Model) error {
316316
return fmt.Errorf("model input is nil")
317317
}
318318

319-
if resp.Email == nil {
320-
return fmt.Errorf("service account email not present")
321-
}
322-
323-
if resp.Id == nil {
324-
return fmt.Errorf("service account id not present")
325-
}
326-
327319
// Build the ID by combining the project ID and email and assign the model's fields.
328-
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), *resp.Email)
329-
model.ServiceAccountId = types.StringPointerValue(resp.Id)
330-
model.Email = types.StringPointerValue(resp.Email)
331-
model.ProjectId = types.StringPointerValue(resp.ProjectId)
320+
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), resp.Email)
321+
model.ServiceAccountId = types.StringValue(resp.Id)
322+
model.Email = types.StringValue(resp.Email)
323+
model.ProjectId = types.StringValue(resp.ProjectId)
332324

333325
return nil
334326
}

stackit/internal/services/serviceaccount/account/resource_test.go

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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/serviceaccount"
8+
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"
99
)
1010

1111
func TestToCreatePayload(t *testing.T) {
@@ -21,7 +21,7 @@ func TestToCreatePayload(t *testing.T) {
2121
Name: types.StringValue("example-name1"),
2222
},
2323
&serviceaccount.CreateServiceAccountPayload{
24-
Name: new("example-name1"),
24+
Name: "example-name1",
2525
},
2626
true,
2727
},
@@ -61,9 +61,9 @@ func TestMapFields(t *testing.T) {
6161
{
6262
"default_values",
6363
&serviceaccount.ServiceAccount{
64-
Id: new("550e8400-e29b-41d4-a716-446655440000"),
65-
ProjectId: new("pid"),
66-
Email: new("mail"),
64+
Id: "550e8400-e29b-41d4-a716-446655440000",
65+
ProjectId: "pid",
66+
Email: "mail",
6767
},
6868
Model{
6969
Id: types.StringValue("pid,mail"),
@@ -79,30 +79,6 @@ func TestMapFields(t *testing.T) {
7979
Model{},
8080
false,
8181
},
82-
{
83-
"nil_response_2",
84-
&serviceaccount.ServiceAccount{},
85-
Model{},
86-
false,
87-
},
88-
{
89-
"no_email",
90-
&serviceaccount.ServiceAccount{
91-
ProjectId: new("pid"),
92-
Id: new("550e8400-e29b-41d4-a716-446655440000"),
93-
},
94-
Model{},
95-
false,
96-
},
97-
{
98-
"no_id",
99-
&serviceaccount.ServiceAccount{
100-
ProjectId: new("pid"),
101-
Email: new("mail"),
102-
},
103-
Model{},
104-
false,
105-
},
10682
}
10783
for _, tt := range tests {
10884
t.Run(tt.description, func(t *testing.T) {

stackit/internal/services/serviceaccount/accounts/datasource.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1818
"github.com/hashicorp/terraform-plugin-framework/types"
1919
"github.com/hashicorp/terraform-plugin-log/tflog"
20-
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
20+
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"
2121

2222
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2323
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
@@ -158,7 +158,7 @@ func (r *serviceAccountsDataSource) Read(ctx context.Context, req datasource.Rea
158158
}
159159

160160
// Fetch all service accounts
161-
listSaResp, err := r.client.ListServiceAccounts(ctx, projectId).Execute()
161+
listSaResp, err := r.client.DefaultAPI.ListServiceAccounts(ctx, projectId).Execute()
162162
if err != nil {
163163
utils.LogError(
164164
ctx,
@@ -175,7 +175,7 @@ func (r *serviceAccountsDataSource) Read(ctx context.Context, req datasource.Rea
175175
ctx = core.LogResponse(ctx)
176176

177177
// Map the response data (filter, sort, and assign) to the model.
178-
err = mapDataSourceFields(*listSaResp.Items, &model, compiledRegex)
178+
err = mapDataSourceFields(listSaResp.Items, &model, compiledRegex)
179179
if err != nil {
180180
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading service accounts", fmt.Sprintf("Error processing API response: %v", err))
181181
return
@@ -195,10 +195,7 @@ func mapDataSourceFields(apiItems []serviceaccount.ServiceAccount, model *Servic
195195
emailSuffix := model.EmailSuffix.ValueString()
196196

197197
for _, sa := range apiItems {
198-
if sa.Email == nil {
199-
continue
200-
}
201-
email := *sa.Email
198+
email := sa.Email
202199

203200
// Apply Filters (If neither is set, these checks simply pass)
204201
if compiledRegex != nil && !compiledRegex.MatchString(email) {
@@ -212,7 +209,7 @@ func mapDataSourceFields(apiItems []serviceaccount.ServiceAccount, model *Servic
212209
nameStr, _ := serviceaccountUtils.ParseNameFromEmail(email)
213210

214211
matchedItems = append(matchedItems, ServiceAccountItem{
215-
ServiceAccountId: types.StringPointerValue(sa.Id),
212+
ServiceAccountId: types.StringValue(sa.Id),
216213
Email: types.StringValue(email),
217214
Name: types.StringValue(nameStr),
218215
})

stackit/internal/services/serviceaccount/accounts/datasource_test.go

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/google/go-cmp/cmp"
88
"github.com/hashicorp/terraform-plugin-framework/types"
9-
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
9+
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"
1010
)
1111

1212
func TestMapDataSourceFields(t *testing.T) {
@@ -34,9 +34,9 @@ func TestMapDataSourceFields(t *testing.T) {
3434
{
3535
description: "default_sort_descending",
3636
apiItems: []serviceaccount.ServiceAccount{
37-
{Email: new(emailA), Id: new(idA)},
38-
{Email: new(emailC), Id: new(idC)},
39-
{Email: new(emailB), Id: new(idB)},
37+
{Email: emailA, Id: idA},
38+
{Email: emailC, Id: idC},
39+
{Email: emailB, Id: idB},
4040
},
4141
initialModel: ServiceAccountsModel{
4242
ProjectId: types.StringValue(projectId),
@@ -57,9 +57,9 @@ func TestMapDataSourceFields(t *testing.T) {
5757
{
5858
description: "sort_ascending",
5959
apiItems: []serviceaccount.ServiceAccount{
60-
{Email: new(emailC), Id: new(idC)},
61-
{Email: new(emailA), Id: new(idA)},
62-
{Email: new(emailB), Id: new(idB)},
60+
{Email: emailC, Id: idC},
61+
{Email: emailA, Id: idA},
62+
{Email: emailB, Id: idB},
6363
},
6464
initialModel: ServiceAccountsModel{
6565
ProjectId: types.StringValue(projectId),
@@ -80,9 +80,9 @@ func TestMapDataSourceFields(t *testing.T) {
8080
{
8181
description: "regex_filter_match",
8282
apiItems: []serviceaccount.ServiceAccount{
83-
{Email: new(emailA), Id: new(idA)},
84-
{Email: new(emailB), Id: new(idB)},
85-
{Email: new(emailC), Id: new(idC)},
83+
{Email: emailA, Id: idA},
84+
{Email: emailB, Id: idB},
85+
{Email: emailC, Id: idC},
8686
},
8787
initialModel: ServiceAccountsModel{
8888
ProjectId: types.StringValue(projectId),
@@ -104,9 +104,9 @@ func TestMapDataSourceFields(t *testing.T) {
104104
{
105105
description: "suffix_filter_match",
106106
apiItems: []serviceaccount.ServiceAccount{
107-
{Email: new(emailA), Id: new(idA)},
108-
{Email: new(emailB), Id: new(idB)},
109-
{Email: new(emailC), Id: new(idC)},
107+
{Email: emailA, Id: idA},
108+
{Email: emailB, Id: idB},
109+
{Email: emailC, Id: idC},
110110
},
111111
initialModel: ServiceAccountsModel{
112112
ProjectId: types.StringValue(projectId),
@@ -122,26 +122,6 @@ func TestMapDataSourceFields(t *testing.T) {
122122
},
123123
isValid: true,
124124
},
125-
{
126-
description: "skip_nil_email",
127-
apiItems: []serviceaccount.ServiceAccount{
128-
{Email: new(emailA), Id: new(idA)},
129-
{Email: nil}, // Should be skipped
130-
},
131-
initialModel: ServiceAccountsModel{
132-
ProjectId: types.StringValue(projectId),
133-
SortAscending: types.BoolValue(true),
134-
},
135-
expectedModel: ServiceAccountsModel{
136-
Id: types.StringValue(projectId),
137-
ProjectId: types.StringValue(projectId),
138-
SortAscending: types.BoolValue(true),
139-
Items: []ServiceAccountItem{
140-
{Email: types.StringValue(emailA), Name: types.StringValue(nameA), ServiceAccountId: types.StringValue(idA)},
141-
},
142-
},
143-
isValid: true,
144-
},
145125
{
146126
description: "nil_model",
147127
apiItems: []serviceaccount.ServiceAccount{},

stackit/internal/services/serviceaccount/key/resource.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/hashicorp/terraform-plugin-framework/types"
2222
"github.com/hashicorp/terraform-plugin-log/tflog"
2323
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
24-
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
24+
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"
2525

2626
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2727
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
@@ -183,7 +183,7 @@ func (r *serviceAccountKeyResource) Create(ctx context.Context, req resource.Cre
183183
}
184184

185185
// Initialize the API request with the required parameters.
186-
saAccountKeyResp, err := r.client.CreateServiceAccountKey(ctx, projectId, serviceAccountEmail).CreateServiceAccountKeyPayload(*payload).Execute()
186+
saAccountKeyResp, err := r.client.DefaultAPI.CreateServiceAccountKey(ctx, projectId, serviceAccountEmail).CreateServiceAccountKeyPayload(*payload).Execute()
187187

188188
ctx = core.LogResponse(ctx)
189189

@@ -224,7 +224,7 @@ func (r *serviceAccountKeyResource) Read(ctx context.Context, req resource.ReadR
224224
serviceAccountEmail := model.ServiceAccountEmail.ValueString()
225225
keyId := model.KeyId.ValueString()
226226

227-
_, err := r.client.GetServiceAccountKey(ctx, projectId, serviceAccountEmail, keyId).Execute()
227+
_, err := r.client.DefaultAPI.GetServiceAccountKey(ctx, projectId, serviceAccountEmail, keyId).Execute()
228228
if err != nil {
229229
var oapiErr *oapierror.GenericOpenAPIError
230230
ok := errors.As(err, &oapiErr)
@@ -279,7 +279,7 @@ func (r *serviceAccountKeyResource) Delete(ctx context.Context, req resource.Del
279279
ctx = tflog.SetField(ctx, "key_id", keyId)
280280

281281
// Call API to delete the existing service account key.
282-
err := r.client.DeleteServiceAccountKey(ctx, projectId, serviceAccountEmail, keyId).Execute()
282+
err := r.client.DefaultAPI.DeleteServiceAccountKey(ctx, projectId, serviceAccountEmail, keyId).Execute()
283283
if err != nil {
284284
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting service account key", fmt.Sprintf("Calling API: %v", err))
285285
return
@@ -333,12 +333,8 @@ func mapCreateResponse(resp *serviceaccount.CreateServiceAccountKeyResponse, mod
333333
return fmt.Errorf("service account key response is nil")
334334
}
335335

336-
if resp.Id == nil {
337-
return fmt.Errorf("service account key id not present")
338-
}
339-
340-
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.ServiceAccountEmail.ValueString(), *resp.Id)
341-
model.KeyId = types.StringPointerValue(resp.Id)
336+
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.ServiceAccountEmail.ValueString(), resp.Id)
337+
model.KeyId = types.StringValue(resp.Id)
342338

343339
jsonData, err := json.Marshal(resp)
344340
if err != nil {

0 commit comments

Comments
 (0)