Skip to content
4 changes: 2 additions & 2 deletions stackit/internal/services/scf/organization/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/services/scf"
scf "github.com/stackitcloud/stackit-sdk-go/services/scf/v1api"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
Expand Down Expand Up @@ -149,7 +149,7 @@ func (s *scfOrganizationDataSource) Read(ctx context.Context, request datasource
ctx = tflog.SetField(ctx, "region", region)

// Read the current scf organization via orgId
scfOrgResponse, err := s.client.GetOrganization(ctx, projectId, region, orgId).Execute()
scfOrgResponse, err := s.client.DefaultAPI.GetOrganization(ctx, projectId, region, orgId).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down
83 changes: 26 additions & 57 deletions stackit/internal/services/scf/organization/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ 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/scf"
"github.com/stackitcloud/stackit-sdk-go/services/scf/wait"
scf "github.com/stackitcloud/stackit-sdk-go/services/scf/v1api"
"github.com/stackitcloud/stackit-sdk-go/services/scf/v1api/wait"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
Expand Down Expand Up @@ -249,7 +249,7 @@ func (s *scfOrganizationResource) Create(ctx context.Context, request resource.C
}

// Create the new scf organization via the API client.
scfOrgCreateResponse, err := s.client.CreateOrganization(ctx, projectId, region).
scfOrgCreateResponse, err := s.client.DefaultAPI.CreateOrganization(ctx, projectId, region).
CreateOrganizationPayload(payload).
Execute()
if err != nil {
Expand All @@ -259,11 +259,7 @@ func (s *scfOrganizationResource) Create(ctx context.Context, request resource.C

ctx = core.LogResponse(ctx)

if scfOrgCreateResponse.Guid == nil {
core.LogAndAddError(ctx, &response.Diagnostics, "Error creating scf organization", "API response did not include org ID")
return
}
orgId := *scfOrgCreateResponse.Guid
orgId := scfOrgCreateResponse.Guid

ctx = utils.SetAndLogStateFields(ctx, &response.Diagnostics, &response.State, map[string]any{
"project_id": projectId,
Expand All @@ -273,19 +269,19 @@ func (s *scfOrganizationResource) Create(ctx context.Context, request resource.C

// Apply the org quota if provided
if quotaId != "" {
applyOrgQuota, err := s.client.ApplyOrganizationQuota(ctx, projectId, region, orgId).ApplyOrganizationQuotaPayload(
applyOrgQuota, err := s.client.DefaultAPI.ApplyOrganizationQuota(ctx, projectId, region, orgId).ApplyOrganizationQuotaPayload(
scf.ApplyOrganizationQuotaPayload{
QuotaId: &quotaId,
QuotaId: quotaId,
}).Execute()
if err != nil {
core.LogAndAddError(ctx, &response.Diagnostics, "Error creating scf organization", fmt.Sprintf("Calling API to apply quota: %v", err))
return
}
model.QuotaId = types.StringPointerValue(applyOrgQuota.QuotaId)
model.QuotaId = types.StringValue(applyOrgQuota.QuotaId)
}

if suspended {
_, err := s.client.UpdateOrganization(ctx, projectId, region, orgId).UpdateOrganizationPayload(
_, err := s.client.DefaultAPI.UpdateOrganization(ctx, projectId, region, orgId).UpdateOrganizationPayload(

scf.UpdateOrganizationPayload{
Suspended: &suspended,
Expand All @@ -297,7 +293,7 @@ func (s *scfOrganizationResource) Create(ctx context.Context, request resource.C
}

// Load the newly created scf organization
scfOrgResponse, err := s.client.GetOrganization(ctx, projectId, region, orgId).Execute()
scfOrgResponse, err := s.client.DefaultAPI.GetOrganization(ctx, projectId, region, orgId).Execute()
if err != nil {
core.LogAndAddError(ctx, &response.Diagnostics, "Error creating scf organization", fmt.Sprintf("Calling API to load created org: %v", err))
return
Expand Down Expand Up @@ -339,7 +335,7 @@ func (s *scfOrganizationResource) Read(ctx context.Context, request resource.Rea
ctx = tflog.SetField(ctx, "org_id", orgId)
ctx = tflog.SetField(ctx, "region", region)
// Read the current scf organization via guid
scfOrgResponse, err := s.client.GetOrganization(ctx, projectId, region, orgId).Execute()
scfOrgResponse, err := s.client.DefaultAPI.GetOrganization(ctx, projectId, region, orgId).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
Expand Down Expand Up @@ -388,15 +384,15 @@ func (s *scfOrganizationResource) Update(ctx context.Context, request resource.U
ctx = tflog.SetField(ctx, "org_id", orgId)
ctx = tflog.SetField(ctx, "region", region)

org, err := s.client.GetOrganization(ctx, projectId, region, orgId).Execute()
org, err := s.client.DefaultAPI.GetOrganization(ctx, projectId, region, orgId).Execute()
if err != nil {
core.LogAndAddError(ctx, &response.Diagnostics, "Error retrieving organization state", fmt.Sprintf("Getting organization state: %v", err))
return
}

// handle a change of the organization name or the suspended flag
if name != org.GetName() || suspended != org.GetSuspended() {
updatedOrg, err := s.client.UpdateOrganization(ctx, projectId, region, orgId).UpdateOrganizationPayload(
updatedOrg, err := s.client.DefaultAPI.UpdateOrganization(ctx, projectId, region, orgId).UpdateOrganizationPayload(
scf.UpdateOrganizationPayload{
Name: &name,
Suspended: &suspended,
Expand All @@ -412,9 +408,9 @@ func (s *scfOrganizationResource) Update(ctx context.Context, request resource.U

// handle a quota change of the org
if quotaId != org.GetQuotaId() {
applyOrgQuota, err := s.client.ApplyOrganizationQuota(ctx, projectId, region, orgId).ApplyOrganizationQuotaPayload(
applyOrgQuota, err := s.client.DefaultAPI.ApplyOrganizationQuota(ctx, projectId, region, orgId).ApplyOrganizationQuotaPayload(
scf.ApplyOrganizationQuotaPayload{
QuotaId: &quotaId,
QuotaId: quotaId,
}).Execute()
if err != nil {
core.LogAndAddError(ctx, &response.Diagnostics, "Error applying organization quota", fmt.Sprintf("Processing API payload: %v", err))
Expand Down Expand Up @@ -460,15 +456,15 @@ func (s *scfOrganizationResource) Delete(ctx context.Context, request resource.D
ctx = tflog.SetField(ctx, "region", region)

// Call API to delete the existing scf organization.
_, err := s.client.DeleteOrganization(ctx, projectId, region, orgId).Execute()
_, err := s.client.DefaultAPI.DeleteOrganization(ctx, projectId, region, orgId).Execute()
if err != nil {
core.LogAndAddError(ctx, &response.Diagnostics, "Error deleting scf organization", fmt.Sprintf("Calling API: %v", err))
return
}

ctx = core.LogResponse(ctx)

_, err = wait.DeleteOrganizationWaitHandler(ctx, s.client, projectId, model.Region.ValueString(), orgId).WaitWithContext(ctx)
_, err = wait.DeleteOrganizationWaitHandler(ctx, s.client.DefaultAPI, projectId, model.Region.ValueString(), orgId).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &response.Diagnostics, "Error waiting for scf org deletion", fmt.Sprintf("SCFOrganization deleting waiting: %v", err))
return
Expand Down Expand Up @@ -511,43 +507,16 @@ func mapFields(response *scf.Organization, model *Model) error {
return fmt.Errorf("model input is nil")
}

var orgId string
if response.Guid != nil {
orgId = *response.Guid
} else if model.OrgId.ValueString() != "" {
orgId = model.OrgId.ValueString()
} else {
return fmt.Errorf("org id is not present")
}

var projectId string
if response.ProjectId != nil {
projectId = *response.ProjectId
} else if model.ProjectId.ValueString() != "" {
projectId = model.ProjectId.ValueString()
} else {
return fmt.Errorf("project id is not present")
}

var region string
if response.Region != nil {
region = *response.Region
} else if model.Region.ValueString() != "" {
region = model.Region.ValueString()
} else {
return fmt.Errorf("region is not present")
}

// Build the ID by combining the project ID and organization id and assign the model's fields.
model.Id = utils.BuildInternalTerraformId(projectId, region, orgId)
model.ProjectId = types.StringValue(projectId)
model.Region = types.StringValue(region)
model.PlatformId = types.StringPointerValue(response.PlatformId)
model.OrgId = types.StringValue(orgId)
model.Name = types.StringPointerValue(response.Name)
model.Status = types.StringPointerValue(response.Status)
model.Suspended = types.BoolPointerValue(response.Suspended)
model.QuotaId = types.StringPointerValue(response.QuotaId)
model.Id = utils.BuildInternalTerraformId(response.ProjectId, response.Region, response.Guid)
model.ProjectId = types.StringValue(response.ProjectId)
model.Region = types.StringValue(response.Region)
model.PlatformId = types.StringValue(response.PlatformId)
model.OrgId = types.StringValue(response.Guid)
model.Name = types.StringValue(response.Name)
model.Status = types.StringValue(response.Status)
model.Suspended = types.BoolValue(response.Suspended)
model.QuotaId = types.StringValue(response.QuotaId)
model.CreateAt = types.StringValue(response.CreatedAt.String())
model.UpdatedAt = types.StringValue(response.UpdatedAt.String())
return nil
Expand All @@ -560,7 +529,7 @@ func toCreatePayload(model *Model) (scf.CreateOrganizationPayload, error) {
}

payload := scf.CreateOrganizationPayload{
Name: model.Name.ValueStringPointer(),
Name: model.Name.ValueString(),
}
if !model.PlatformId.IsNull() && !model.PlatformId.IsUnknown() {
payload.PlatformId = model.PlatformId.ValueStringPointer()
Expand Down
62 changes: 24 additions & 38 deletions stackit/internal/services/scf/organization/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/services/scf"
scf "github.com/stackitcloud/stackit-sdk-go/services/scf/v1api"
)

var (
Expand All @@ -34,23 +34,23 @@ func TestMapFields(t *testing.T) {
{
description: "minimal_input",
input: &scf.Organization{
Guid: new(testOrgId),
Name: new("scf-org-min-instance"),
Region: new(testRegion),
CreatedAt: &createdTime,
UpdatedAt: &createdTime,
ProjectId: new(testProjectId),
Guid: testOrgId,
Name: "scf-org-min-instance",
Region: testRegion,
CreatedAt: createdTime,
UpdatedAt: createdTime,
ProjectId: testProjectId,
},
expected: &Model{
Id: types.StringValue(fmt.Sprintf("%s,%s,%s", testProjectId, testRegion, testOrgId)),
ProjectId: types.StringValue(testProjectId),
Region: types.StringValue(testRegion),
Name: types.StringValue("scf-org-min-instance"),
PlatformId: types.StringNull(),
PlatformId: types.StringValue(""),
OrgId: types.StringValue(testOrgId),
QuotaId: types.StringNull(),
Status: types.StringNull(),
Suspended: types.BoolNull(),
QuotaId: types.StringValue(""),
Status: types.StringValue(""),
Suspended: types.BoolValue(false),
CreateAt: types.StringValue("2025-01-01 00:00:00 +0000 UTC"),
UpdatedAt: types.StringValue("2025-01-01 00:00:00 +0000 UTC"),
},
Expand All @@ -59,16 +59,16 @@ func TestMapFields(t *testing.T) {
{
description: "max_input",
input: &scf.Organization{
CreatedAt: &createdTime,
Guid: new(testOrgId),
Name: new("scf-full-org"),
PlatformId: new(testPlatformId),
ProjectId: new(testProjectId),
QuotaId: new(testQuotaId),
Region: new(testRegion),
Status: nil,
Suspended: new(true),
UpdatedAt: &createdTime,
CreatedAt: createdTime,
Guid: testOrgId,
Name: "scf-full-org",
PlatformId: testPlatformId,
ProjectId: testProjectId,
QuotaId: testQuotaId,
Region: testRegion,
Status: "",
Suspended: true,
UpdatedAt: createdTime,
},
expected: &Model{
Id: types.StringValue(fmt.Sprintf("%s,%s,%s", testProjectId, testRegion, testOrgId)),
Expand All @@ -80,7 +80,7 @@ func TestMapFields(t *testing.T) {
CreateAt: types.StringValue("2025-01-01 00:00:00 +0000 UTC"),
UpdatedAt: types.StringValue("2025-01-01 00:00:00 +0000 UTC"),
QuotaId: types.StringValue(testQuotaId),
Status: types.StringNull(),
Status: types.StringValue(""),
Suspended: types.BoolValue(true),
},
isValid: true,
Expand All @@ -91,20 +91,6 @@ func TestMapFields(t *testing.T) {
expected: nil,
isValid: false,
},
{
description: "empty_org",
input: &scf.Organization{},
expected: nil,
isValid: false,
},
{
description: "missing_id",
input: &scf.Organization{
Name: new("scf-missing-id"),
},
expected: nil,
isValid: false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
Expand Down Expand Up @@ -143,8 +129,8 @@ func TestToCreatePayload(t *testing.T) {
PlatformId: types.StringValue(testPlatformId),
},
expected: scf.CreateOrganizationPayload{
Name: new("example-org"),
PlatformId: new(testPlatformId),
Name: "example-org",
PlatformId: &testPlatformId,
},
expectError: false,
},
Expand Down
58 changes: 11 additions & 47 deletions stackit/internal/services/scf/organizationmanager/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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/scf"
scf "github.com/stackitcloud/stackit-sdk-go/services/scf/v1api"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
Expand Down Expand Up @@ -152,7 +152,7 @@ func (s *scfOrganizationManagerDataSource) Read(ctx context.Context, request dat
ctx = tflog.SetField(ctx, "org_id", orgId)
ctx = tflog.SetField(ctx, "region", region)
// Read the current scf organization manager via orgId
ScfOrgManager, err := s.client.GetOrgManagerExecute(ctx, projectId, region, orgId)
ScfOrgManager, err := s.client.DefaultAPI.GetOrgManager(ctx, projectId, region, orgId).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down Expand Up @@ -189,53 +189,17 @@ func mapFieldsDataSource(response *scf.OrgManager, model *DataSourceModel) error
if model == nil {
return fmt.Errorf("model input is nil")
}

var projectId string
if response.ProjectId != nil {
projectId = *response.ProjectId
} else if model.ProjectId.ValueString() != "" {
projectId = model.ProjectId.ValueString()
} else {
return fmt.Errorf("project id is not present")
}

var region string
if response.Region != nil {
region = *response.Region
} else if model.Region.ValueString() != "" {
region = model.Region.ValueString()
} else {
return fmt.Errorf("region is not present")
}

var orgId string
if response.OrgId != nil {
orgId = *response.OrgId
} else if model.OrgId.ValueString() != "" {
orgId = model.OrgId.ValueString()
} else {
return fmt.Errorf("org id is not present")
}

var userId string
if response.Guid != nil {
userId = *response.Guid
if model.UserId.ValueString() != "" && userId != model.UserId.ValueString() {
return fmt.Errorf("user id mismatch in response and model")
}
} else if model.UserId.ValueString() != "" {
userId = model.UserId.ValueString()
} else {
return fmt.Errorf("user id is not present")
if userId := model.UserId.ValueString(); userId != "" && userId != response.Guid {
return fmt.Errorf("user id mismatch in response and model")
}

model.Id = utils.BuildInternalTerraformId(projectId, region, orgId, userId)
model.Region = types.StringValue(region)
model.PlatformId = types.StringPointerValue(response.PlatformId)
model.ProjectId = types.StringValue(projectId)
model.OrgId = types.StringValue(orgId)
model.UserId = types.StringValue(userId)
model.UserName = types.StringPointerValue(response.Username)
model.Id = utils.BuildInternalTerraformId(response.ProjectId, response.Region, response.OrgId, response.Guid)
model.Region = types.StringValue(response.Region)
model.PlatformId = types.StringValue(response.PlatformId)
model.ProjectId = types.StringValue(response.ProjectId)
model.OrgId = types.StringValue(response.OrgId)
model.UserId = types.StringValue(response.Guid)
model.UserName = types.StringValue(response.Username)
model.CreateAt = types.StringValue(response.CreatedAt.String())
model.UpdatedAt = types.StringValue(response.UpdatedAt.String())
return nil
Expand Down
Loading
Loading