Skip to content

Commit

Permalink
lxc: Add support for creating project from yaml (from Incus) (#13907)
Browse files Browse the repository at this point in the history
Cherry-picked from lxc/incus#765
  • Loading branch information
tomponline authored Aug 23, 2024
2 parents 59ad83c + 13a5bea commit ec9c260
Show file tree
Hide file tree
Showing 43 changed files with 2,821 additions and 2,446 deletions.
53 changes: 38 additions & 15 deletions lxc/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func (c *cmdProjectCreate) command() *cobra.Command {
cmd.Short = i18n.G("Create projects")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Create projects`))
cmd.Example = cli.FormatSection("", i18n.G(`lxc project create p1
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.RunE = c.run
Expand All @@ -100,12 +105,27 @@ func (c *cmdProjectCreate) command() *cobra.Command {
}

func (c *cmdProjectCreate) run(cmd *cobra.Command, args []string) error {
var stdinData api.ProjectPut

// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 1, 1)
if exit {
return err
}

// If stdin isn't a terminal, read text from it
if !termios.IsTerminal(getStdinFd()) {
contents, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}

err = yaml.Unmarshal(contents, &stdinData)
if err != nil {
return err
}
}

// Parse remote
resources, err := c.global.ParseServers(args[0])
if err != nil {
Expand All @@ -115,21 +135,24 @@ func (c *cmdProjectCreate) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing project name"))
return fmt.Errorf("%s", i18n.G("Missing project name"))
}

// Create the project
project := api.ProjectsPost{}
project.Name = resource.name
project.ProjectPut = stdinData

if project.Config == nil {
project.Config = map[string]string{}
for _, entry := range c.flagConfig {
key, value, found := strings.Cut(entry, "=")
if !found {
return fmt.Errorf(i18n.G("Bad key=value pair: %q"), entry)
}

project.Config = map[string]string{}
for _, entry := range c.flagConfig {
key, value, found := strings.Cut(entry, "=")
if !found {
return fmt.Errorf(i18n.G("Bad key=value pair: %q"), entry)
project.Config[key] = value
}

project.Config[key] = value
}

err = resource.server.CreateProject(project)
Expand Down Expand Up @@ -184,7 +207,7 @@ func (c *cmdProjectDelete) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing project name"))
return fmt.Errorf("%s", i18n.G("Missing project name"))
}

// Delete the project
Expand Down Expand Up @@ -266,7 +289,7 @@ func (c *cmdProjectEdit) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing project name"))
return fmt.Errorf("%s", i18n.G("Missing project name"))
}

// If stdin isn't a terminal, read text from it
Expand Down Expand Up @@ -370,7 +393,7 @@ func (c *cmdProjectGet) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing project name"))
return fmt.Errorf("%s", i18n.G("Missing project name"))
}

// Get the configuration key
Expand Down Expand Up @@ -544,7 +567,7 @@ func (c *cmdProjectRename) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing project name"))
return fmt.Errorf("%s", i18n.G("Missing project name"))
}

// Rename the project
Expand Down Expand Up @@ -604,7 +627,7 @@ func (c *cmdProjectSet) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing project name"))
return fmt.Errorf("%s", i18n.G("Missing project name"))
}

// Get the project
Expand Down Expand Up @@ -711,7 +734,7 @@ func (c *cmdProjectShow) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing project name"))
return fmt.Errorf("%s", i18n.G("Missing project name"))
}

// Show the project
Expand Down Expand Up @@ -824,7 +847,7 @@ func (c *cmdProjectInfo) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing project name"))
return fmt.Errorf("%s", i18n.G("Missing project name"))
}

// Get the current allocations
Expand Down
Loading

0 comments on commit ec9c260

Please sign in to comment.