fix: refactor identity data handling in resource.go
This commit is contained in:
parent
86fc98461c
commit
119356d8cb
4 changed files with 115 additions and 84 deletions
|
|
@ -178,18 +178,11 @@ func (r *databaseResource) Create(
|
|||
return
|
||||
}
|
||||
|
||||
// Read identity data
|
||||
var identityData DatabaseResourceIdentityModel
|
||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := identityData.ProjectID.ValueString()
|
||||
region := identityData.ProjectID.ValueString()
|
||||
instanceId := identityData.InstanceID.ValueString()
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := model.InstanceId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
|
|
@ -218,8 +211,6 @@ func (r *databaseResource) Create(
|
|||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
if databaseResp == nil || databaseResp.Id == nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
|
|
@ -231,6 +222,19 @@ func (r *databaseResource) Create(
|
|||
}
|
||||
databaseId := *databaseResp.Id
|
||||
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Save identity into Terraform state
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseID: types.Int64Value(databaseId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
database, err := getDatabaseById(ctx, r.client, projectId, region, instanceId, databaseId)
|
||||
if err != nil {
|
||||
|
|
@ -255,18 +259,6 @@ func (r *databaseResource) Create(
|
|||
return
|
||||
}
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseID: types.Int64Value(databaseId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set state to fully populated data
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -337,6 +329,18 @@ func (r *databaseResource) Read(
|
|||
return
|
||||
}
|
||||
|
||||
// Save identity into Terraform state
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseID: types.Int64Value(databaseId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
|
|
@ -440,9 +444,21 @@ func (r *databaseResource) Update(
|
|||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Save identity into Terraform state
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseID: types.Int64Value(databaseId64),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set state to fully populated data
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -155,18 +155,11 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
|
|||
return
|
||||
}
|
||||
|
||||
// Read identity data
|
||||
var identityData DatabaseResourceIdentityModel
|
||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := identityData.ProjectID.ValueString()
|
||||
region := identityData.ProjectID.ValueString()
|
||||
instanceId := identityData.InstanceID.ValueString()
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := model.Region.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
|
|
@ -215,6 +208,18 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseName: types.StringValue(databaseName),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
database, err := r.client.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
|
|
@ -238,18 +243,6 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
|
|||
return
|
||||
}
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseName: types.StringValue(databaseName),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set state to fully populated data
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -316,9 +309,20 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
|
|||
return
|
||||
}
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseName: types.StringValue(databaseName),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
|
@ -382,7 +386,7 @@ func (r *databaseResource) ModifyPlan(
|
|||
req resource.ModifyPlanRequest,
|
||||
resp *resource.ModifyPlanResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel sqlserverflexalphaGen.DatabaseModel
|
||||
var configModel resourceModel
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
|
|
@ -392,7 +396,7 @@ func (r *databaseResource) ModifyPlan(
|
|||
return
|
||||
}
|
||||
|
||||
var planModel sqlserverflexalphaGen.DatabaseModel
|
||||
var planModel resourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -485,8 +489,8 @@ func (r *databaseResource) extractIdentityData(
|
|||
model resourceModel,
|
||||
identity DatabaseResourceIdentityModel,
|
||||
) (projectId, region, instanceId, databaseName string, err error) {
|
||||
if !model.DatabaseName.IsNull() && !model.DatabaseName.IsUnknown() {
|
||||
databaseName = model.DatabaseName.ValueString()
|
||||
if !model.Name.IsNull() && !model.Name.IsUnknown() {
|
||||
databaseName = model.Name.ValueString()
|
||||
} else {
|
||||
if identity.DatabaseName.IsNull() || identity.DatabaseName.IsUnknown() {
|
||||
return "", "", "", "", fmt.Errorf("database_name not found in config")
|
||||
|
|
|
|||
|
|
@ -160,23 +160,16 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
|
|||
return
|
||||
}
|
||||
|
||||
// Read identity data
|
||||
var identityData DatabaseResourceIdentityModel
|
||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := identityData.ProjectID.ValueString()
|
||||
region := identityData.Region.ValueString()
|
||||
instanceId := identityData.InstanceID.ValueString()
|
||||
projectId := data.ProjectId.ValueString()
|
||||
region := data.Region.ValueString()
|
||||
instanceId := data.InstanceId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
|
||||
databaseName := identityData.DatabaseName.ValueString()
|
||||
databaseName := data.Name.ValueString()
|
||||
ctx = tflog.SetField(ctx, "database_name", databaseName)
|
||||
|
||||
payLoad := sqlserverflexbeta.CreateDatabaseRequestPayload{
|
||||
|
|
@ -215,6 +208,18 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseName: types.StringValue(databaseName),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: is this neccessary to wait for the database-> API say 200 ?
|
||||
/*waitResp, err := wait.CreateDatabaseWaitHandler(
|
||||
ctx,
|
||||
|
|
@ -301,18 +306,6 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
|
|||
return
|
||||
}
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseName: types.StringValue(databaseName),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set state to fully populated data
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -381,9 +374,20 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
|
|||
return
|
||||
}
|
||||
|
||||
// Save identity into Terraform state
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseName: types.StringValue(databaseName),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
|
@ -453,13 +457,13 @@ func (r *databaseResource) ModifyPlan(
|
|||
return
|
||||
}
|
||||
|
||||
var configModel sqlserverflexbetaResGen.DatabaseModel
|
||||
var configModel resourceModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
var planModel sqlserverflexbetaResGen.DatabaseModel
|
||||
var planModel resourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -562,8 +566,8 @@ func (r *databaseResource) extractIdentityData(
|
|||
model resourceModel,
|
||||
identity DatabaseResourceIdentityModel,
|
||||
) (projectId, region, instanceId, databaseName string, err error) {
|
||||
if !model.DatabaseName.IsNull() && !model.DatabaseName.IsUnknown() {
|
||||
databaseName = model.DatabaseName.ValueString()
|
||||
if !model.Name.IsNull() && !model.Name.IsUnknown() {
|
||||
databaseName = model.Name.ValueString()
|
||||
} else {
|
||||
if identity.DatabaseName.IsNull() || identity.DatabaseName.IsUnknown() {
|
||||
return "", "", "", "", fmt.Errorf("database_name not found in config")
|
||||
|
|
|
|||
|
|
@ -160,7 +160,7 @@ func (r *instanceResource) ModifyPlan(
|
|||
if req.Plan.Raw.IsNull() {
|
||||
return
|
||||
}
|
||||
var planModel sqlserverflexbetaResGen.InstanceModel
|
||||
var planModel resourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -334,6 +334,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
|
|||
return
|
||||
}
|
||||
|
||||
// Save identity into Terraform state
|
||||
identity := InstanceResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
|
|
@ -346,6 +347,9 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
|
|||
|
||||
// Save updated data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "sqlserverflexbeta.Instance read")
|
||||
}
|
||||
|
|
@ -433,6 +437,9 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
|
|||
|
||||
// Save updated data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "sqlserverflexbeta.Instance updated")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue