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
4 changes: 3 additions & 1 deletion btp/provider/datasource_subaccount_service_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -92,6 +93,7 @@ You must be assigned to the admin or viewer role of the subaccount.`,
"parameters": schema.StringAttribute{
MarkdownDescription: "The parameters of the service binding as a valid JSON object.",
Computed: true,
CustomType: jsontypes.NormalizedType{},
},
"state": schema.StringAttribute{
MarkdownDescription: "The current state of the service binding. Possible values are: \n" +
Expand Down Expand Up @@ -148,7 +150,7 @@ func (ds *subaccountServiceBindingDataSource) Read(ctx context.Context, req data
}

data, diags = subaccountServiceBindingValueFrom(ctx, cliRes)
data.Parameters = types.StringNull() // the API doesn't return parameters for already created instances
data.Parameters = jsontypes.NewNormalizedNull() // the API doesn't return parameters for already created instances
resp.Diagnostics.Append(diags...)

diags = resp.State.Set(ctx, &data)
Expand Down
8 changes: 5 additions & 3 deletions btp/provider/resource_subaccount_environment_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -93,6 +94,7 @@ __Further documentation:__
"parameters": schema.StringAttribute{
MarkdownDescription: "The configuration parameters for the environment instance.",
Required: true,
CustomType: jsontypes.NormalizedType{},
},
"landscape_label": schema.StringAttribute{
MarkdownDescription: "The name of the landscape within the logged in region on which the environment instance is created.",
Expand Down Expand Up @@ -222,7 +224,7 @@ func (rs *subaccountEnvironmentInstanceResource) Read(ctx context.Context, req r
//The "parameter" string contains a status field that needs to be omitted as it is not a parameter that can be defined by the caller
// This way we stay consistent between CREATE and IMPORT of environment instances via Terraform
reStatus := regexp.MustCompile(`,"status":"(.*?)"`)
updatedState.Parameters = types.StringValue(reStatus.ReplaceAllString(updatedState.Parameters.ValueString(), ""))
updatedState.Parameters = jsontypes.NewNormalizedValue(reStatus.ReplaceAllString(updatedState.Parameters.ValueString(), ""))
}

resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -257,7 +259,7 @@ func (rs *subaccountEnvironmentInstanceResource) Create(ctx context.Context, req

timeoutsLocal := plan.Timeouts
plan, diags = subaccountEnvironmentInstanceValueFrom(ctx, cliRes)
plan.Parameters = types.StringValue(parameters)
plan.Parameters = jsontypes.NewNormalizedValue(parameters)
resp.Diagnostics.Append(diags...)

createTimeout, diags := timeoutsLocal.Create(ctx, tfutils.DefaultTimeout)
Expand Down Expand Up @@ -292,7 +294,7 @@ func (rs *subaccountEnvironmentInstanceResource) Create(ctx context.Context, req
}

plan, diags = subaccountEnvironmentInstanceValueFrom(ctx, updatedRes.(provisioning.EnvironmentInstanceResponseObject))
plan.Parameters = types.StringValue(parameters)
plan.Parameters = jsontypes.NewNormalizedValue(parameters)
plan.Timeouts = timeoutsLocal
resp.Diagnostics.Append(diags...)

Expand Down
13 changes: 7 additions & 6 deletions btp/provider/resource_subaccount_environment_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ func TestResourceSubaccountEnvironmentInstance(t *testing.T) {
),
},
{
ResourceName: "btp_subaccount_environment_instance.uut",
ImportStateIdFunc: getEnvironmentInstanceIdForImport("btp_subaccount_environment_instance.uut"),
ImportState: true,
ImportStateVerify: true,
ResourceName: "btp_subaccount_environment_instance.uut",
ImportStateIdFunc: getEnvironmentInstanceIdForImport("btp_subaccount_environment_instance.uut"),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"parameters"},
},
},
})
Expand Down Expand Up @@ -200,8 +201,8 @@ func hclResourceSubaccountEnvironmentInstanceCF(resourceName string, subaccountN
InstanceName: orgName,
Users: []cfUsers{
{
Id: user,
Email: user,
Id: user,
},
},
}
Expand Down Expand Up @@ -244,8 +245,8 @@ func hclResourceSubaccountEnvironmentInstanceCFTimeout(resourceName string, suba
InstanceName: orgName,
Users: []cfUsers{
{
Id: user,
Email: user,
Id: user,
},
},
}
Expand Down
4 changes: 3 additions & 1 deletion btp/provider/resource_subaccount_service_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -72,6 +73,7 @@ You must be assigned to the admin or the service administrator role of the subac
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{}`),
CustomType: jsontypes.NormalizedType{},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
Expand Down Expand Up @@ -160,7 +162,7 @@ func (rs *subaccountServiceBindingResource) Read(ctx context.Context, req resour
updatedState.Parameters = state.Parameters
} else if updatedState.Parameters.IsNull() && state.Parameters.IsNull() {
// During the import of the resource both values might be empty, so we need to apply the default value form the schema if not existing
updatedState.Parameters = types.StringValue("{}")
updatedState.Parameters = jsontypes.NewNormalizedValue("{}")
}

