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

Use generic helper functions #12320

Merged
merged 7 commits into from
Sep 27, 2023
Merged
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
2 changes: 1 addition & 1 deletion client/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func httpsLXD(ctx context.Context, requestURL string, args *ConnectionArgs) (Ins
eventListeners: make(map[string][]*EventListener),
}

if shared.StringInSlice(args.AuthType, []string{"candid", "oidc"}) {
if shared.ValueInSlice(args.AuthType, []string{"candid", "oidc"}) {
server.RequireAuthenticated(true)
}

Expand Down
2 changes: 1 addition & 1 deletion client/lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *ProtocolLXD) GetConnectionInfo() (*ConnectionInfo, error) {
}

url := fmt.Sprintf("https://%s", addr)
if !shared.StringInSlice(url, urls) {
if !shared.ValueInSlice(url, urls) {
urls = append(urls, url)
}
}
Expand Down
4 changes: 2 additions & 2 deletions client/lxd_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (r *ProtocolLXD) CopyContainer(source InstanceServer, container api.Contain
}
}

if shared.StringInSlice(args.Mode, []string{"push", "relay"}) {
if shared.ValueInSlice(args.Mode, []string{"push", "relay"}) {
if !r.HasExtension("container_push") {
return nil, fmt.Errorf("The target server is missing the required \"container_push\" API extension")
}
Expand Down Expand Up @@ -1002,7 +1002,7 @@ func (r *ProtocolLXD) CopyContainerSnapshot(source InstanceServer, containerName
// Process the copy arguments
if args != nil {
// Quick checks.
if shared.StringInSlice(args.Mode, []string{"push", "relay"}) {
if shared.ValueInSlice(args.Mode, []string{"push", "relay"}) {
if !r.HasExtension("container_push") {
return nil, fmt.Errorf("The target server is missing the required \"container_push\" API extension")
}
Expand Down
2 changes: 1 addition & 1 deletion client/lxd_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (r *ProtocolLXD) getEvents(allProjects bool) (*EventListener, error) {
for _, listener := range r.eventListeners[listener.projectName] {
listener.targetsLock.Lock()
for _, target := range listener.targets {
if target.types != nil && !shared.StringInSlice(event.Type, target.types) {
if target.types != nil && !shared.ValueInSlice(event.Type, target.types) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion client/lxd_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func lxdDownloadImage(fingerprint string, uri string, userAgent string, do func(
return nil, err
}

if !shared.StringInSlice(part.FormName(), []string{"rootfs", "rootfs.img"}) {
if !shared.ValueInSlice(part.FormName(), []string{"rootfs", "rootfs.img"}) {
return nil, fmt.Errorf("Invalid multipart image")
}

Expand Down
4 changes: 2 additions & 2 deletions client/lxd_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func (r *ProtocolLXD) CopyInstance(source InstanceServer, instance api.Instance,
}
}

if shared.StringInSlice(args.Mode, []string{"push", "relay"}) {
if shared.ValueInSlice(args.Mode, []string{"push", "relay"}) {
if !r.HasExtension("container_push") {
return nil, fmt.Errorf("The target server is missing the required \"container_push\" API extension")
}
Expand Down Expand Up @@ -1720,7 +1720,7 @@ func (r *ProtocolLXD) CopyInstanceSnapshot(source InstanceServer, instanceName s
// Process the copy arguments
if args != nil {
// Quick checks.
if shared.StringInSlice(args.Mode, []string{"push", "relay"}) {
if shared.ValueInSlice(args.Mode, []string{"push", "relay"}) {
if !r.HasExtension("container_push") {
return nil, fmt.Errorf("The target server is missing the required \"container_push\" API extension")
}
Expand Down
2 changes: 1 addition & 1 deletion client/lxd_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (r *ProtocolLXD) HasExtension(extension string) bool {
return true
}

return shared.StringInSlice(extension, r.server.APIExtensions)
return shared.ValueInSlice(extension, r.server.APIExtensions)
}

// CheckExtension checks if the server has the specified extension.
Expand Down
10 changes: 5 additions & 5 deletions lxc-to-lxd/main_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *cmdMigrate) RunE(cmd *cobra.Command, args []string) error {

// Retrieve LXC containers
for _, container := range liblxc.Containers(c.flagLXCPath) {
if !c.flagAll && !shared.StringInSlice(container.Name(), c.flagContainers) {
if !c.flagAll && !shared.ValueInSlice(container.Name(), c.flagContainers) {
continue
}

Expand Down Expand Up @@ -335,7 +335,7 @@ func convertContainer(d lxd.ContainerServer, container *liblxc.Container, storag
if value != nil {
for _, cap := range strings.Split(value[0], " ") {
// Ignore capabilities that are dropped in LXD containers by default.
if shared.StringInSlice(cap, []string{"mac_admin", "mac_override", "sys_module",
if shared.ValueInSlice(cap, []string{"mac_admin", "mac_override", "sys_module",
"sys_time"}) {
continue
}
Expand Down Expand Up @@ -570,20 +570,20 @@ func convertStorageConfig(conf []string, devices map[string]map[string]string) e
}

// Ignore mounts that are present in LXD containers by default.
if shared.StringInSlice(parts[0], []string{"proc", "sysfs"}) {
if shared.ValueInSlice(parts[0], []string{"proc", "sysfs"}) {
continue
}

device := make(map[string]string)
device["type"] = "disk"

// Deal with read-only mounts
if shared.StringInSlice("ro", strings.Split(parts[3], ",")) {
if shared.ValueInSlice("ro", strings.Split(parts[3], ",")) {
device["readonly"] = "true"
}

// Deal with optional mounts
if shared.StringInSlice("optional", strings.Split(parts[3], ",")) {
if shared.ValueInSlice("optional", strings.Split(parts[3], ",")) {
device["optional"] = "true"
} else {
if strings.HasPrefix(parts[0], "/") {
Expand Down
4 changes: 2 additions & 2 deletions lxc/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ func (c *cmdAction) Command(action string) *cobra.Command {
cmd.Flags().BoolVar(&c.flagStateless, "stateless", false, i18n.G("Ignore the instance state"))
}

if shared.StringInSlice(action, []string{"start", "restart", "stop"}) {
if shared.ValueInSlice(action, []string{"start", "restart", "stop"}) {
cmd.Flags().StringVar(&c.flagConsole, "console", "", i18n.G("Immediately attach to the console")+"``")
cmd.Flags().Lookup("console").NoOptDefVal = "console"
}

if shared.StringInSlice(action, []string{"restart", "stop"}) {
if shared.ValueInSlice(action, []string{"restart", "stop"}) {
cmd.Flags().BoolVarP(&c.flagForce, "force", "f", false, i18n.G("Force the instance to stop"))
cmd.Flags().IntVar(&c.flagTimeout, "timeout", -1, i18n.G("Time to wait for the instance to shutdown cleanly")+"``")
}
Expand Down
2 changes: 1 addition & 1 deletion lxc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ Are you really sure you want to force removing %s? (yes/no): `), name)
input, _ := reader.ReadString('\n')
input = strings.TrimSuffix(input, "\n")

if !shared.StringInSlice(strings.ToLower(input), []string{i18n.G("yes")}) {
if !shared.ValueInSlice(strings.ToLower(input), []string{i18n.G("yes")}) {
return fmt.Errorf(i18n.G("User aborted delete operation"))
}

Expand Down
4 changes: 2 additions & 2 deletions lxc/cluster_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func (c *cmdClusterGroupRemove) Run(cmd *cobra.Command, args []string) error {
return err
}

if !shared.StringInSlice(args[1], member.Groups) {
if !shared.ValueInSlice(args[1], member.Groups) {
return fmt.Errorf(i18n.G("Cluster group %s isn't currently applied to %s"), args[1], resource.name)
}

Expand Down Expand Up @@ -661,7 +661,7 @@ func (c *cmdClusterGroupAdd) Run(cmd *cobra.Command, args []string) error {
return err
}

if shared.StringInSlice(args[1], member.Groups) {
if shared.ValueInSlice(args[1], member.Groups) {
return fmt.Errorf(i18n.G("Cluster member %s is already in group %s"), resource.name, args[1])
}

Expand Down
6 changes: 3 additions & 3 deletions lxc/cluster_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *cmdClusterRoleAdd) Run(cmd *cobra.Command, args []string) error {
memberWritable := member.Writable()
newRoles := shared.SplitNTrimSpace(args[1], ",", -1, false)
for _, newRole := range newRoles {
if shared.StringInSlice(newRole, memberWritable.Roles) {
if shared.ValueInSlice(newRole, memberWritable.Roles) {
return fmt.Errorf(i18n.G("Member %q already has role %q"), resource.name, newRole)
}
}
Expand Down Expand Up @@ -138,12 +138,12 @@ func (c *cmdClusterRoleRemove) Run(cmd *cobra.Command, args []string) error {
memberWritable := member.Writable()
rolesToRemove := shared.SplitNTrimSpace(args[1], ",", -1, false)
for _, roleToRemove := range rolesToRemove {
if !shared.StringInSlice(roleToRemove, memberWritable.Roles) {
if !shared.ValueInSlice(roleToRemove, memberWritable.Roles) {
return fmt.Errorf(i18n.G("Member %q does not have role %q"), resource.name, roleToRemove)
}
}

memberWritable.Roles = shared.RemoveElementsFromStringSlice(memberWritable.Roles, rolesToRemove...)
memberWritable.Roles = shared.RemoveElementsFromSlice(memberWritable.Roles, rolesToRemove...)

return resource.server.UpdateClusterMember(resource.name, memberWritable, etag)
}
4 changes: 2 additions & 2 deletions lxc/config/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (c *Config) GetInstanceServer(name string) (lxd.InstanceServer, error) {
}

// HTTPs
if !shared.StringInSlice(remote.AuthType, []string{"candid", "oidc"}) && (args.TLSClientCert == "" || args.TLSClientKey == "") {
if !shared.ValueInSlice(remote.AuthType, []string{"candid", "oidc"}) && (args.TLSClientCert == "" || args.TLSClientKey == "") {
return nil, fmt.Errorf("Missing TLS client certificate and key")
}

Expand Down Expand Up @@ -295,7 +295,7 @@ func (c *Config) getConnectionArgs(name string) (*lxd.ConnectionArgs, error) {
}

// Stop here if no client certificate involved
if remote.Protocol == "simplestreams" || shared.StringInSlice(remote.AuthType, []string{"candid", "oidc"}) {
if remote.Protocol == "simplestreams" || shared.ValueInSlice(remote.AuthType, []string{"candid", "oidc"}) {
return &args, nil
}

Expand Down
2 changes: 1 addition & 1 deletion lxc/config_trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *cmdConfigTrustAdd) Run(cmd *cobra.Command, args []string) error {
}

// Validate flags.
if !shared.StringInSlice(c.flagType, []string{"client", "metrics"}) {
if !shared.ValueInSlice(c.flagType, []string{"client", "metrics"}) {
return fmt.Errorf(i18n.G("Unknown certificate type %q"), c.flagType)
}

Expand Down
2 changes: 1 addition & 1 deletion lxc/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *cmdConsole) Run(cmd *cobra.Command, args []string) error {
}

// Validate flags.
if !shared.StringInSlice(c.flagType, []string{"console", "vga"}) {
if !shared.ValueInSlice(c.flagType, []string{"console", "vga"}) {
return fmt.Errorf(i18n.G("Unknown output type %q"), c.flagType)
}

Expand Down
2 changes: 1 addition & 1 deletion lxc/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *cmdDelete) promptDelete(name string) error {
input, _ := reader.ReadString('\n')
input = strings.TrimSuffix(input, "\n")

if !shared.StringInSlice(strings.ToLower(input), []string{i18n.G("yes")}) {
if !shared.ValueInSlice(strings.ToLower(input), []string{i18n.G("yes")}) {
return fmt.Errorf(i18n.G("User aborted delete operation"))
}

Expand Down
4 changes: 2 additions & 2 deletions lxc/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ func (c *cmdList) IP4ColumnData(cInfo api.InstanceFull) string {
}

for _, addr := range net.Addresses {
if shared.StringInSlice(addr.Scope, []string{"link", "local"}) {
if shared.ValueInSlice(addr.Scope, []string{"link", "local"}) {
continue
}

Expand All @@ -791,7 +791,7 @@ func (c *cmdList) IP6ColumnData(cInfo api.InstanceFull) string {
}

for _, addr := range net.Addresses {
if shared.StringInSlice(addr.Scope, []string{"link", "local"}) {
if shared.ValueInSlice(addr.Scope, []string{"link", "local"}) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion lxc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (c *cmdGlobal) PreRun(cmd *cobra.Command, args []string) error {
flush = true
}

if !shared.StringInSlice(cmd.Name(), []string{"init", "launch"}) {
if !shared.ValueInSlice(cmd.Name(), []string{"init", "launch"}) {
fmt.Fprintf(os.Stderr, i18n.G(`To start your first container, try: lxc launch ubuntu:22.04
Or for a virtual machine: lxc launch ubuntu:22.04 --vm`)+"\n")
flush = true
Expand Down
2 changes: 1 addition & 1 deletion lxc/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c *cmdMonitor) Run(cmd *cobra.Command, args []string) error {
return err
}

if !shared.StringInSlice(c.flagFormat, []string{"json", "pretty", "yaml"}) {
if !shared.ValueInSlice(c.flagFormat, []string{"json", "pretty", "yaml"}) {
return fmt.Errorf(i18n.G("Invalid format: %s"), c.flagFormat)
}

Expand Down
2 changes: 1 addition & 1 deletion lxc/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ func (c *cmdNetworkList) Run(cmd *cobra.Command, args []string) error {

data := [][]string{}
for _, network := range networks {
if shared.StringInSlice(network.Type, []string{"loopback", "unknown"}) {
if shared.ValueInSlice(network.Type, []string{"loopback", "unknown"}) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion lxc/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ func (c *cmdProfileRemove) Run(cmd *cobra.Command, args []string) error {
return err
}

if !shared.StringInSlice(args[1], inst.Profiles) {
if !shared.ValueInSlice(args[1], inst.Profiles) {
return fmt.Errorf(i18n.G("Profile %s isn't currently applied to %s"), args[1], resource.name)
}

Expand Down
4 changes: 2 additions & 2 deletions lxc/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,15 +839,15 @@ func (c *cmdProjectInfo) Run(cmd *cobra.Command, args []string) error {
for k, v := range projectState.Resources {
limit := i18n.G("UNLIMITED")
if v.Limit >= 0 {
if shared.StringInSlice(k, byteLimits) {
if shared.ValueInSlice(k, byteLimits) {
limit = units.GetByteSizeStringIEC(v.Limit, 2)
} else {
limit = fmt.Sprintf("%d", v.Limit)
}
}

usage := ""
if shared.StringInSlice(k, byteLimits) {
if shared.ValueInSlice(k, byteLimits) {
usage = units.GetByteSizeStringIEC(v.Usage, 2)
} else {
usage = fmt.Sprintf("%d", v.Usage)
Expand Down
2 changes: 1 addition & 1 deletion lxc/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *cmdQuery) Run(cmd *cobra.Command, args []string) error {
return fmt.Errorf(i18n.G("--project cannot be used with the query command"))
}

if !shared.StringInSlice(c.flagAction, []string{"GET", "PUT", "POST", "PATCH", "DELETE"}) {
if !shared.ValueInSlice(c.flagAction, []string{"GET", "PUT", "POST", "PATCH", "DELETE"}) {
return fmt.Errorf(i18n.G("Action %q isn't supported by this tool"), c.flagAction)
}

Expand Down
10 changes: 5 additions & 5 deletions lxc/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (c *cmdRemoteAdd) findProject(d lxd.InstanceServer, project string) (string
}

// Deal with multiple projects.
if shared.StringInSlice("default", names) {
if shared.ValueInSlice("default", names) {
// If we have access to the default project, use it.
return "", nil
}
Expand Down Expand Up @@ -509,15 +509,15 @@ func (c *cmdRemoteAdd) Run(cmd *cobra.Command, args []string) error {

// If not specified, the preferred order of authentication is 1) OIDC 2) Candid 3) TLS.
if c.flagAuthType == "" {
if !srv.Public && shared.StringInSlice("oidc", srv.AuthMethods) {
if !srv.Public && shared.ValueInSlice("oidc", srv.AuthMethods) {
c.flagAuthType = "oidc"
} else if !srv.Public && shared.StringInSlice("candid", srv.AuthMethods) {
} else if !srv.Public && shared.ValueInSlice("candid", srv.AuthMethods) {
c.flagAuthType = "candid"
} else {
c.flagAuthType = "tls"
}

if shared.StringInSlice(c.flagAuthType, []string{"oidc", "candid"}) {
if shared.ValueInSlice(c.flagAuthType, []string{"oidc", "candid"}) {
// Update the remote configuration
remote := conf.Remotes[server]
remote.AuthType = c.flagAuthType
Expand All @@ -543,7 +543,7 @@ func (c *cmdRemoteAdd) Run(cmd *cobra.Command, args []string) error {
}
}

if !srv.Public && !shared.StringInSlice(c.flagAuthType, srv.AuthMethods) {
if !srv.Public && !shared.ValueInSlice(c.flagAuthType, srv.AuthMethods) {
return fmt.Errorf(i18n.G("Authentication type '%s' not supported by server"), c.flagAuthType)
}

Expand Down
4 changes: 2 additions & 2 deletions lxc/storage_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (c *cmdStorageVolume) parseVolume(defaultType string, name string) (string,
fields := strings.SplitN(name, "/", 2)
if len(fields) == 1 {
return fields[0], defaultType
} else if len(fields) == 2 && !shared.StringInSlice(fields[0], []string{"custom", "image", "container", "virtual-machine"}) {
} else if len(fields) == 2 && !shared.ValueInSlice(fields[0], []string{"custom", "image", "container", "virtual-machine"}) {
return name, defaultType
}

Expand Down Expand Up @@ -2437,7 +2437,7 @@ func (c *cmdStorageVolumeImport) Run(cmd *cobra.Command, args []string) error {
}
} else {
// Validate type flag
if !shared.StringInSlice(c.flagType, []string{"backup", "iso"}) {
if !shared.ValueInSlice(c.flagType, []string{"backup", "iso"}) {
return fmt.Errorf("Import type needs to be \"backup\" or \"iso\"")
}
}
Expand Down
4 changes: 2 additions & 2 deletions lxd-agent/main_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *cmdAgent) Run(cmd *cobra.Command, args []string) error {
}

// Sync the hostname.
if shared.PathExists("/proc/sys/kernel/hostname") && shared.StringInSlice("/etc/hostname", files) {
if shared.PathExists("/proc/sys/kernel/hostname") && shared.ValueInSlice("/etc/hostname", files) {
// Open the two files.
src, err := os.Open("/etc/hostname")
if err != nil {
Expand All @@ -90,7 +90,7 @@ func (c *cmdAgent) Run(cmd *cobra.Command, args []string) error {
}

// Run cloud-init.
if shared.PathExists("/etc/cloud") && shared.StringInSlice("/var/lib/cloud/seed/nocloud-net/meta-data", files) {
if shared.PathExists("/etc/cloud") && shared.ValueInSlice("/var/lib/cloud/seed/nocloud-net/meta-data", files) {
logger.Info("Seeding cloud-init")

cloudInitPath := "/run/cloud-init"
Expand Down
2 changes: 1 addition & 1 deletion lxd-agent/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func getFilesystemMetrics(d *Daemon) (map[string]metrics.FilesystemMetrics, erro
}

// Skip uninteresting mounts
if shared.StringInSlice(fields[2], defFSTypesExcluded) || defMountPointsExcluded.MatchString(fields[1]) {
if shared.ValueInSlice(fields[2], defFSTypesExcluded) || defMountPointsExcluded.MatchString(fields[1]) {
continue
}

Expand Down
Loading