Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/secretsmanager v0.18.0
github.com/stackitcloud/stackit-sdk-go/services/serverbackup v1.6.0
github.com/stackitcloud/stackit-sdk-go/services/serverupdate v1.5.0
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.12.0
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.18.1
github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v1.4.1
github.com/stackitcloud/stackit-sdk-go/services/sfs v0.6.3
github.com/stackitcloud/stackit-sdk-go/services/ske v1.12.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,8 @@ github.com/stackitcloud/stackit-sdk-go/services/serverbackup v1.6.0 h1:OGAaEbuox
github.com/stackitcloud/stackit-sdk-go/services/serverbackup v1.6.0/go.mod h1:h2fhcXRiSFP9yJXY8eb37e+2PhMW11g1GB8LL/EQ1aU=
github.com/stackitcloud/stackit-sdk-go/services/serverupdate v1.5.0 h1:4MYNb3VQjVnVPfJ9xhDbSQgoSkxQZJ0tsv9N7O43/RI=
github.com/stackitcloud/stackit-sdk-go/services/serverupdate v1.5.0/go.mod h1:iVCh5xZW/DHBMnJW4Zrw8KMhQIBRAETcnTPi5bbcQIE=
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.12.0 h1:l1EDIlXce2C8JcbBDHVa6nZ4SjPTqmnALTgrhms+NKI=
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.12.0/go.mod h1:EXq8/J7t9p8zPmdIq+atuxyAbnQwxrQT18fI+Qpv98k=
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.18.1 h1:T/5murYRgKIuieEY4PvLM0WipaSD2MsyR1VuQoKt+MQ=
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.18.1/go.mod h1:D8P9ZhWfXRE8gNQKUae9pyZGpskS949ezR7QQIGAMp8=
github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v1.4.1 h1:HZnZju8yqpvRIs71PEk54Jov6p+jiKIIlN+J+4tvcL0=
github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v1.4.1/go.mod h1:wBxlGcNeQPIh1aS4xYqJuN2z6haSHRwzne6drN5ROfM=
github.com/stackitcloud/stackit-sdk-go/services/sfs v0.6.3 h1:LEdPJ6f9pbrft+HlIIzRcCQog58b7UKVm4ObiOH8H4o=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"

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

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

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

Expand Down
30 changes: 11 additions & 19 deletions stackit/internal/services/serviceaccount/account/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"

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

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

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

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

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

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

return &serviceaccount.CreateServiceAccountPayload{
Name: conversion.StringValueToPointer(model.Name),
Name: model.Name.ValueString(),
}, nil
}

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

if resp.Email == nil {
return fmt.Errorf("service account email not present")
}

if resp.Id == nil {
return fmt.Errorf("service account id not present")
}

// Build the ID by combining the project ID and email and assign the model's fields.
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), *resp.Email)
model.ServiceAccountId = types.StringPointerValue(resp.Id)
model.Email = types.StringPointerValue(resp.Email)
model.ProjectId = types.StringPointerValue(resp.ProjectId)
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), resp.Email)
model.ServiceAccountId = types.StringValue(resp.Id)
model.Email = types.StringValue(resp.Email)
model.ProjectId = types.StringValue(resp.ProjectId)

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"
)

