Skip to content

Commit

Permalink
Revert Host Maintenance Policy Support
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeyaraj-crusoe committed Dec 13, 2024
1 parent 2d23847 commit ed40f1f
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 94 deletions.
1 change: 0 additions & 1 deletion docs/data-sources/compute_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ description: |-
### Optional

- `disks` (Attributes List) (see [below for nested schema](#nestedatt--disks))
- `maintenance_policy` (String)
- `name` (String)
- `project_id` (String)
- `reservation_id` (String)
Expand Down
1 change: 0 additions & 1 deletion docs/data-sources/instance_templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Optional:
- `ib_partition` (String)
- `image` (String)
- `location` (String)
- `maintenance_policy` (String)
- `placement_policy` (String)
- `project_id` (String)
- `public_ip_address_type` (String)
Expand Down
1 change: 0 additions & 1 deletion docs/resources/compute_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ description: |-
- `host_channel_adapters` (Attributes List) (see [below for nested schema](#nestedatt--host_channel_adapters))
- `image` (String)
- `location` (String)
- `maintenance_policy` (String)
- `network_interfaces` (Attributes List) (see [below for nested schema](#nestedatt--network_interfaces))
- `project_id` (String)
- `reservation_id` (String) ID of the reservation to which the VM belongs. If not provided or null, the lowest-cost reservation will be used by default. To opt out of using a reservation, set this to an empty string.
Expand Down
1 change: 0 additions & 1 deletion docs/resources/instance_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ description: |-
- `ib_partition` (String)
- `image` (String)
- `location` (String)
- `maintenance_policy` (String)
- `placement_policy` (String)
- `project_id` (String)
- `public_ip_address_type` (String)
Expand Down
32 changes: 13 additions & 19 deletions internal/instance_template/instance_template_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type instanceTemplatesModel struct {
IBPartition string `tfsdk:"ib_partition"`
Disks []diskModel `tfsdk:"disks"`
PlacementPolicy string `tfsdk:"placement_policy"`
MaintenancePolicy string `tfsdk:"maintenance_policy"`
}

func NewInstanceTemplatesDataSource() datasource.DataSource {
Expand Down Expand Up @@ -132,10 +131,6 @@ func (ds *instanceTemplatesDataSource) Schema(ctx context.Context, request datas
Optional: true,
Computed: true,
},
"maintenance_policy": schema.StringAttribute{
Optional: true,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -173,20 +168,19 @@ func (ds *instanceTemplatesDataSource) Read(ctx context.Context, req datasource.
var state instanceTemplatesDataSourceModel
for i := range dataResp.Items {
state.InstanceTemplates = append(state.InstanceTemplates, instanceTemplatesModel{
ID: dataResp.Items[i].Id,
Name: dataResp.Items[i].Name,
Type: dataResp.Items[i].Type_,
SSHKey: dataResp.Items[i].SshPublicKey,
Location: dataResp.Items[i].Location,
ImageName: dataResp.Items[i].ImageName,
StartupScript: dataResp.Items[i].StartupScript,
ShutdownScript: dataResp.Items[i].ShutdownScript,
SubnetId: dataResp.Items[i].SubnetId,
IBPartition: dataResp.Items[i].IbPartitionId,
ProjectID: dataResp.Items[i].ProjectId,
Disks: disks,
PlacementPolicy: dataResp.Items[i].PlacementPolicy,
MaintenancePolicy: dataResp.Items[i].MaintenancePolicy,
ID: dataResp.Items[i].Id,
Name: dataResp.Items[i].Name,
Type: dataResp.Items[i].Type_,
SSHKey: dataResp.Items[i].SshPublicKey,
Location: dataResp.Items[i].Location,
ImageName: dataResp.Items[i].ImageName,
StartupScript: dataResp.Items[i].StartupScript,
ShutdownScript: dataResp.Items[i].ShutdownScript,
SubnetId: dataResp.Items[i].SubnetId,
IBPartition: dataResp.Items[i].IbPartitionId,
ProjectID: dataResp.Items[i].ProjectId,
Disks: disks,
PlacementPolicy: dataResp.Items[i].PlacementPolicy,
})
}

Expand Down
20 changes: 4 additions & 16 deletions internal/instance_template/instance_template_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import (
)

const (
spreadPlacementPolicy = "spread"
unspecifiedPolicy = "unspecified"
stopVMPolicy = "stop-vm"
spreadPlacementPolicy = "spread"
unspecifiedPlacementPolicy = "unspecified"
)

type instanceTemplateResource struct {
Expand All @@ -47,7 +46,6 @@ type instanceTemplateResourceModel struct {
DisksToCreate types.Set `tfsdk:"disks"`
ReservationID types.String `tfsdk:"reservation_id"`
PlacementPolicy types.String `tfsdk:"placement_policy"`
MaintenancePolicy types.String `tfsdk:"maintenance_policy"`
}

type diskToCreateResourceModel struct {
Expand Down Expand Up @@ -177,15 +175,8 @@ func (r *instanceTemplateResource) Schema(ctx context.Context, req resource.Sche
"placement_policy": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString(unspecifiedPolicy),
Validators: []validator.String{stringvalidator.OneOf(spreadPlacementPolicy, unspecifiedPolicy)},
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace(), stringplanmodifier.UseStateForUnknown()}, // cannot be updated in place
},
"maintenance_policy": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString(unspecifiedPolicy),
Validators: []validator.String{stringvalidator.OneOf(unspecifiedPolicy, stopVMPolicy)},
Default: stringdefault.StaticString("unspecified"),
Validators: []validator.String{stringvalidator.OneOf(spreadPlacementPolicy, unspecifiedPlacementPolicy)},
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace(), stringplanmodifier.UseStateForUnknown()}, // cannot be updated in place
},
},
Expand Down Expand Up @@ -248,7 +239,6 @@ func (r *instanceTemplateResource) Create(ctx context.Context, req resource.Crea
PublicIpAddressType: plan.PublicIpAddressType.ValueString(),
ReservationId: plan.ReservationID.ValueString(),
PlacementPolicy: plan.PlacementPolicy.ValueString(),
MaintenancePolicy: plan.MaintenancePolicy.ValueString(),
}, projectID)
if err != nil {
resp.Diagnostics.AddError("Failed to create instance template",
Expand All @@ -265,7 +255,6 @@ func (r *instanceTemplateResource) Create(ctx context.Context, req resource.Crea
plan.Image = types.StringValue(dataResp.ImageName)
plan.PlacementPolicy = types.StringValue(dataResp.PlacementPolicy)
plan.ReservationID = types.StringValue(dataResp.ReservationId)
plan.MaintenancePolicy = types.StringValue(dataResp.MaintenancePolicy)

disksToCreateResource := make([]diskToCreateResourceModel, 0, len(dataResp.Disks))
for _, diskToCreate := range disksToCreate {
Expand Down Expand Up @@ -333,7 +322,6 @@ func (r *instanceTemplateResource) Read(ctx context.Context, req resource.ReadRe
state.ProjectID = types.StringValue(instanceTemplate.ProjectId)
state.PublicIpAddressType = types.StringValue(instanceTemplate.PublicIpAddressType)
state.ID = types.StringValue(instanceTemplate.Id)
state.MaintenancePolicy = types.StringValue(instanceTemplate.MaintenancePolicy)

if instanceTemplate.IbPartitionId != "" {
state.IBPartition = types.StringValue(instanceTemplate.IbPartitionId)
Expand Down
1 change: 0 additions & 1 deletion internal/vm/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ func vmToTerraformResourceModel(instance *swagger.InstanceV1Alpha5, state *vmRes
networkInterfaces, _ := vmNetworkInterfacesToTerraformResourceModel(instance.NetworkInterfaces)
state.NetworkInterfaces = networkInterfaces
state.ReservationID = types.StringValue(instance.ReservationId)
state.MaintenancePolicy = types.StringValue(instance.MaintenancePolicy)

if len(instance.Disks) > 0 {
disks := make([]vmDiskResourceModel, 0, len(instance.Disks))
Expand Down
5 changes: 0 additions & 5 deletions internal/vm/vm_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type vmDataSourceFilter struct {
Type *string `tfsdk:"type"`
Disks []vmDiskResourceModel `tfsdk:"disks"`
NetworkInterfaces []vmNetworkInterfaceDataModel `tfsdk:"network_interfaces"`
MaintenancePolicy *string `tfsdk:"maintenance_policy"`
}

type vmNetworkInterfaceDataModel struct {
Expand Down Expand Up @@ -136,9 +135,6 @@ func (ds *vmDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, re
"reservation_id": schema.StringAttribute{
Optional: true,
},
"maintenance_policy": schema.StringAttribute{
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -186,7 +182,6 @@ func (ds *vmDataSource) Read(ctx context.Context, req datasource.ReadRequest, re
state.ProjectID = &vm.ProjectId
state.Name = &vm.Name
state.Type = &vm.Type_
state.MaintenancePolicy = &vm.MaintenancePolicy
attachedDisks := make([]vmDiskResourceModel, 0, len(vm.Disks))
for _, disk := range vm.Disks {
attachedDisks = append(attachedDisks, vmDiskResourceModel{
Expand Down
50 changes: 1 addition & 49 deletions internal/vm/vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -25,11 +23,6 @@ import (

var errProjectNotFound = errors.New("project for instance not found")

const (
unspecifiedPolicy = "unspecified"
stopVMPolicy = "stop-vm"
)

type vmResource struct {
client *swagger.APIClient
}
Expand All @@ -49,7 +42,6 @@ type vmResourceModel struct {
NetworkInterfaces types.List `tfsdk:"network_interfaces"`
HostChannelAdapters types.List `tfsdk:"host_channel_adapters"`
ReservationID types.String `tfsdk:"reservation_id"`
MaintenancePolicy types.String `tfsdk:"maintenance_policy"`
}

type vmNetworkInterfaceResourceModel struct {
Expand Down Expand Up @@ -250,16 +242,9 @@ func (r *vmResource) Schema(ctx context.Context, req resource.SchemaRequest, res
"reservation_id": schema.StringAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, // maintain across updates
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, // cannot be updated in place
Description: "ID of the reservation to which the VM belongs. If not provided or null, the lowest-cost reservation will be used by default. To opt out of using a reservation, set this to an empty string.",
},
"maintenance_policy": schema.StringAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, // maintain across updates
Default: stringdefault.StaticString(unspecifiedPolicy),
Validators: []validator.String{stringvalidator.OneOf(unspecifiedPolicy, stopVMPolicy)},
},
},
}
}
Expand Down Expand Up @@ -369,7 +354,6 @@ func (r *vmResource) Create(ctx context.Context, req resource.CreateRequest, res
Disks: diskIds,
HostChannelAdapters: hostChannelAdapters,
ReservationSpecification: reservationSpecification,
MaintenancePolicy: plan.MaintenancePolicy.ValueString(),
}, projectID)
if err != nil {
resp.Diagnostics.AddError("Failed to create instance",
Expand All @@ -392,7 +376,6 @@ func (r *vmResource) Create(ctx context.Context, req resource.CreateRequest, res
plan.ReservationID = types.StringValue(instance.ReservationId)
plan.FQDN = types.StringValue(fmt.Sprintf("%s.%s.compute.internal", instance.Name, instance.Location))
plan.ProjectID = types.StringValue(projectID)
plan.MaintenancePolicy = types.StringValue(instance.MaintenancePolicy)

networkInterfaces, networkDiags := vmNetworkInterfacesToTerraformResourceModel(instance.NetworkInterfaces)
resp.Diagnostics.Append(networkDiags...)
Expand Down Expand Up @@ -692,37 +675,6 @@ func (r *vmResource) Update(ctx context.Context, req resource.UpdateRequest, res

return
}

// handle updating maintenance policy
if !plan.MaintenancePolicy.IsUnknown() {
patchResp, httpResp, err := r.client.VMsApi.UpdateInstance(ctx,
swagger.InstancesPatchRequestV1Alpha5{
Action: "UPDATE",
MaintenancePolicy: plan.MaintenancePolicy.ValueString(),
},
plan.ProjectID.ValueString(),
plan.ID.ValueString(),
)
if err != nil {
resp.Diagnostics.AddError("Failed to update instance maintenance policy",
fmt.Sprintf("There was an error requesting to update the instance's maintenance policy: %v", err))

return
}
defer httpResp.Body.Close()

_, err = common.AwaitOperation(ctx, patchResp.Operation, state.ProjectID.ValueString(), r.client.VMOperationsApi.GetComputeVMsInstancesOperation)
if err != nil {
resp.Diagnostics.AddError("Failed to update instance maintenance policy",
fmt.Sprintf("There was an error updating the instance's maintenance policy: %s", common.UnpackAPIError(err)))

return
}

state.MaintenancePolicy = plan.MaintenancePolicy
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
}

//nolint:gocritic // Implements Terraform defined interface
Expand Down

0 comments on commit ed40f1f

Please sign in to comment.