Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lxd: Add storage option to new projects #14836

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/rest-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4641,6 +4641,9 @@ definitions:
example: foo
type: string
x-go-name: Name
storage:
type: string
x-go-name: StoragePool
type: object
x-go-package: github.com/canonical/lxd/shared/api
Resources:
Expand Down
9 changes: 6 additions & 3 deletions lxc/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ func (c *cmdProject) command() *cobra.Command {

// Create.
type cmdProjectCreate struct {
global *cmdGlobal
project *cmdProject
flagConfig []string
global *cmdGlobal
project *cmdProject
flagConfig []string
flagStorage string
}

func (c *cmdProjectCreate) command() *cobra.Command {
Expand All @@ -100,6 +101,7 @@ lxc project create p1 < config.yaml
Create a project with configuration from config.yaml`))

cmd.Flags().StringArrayVarP(&c.flagConfig, "config", "c", nil, i18n.G("Config key/value to apply to the new project")+"``")
cmd.Flags().StringVarP(&c.flagStorage, "storage", "s", "", i18n.G("Storage pool name to be used as the root device in the default profile")+"``")

cmd.RunE = c.run

Expand Down Expand Up @@ -152,6 +154,7 @@ func (c *cmdProjectCreate) run(cmd *cobra.Command, args []string) error {
project := api.ProjectsPost{}
project.Name = resource.name
project.ProjectPut = stdinData
project.StoragePool = c.flagStorage

if project.Config == nil {
project.Config = map[string]string{}
Expand Down
24 changes: 20 additions & 4 deletions lxd/api_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,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 @@ -357,18 +357,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 = "Default LXD profile for project " + 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 @@ -731,7 +747,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
Loading
Loading