func TestToCreatePayload(t *testing.T) {
Expand All @@ -21,7 +21,7 @@ func TestToCreatePayload(t *testing.T) {
Name: types.StringValue("example-name1"),
},
&serviceaccount.CreateServiceAccountPayload{
Name: new("example-name1"),
Name: "example-name1",
},
true,
},
Expand Down Expand Up @@ -61,9 +61,9 @@ func TestMapFields(t *testing.T) {
{
"default_values",
&serviceaccount.ServiceAccount{
Id: new("550e8400-e29b-41d4-a716-446655440000"),
ProjectId: new("pid"),
Email: new("mail"),
Id: "550e8400-e29b-41d4-a716-446655440000",
ProjectId: "pid",
Email: "mail",
},
Model{
Id: types.StringValue("pid,mail"),
Expand All @@ -79,30 +79,6 @@ func TestMapFields(t *testing.T) {
Model{},
false,
},
{
"nil_response_2",
&serviceaccount.ServiceAccount{},
Model{},
false,
},
{
"no_email",
&serviceaccount.ServiceAccount{
ProjectId: new("pid"),
Id: new("550e8400-e29b-41d4-a716-446655440000"),
},
Model{},
false,
},
{
"no_id",
&serviceaccount.ServiceAccount{
ProjectId: new("pid"),
Email: new("mail"),
},
Model{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"

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

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

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

for _, sa := range apiItems {
if sa.Email == nil {
continue
}
email := *sa.Email
email := sa.Email

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

matchedItems = append(matchedItems, ServiceAccountItem{
ServiceAccountId: types.StringPointerValue(sa.Id),
ServiceAccountId: types.StringValue(sa.Id),
Email: types.StringValue(email),
Name: types.StringValue(nameStr),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"
)

func TestMapDataSourceFields(t *testing.T) {
Expand Down Expand Up @@ -34,9 +34,9 @@ func TestMapDataSourceFields(t *testing.T) {
{
description: "default_sort_descending",
apiItems: []serviceaccount.ServiceAccount{
{Email: new(emailA), Id: new(idA)},
{Email: new(emailC), Id: new(idC)},
{Email: new(emailB), Id: new(idB)},
{Email: emailA, Id: idA},
{Email: emailC, Id: idC},
{Email: emailB, Id: idB},
},
initialModel: ServiceAccountsModel{
ProjectId: types.StringValue(projectId),
Expand All @@ -57,9 +57,9 @@ func TestMapDataSourceFields(t *testing.T) {
{
description: "sort_ascending",
apiItems: []serviceaccount.ServiceAccount{
{Email: new(emailC), Id: new(idC)},
{Email: new(emailA), Id: new(idA)},
{Email: new(emailB), Id: new(idB)},
{Email: emailC, Id: idC},
{Email: emailA, Id: idA},
{Email: emailB, Id: idB},
},
initialModel: ServiceAccountsModel{
ProjectId: types.StringValue(projectId),
Expand All @@ -80,9 +80,9 @@ func TestMapDataSourceFields(t *testing.T) {
{
description: "regex_filter_match",
apiItems: []serviceaccount.ServiceAccount{
{Email: new(emailA), Id: new(idA)},
{Email: new(emailB), Id: new(idB)},
{Email: new(emailC), Id: new(idC)},
{Email: emailA, Id: idA},
{Email: emailB, Id: idB},
{Email: emailC, Id: idC},
},
initialModel: ServiceAccountsModel{
ProjectId: types.StringValue(projectId),
Expand All @@ -104,9 +104,9 @@ func TestMapDataSourceFields(t *testing.T) {
{
description: "suffix_filter_match",
apiItems: []serviceaccount.ServiceAccount{
{Email: new(emailA), Id: new(idA)},
{Email: new(emailB), Id: new(idB)},
{Email: new(emailC), Id: new(idC)},
{Email: emailA, Id: idA},
{Email: emailB, Id: idB},
{Email: emailC, Id: idC},
},
initialModel: ServiceAccountsModel{
ProjectId: types.StringValue(projectId),
Expand All @@ -122,26 +122,6 @@ func TestMapDataSourceFields(t *testing.T) {
},
isValid: true,
},
{
description: "skip_nil_email",
apiItems: []serviceaccount.ServiceAccount{
{Email: new(emailA), Id: new(idA)},
{Email: nil}, // Should be skipped
},
initialModel: ServiceAccountsModel{
ProjectId: types.StringValue(projectId),
SortAscending: types.BoolValue(true),
},
expectedModel: ServiceAccountsModel{
Id: types.StringValue(projectId),
ProjectId: types.StringValue(projectId),
SortAscending: types.BoolValue(true),
Items: []ServiceAccountItem{
{Email: types.StringValue(emailA), Name: types.StringValue(nameA), ServiceAccountId: types.StringValue(idA)},
},
},
isValid: true,
},
{
description: "nil_model",
apiItems: []serviceaccount.ServiceAccount{},
Expand Down
16 changes: 6 additions & 10 deletions stackit/internal/services/serviceaccount/key/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"

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

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

ctx = core.LogResponse(ctx)

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

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

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

if resp.Id == nil {
return fmt.Errorf("service account key id not present")
}

model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.ServiceAccountEmail.ValueString(), *resp.Id)
model.KeyId = types.StringPointerValue(resp.Id)
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.ServiceAccountEmail.ValueString(), resp.Id)
model.KeyId = types.StringValue(resp.Id)

jsonData, err := json.Marshal(resp)
if err != nil {
Expand Down
Loading
Loading