feat: refactor and extend builder
This commit is contained in:
parent
470e89e5f3
commit
1e2ee14821
1 changed files with 82 additions and 8 deletions
|
|
@ -50,7 +50,7 @@ func (r *{{.NameCamel}}Resource) Metadata(ctx context.Context, req resource.Meta
|
|||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = {{.PackageName}}Gen.{{.NamePascal}}ResourceSchema(ctx)
|
||||
resp.Schema = {{.PackageName}}ResGen.{{.NamePascal}}ResourceSchema(ctx)
|
||||
}
|
||||
|
||||
func (r *instanceResource) IdentitySchema(_ context.Context, _ resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) {
|
||||
|
|
@ -107,7 +107,7 @@ func (r *{{.NameCamel}}Resource) Configure(
|
|||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
var data {{.PackageName}}ResGen.{{.NamePascal}}Model
|
||||
|
||||
// Read Terraform plan data into the model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
|
|
@ -131,6 +131,33 @@ func (r *{{.NameCamel}}Resource) Create(ctx context.Context, req resource.Create
|
|||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
// TODO: Create API call logic
|
||||
/*
|
||||
// Generate API request body from model
|
||||
payload, err := toCreatePayload(ctx, &model)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error creating {{.NamePascal}}",
|
||||
fmt.Sprintf("Creating API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
// Create new {{.NamePascal}}
|
||||
createResp, err := r.client.Create{{.NamePascal}}Request(
|
||||
ctx,
|
||||
projectId,
|
||||
region,
|
||||
).Create{{.NamePascal}}RequestPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating {{.NamePascal}}", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
{{.NamePascal}}Id := *createResp.Id
|
||||
*/
|
||||
|
||||
// Example data value setting
|
||||
data.{{.NameCamel | ucfirst}}Id = types.StringValue("id-from-response")
|
||||
|
|
@ -139,7 +166,8 @@ func (r *{{.NameCamel}}Resource) Create(ctx context.Context, req resource.Create
|
|||
identity := {{.NamePascal}}ResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
// InstanceID: types.StringValue(instanceId),
|
||||
// TODO: add missing values
|
||||
{{.NamePascal}}ID: types.StringValue({{.NamePascal}}Id),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -147,6 +175,52 @@ func (r *{{.NameCamel}}Resource) Create(ctx context.Context, req resource.Create
|
|||
}
|
||||
|
||||
// TODO: implement wait handler if needed
|
||||
/*
|
||||
|
||||
waitResp, err := wait.Create{{.NamePascal}}WaitHandler(
|
||||
ctx,
|
||||
r.client,
|
||||
projectId,
|
||||
{{.NamePascal}}Id,
|
||||
region,
|
||||
).SetSleepBeforeWait(
|
||||
30 * time.Second,
|
||||
).SetTimeout(
|
||||
90 * time.Minute,
|
||||
).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error creating {{.NamePascal}}",
|
||||
fmt.Sprintf("{{.NamePascal}} creation waiting: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if waitResp.Id == nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error creating {{.NamePascal}}",
|
||||
"{{.NamePascal}} creation waiting: returned id is nil",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapResponseToModel(ctx, waitResp, &model, resp.Diagnostics)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error creating {{.NamePascal}}",
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
|
|
@ -155,7 +229,7 @@ func (r *{{.NameCamel}}Resource) Create(ctx context.Context, req resource.Create
|
|||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var data {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
var data {{.PackageName}}ResGen.{{.NamePascal}}Model
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -198,7 +272,7 @@ func (r *{{.NameCamel}}Resource) Read(ctx context.Context, req resource.ReadRequ
|
|||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
var data {{.PackageName}}ResGen.{{.NamePascal}}Model
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -230,7 +304,7 @@ func (r *{{.NameCamel}}Resource) Update(ctx context.Context, req resource.Update
|
|||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var data {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
var data {{.PackageName}}ResGen.{{.NamePascal}}Model
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -265,7 +339,7 @@ func (r *{{.NameCamel}}Resource) ModifyPlan(
|
|||
req resource.ModifyPlanRequest,
|
||||
resp *resource.ModifyPlanResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
var configModel {{.PackageName}}ResGen.{{.NamePascal}}Model
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
|
|
@ -275,7 +349,7 @@ func (r *{{.NameCamel}}Resource) ModifyPlan(
|
|||
return
|
||||
}
|
||||
|
||||
var planModel {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
var planModel {{.PackageName}}ResGen.{{.NamePascal}}Model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue