Skip to content

Commit

Permalink
instance: Allow instance rename
Browse files Browse the repository at this point in the history
Signed-off-by: Din Music <[email protected]>
  • Loading branch information
MusicDin committed Sep 20, 2024
1 parent e14c31b commit 2713cd0
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions internal/instance/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ func (r InstanceResource) Schema(ctx context.Context, _ resource.SchemaRequest,
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},

"description": schema.StringAttribute{
Expand Down Expand Up @@ -789,13 +786,25 @@ func (r InstanceResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

instanceName := plan.Name.ValueString()
instanceName := state.Name.ValueString()
instanceState, _, err := server.GetInstanceState(instanceName)
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("Failed to retrieve state of instance %q", instanceName), err.Error())
return
}

// Handle instance rename if instance is already stopped.
newInstanceName := plan.Name.ValueString()
if instanceName != newInstanceName && isInstanceStopped(*instanceState) {
err := renameInstance(ctx, server, instanceName, newInstanceName)
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("Failed to rename instance %q", instanceName), err.Error())
return
}

instanceName = newInstanceName
}

// Indicates if the instance has been just started.
instanceStarted := false
instanceRunning := isInstanceOperational(*instanceState)
Expand Down Expand Up @@ -832,6 +841,17 @@ func (r InstanceResource) Update(ctx context.Context, req resource.UpdateRequest
}
}

// Handle instance rename.
if instanceName != newInstanceName {
err := renameInstance(ctx, server, instanceName, newInstanceName)
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("Failed to rename instance %q", instanceName), err.Error())
return
}

instanceName = newInstanceName
}

// Get instance.
instance, etag, err := server.GetInstance(instanceName)
if err != nil {
Expand Down Expand Up @@ -1307,6 +1327,17 @@ func stopInstance(ctx context.Context, server lxd.InstanceServer, instanceName s
return true, nil
}

// renameInstance renames an instance with the given old name to a new name.
// Instance has to be stopped beforehand, otherwise the operation will fail.
func renameInstance(ctx context.Context, server lxd.InstanceServer, oldName string, newName string) error {
op, err := server.RenameInstance(oldName, api.InstancePost{Name: newName})
if err != nil {
return err
}

return op.WaitContext(ctx)
}

// waitInstanceNetwork waits for an instance with the given name to receive
// an IPv4 address on any interface (excluding loopback). This should be
// called only if the instance is running.
Expand Down

0 comments on commit 2713cd0

Please sign in to comment.