resp.Diagnostics.Append(diags...)
Expand Down
4 changes: 3 additions & 1 deletion btp/provider/resource_subaccount_service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"strings"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -94,6 +95,7 @@ You must be assigned to the admin or the service administrator role of the subac
MarkdownDescription: "The configuration parameters for the service instance.",
Optional: true,
Sensitive: true,
CustomType: jsontypes.NormalizedType{},
Validators: []validator.String{
jsonvalidator.ValidJSON(),
},
Expand Down Expand Up @@ -179,7 +181,7 @@ func (rs *subaccountServiceInstanceResource) Read(ctx context.Context, req resou

// Handle resource import
if cliRes.Parameters != "" && state.Parameters.ValueString() == "" {
newState.Parameters = types.StringValue(cliRes.Parameters)
newState.Parameters = jsontypes.NewNormalizedValue(cliRes.Parameters)
} else {
newState.Parameters = state.Parameters
}
Expand Down
4 changes: 3 additions & 1 deletion btp/provider/resource_subaccount_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"strings"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -84,6 +85,7 @@ You must be assigned to the admin role of the subaccount.`,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{}`),
CustomType: jsontypes.NormalizedType{},
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Expand Down Expand Up @@ -258,7 +260,7 @@ func (rs *subaccountSubscriptionResource) Read(ctx context.Context, req resource
newState.Parameters = state.Parameters
} else if newState.Parameters.IsNull() && state.Parameters.IsNull() {
// During the import of the resource both values might be empty, so we need to apply the default value form the schema if not existing
newState.Parameters = types.StringValue("{}")
newState.Parameters = jsontypes.NewNormalizedValue("{}")
}

resp.Diagnostics.Append(diags...)
Expand Down
60 changes: 36 additions & 24 deletions btp/provider/type_subaccount_environment_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,39 @@ package provider
import (
"context"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/SAP/terraform-provider-btp/internal/btpcli/types/provisioning"
"github.com/SAP/terraform-provider-btp/internal/tfutils"
)

