Skip to content

Commit

Permalink
lxc: allow creation of various resources from YAML (from Incus) (#13995)
Browse files Browse the repository at this point in the history
cherry-picked from: lxc/incus#786
cherry-picked from: lxc/incus#782
cherry-picked from: lxc/incus#778
cherry-picked from: lxc/incus#775
cherry-picked from: lxc/incus#767
cherry-picked from: lxc/incus#863
cherry-picked from: lxc/incus#864
cherry-picked from: lxc/incus#891
  • Loading branch information
tomponline authored Sep 3, 2024
2 parents 481ebca + 4edd778 commit 495e793
Show file tree
Hide file tree
Showing 52 changed files with 17,736 additions and 15,410 deletions.
40 changes: 31 additions & 9 deletions lxc/cluster_group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -113,7 +114,7 @@ func (c *cmdClusterGroupAssign) run(cmd *cobra.Command, args []string) error {

// Assign the cluster group
if resource.name == "" {
return fmt.Errorf(i18n.G("Missing cluster member name"))
return errors.New(i18n.G("Missing cluster member name"))
}

member, etag, err := resource.server.GetClusterMember(resource.name)
Expand Down Expand Up @@ -157,19 +158,39 @@ func (c *cmdClusterGroupCreate) command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Create a cluster group`))

cmd.Example = cli.FormatSection("", i18n.G(`lxc cluster group create g1
lxc cluster group create g1 < config.yaml
Create a cluster group with configuration from config.yaml`))

cmd.RunE = c.run

return cmd
}

// It creates new cluster group after performing checks, parsing arguments, and making the server call for creation.
func (c *cmdClusterGroupCreate) run(cmd *cobra.Command, args []string) error {
var stdinData api.ClusterGroupPut

// 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 @@ -179,12 +200,13 @@ func (c *cmdClusterGroupCreate) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing cluster group name"))
return errors.New(i18n.G("Missing cluster group name"))
}

// Create the cluster group
group := api.ClusterGroupsPost{
Name: resource.name,
Name: resource.name,
ClusterGroupPut: stdinData,
}

err = resource.server.CreateClusterGroup(group)
Expand Down Expand Up @@ -236,7 +258,7 @@ func (c *cmdClusterGroupDelete) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing cluster group name"))
return errors.New(i18n.G("Missing cluster group name"))
}

// Delete the cluster group
Expand Down Expand Up @@ -288,7 +310,7 @@ func (c *cmdClusterGroupEdit) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing cluster group name"))
return errors.New(i18n.G("Missing cluster group name"))
}

// If stdin isn't a terminal, read text from it
Expand Down Expand Up @@ -416,7 +438,7 @@ func (c *cmdClusterGroupList) run(cmd *cobra.Command, args []string) error {
}

if !cluster.Enabled {
return fmt.Errorf(i18n.G("LXD server isn't part of a cluster"))
return errors.New(i18n.G("LXD server isn't part of a cluster"))
}

groups, err := resource.server.GetClusterGroups()
Expand Down Expand Up @@ -478,7 +500,7 @@ func (c *cmdClusterGroupRemove) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing cluster member name"))
return errors.New(i18n.G("Missing cluster member name"))
}

// Remove the cluster group
Expand Down Expand Up @@ -599,7 +621,7 @@ func (c *cmdClusterGroupShow) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing cluster group name"))
return errors.New(i18n.G("Missing cluster group name"))
}

// Show the cluster group
Expand Down Expand Up @@ -652,7 +674,7 @@ func (c *cmdClusterGroupAdd) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing cluster member name"))
return errors.New(i18n.G("Missing cluster member name"))
}

// Retrieve cluster member information.
Expand Down
35 changes: 20 additions & 15 deletions lxc/network_acl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -121,7 +122,7 @@ func (c *cmdNetworkACLList) run(cmd *cobra.Command, args []string) error {

// List the networks.
if resource.name != "" {
return fmt.Errorf(i18n.G("Filtering isn't supported yet"))
return errors.New(i18n.G("Filtering isn't supported yet"))
}

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

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

// Show the network ACL config.
Expand Down Expand Up @@ -236,7 +237,7 @@ func (c *cmdNetworkACLShowLog) run(cmd *cobra.Command, args []string) error {

resource := resources[0]
if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

// Get the ACL log.
Expand Down Expand Up @@ -287,7 +288,7 @@ func (c *cmdNetworkACLGet) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

resp, _, err := resource.server.GetNetworkACL(resource.name)
Expand Down Expand Up @@ -325,6 +326,10 @@ func (c *cmdNetworkACLCreate) command() *cobra.Command {
cmd.Use = usage("create", i18n.G("[<remote>:]<ACL> [key=value...]"))
cmd.Short = i18n.G("Create new network ACLs")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Create new network ACLs"))
cmd.Example = cli.FormatSection("", i18n.G(`lxc network acl create a1
lxc network acl create a1 < config.yaml
Create network acl with configuration from config.yaml`))

cmd.RunE = c.run

Expand All @@ -347,7 +352,7 @@ func (c *cmdNetworkACLCreate) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

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

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

// Get the network ACL.
Expand Down Expand Up @@ -569,7 +574,7 @@ func (c *cmdNetworkACLEdit) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

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

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

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

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

// Delete the network ACL.
Expand Down Expand Up @@ -841,7 +846,7 @@ func (c *cmdNetworkACLRule) runAdd(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

// Get config keys from arguments.
Expand Down Expand Up @@ -874,7 +879,7 @@ func (c *cmdNetworkACLRule) runAdd(cmd *cobra.Command, args []string) error {
} else if args[1] == "egress" {
netACL.Egress = append(netACL.Egress, *rule)
} else {
return fmt.Errorf(i18n.G("The direction argument must be one of: ingress, egress"))
return errors.New(i18n.G("The direction argument must be one of: ingress, egress"))
}

return resource.server.UpdateNetworkACL(resource.name, netACL.Writable(), etag)
Expand Down Expand Up @@ -908,7 +913,7 @@ func (c *cmdNetworkACLRule) runRemove(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing network ACL name"))
return errors.New(i18n.G("Missing network ACL name"))
}

// Get config filters from arguments.
Expand Down Expand Up @@ -963,7 +968,7 @@ func (c *cmdNetworkACLRule) runRemove(cmd *cobra.Command, args []string) error {
for _, r := range rules {
if isFilterMatch(&r, filters) {
if removed && !c.flagRemoveForce {
return nil, fmt.Errorf(i18n.G("Multiple rules match. Use --force to remove them all"))
return nil, errors.New(i18n.G("Multiple rules match. Use --force to remove them all"))
}

removed = true
Expand All @@ -974,7 +979,7 @@ func (c *cmdNetworkACLRule) runRemove(cmd *cobra.Command, args []string) error {
}

if !removed {
return nil, fmt.Errorf(i18n.G("No matching rule(s) found"))
return nil, errors.New(i18n.G("No matching rule(s) found"))
}

return newRules, nil
Expand All @@ -996,7 +1001,7 @@ func (c *cmdNetworkACLRule) runRemove(cmd *cobra.Command, args []string) error {

netACL.Egress = rules
} else {
return fmt.Errorf(i18n.G("The direction argument must be one of: ingress, egress"))
return errors.New(i18n.G("The direction argument must be one of: ingress, egress"))
}

return resource.server.UpdateNetworkACL(resource.name, netACL.Writable(), etag)
Expand Down
Loading

0 comments on commit 495e793

Please sign in to comment.