Skip to content

Commit

Permalink
lxd: Utilize new --storage flag to enable setting storage on project …
Browse files Browse the repository at this point in the history
…create

Signed-off-by: Mark Bolton <[email protected]>
  • Loading branch information
boltmark committed Feb 2, 2025
1 parent c9d62f5 commit bbd39cf
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lxd/api_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func projectsPost(d *Daemon, r *http.Request) response.Response {
}

if shared.IsTrue(project.Config["features.profiles"]) {
err = projectCreateDefaultProfile(tx, project.Name)
err = projectCreateDefaultProfile(tx, project.Name, project.StoragePool)
if err != nil {
return err
}
Expand All @@ -339,18 +339,34 @@ func projectsPost(d *Daemon, r *http.Request) response.Response {
}

// Create the default profile of a project.
func projectCreateDefaultProfile(tx *db.ClusterTx, project string) error {
func projectCreateDefaultProfile(tx *db.ClusterTx, project, storagePool string) error {
// Create a default profile
profile := cluster.Profile{}
profile.Project = project
profile.Name = api.ProjectDefaultName
profile.Description = fmt.Sprintf("Default LXD profile for project %s", project)

_, err := cluster.CreateProfile(context.TODO(), tx.Tx(), profile)
profileID, err := cluster.CreateProfile(context.TODO(), tx.Tx(), profile)
if err != nil {
return fmt.Errorf("Add default profile to database: %w", err)
}

if storagePool != "" {
rootDev := map[string]string{}
rootDev["path"] = "/"
rootDev["pool"] = storagePool
device := cluster.Device{
Name: "root",
Type: cluster.TypeDisk,
Config: rootDev,
}

err = cluster.CreateProfileDevices(context.TODO(), tx.Tx(), profileID, map[string]cluster.Device{"root": device})
if err != nil {
return fmt.Errorf("Add root device to default profile of new project: %w", err)
}
}

return nil
}

Expand Down Expand Up @@ -701,7 +717,7 @@ func projectChange(s *state.State, project *api.Project, req api.ProjectPut) res

if shared.ValueInSlice("features.profiles", configChanged) {
if shared.IsTrue(req.Config["features.profiles"]) {
err = projectCreateDefaultProfile(tx, project.Name)
err = projectCreateDefaultProfile(tx, project.Name, "")
if err != nil {
return err
}
Expand Down

0 comments on commit bbd39cf

Please sign in to comment.