type subaccountEnvironmentInstanceType struct {
SubaccountId types.String `tfsdk:"subaccount_id"`
Id types.String `tfsdk:"id"`
BrokerId types.String `tfsdk:"broker_id"`
CreatedDate types.String `tfsdk:"created_date"`
CustomLabels types.Map `tfsdk:"custom_labels"`
DashboardUrl types.String `tfsdk:"dashboard_url"`
Description types.String `tfsdk:"description"`
EnvironmentType types.String `tfsdk:"environment_type"`
Labels types.String `tfsdk:"labels"`
LandscapeLabel types.String `tfsdk:"landscape_label"`
LastModified types.String `tfsdk:"last_modified"`
Name types.String `tfsdk:"name"`
Operation types.String `tfsdk:"operation"`
Parameters types.String `tfsdk:"parameters"`
PlanId types.String `tfsdk:"plan_id"`
PlanName types.String `tfsdk:"plan_name"`
PlatformId types.String `tfsdk:"platform_id"`
ServiceId types.String `tfsdk:"service_id"`
ServiceName types.String `tfsdk:"service_name"`
State types.String `tfsdk:"state"`
TenantId types.String `tfsdk:"tenant_id"`
Type_ types.String `tfsdk:"type"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
SubaccountId types.String `tfsdk:"subaccount_id"`
Id types.String `tfsdk:"id"`
BrokerId types.String `tfsdk:"broker_id"`
CreatedDate types.String `tfsdk:"created_date"`
CustomLabels types.Map `tfsdk:"custom_labels"`
DashboardUrl types.String `tfsdk:"dashboard_url"`
Description types.String `tfsdk:"description"`
EnvironmentType types.String `tfsdk:"environment_type"`
Labels types.String `tfsdk:"labels"`
LandscapeLabel types.String `tfsdk:"landscape_label"`
LastModified types.String `tfsdk:"last_modified"`
Name types.String `tfsdk:"name"`
Operation types.String `tfsdk:"operation"`
Parameters jsontypes.Normalized `tfsdk:"parameters"`
PlanId types.String `tfsdk:"plan_id"`
PlanName types.String `tfsdk:"plan_name"`
PlatformId types.String `tfsdk:"platform_id"`
ServiceId types.String `tfsdk:"service_id"`
ServiceName types.String `tfsdk:"service_name"`
State types.String `tfsdk:"state"`
TenantId types.String `tfsdk:"tenant_id"`
Type_ types.String `tfsdk:"type"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

func subaccountEnvironmentInstanceValueFrom(ctx context.Context, value provisioning.EnvironmentInstanceResponseObject) (subaccountEnvironmentInstanceType, diag.Diagnostics) {
Expand All @@ -49,7 +51,6 @@ func subaccountEnvironmentInstanceValueFrom(ctx context.Context, value provision
LastModified: timeToValue(value.ModifiedDate.Time()),
Name: types.StringValue(value.Name),
Operation: types.StringValue(value.Operation),
Parameters: types.StringValue(value.Parameters),
PlanId: types.StringValue(value.PlanId),
PlanName: types.StringValue(value.PlanName),
PlatformId: types.StringValue(value.PlatformId),
Expand All @@ -63,6 +64,17 @@ func subaccountEnvironmentInstanceValueFrom(ctx context.Context, value provision

var diags, diagnostics diag.Diagnostics

normalized, err := tfutils.NormalizeJSON(value.Parameters)
if err != nil {
diags.AddError(
"Invalid JSON in Environment Instance Parameters",
"Could not normalize the JSON string found in the Environment Instance Parameters attribute: "+err.Error(),
)
diagnostics.Append(diags...)
return environmentInstance, diagnostics
}
environmentInstance.Parameters = jsontypes.NewNormalizedValue(normalized)

environmentInstance.CustomLabels, diags = types.MapValueFrom(ctx, types.SetType{ElemType: types.StringType}, value.CustomLabels)
diagnostics.Append(diags...)

Expand Down
27 changes: 14 additions & 13 deletions btp/provider/type_subaccount_service_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"

Expand All @@ -11,19 +12,19 @@ import (
)

type subaccountServiceBindingType struct {
SubaccountId types.String `tfsdk:"subaccount_id"`
ServiceInstanceId types.String `tfsdk:"service_instance_id"`
Name types.String `tfsdk:"name"`
Parameters types.String `tfsdk:"parameters"`
Id types.String `tfsdk:"id"`
Ready types.Bool `tfsdk:"ready"`
Context types.String `tfsdk:"context"`
BindResource types.Map `tfsdk:"bind_resource"`
Credentials types.String `tfsdk:"credentials"`
State types.String `tfsdk:"state"`
CreatedDate types.String `tfsdk:"created_date"`
LastModified types.String `tfsdk:"last_modified"`
Labels types.Map `tfsdk:"labels"`
SubaccountId types.String `tfsdk:"subaccount_id"`
ServiceInstanceId types.String `tfsdk:"service_instance_id"`
Name types.String `tfsdk:"name"`
Parameters jsontypes.Normalized `tfsdk:"parameters"`
Id types.String `tfsdk:"id"`
Ready types.Bool `tfsdk:"ready"`
Context types.String `tfsdk:"context"`
BindResource types.Map `tfsdk:"bind_resource"`
Credentials types.String `tfsdk:"credentials"`
State types.String `tfsdk:"state"`
CreatedDate types.String `tfsdk:"created_date"`
LastModified types.String `tfsdk:"last_modified"`
Labels types.Map `tfsdk:"labels"`
}

func subaccountServiceBindingValueFrom(ctx context.Context, value servicemanager.ServiceBindingResponseObject) (subaccountServiceBindingType, diag.Diagnostics) {
Expand Down
35 changes: 18 additions & 17 deletions btp/provider/type_subaccount_service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -12,23 +13,23 @@ import (
)

type subaccountServiceInstanceType struct {
SubaccountId types.String `tfsdk:"subaccount_id"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Parameters types.String `tfsdk:"parameters"`
Ready types.Bool `tfsdk:"ready"`
ServicePlanId types.String `tfsdk:"serviceplan_id"`
PlatformId types.String `tfsdk:"platform_id"`
ReferencedInstanceId types.String `tfsdk:"referenced_instance_id"`
Shared types.Bool `tfsdk:"shared"`
Context types.String `tfsdk:"context"`
Usable types.Bool `tfsdk:"usable"`
State types.String `tfsdk:"state"`
CreatedDate types.String `tfsdk:"created_date"`
LastModified types.String `tfsdk:"last_modified"`
Labels types.Map `tfsdk:"labels"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
DashboardUrl types.String `tfsdk:"dashboard_url"`
SubaccountId types.String `tfsdk:"subaccount_id"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Parameters jsontypes.Normalized `tfsdk:"parameters"`
Ready types.Bool `tfsdk:"ready"`
ServicePlanId types.String `tfsdk:"serviceplan_id"`
PlatformId types.String `tfsdk:"platform_id"`
ReferencedInstanceId types.String `tfsdk:"referenced_instance_id"`
Shared types.Bool `tfsdk:"shared"`
Context types.String `tfsdk:"context"`
Usable types.Bool `tfsdk:"usable"`
State types.String `tfsdk:"state"`
CreatedDate types.String `tfsdk:"created_date"`
LastModified types.String `tfsdk:"last_modified"`
Labels types.Map `tfsdk:"labels"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
DashboardUrl types.String `tfsdk:"dashboard_url"`
}

func subaccountServiceInstanceValueFrom(ctx context.Context, value servicemanager.ServiceInstanceResponseObject) (subaccountServiceInstanceType, diag.Diagnostics) {
Expand Down
Loading