feat: initial copy of v0.1.0
This commit is contained in:
parent
4cc801a7f3
commit
7d4cbb6b08
538 changed files with 63361 additions and 55213 deletions
|
|
@ -0,0 +1,130 @@
|
|||
package postgresflexalpha
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
||||
postgresflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance/datasources_gen"
|
||||
postgresflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/utils"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
)
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
var (
|
||||
_ datasource.DataSource = &instanceDataSource{}
|
||||
)
|
||||
|
||||
// NewInstanceDataSource is a helper function to simplify the provider implementation.
|
||||
func NewInstanceDataSource() datasource.DataSource {
|
||||
return &instanceDataSource{}
|
||||
}
|
||||
|
||||
// dataSourceModel maps the data source schema data.
|
||||
type dataSourceModel struct {
|
||||
postgresflexalpha2.InstanceModel
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
// instanceDataSource is the data source implementation.
|
||||
type instanceDataSource struct {
|
||||
client *v3alpha1api.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the data source type name.
|
||||
func (r *instanceDataSource) Metadata(
|
||||
_ context.Context,
|
||||
req datasource.MetadataRequest,
|
||||
resp *datasource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_postgresflexalpha_instance"
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (r *instanceDataSource) Configure(
|
||||
ctx context.Context,
|
||||
req datasource.ConfigureRequest,
|
||||
resp *datasource.ConfigureResponse,
|
||||
) {
|
||||
var ok bool
|
||||
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
apiClient := postgresflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
r.client = apiClient
|
||||
tflog.Info(ctx, "Postgres Flex instance client configured")
|
||||
}
|
||||
|
||||
// Schema defines the schema for the data source.
|
||||
func (r *instanceDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = postgresflexalpha2.InstanceDataSourceSchema(ctx)
|
||||
}
|
||||
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (r *instanceDataSource) Read(
|
||||
ctx context.Context,
|
||||
req datasource.ReadRequest,
|
||||
resp *datasource.ReadResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model dataSourceModel
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
instanceResp, err := r.client.DefaultAPI.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading instance",
|
||||
fmt.Sprintf("Instance with ID %q does not exist in project %q.", instanceId, projectId),
|
||||
map[int]string{
|
||||
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
|
||||
},
|
||||
)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
err = mapGetDataInstanceResponseToModel(ctx, &model, instanceResp)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Postgres Flex instance read")
|
||||
}
|
||||
|
|
@ -1,222 +0,0 @@
|
|||
package postgresflexa
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/stackitcloud/terraform-provider-stackit/pkg/postgresflexalpha"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
||||
postgresflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/postgresflexalpha/utils"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/pkg/postgresflexalpha/wait"
|
||||
)
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
var (
|
||||
_ datasource.DataSource = &instanceDataSource{}
|
||||
)
|
||||
|
||||
// NewInstanceDataSource is a helper function to simplify the provider implementation.
|
||||
func NewInstanceDataSource() datasource.DataSource {
|
||||
return &instanceDataSource{}
|
||||
}
|
||||
|
||||
// instanceDataSource is the data source implementation.
|
||||
type instanceDataSource struct {
|
||||
client *postgresflexalpha.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the data source type name.
|
||||
func (r *instanceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_postgresflexalpha_instance"
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (r *instanceDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
var ok bool
|
||||
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
apiClient := postgresflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
r.client = apiClient
|
||||
tflog.Info(ctx, "Postgres Flex instance client configured")
|
||||
}
|
||||
|
||||
// Schema defines the schema for the data source.
|
||||
func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
descriptions := map[string]string{
|
||||
"main": "Postgres Flex instance data source schema. Must have a `region` specified in the provider configuration.",
|
||||
"id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`region`,`instance_id`\".",
|
||||
"instance_id": "ID of the PostgresFlex instance.",
|
||||
"project_id": "STACKIT project ID to which the instance is associated.",
|
||||
"name": "Instance name.",
|
||||
"acl": "The Access Control List (ACL) for the PostgresFlex instance.",
|
||||
"region": "The resource region. If not defined, the provider region is used.",
|
||||
}
|
||||
|
||||
resp.Schema = schema.Schema{
|
||||
Description: descriptions["main"],
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Description: descriptions["id"],
|
||||
Computed: true,
|
||||
},
|
||||
"instance_id": schema.StringAttribute{
|
||||
Description: descriptions["instance_id"],
|
||||
Required: true,
|
||||
Validators: []validator.String{
|
||||
validate.UUID(),
|
||||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"project_id": schema.StringAttribute{
|
||||
Description: descriptions["project_id"],
|
||||
Required: true,
|
||||
Validators: []validator.String{
|
||||
validate.UUID(),
|
||||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"name": schema.StringAttribute{
|
||||
Description: descriptions["name"],
|
||||
Computed: true,
|
||||
},
|
||||
"acl": schema.ListAttribute{
|
||||
Description: descriptions["acl"],
|
||||
ElementType: types.StringType,
|
||||
Computed: true,
|
||||
},
|
||||
"backup_schedule": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"flavor": schema.SingleNestedAttribute{
|
||||
Computed: true,
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"description": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"cpu": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"ram": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
"replicas": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"storage": schema.SingleNestedAttribute{
|
||||
Computed: true,
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"class": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"size": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
"version": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"region": schema.StringAttribute{
|
||||
// the region cannot be found, so it has to be passed
|
||||
Optional: true,
|
||||
Description: descriptions["region"],
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
instanceResp, err := r.client.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading instance",
|
||||
fmt.Sprintf("Instance with ID %q does not exist in project %q.", instanceId, projectId),
|
||||
map[int]string{
|
||||
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
|
||||
},
|
||||
)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
if instanceResp != nil && instanceResp.Status != nil && *instanceResp.Status == wait.InstanceStateDeleted {
|
||||
resp.State.RemoveResource(ctx)
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", "Instance was deleted successfully")
|
||||
return
|
||||
}
|
||||
|
||||
var flavor = &flavorModel{}
|
||||
if !(model.Flavor.IsNull() || model.Flavor.IsUnknown()) {
|
||||
diags = model.Flavor.As(ctx, flavor, basetypes.ObjectAsOptions{})
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
}
|
||||
var storage = &storageModel{}
|
||||
if !(model.Storage.IsNull() || model.Storage.IsUnknown()) {
|
||||
diags = model.Storage.As(ctx, storage, basetypes.ObjectAsOptions{})
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = mapFields(ctx, instanceResp, &model, flavor, storage, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Postgres Flex instance read")
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,241 @@
|
|||
package postgresflexalpha
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
postgresflexalphadatasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance/datasources_gen"
|
||||
postgresflexalpharesource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance/resources_gen"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
)
|
||||
|
||||
func mapGetInstanceResponseToModel(
|
||||
ctx context.Context,
|
||||
m *postgresflexalpharesource.InstanceModel,
|
||||
resp *postgresflex.GetInstanceResponse,
|
||||
) error {
|
||||
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
m.Encryption = postgresflexalpharesource.NewEncryptionValueNull()
|
||||
if resp.HasEncryption() {
|
||||
m.Encryption = postgresflexalpharesource.NewEncryptionValueMust(
|
||||
m.Encryption.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"kek_key_id": types.StringValue(resp.Encryption.GetKekKeyId()),
|
||||
"kek_key_ring_id": types.StringValue(resp.Encryption.GetKekKeyRingId()),
|
||||
"kek_key_version": types.StringValue(resp.Encryption.GetKekKeyVersion()),
|
||||
"service_account": types.StringValue(resp.Encryption.GetServiceAccount()),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
isConnectionInfoIncomplete := resp.ConnectionInfo.Write.Host == "" || resp.ConnectionInfo.Write.Port == 0
|
||||
|
||||
if isConnectionInfoIncomplete {
|
||||
m.ConnectionInfo = postgresflexalpharesource.NewConnectionInfoValueNull()
|
||||
} else {
|
||||
m.ConnectionInfo = postgresflexalpharesource.NewConnectionInfoValueMust(
|
||||
postgresflexalpharesource.ConnectionInfoValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
// careful - we can not use NewWriteValueMust here
|
||||
"write": basetypes.NewObjectValueMust(
|
||||
postgresflexalpharesource.WriteValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"host": types.StringValue(resp.ConnectionInfo.Write.Host),
|
||||
// note: IDE does not show that port is actually an int64 in the Schema
|
||||
"port": types.Int64Value(int64(resp.ConnectionInfo.Write.Port)),
|
||||
},
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||
if m.Id.IsNull() || m.Id.IsUnknown() {
|
||||
m.Id = utils.BuildInternalTerraformId(
|
||||
m.ProjectId.ValueString(),
|
||||
m.Region.ValueString(),
|
||||
m.InstanceId.ValueString(),
|
||||
)
|
||||
}
|
||||
m.InstanceId = types.StringValue(resp.Id)
|
||||
|
||||
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||
|
||||
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed converting network acl from response")
|
||||
}
|
||||
|
||||
m.Acl = netAcl
|
||||
|
||||
netInstAdd := types.StringValue("")
|
||||
if instAdd, ok := resp.Network.GetInstanceAddressOk(); ok {
|
||||
netInstAdd = types.StringValue(*instAdd)
|
||||
}
|
||||
|
||||
netRtrAdd := types.StringValue("")
|
||||
if rtrAdd, ok := resp.Network.GetRouterAddressOk(); ok {
|
||||
netRtrAdd = types.StringValue(*rtrAdd)
|
||||
}
|
||||
|
||||
net, diags := postgresflexalpharesource.NewNetworkValue(
|
||||
postgresflexalpharesource.NetworkValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"access_scope": basetypes.NewStringValue(string(resp.Network.GetAccessScope())),
|
||||
"acl": netAcl,
|
||||
"instance_address": netInstAdd,
|
||||
"router_address": netRtrAdd,
|
||||
},
|
||||
)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed converting network from response")
|
||||
}
|
||||
|
||||
m.Network = net
|
||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
m.RetentionDays = types.Int64Value(int64(resp.GetRetentionDays()))
|
||||
|
||||
m.Name = types.StringValue(resp.GetName())
|
||||
|
||||
m.Status = types.StringValue(string(resp.GetStatus()))
|
||||
|
||||
storage, diags := postgresflexalpharesource.NewStorageValue(
|
||||
postgresflexalpharesource.StorageValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"performance_class": types.StringValue(resp.Storage.GetPerformanceClass()),
|
||||
"size": types.Int64Value(int64(resp.Storage.GetSize())),
|
||||
},
|
||||
)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed converting storage from response")
|
||||
}
|
||||
m.Storage = storage
|
||||
|
||||
m.Version = types.StringValue(resp.GetVersion())
|
||||
return nil
|
||||
}
|
||||
|
||||
func mapGetDataInstanceResponseToModel(
|
||||
ctx context.Context,
|
||||
m *dataSourceModel,
|
||||
resp *postgresflex.GetInstanceResponse,
|
||||
) error {
|
||||
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
handleEncryption(m, resp)
|
||||
handleConnectionInfo(ctx, m, resp)
|
||||
|
||||
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||
m.Id = utils.BuildInternalTerraformId(m.ProjectId.ValueString(), m.Region.ValueString(), m.InstanceId.ValueString())
|
||||
m.InstanceId = types.StringValue(resp.Id)
|
||||
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||
m.Name = types.StringValue(resp.GetName())
|
||||
|
||||
err := handleNetwork(ctx, m, resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
m.RetentionDays = types.Int64Value(int64(resp.GetRetentionDays()))
|
||||
m.Status = types.StringValue(string(resp.GetStatus()))
|
||||
storage, diags := postgresflexalphadatasource.NewStorageValue(
|
||||
postgresflexalphadatasource.StorageValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"performance_class": types.StringValue(resp.Storage.GetPerformanceClass()),
|
||||
"size": types.Int64Value(int64(resp.Storage.GetSize())),
|
||||
},
|
||||
)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed converting storage from response")
|
||||
}
|
||||
m.Storage = storage
|
||||
m.Version = types.StringValue(resp.GetVersion())
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleConnectionInfo(ctx context.Context, m *dataSourceModel, resp *postgresflex.GetInstanceResponse) {
|
||||
isConnectionInfoIncomplete := resp.ConnectionInfo.Write.Host == "" || resp.ConnectionInfo.Write.Port == 0
|
||||
|
||||
if isConnectionInfoIncomplete {
|
||||
m.ConnectionInfo = postgresflexalphadatasource.NewConnectionInfoValueNull()
|
||||
} else {
|
||||
m.ConnectionInfo = postgresflexalphadatasource.NewConnectionInfoValueMust(
|
||||
postgresflexalphadatasource.ConnectionInfoValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"write": types.ObjectValueMust(
|
||||
postgresflexalphadatasource.WriteValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"host": types.StringValue(resp.ConnectionInfo.Write.Host),
|
||||
"port": types.Int64Value(int64(resp.ConnectionInfo.Write.Port)),
|
||||
},
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func handleNetwork(ctx context.Context, m *dataSourceModel, resp *postgresflex.GetInstanceResponse) error {
|
||||
netACL, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed converting network acl from response")
|
||||
}
|
||||
|
||||
instAddr := ""
|
||||
if iA, ok := resp.Network.GetInstanceAddressOk(); ok {
|
||||
instAddr = *iA
|
||||
}
|
||||
|
||||
rtrAddr := ""
|
||||
if rA, ok := resp.Network.GetRouterAddressOk(); ok {
|
||||
rtrAddr = *rA
|
||||
}
|
||||
|
||||
net, diags := postgresflexalphadatasource.NewNetworkValue(
|
||||
postgresflexalphadatasource.NetworkValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"access_scope": types.StringValue(string(resp.Network.GetAccessScope())),
|
||||
"acl": netACL,
|
||||
"instance_address": types.StringValue(instAddr),
|
||||
"router_address": types.StringValue(rtrAddr),
|
||||
},
|
||||
)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed converting network from response")
|
||||
}
|
||||
m.Network = net
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleEncryption(m *dataSourceModel, resp *postgresflex.GetInstanceResponse) {
|
||||
keyId := ""
|
||||
if keyIdVal, ok := resp.Encryption.GetKekKeyIdOk(); ok {
|
||||
keyId = *keyIdVal
|
||||
}
|
||||
|
||||
keyRingId := ""
|
||||
if keyRingIdVal, ok := resp.Encryption.GetKekKeyRingIdOk(); ok {
|
||||
keyRingId = *keyRingIdVal
|
||||
}
|
||||
|
||||
keyVersion := ""
|
||||
if keyVersionVal, ok := resp.Encryption.GetKekKeyVersionOk(); ok {
|
||||
keyVersion = *keyVersionVal
|
||||
}
|
||||
|
||||
svcAcc := ""
|
||||
if svcAccVal, ok := resp.Encryption.GetServiceAccountOk(); ok {
|
||||
svcAcc = *svcAccVal
|
||||
}
|
||||
|
||||
m.Encryption = postgresflexalphadatasource.EncryptionValue{
|
||||
KekKeyId: types.StringValue(keyId),
|
||||
KekKeyRingId: types.StringValue(keyRingId),
|
||||
KekKeyVersion: types.StringValue(keyVersion),
|
||||
ServiceAccount: types.StringValue(svcAcc),
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,191 @@
|
|||
package postgresflexalpha
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
||||
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
postgresflexalpharesource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance/resources_gen"
|
||||
utils2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
)
|
||||
|
||||
func Test_handleConnectionInfo(t *testing.T) {
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
m *dataSourceModel
|
||||
hostName string
|
||||
port int32
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
}{
|
||||
{
|
||||
name: "empty connection info",
|
||||
args: args{
|
||||
ctx: context.TODO(),
|
||||
m: &dataSourceModel{},
|
||||
hostName: "",
|
||||
port: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "empty connection info host",
|
||||
args: args{
|
||||
ctx: context.TODO(),
|
||||
m: &dataSourceModel{},
|
||||
hostName: "",
|
||||
port: 1234,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "empty connection info port",
|
||||
args: args{
|
||||
ctx: context.TODO(),
|
||||
m: &dataSourceModel{},
|
||||
hostName: "hostname",
|
||||
port: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid connection info",
|
||||
args: args{
|
||||
ctx: context.TODO(),
|
||||
m: &dataSourceModel{},
|
||||
hostName: "host",
|
||||
port: 1000,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
resp := &postgresflex.GetInstanceResponse{
|
||||
ConnectionInfo: postgresflex.InstanceConnectionInfo{
|
||||
Write: postgresflex.InstanceConnectionInfoWrite{
|
||||
Host: tt.args.hostName,
|
||||
Port: int32(tt.args.port),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
handleConnectionInfo(tt.args.ctx, tt.args.m, resp)
|
||||
|
||||
if tt.args.hostName == "" || tt.args.port == 0 {
|
||||
if !tt.args.m.ConnectionInfo.IsNull() {
|
||||
t.Errorf("expected connection info to be null")
|
||||
}
|
||||
}
|
||||
|
||||
if tt.args.hostName != "" && tt.args.port != 0 {
|
||||
res := tt.args.m.ConnectionInfo.Write.Attributes()
|
||||
gotHost := ""
|
||||
if r, ok := res["host"]; ok {
|
||||
gotHost = utils2.RemoveQuotes(r.String())
|
||||
}
|
||||
if gotHost != tt.args.hostName {
|
||||
t.Errorf("host value incorrect: want: %s - got: %s", tt.args.hostName, gotHost)
|
||||
}
|
||||
|
||||
gotPort, ok := res["port"]
|
||||
if !ok {
|
||||
t.Errorf("could not find a value for port in connection_info.write")
|
||||
}
|
||||
if !gotPort.Equal(types.Int64Value(int64(tt.args.port))) {
|
||||
t.Errorf("port value incorrect: want: %d - got: %s", tt.args.port, gotPort.String())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_handleEncryption(t *testing.T) {
|
||||
t.Skipf("please implement")
|
||||
type args struct {
|
||||
m *dataSourceModel
|
||||
resp *postgresflex.GetInstanceResponse
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
handleEncryption(tt.args.m, tt.args.resp)
|
||||
t.Logf("need to implement more")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_handleNetwork(t *testing.T) {
|
||||
t.Skipf("please implement")
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
m *dataSourceModel
|
||||
resp *postgresflex.GetInstanceResponse
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := handleNetwork(tt.args.ctx, tt.args.m, tt.args.resp); (err != nil) != tt.wantErr {
|
||||
t.Errorf("handleNetwork() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mapGetDataInstanceResponseToModel(t *testing.T) {
|
||||
t.Skipf("please implement")
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
m *dataSourceModel
|
||||
resp *postgresflex.GetInstanceResponse
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := mapGetDataInstanceResponseToModel(tt.args.ctx, tt.args.m, tt.args.resp); (err != nil) != tt.wantErr {
|
||||
t.Errorf("mapGetDataInstanceResponseToModel() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mapGetInstanceResponseToModel(t *testing.T) {
|
||||
t.Skipf("please implement")
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
m *postgresflexalpharesource.InstanceModel
|
||||
resp *postgresflex.GetInstanceResponse
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := mapGetInstanceResponseToModel(tt.args.ctx, tt.args.m, tt.args.resp); (err != nil) != tt.wantErr {
|
||||
t.Errorf("mapGetInstanceResponseToModel() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
fields:
|
||||
- name: 'backup_schedule'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'connection_info.host'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'connection_info.port'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'encryption.kek_key_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'encryption.kek_key_ring_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'encryption.kek_key_version'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'encryption.service_account'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'flavor_id'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'id'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'instance_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
- validate.UUID
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'is_deletable'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'name'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'network.access_scope'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'network.acl'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'network.instance_address'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'network.router_address'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'project_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
- validate.UUID
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'region'
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'replicas'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'retention_days'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'status'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'storage'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'storage.performance_class'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'storage.size'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'version'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,768 +0,0 @@
|
|||
// Copyright (c) STACKIT
|
||||
|
||||
package postgresflex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
postgresflex "github.com/stackitcloud/terraform-provider-stackit/pkg/postgresflexalpha"
|
||||
)
|
||||
|
||||
type postgresFlexClientMocked struct {
|
||||
returnError bool
|
||||
getFlavorsResp *postgresflex.GetFlavorsResponse
|
||||
}
|
||||
|
||||
func (c *postgresFlexClientMocked) ListFlavorsExecute(_ context.Context, _, _ string) (*postgresflex.GetFlavorsResponse, error) {
|
||||
if c.returnError {
|
||||
return nil, fmt.Errorf("get flavors failed")
|
||||
}
|
||||
|
||||
return c.getFlavorsResp, nil
|
||||
}
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
const testRegion = "region"
|
||||
tests := []struct {
|
||||
description string
|
||||
state Model
|
||||
input *postgresflex.GetInstanceResponse
|
||||
flavor *flavorModel
|
||||
storage *storageModel
|
||||
region string
|
||||
expected Model
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&postgresflex.GetInstanceResponse{},
|
||||
&flavorModel{},
|
||||
&storageModel{},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Name: types.StringNull(),
|
||||
ACL: types.ListNull(types.StringType),
|
||||
BackupSchedule: types.StringNull(),
|
||||
Flavor: types.ObjectValueMust(flavorTypes, map[string]attr.Value{
|
||||
"id": types.StringNull(),
|
||||
"description": types.StringNull(),
|
||||
"cpu": types.Int64Null(),
|
||||
"ram": types.Int64Null(),
|
||||
}),
|
||||
Replicas: types.Int64Null(),
|
||||
Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
|
||||
"class": types.StringNull(),
|
||||
"size": types.Int64Null(),
|
||||
}),
|
||||
Version: types.StringNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"simple_values",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&postgresflex.GetInstanceResponse{
|
||||
Acl: &[]string{
|
||||
"ip1",
|
||||
"ip2",
|
||||
"",
|
||||
},
|
||||
BackupSchedule: utils.Ptr("schedule"),
|
||||
//Flavor: &postgresflex.Flavor{
|
||||
// Cpu: utils.Ptr(int64(12)),
|
||||
// Description: utils.Ptr("description"),
|
||||
// Id: utils.Ptr("flavor_id"),
|
||||
// Memory: utils.Ptr(int64(34)),
|
||||
//},
|
||||
Id: utils.Ptr("iid"),
|
||||
Name: utils.Ptr("name"),
|
||||
Replicas: postgresflex.GetInstanceResponseGetReplicasAttributeType(utils.Ptr(int32(56))),
|
||||
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr("status")),
|
||||
Storage: &postgresflex.Storage{
|
||||
PerformanceClass: utils.Ptr("class"),
|
||||
Size: utils.Ptr(int64(78)),
|
||||
},
|
||||
Version: utils.Ptr("version"),
|
||||
},
|
||||
&flavorModel{},
|
||||
&storageModel{},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Name: types.StringValue("name"),
|
||||
ACL: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ip1"),
|
||||
types.StringValue("ip2"),
|
||||
types.StringValue(""),
|
||||
}),
|
||||
BackupSchedule: types.StringValue("schedule"),
|
||||
Flavor: types.ObjectValueMust(flavorTypes, map[string]attr.Value{
|
||||
"id": types.StringValue("flavor_id"),
|
||||
"description": types.StringValue("description"),
|
||||
"cpu": types.Int64Value(12),
|
||||
"ram": types.Int64Value(34),
|
||||
}),
|
||||
Replicas: types.Int64Value(56),
|
||||
Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
|
||||
"class": types.StringValue("class"),
|
||||
"size": types.Int64Value(78),
|
||||
}),
|
||||
Version: types.StringValue("version"),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"simple_values_no_flavor_and_storage",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&postgresflex.GetInstanceResponse{
|
||||
Acl: &[]string{
|
||||
"ip1",
|
||||
"ip2",
|
||||
"",
|
||||
},
|
||||
BackupSchedule: utils.Ptr("schedule"),
|
||||
FlavorId: nil,
|
||||
Id: utils.Ptr("iid"),
|
||||
Name: utils.Ptr("name"),
|
||||
Replicas: postgresflex.GetInstanceResponseGetReplicasAttributeType(utils.Ptr(int32(56))),
|
||||
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr("status")),
|
||||
Storage: nil,
|
||||
Version: utils.Ptr("version"),
|
||||
},
|
||||
&flavorModel{
|
||||
CPU: types.Int64Value(12),
|
||||
RAM: types.Int64Value(34),
|
||||
},
|
||||
&storageModel{
|
||||
Class: types.StringValue("class"),
|
||||
Size: types.Int64Value(78),
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Name: types.StringValue("name"),
|
||||
ACL: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ip1"),
|
||||
types.StringValue("ip2"),
|
||||
types.StringValue(""),
|
||||
}),
|
||||
BackupSchedule: types.StringValue("schedule"),
|
||||
Flavor: types.ObjectValueMust(flavorTypes, map[string]attr.Value{
|
||||
"id": types.StringNull(),
|
||||
"description": types.StringNull(),
|
||||
"cpu": types.Int64Value(12),
|
||||
"ram": types.Int64Value(34),
|
||||
}),
|
||||
Replicas: types.Int64Value(56),
|
||||
Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
|
||||
"class": types.StringValue("class"),
|
||||
"size": types.Int64Value(78),
|
||||
}),
|
||||
Version: types.StringValue("version"),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"acl_unordered",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ACL: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ip2"),
|
||||
types.StringValue(""),
|
||||
types.StringValue("ip1"),
|
||||
}),
|
||||
},
|
||||
&postgresflex.GetInstanceResponse{
|
||||
Acl: &[]string{
|
||||
"",
|
||||
"ip1",
|
||||
"ip2",
|
||||
},
|
||||
BackupSchedule: utils.Ptr("schedule"),
|
||||
FlavorId: nil,
|
||||
Id: utils.Ptr("iid"),
|
||||
Name: utils.Ptr("name"),
|
||||
Replicas: postgresflex.GetInstanceResponseGetReplicasAttributeType(utils.Ptr(int32(56))),
|
||||
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr("status")),
|
||||
Storage: nil,
|
||||
Version: utils.Ptr("version"),
|
||||
},
|
||||
&flavorModel{
|
||||
CPU: types.Int64Value(12),
|
||||
RAM: types.Int64Value(34),
|
||||
},
|
||||
&storageModel{
|
||||
Class: types.StringValue("class"),
|
||||
Size: types.Int64Value(78),
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Name: types.StringValue("name"),
|
||||
ACL: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ip2"),
|
||||
types.StringValue(""),
|
||||
types.StringValue("ip1"),
|
||||
}),
|
||||
BackupSchedule: types.StringValue("schedule"),
|
||||
Flavor: types.ObjectValueMust(flavorTypes, map[string]attr.Value{
|
||||
"id": types.StringNull(),
|
||||
"description": types.StringNull(),
|
||||
"cpu": types.Int64Value(12),
|
||||
"ram": types.Int64Value(34),
|
||||
}),
|
||||
Replicas: types.Int64Value(56),
|
||||
Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
|
||||
"class": types.StringValue("class"),
|
||||
"size": types.Int64Value(78),
|
||||
}),
|
||||
Version: types.StringValue("version"),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
nil,
|
||||
&flavorModel{},
|
||||
&storageModel{},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&postgresflex.GetInstanceResponse{},
|
||||
&flavorModel{},
|
||||
&storageModel{},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
err := mapFields(context.Background(), tt.input, &tt.state, tt.flavor, tt.storage, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(tt.state, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToCreatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *Model
|
||||
inputAcl []string
|
||||
inputFlavor *flavorModel
|
||||
inputStorage *storageModel
|
||||
inputEncryption *encryptionModel
|
||||
inputNetwork *networkModel
|
||||
expected *postgresflex.CreateInstanceRequestPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&Model{},
|
||||
[]string{},
|
||||
&flavorModel{},
|
||||
&storageModel{},
|
||||
&encryptionModel{},
|
||||
&networkModel{},
|
||||
&postgresflex.CreateInstanceRequestPayload{
|
||||
Acl: &[]string{},
|
||||
Storage: postgresflex.CreateInstanceRequestPayloadGetStorageAttributeType(&postgresflex.Storage{}),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"simple_values",
|
||||
&Model{
|
||||
BackupSchedule: types.StringValue("schedule"),
|
||||
Name: types.StringValue("name"),
|
||||
Replicas: types.Int64Value(12),
|
||||
Version: types.StringValue("version"),
|
||||
},
|
||||
[]string{
|
||||
"ip_1",
|
||||
"ip_2",
|
||||
},
|
||||
&flavorModel{
|
||||
Id: types.StringValue("flavor_id"),
|
||||
},
|
||||
&storageModel{
|
||||
Class: types.StringValue("class"),
|
||||
Size: types.Int64Value(34),
|
||||
},
|
||||
&encryptionModel{},
|
||||
&networkModel{},
|
||||
&postgresflex.CreateInstanceRequestPayload{
|
||||
Acl: &[]string{
|
||||
"ip_1",
|
||||
"ip_2",
|
||||
},
|
||||
BackupSchedule: utils.Ptr("schedule"),
|
||||
FlavorId: utils.Ptr("flavor_id"),
|
||||
Name: utils.Ptr("name"),
|
||||
Replicas: postgresflex.CreateInstanceRequestPayloadGetReplicasAttributeType(utils.Ptr(int32(56))),
|
||||
Storage: postgresflex.CreateInstanceRequestPayloadGetStorageAttributeType(&postgresflex.Storage{
|
||||
PerformanceClass: utils.Ptr("class"),
|
||||
Size: utils.Ptr(int64(34)),
|
||||
}),
|
||||
Version: utils.Ptr("version"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&Model{
|
||||
BackupSchedule: types.StringNull(),
|
||||
Name: types.StringNull(),
|
||||
Replicas: types.Int64Value(2123456789),
|
||||
Version: types.StringNull(),
|
||||
},
|
||||
[]string{
|
||||
"",
|
||||
},
|
||||
&flavorModel{
|
||||
Id: types.StringNull(),
|
||||
},
|
||||
&storageModel{
|
||||
Class: types.StringNull(),
|
||||
Size: types.Int64Null(),
|
||||
},
|
||||
&encryptionModel{},
|
||||
&networkModel{},
|
||||
&postgresflex.CreateInstanceRequestPayload{
|
||||
Acl: &[]string{
|
||||
"",
|
||||
},
|
||||
BackupSchedule: nil,
|
||||
FlavorId: nil,
|
||||
Name: nil,
|
||||
Replicas: postgresflex.CreateInstanceRequestPayloadGetReplicasAttributeType(utils.Ptr(int32(2123456789))),
|
||||
Storage: postgresflex.CreateInstanceRequestPayloadGetStorageAttributeType(&postgresflex.Storage{
|
||||
PerformanceClass: nil,
|
||||
Size: nil,
|
||||
}),
|
||||
Version: nil,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_model",
|
||||
nil,
|
||||
[]string{},
|
||||
&flavorModel{},
|
||||
&storageModel{},
|
||||
&encryptionModel{},
|
||||
&networkModel{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_acl",
|
||||
&Model{},
|
||||
nil,
|
||||
&flavorModel{},
|
||||
&storageModel{},
|
||||
&encryptionModel{},
|
||||
&networkModel{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_flavor",
|
||||
&Model{},
|
||||
[]string{},
|
||||
nil,
|
||||
&storageModel{},
|
||||
&encryptionModel{},
|
||||
&networkModel{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_storage",
|
||||
&Model{},
|
||||
[]string{},
|
||||
&flavorModel{},
|
||||
nil,
|
||||
&encryptionModel{},
|
||||
&networkModel{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
output, err := toCreatePayload(tt.input, tt.inputAcl, tt.inputFlavor, tt.inputStorage, tt.inputEncryption, tt.inputNetwork)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(output, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//func TestToUpdatePayload(t *testing.T) {
|
||||
// tests := []struct {
|
||||
// description string
|
||||
// input *Model
|
||||
// inputAcl []string
|
||||
// inputFlavor *flavorModel
|
||||
// inputStorage *storageModel
|
||||
// expected *postgresflex.PartialUpdateInstancePayload
|
||||
// isValid bool
|
||||
// }{
|
||||
// {
|
||||
// "default_values",
|
||||
// &Model{},
|
||||
// []string{},
|
||||
// &flavorModel{},
|
||||
// &storageModel{},
|
||||
// &postgresflex.PartialUpdateInstancePayload{
|
||||
// Acl: &postgresflex.ACL{
|
||||
// Items: &[]string{},
|
||||
// },
|
||||
// Storage: &postgresflex.Storage{},
|
||||
// },
|
||||
// true,
|
||||
// },
|
||||
// {
|
||||
// "simple_values",
|
||||
// &Model{
|
||||
// BackupSchedule: types.StringValue("schedule"),
|
||||
// Name: types.StringValue("name"),
|
||||
// Replicas: types.Int64Value(12),
|
||||
// Version: types.StringValue("version"),
|
||||
// },
|
||||
// []string{
|
||||
// "ip_1",
|
||||
// "ip_2",
|
||||
// },
|
||||
// &flavorModel{
|
||||
// Id: types.StringValue("flavor_id"),
|
||||
// },
|
||||
// &storageModel{
|
||||
// Class: types.StringValue("class"),
|
||||
// Size: types.Int64Value(34),
|
||||
// },
|
||||
// &postgresflex.PartialUpdateInstancePayload{
|
||||
// Acl: &postgresflex.ACL{
|
||||
// Items: &[]string{
|
||||
// "ip_1",
|
||||
// "ip_2",
|
||||
// },
|
||||
// },
|
||||
// BackupSchedule: utils.Ptr("schedule"),
|
||||
// FlavorId: utils.Ptr("flavor_id"),
|
||||
// Name: utils.Ptr("name"),
|
||||
// Replicas: utils.Ptr(int64(12)),
|
||||
// Storage: &postgresflex.Storage{
|
||||
// Class: utils.Ptr("class"),
|
||||
// Size: utils.Ptr(int64(34)),
|
||||
// },
|
||||
// Version: utils.Ptr("version"),
|
||||
// },
|
||||
// true,
|
||||
// },
|
||||
// {
|
||||
// "null_fields_and_int_conversions",
|
||||
// &Model{
|
||||
// BackupSchedule: types.StringNull(),
|
||||
// Name: types.StringNull(),
|
||||
// Replicas: types.Int64Value(2123456789),
|
||||
// Version: types.StringNull(),
|
||||
// },
|
||||
// []string{
|
||||
// "",
|
||||
// },
|
||||
// &flavorModel{
|
||||
// Id: types.StringNull(),
|
||||
// },
|
||||
// &storageModel{
|
||||
// Class: types.StringNull(),
|
||||
// Size: types.Int64Null(),
|
||||
// },
|
||||
// &postgresflex.PartialUpdateInstancePayload{
|
||||
// Acl: &postgresflex.ACL{
|
||||
// Items: &[]string{
|
||||
// "",
|
||||
// },
|
||||
// },
|
||||
// BackupSchedule: nil,
|
||||
// FlavorId: nil,
|
||||
// Name: nil,
|
||||
// Replicas: utils.Ptr(int64(2123456789)),
|
||||
// Storage: &postgresflex.Storage{
|
||||
// Class: nil,
|
||||
// Size: nil,
|
||||
// },
|
||||
// Version: nil,
|
||||
// },
|
||||
// true,
|
||||
// },
|
||||
// {
|
||||
// "nil_model",
|
||||
// nil,
|
||||
// []string{},
|
||||
// &flavorModel{},
|
||||
// &storageModel{},
|
||||
// nil,
|
||||
// false,
|
||||
// },
|
||||
// {
|
||||
// "nil_acl",
|
||||
// &Model{},
|
||||
// nil,
|
||||
// &flavorModel{},
|
||||
// &storageModel{},
|
||||
// nil,
|
||||
// false,
|
||||
// },
|
||||
// {
|
||||
// "nil_flavor",
|
||||
// &Model{},
|
||||
// []string{},
|
||||
// nil,
|
||||
// &storageModel{},
|
||||
// nil,
|
||||
// false,
|
||||
// },
|
||||
// {
|
||||
// "nil_storage",
|
||||
// &Model{},
|
||||
// []string{},
|
||||
// &flavorModel{},
|
||||
// nil,
|
||||
// nil,
|
||||
// false,
|
||||
// },
|
||||
// }
|
||||
// for _, tt := range tests {
|
||||
// t.Run(tt.description, func(t *testing.T) {
|
||||
// output, err := toUpdatePayload(tt.input, tt.inputAcl, tt.inputFlavor, tt.inputStorage)
|
||||
// if !tt.isValid && err == nil {
|
||||
// t.Fatalf("Should have failed")
|
||||
// }
|
||||
// if tt.isValid && err != nil {
|
||||
// t.Fatalf("Should not have failed: %v", err)
|
||||
// }
|
||||
// if tt.isValid {
|
||||
// diff := cmp.Diff(output, tt.expected)
|
||||
// if diff != "" {
|
||||
// t.Fatalf("Data does not match: %s", diff)
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//func TestLoadFlavorId(t *testing.T) {
|
||||
// tests := []struct {
|
||||
// description string
|
||||
// inputFlavor *flavorModel
|
||||
// mockedResp *postgresflex.ListFlavorsResponse
|
||||
// expected *flavorModel
|
||||
// getFlavorsFails bool
|
||||
// isValid bool
|
||||
// }{
|
||||
// {
|
||||
// "ok_flavor",
|
||||
// &flavorModel{
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// &postgresflex.ListFlavorsResponse{
|
||||
// Flavors: &[]postgresflex.Flavor{
|
||||
// {
|
||||
// Id: utils.Ptr("fid-1"),
|
||||
// Cpu: utils.Ptr(int64(2)),
|
||||
// Description: utils.Ptr("description"),
|
||||
// Memory: utils.Ptr(int64(8)),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// &flavorModel{
|
||||
// Id: types.StringValue("fid-1"),
|
||||
// Description: types.StringValue("description"),
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// false,
|
||||
// true,
|
||||
// },
|
||||
// {
|
||||
// "ok_flavor_2",
|
||||
// &flavorModel{
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// &postgresflex.ListFlavorsResponse{
|
||||
// Flavors: &[]postgresflex.Flavor{
|
||||
// {
|
||||
// Id: utils.Ptr("fid-1"),
|
||||
// Cpu: utils.Ptr(int64(2)),
|
||||
// Description: utils.Ptr("description"),
|
||||
// Memory: utils.Ptr(int64(8)),
|
||||
// },
|
||||
// {
|
||||
// Id: utils.Ptr("fid-2"),
|
||||
// Cpu: utils.Ptr(int64(1)),
|
||||
// Description: utils.Ptr("description"),
|
||||
// Memory: utils.Ptr(int64(4)),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// &flavorModel{
|
||||
// Id: types.StringValue("fid-1"),
|
||||
// Description: types.StringValue("description"),
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// false,
|
||||
// true,
|
||||
// },
|
||||
// {
|
||||
// "no_matching_flavor",
|
||||
// &flavorModel{
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// &postgresflex.ListFlavorsResponse{
|
||||
// Flavors: &[]postgresflex.Flavor{
|
||||
// {
|
||||
// Id: utils.Ptr("fid-1"),
|
||||
// Cpu: utils.Ptr(int64(1)),
|
||||
// Description: utils.Ptr("description"),
|
||||
// Memory: utils.Ptr(int64(8)),
|
||||
// },
|
||||
// {
|
||||
// Id: utils.Ptr("fid-2"),
|
||||
// Cpu: utils.Ptr(int64(1)),
|
||||
// Description: utils.Ptr("description"),
|
||||
// Memory: utils.Ptr(int64(4)),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// &flavorModel{
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// false,
|
||||
// false,
|
||||
// },
|
||||
// {
|
||||
// "nil_response",
|
||||
// &flavorModel{
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// &postgresflex.ListFlavorsResponse{},
|
||||
// &flavorModel{
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// false,
|
||||
// false,
|
||||
// },
|
||||
// {
|
||||
// "error_response",
|
||||
// &flavorModel{
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// &postgresflex.ListFlavorsResponse{},
|
||||
// &flavorModel{
|
||||
// CPU: types.Int64Value(2),
|
||||
// RAM: types.Int64Value(8),
|
||||
// },
|
||||
// true,
|
||||
// false,
|
||||
// },
|
||||
// }
|
||||
// for _, tt := range tests {
|
||||
// t.Run(tt.description, func(t *testing.T) {
|
||||
// client := &postgresFlexClientMocked{
|
||||
// returnError: tt.getFlavorsFails,
|
||||
// getFlavorsResp: tt.mockedResp,
|
||||
// }
|
||||
// model := &Model{
|
||||
// ProjectId: types.StringValue("pid"),
|
||||
// }
|
||||
// flavorModel := &flavorModel{
|
||||
// CPU: tt.inputFlavor.CPU,
|
||||
// RAM: tt.inputFlavor.RAM,
|
||||
// }
|
||||
// err := loadFlavorId(context.Background(), client, model, flavorModel)
|
||||
// if !tt.isValid && err == nil {
|
||||
// t.Fatalf("Should have failed")
|
||||
// }
|
||||
// if tt.isValid && err != nil {
|
||||
// t.Fatalf("Should not have failed: %v", err)
|
||||
// }
|
||||
// if tt.isValid {
|
||||
// diff := cmp.Diff(flavorModel, tt.expected)
|
||||
// if diff != "" {
|
||||
// t.Fatalf("Data does not match: %s", diff)
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
//}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,87 +0,0 @@
|
|||
// Copyright (c) STACKIT
|
||||
|
||||
package postgresflex
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
)
|
||||
|
||||
type useStateForUnknownIfFlavorUnchangedModifier struct {
|
||||
Req resource.SchemaRequest
|
||||
}
|
||||
|
||||
// UseStateForUnknownIfFlavorUnchanged returns a plan modifier similar to UseStateForUnknown
|
||||
// if the RAM and CPU values are not changed in the plan. Otherwise, the plan modifier does nothing.
|
||||
func UseStateForUnknownIfFlavorUnchanged(req resource.SchemaRequest) planmodifier.String {
|
||||
return useStateForUnknownIfFlavorUnchangedModifier{
|
||||
Req: req,
|
||||
}
|
||||
}
|
||||
|
||||
func (m useStateForUnknownIfFlavorUnchangedModifier) Description(context.Context) string {
|
||||
return "UseStateForUnknownIfFlavorUnchanged returns a plan modifier similar to UseStateForUnknown if the RAM and CPU values are not changed in the plan. Otherwise, the plan modifier does nothing."
|
||||
}
|
||||
|
||||
func (m useStateForUnknownIfFlavorUnchangedModifier) MarkdownDescription(ctx context.Context) string {
|
||||
return m.Description(ctx)
|
||||
}
|
||||
|
||||
func (m useStateForUnknownIfFlavorUnchangedModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
// Do nothing if there is no state value.
|
||||
if req.StateValue.IsNull() {
|
||||
return
|
||||
}
|
||||
|
||||
// Do nothing if there is a known planned value.
|
||||
if !req.PlanValue.IsUnknown() {
|
||||
return
|
||||
}
|
||||
|
||||
// Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up.
|
||||
if req.ConfigValue.IsUnknown() {
|
||||
return
|
||||
}
|
||||
|
||||
// The above checks are taken from the UseStateForUnknown plan modifier implementation
|
||||
// (https://github.com/hashicorp/terraform-plugin-framework/blob/main/resource/schema/stringplanmodifier/use_state_for_unknown.go#L38)
|
||||
|
||||
var stateModel Model
|
||||
diags := req.State.Get(ctx, &stateModel)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
var stateFlavor = &flavorModel{}
|
||||
if !(stateModel.Flavor.IsNull() || stateModel.Flavor.IsUnknown()) {
|
||||
diags = stateModel.Flavor.As(ctx, stateFlavor, basetypes.ObjectAsOptions{})
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var planModel Model
|
||||
diags = req.Plan.Get(ctx, &planModel)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
var planFlavor = &flavorModel{}
|
||||
if !(planModel.Flavor.IsNull() || planModel.Flavor.IsUnknown()) {
|
||||
diags = planModel.Flavor.As(ctx, planFlavor, basetypes.ObjectAsOptions{})
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if planFlavor.CPU == stateFlavor.CPU && planFlavor.RAM == stateFlavor.RAM {
|
||||
resp.PlanValue = req.StateValue
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue