chore: work save
[skip ci]
This commit is contained in:
parent
ba579760d5
commit
042e8115a6
4 changed files with 44 additions and 115 deletions
|
|
@ -55,13 +55,13 @@ func mapGetInstanceResponseToModel(
|
||||||
}
|
}
|
||||||
|
|
||||||
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||||
if m.Id.IsNull() || m.Id.IsUnknown() {
|
//if m.Id.IsNull() || m.Id.IsUnknown() {
|
||||||
m.Id = utils.BuildInternalTerraformId(
|
m.Id = utils.BuildInternalTerraformId(
|
||||||
m.ProjectId.ValueString(),
|
m.ProjectId.ValueString(),
|
||||||
m.Region.ValueString(),
|
m.Region.ValueString(),
|
||||||
m.InstanceId.ValueString(),
|
m.InstanceId.ValueString(),
|
||||||
)
|
)
|
||||||
}
|
//}
|
||||||
m.InstanceId = types.StringValue(resp.Id)
|
m.InstanceId = types.StringValue(resp.Id)
|
||||||
|
|
||||||
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||||
|
|
|
||||||
|
|
@ -341,7 +341,7 @@ func (r *instanceResource) Read(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !model.Id.IsUnknown() && !model.Id.IsNull() {
|
if model.Id.IsUnknown() || model.Id.IsNull() {
|
||||||
model.Id = utils.BuildInternalTerraformId(projectID, region, instanceID)
|
model.Id = utils.BuildInternalTerraformId(projectID, region, instanceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||||
|
|
||||||
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3beta1api"
|
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3beta1api"
|
||||||
|
|
||||||
|
|
@ -27,7 +28,7 @@ func mapResponseToModel(
|
||||||
m.Edition = types.StringValue(string(resp.GetEdition()))
|
m.Edition = types.StringValue(string(resp.GetEdition()))
|
||||||
m.Encryption = handleEncryption(ctx, m, resp)
|
m.Encryption = handleEncryption(ctx, m, resp)
|
||||||
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||||
m.Id = types.StringValue(resp.GetId())
|
m.Id = utils.BuildInternalTerraformId(m.ProjectId.ValueString(), m.Region.ValueString(), resp.GetId())
|
||||||
m.InstanceId = types.StringValue(resp.GetId())
|
m.InstanceId = types.StringValue(resp.GetId())
|
||||||
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||||
m.Name = types.StringValue(resp.GetName())
|
m.Name = types.StringValue(resp.GetName())
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import (
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||||
|
|
@ -32,7 +31,6 @@ var (
|
||||||
_ resource.ResourceWithConfigure = &instanceResource{}
|
_ resource.ResourceWithConfigure = &instanceResource{}
|
||||||
_ resource.ResourceWithImportState = &instanceResource{}
|
_ resource.ResourceWithImportState = &instanceResource{}
|
||||||
_ resource.ResourceWithModifyPlan = &instanceResource{}
|
_ resource.ResourceWithModifyPlan = &instanceResource{}
|
||||||
_ resource.ResourceWithIdentity = &instanceResource{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewInstanceResource() resource.Resource {
|
func NewInstanceResource() resource.Resource {
|
||||||
|
|
@ -47,12 +45,6 @@ type instanceResource struct {
|
||||||
// resourceModel describes the resource data model.
|
// resourceModel describes the resource data model.
|
||||||
type resourceModel = sqlserverflexbetaResGen.InstanceModel
|
type resourceModel = sqlserverflexbetaResGen.InstanceModel
|
||||||
|
|
||||||
type InstanceResourceIdentityModel struct {
|
|
||||||
ProjectID types.String `tfsdk:"project_id"`
|
|
||||||
Region types.String `tfsdk:"region"`
|
|
||||||
InstanceID types.String `tfsdk:"instance_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *instanceResource) Metadata(
|
func (r *instanceResource) Metadata(
|
||||||
_ context.Context,
|
_ context.Context,
|
||||||
req resource.MetadataRequest,
|
req resource.MetadataRequest,
|
||||||
|
|
@ -81,26 +73,6 @@ func (r *instanceResource) Schema(ctx context.Context, _ resource.SchemaRequest,
|
||||||
resp.Schema = s
|
resp.Schema = s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *instanceResource) IdentitySchema(
|
|
||||||
_ context.Context,
|
|
||||||
_ resource.IdentitySchemaRequest,
|
|
||||||
resp *resource.IdentitySchemaResponse,
|
|
||||||
) {
|
|
||||||
resp.IdentitySchema = identityschema.Schema{
|
|
||||||
Attributes: map[string]identityschema.Attribute{
|
|
||||||
"project_id": identityschema.StringAttribute{
|
|
||||||
RequiredForImport: true, // must be set during import by the practitioner
|
|
||||||
},
|
|
||||||
"region": identityschema.StringAttribute{
|
|
||||||
RequiredForImport: true, // can be defaulted by the provider configuration
|
|
||||||
},
|
|
||||||
"instance_id": identityschema.StringAttribute{
|
|
||||||
RequiredForImport: true, // can be defaulted by the provider configuration
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configure adds the provider configured client to the resource.
|
// Configure adds the provider configured client to the resource.
|
||||||
func (r *instanceResource) Configure(
|
func (r *instanceResource) Configure(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
|
|
@ -190,9 +162,9 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
|
||||||
|
|
||||||
ctx = core.InitProviderContext(ctx)
|
ctx = core.InitProviderContext(ctx)
|
||||||
|
|
||||||
projectId := data.ProjectId.ValueString()
|
projectID := data.ProjectId.ValueString()
|
||||||
region := data.Region.ValueString()
|
region := data.Region.ValueString()
|
||||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
ctx = tflog.SetField(ctx, "project_id", projectID)
|
||||||
ctx = tflog.SetField(ctx, "region", region)
|
ctx = tflog.SetField(ctx, "region", region)
|
||||||
|
|
||||||
// Generate API request body from model
|
// Generate API request body from model
|
||||||
|
|
@ -210,7 +182,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
|
||||||
// Create new Instance
|
// Create new Instance
|
||||||
createResp, err := r.client.DefaultAPI.CreateInstanceRequest(
|
createResp, err := r.client.DefaultAPI.CreateInstanceRequest(
|
||||||
ctx,
|
ctx,
|
||||||
projectId,
|
projectID,
|
||||||
region,
|
region,
|
||||||
).CreateInstanceRequestPayload(*payload).Execute()
|
).CreateInstanceRequestPayload(*payload).Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -220,24 +192,25 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
|
||||||
|
|
||||||
ctx = core.LogResponse(ctx)
|
ctx = core.LogResponse(ctx)
|
||||||
|
|
||||||
instanceId := createResp.Id
|
instanceID := createResp.Id
|
||||||
data.InstanceId = types.StringValue(instanceId)
|
data.InstanceId = types.StringValue(instanceID)
|
||||||
|
data.Id = utils.BuildInternalTerraformId(projectID, region, instanceID)
|
||||||
|
|
||||||
identity := InstanceResourceIdentityModel{
|
// Set data returned by API in id
|
||||||
ProjectID: types.StringValue(projectId),
|
resp.Diagnostics.Append(
|
||||||
Region: types.StringValue(region),
|
resp.State.SetAttribute(
|
||||||
InstanceID: types.StringValue(instanceId),
|
ctx,
|
||||||
}
|
path.Root("id"),
|
||||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
utils.BuildInternalTerraformId(projectID, region, instanceID),
|
||||||
if resp.Diagnostics.HasError() {
|
)...,
|
||||||
return
|
)
|
||||||
}
|
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceID)...)
|
||||||
|
|
||||||
waitResp, err := wait.CreateInstanceWaitHandler(
|
waitResp, err := wait.CreateInstanceWaitHandler(
|
||||||
ctx,
|
ctx,
|
||||||
r.client.DefaultAPI,
|
r.client.DefaultAPI,
|
||||||
projectId,
|
projectID,
|
||||||
instanceId,
|
instanceID,
|
||||||
region,
|
region,
|
||||||
).SetSleepBeforeWait(
|
).SetSleepBeforeWait(
|
||||||
10 * time.Second,
|
10 * time.Second,
|
||||||
|
|
@ -326,17 +299,6 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save identity into Terraform state
|
|
||||||
identity := InstanceResourceIdentityModel{
|
|
||||||
ProjectID: types.StringValue(projectId),
|
|
||||||
Region: types.StringValue(region),
|
|
||||||
InstanceID: types.StringValue(instanceId),
|
|
||||||
}
|
|
||||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
|
||||||
if resp.Diagnostics.HasError() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save updated data into Terraform state
|
// Save updated data into Terraform state
|
||||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
|
|
@ -417,16 +379,6 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
identity := InstanceResourceIdentityModel{
|
|
||||||
ProjectID: types.StringValue(projectId),
|
|
||||||
Region: types.StringValue(region),
|
|
||||||
InstanceID: types.StringValue(instanceId),
|
|
||||||
}
|
|
||||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
|
||||||
if resp.Diagnostics.HasError() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save updated data into Terraform state
|
// Save updated data into Terraform state
|
||||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
|
|
@ -445,21 +397,14 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read identity data
|
|
||||||
var identityData InstanceResourceIdentityModel
|
|
||||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
|
||||||
if resp.Diagnostics.HasError() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = core.InitProviderContext(ctx)
|
ctx = core.InitProviderContext(ctx)
|
||||||
|
|
||||||
projectId := identityData.ProjectID.ValueString()
|
projectId := data.ProjectId.ValueString()
|
||||||
region := identityData.Region.ValueString()
|
region := data.Region.ValueString()
|
||||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||||
ctx = tflog.SetField(ctx, "region", region)
|
ctx = tflog.SetField(ctx, "region", region)
|
||||||
|
|
||||||
instanceId := identityData.InstanceID.ValueString()
|
instanceId := data.InstanceId.ValueString()
|
||||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||||
|
|
||||||
// Delete existing instance
|
// Delete existing instance
|
||||||
|
|
@ -506,41 +451,24 @@ func (r *instanceResource) ImportState(
|
||||||
) {
|
) {
|
||||||
ctx = core.InitProviderContext(ctx)
|
ctx = core.InitProviderContext(ctx)
|
||||||
|
|
||||||
if req.ID != "" {
|
idParts := strings.Split(req.ID, core.Separator)
|
||||||
idParts := strings.Split(req.ID, core.Separator)
|
|
||||||
|
|
||||||
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
|
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
|
||||||
core.LogAndAddError(
|
core.LogAndAddError(
|
||||||
ctx, &resp.Diagnostics,
|
ctx, &resp.Diagnostics,
|
||||||
"Error importing instance",
|
"Error importing instance",
|
||||||
fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
"Expected import identifier with format [project_id],[region],[instance_id] Got: %q",
|
"Expected import identifier with format [project_id],[region],[instance_id] Got: %q",
|
||||||
req.ID,
|
req.ID,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
|
|
||||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...)
|
|
||||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no ID is provided, attempt to read identity attributes from the import configuration
|
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), utils.BuildInternalTerraformId(idParts...).ValueString())...)
|
||||||
var identityData InstanceResourceIdentityModel
|
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
|
||||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...)
|
||||||
if resp.Diagnostics.HasError() {
|
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
projectId := identityData.ProjectID.ValueString()
|
|
||||||
region := identityData.Region.ValueString()
|
|
||||||
instanceId := identityData.InstanceID.ValueString()
|
|
||||||
|
|
||||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)...)
|
|
||||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), region)...)
|
|
||||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)...)
|
|
||||||
|
|
||||||
tflog.Info(ctx, "Sqlserverflexbeta instance state imported")
|
tflog.Info(ctx, "Sqlserverflexbeta instance state imported")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue