Skip to content

Commit

Permalink
lxd: Adds/removes/renames instances in authorizer.
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Laing <mark.laing@canonical.com>
  • Loading branch information
markylaing committed Sep 15, 2023
1 parent f53700c commit 4a486fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lxd/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ func lxcCreate(s *state.State, args db.InstanceArgs, p api.Project) (instance.In
if d.isSnapshot {
d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceSnapshotCreated.Event(d, nil))
} else {
// Add instance to authorizer.
err = d.state.Authorizer.AddInstance(d.project.Name, d.Name())
if err != nil {
logger.Error("Failed to add instance to authorizer", logger.Ctx{"instanceName": d.Name(), "projectName": d.project.Name, "error": err})
}

d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceCreated.Event(d, map[string]any{
"type": api.InstanceTypeContainer,
"storage-pool": d.storagePool.Name(),
Expand Down Expand Up @@ -3811,6 +3817,11 @@ func (d *lxc) delete(force bool) error {
if d.isSnapshot {
d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceSnapshotDeleted.Event(d, nil))
} else {
err = d.state.Authorizer.DeleteInstance(d.project.Name, d.Name())
if err != nil {
logger.Error("Failed to remove instance from authorizer", logger.Ctx{"name": d.Name(), "project": d.project.Name, "error": err})
}

d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceDeleted.Event(d, nil))
}

Expand Down Expand Up @@ -3981,6 +3992,11 @@ func (d *lxc) Rename(newName string, applyTemplateTrigger bool) error {
if d.isSnapshot {
d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceSnapshotRenamed.Event(d, map[string]any{"old_name": oldName}))
} else {
err = d.state.Authorizer.RenameInstance(d.project.Name, oldName, newName)
if err != nil {
logger.Error("Failed to rename instance in authorizer", logger.Ctx{"old_name": oldName, "new_name": newName, "project": d.project.Name, "error": err})
}

d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceRenamed.Event(d, map[string]any{"old_name": oldName}))
}

Expand Down
15 changes: 15 additions & 0 deletions lxd/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ func qemuCreate(s *state.State, args db.InstanceArgs, p api.Project) (instance.I
if d.isSnapshot {
d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceSnapshotCreated.Event(d, nil))
} else {
err = d.state.Authorizer.AddInstance(d.project.Name, d.Name())
if err != nil {
logger.Error("Failed to add instance to authorizer", logger.Ctx{"name": d.Name(), "project": d.project.Name, "error": err})
}

d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceCreated.Event(d, map[string]any{
"type": api.InstanceTypeVM,
"storage-pool": d.storagePool.Name(),
Expand Down Expand Up @@ -4874,6 +4879,11 @@ func (d *qemu) Rename(newName string, applyTemplateTrigger bool) error {
if d.isSnapshot {
d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceSnapshotRenamed.Event(d, map[string]any{"old_name": oldName}))
} else {
err = d.state.Authorizer.RenameInstance(d.project.Name, oldName, newName)
if err != nil {
logger.Error("Failed to rename instance in authorizer", logger.Ctx{"old_name": oldName, "new_name": newName, "project": d.project.Name, "error": err})
}

d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceRenamed.Event(d, map[string]any{"old_name": oldName}))
}

Expand Down Expand Up @@ -5673,6 +5683,11 @@ func (d *qemu) delete(force bool) error {
if d.isSnapshot {
d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceSnapshotDeleted.Event(d, nil))
} else {
err = d.state.Authorizer.DeleteInstance(d.project.Name, d.Name())
if err != nil {
logger.Error("Failed to remove instance from authorizer", logger.Ctx{"name": d.Name(), "project": d.project.Name, "error": err})
}

d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceDeleted.Event(d, nil))
}

Expand Down

0 comments on commit 4a486fc

Please sign in to comment.