Skip to content

Commit

Permalink
Introduce source type constants (#14587)
Browse files Browse the repository at this point in the history
Replace hardcoded strings for container/instance source type.

Also fixed some issue reported by static analysis.
  • Loading branch information
tomponline authored Dec 6, 2024
2 parents ac7aa74 + f33ea82 commit 81495c0
Show file tree
Hide file tree
Showing 59 changed files with 1,807 additions and 1,757 deletions.
8 changes: 4 additions & 4 deletions client/lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func lxdParseResponse(resp *http.Response) (*api.Response, string, error) {

// Handle errors
if response.Type == api.ErrorResponse {
return nil, "", api.StatusErrorf(resp.StatusCode, response.Error)
return nil, "", api.StatusErrorf(resp.StatusCode, "%s", response.Error)
}

return &response, etag, nil
Expand Down Expand Up @@ -354,7 +354,7 @@ func (r *ProtocolLXD) queryStruct(method string, path string, data any, ETag str

// Log the data
logger.Debugf("Got response struct from LXD")
logger.Debugf(logger.Pretty(target))
logger.Debug(logger.Pretty(target))

return etag, nil
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func (r *ProtocolLXD) queryOperation(method string, path string, data any, ETag

// Log the data
logger.Debugf("Got operation from LXD")
logger.Debugf(logger.Pretty(op.Operation))
logger.Debug(logger.Pretty(op.Operation))

return &op, etag, nil
}
Expand Down Expand Up @@ -492,7 +492,7 @@ func (r *ProtocolLXD) getUnderlyingHTTPTransport() (*http.Transport, error) {
// is also updated with the minimal source fields.
func (r *ProtocolLXD) getSourceImageConnectionInfo(source ImageServer, image api.Image, instSrc *api.InstanceSource) (info *ConnectionInfo, err error) {
// Set the minimal source fields
instSrc.Type = "image"
instSrc.Type = api.SourceTypeImage

// Optimization for the local image case
if r.isSameServer(source) {
Expand Down
64 changes: 43 additions & 21 deletions client/lxd_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (r *ProtocolLXD) tryCreateContainer(req api.ContainersPost, urls []string)
// CreateContainerFromImage is a convenience function to make it easier to create a container from an existing image.
func (r *ProtocolLXD) CreateContainerFromImage(source ImageServer, image api.Image, req api.ContainersPost) (RemoteOperation, error) {
// Set the minimal source fields
req.Source.Type = "image"
req.Source.Type = api.SourceTypeImage

// Optimization for the local image case
if r.isSameServer(source) {
Expand Down Expand Up @@ -372,7 +372,7 @@ func (r *ProtocolLXD) CopyContainer(source InstanceServer, container api.Contain
}

// Local copy source fields
req.Source.Type = "copy"
req.Source.Type = api.SourceTypeCopy
req.Source.Source = container.Name

// Copy the container
Expand Down Expand Up @@ -411,7 +411,7 @@ func (r *ProtocolLXD) CopyContainer(source InstanceServer, container api.Contain
}

// Create the container
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"
req.Source.Refresh = args.Refresh

Expand All @@ -424,7 +424,10 @@ func (r *ProtocolLXD) CopyContainer(source InstanceServer, container api.Contain

targetSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
targetSecrets[k] = v.(string)
value, ok := v.(string)
if ok {
targetSecrets[k] = value
}
}

// Prepare the source request
Expand Down Expand Up @@ -452,13 +455,16 @@ func (r *ProtocolLXD) CopyContainer(source InstanceServer, container api.Contain

sourceSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
sourceSecrets[k] = v.(string)
value, ok := v.(string)
if ok {
sourceSecrets[k] = value
}
}

// Relay mode migration
if args != nil && args.Mode == "relay" {
// Push copy source fields
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"

// Start the process
Expand All @@ -472,7 +478,10 @@ func (r *ProtocolLXD) CopyContainer(source InstanceServer, container api.Contain
// Extract the websockets
targetSecrets := map[string]string{}
for k, v := range targetOpAPI.Metadata {
targetSecrets[k] = v.(string)
value, ok := v.(string)
if ok {
targetSecrets[k] = value
}
}

// Launch the relay
Expand All @@ -497,7 +506,7 @@ func (r *ProtocolLXD) CopyContainer(source InstanceServer, container api.Contain
}

// Pull mode migration
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "pull"
req.Source.Operation = opAPI.ID
req.Source.Websockets = sourceSecrets
Expand Down Expand Up @@ -651,11 +660,13 @@ func (r *ProtocolLXD) ExecContainer(containerName string, exec api.ContainerExec
// Parse the fds
fds := map[string]string{}

value, ok := opAPI.Metadata["fds"]
values, ok := opAPI.Metadata["fds"].(map[string]any)
if ok {
values := value.(map[string]any)
for k, v := range values {
fds[k] = v.(string)
fd, ok := v.(string)
if ok {
fds[k] = fd
}
}
}

Expand Down Expand Up @@ -1075,7 +1086,7 @@ func (r *ProtocolLXD) CopyContainerSnapshot(source InstanceServer, containerName
}

// Local copy source fields
req.Source.Type = "copy"
req.Source.Type = api.SourceTypeCopy
req.Source.Source = fmt.Sprintf("%s/%s", cName, sName)

// Copy the container
Expand Down Expand Up @@ -1117,7 +1128,7 @@ func (r *ProtocolLXD) CopyContainerSnapshot(source InstanceServer, containerName
}

// Create the container
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"

op, err := r.CreateContainer(req)
Expand All @@ -1129,7 +1140,10 @@ func (r *ProtocolLXD) CopyContainerSnapshot(source InstanceServer, containerName

targetSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
targetSecrets[k] = v.(string)
value, ok := v.(string)
if ok {
targetSecrets[k] = value
}
}

// Prepare the source request
Expand Down Expand Up @@ -1157,13 +1171,16 @@ func (r *ProtocolLXD) CopyContainerSnapshot(source InstanceServer, containerName

sourceSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
sourceSecrets[k] = v.(string)
value, ok := v.(string)
if ok {
sourceSecrets[k] = value
}
}

// Relay mode migration
if args != nil && args.Mode == "relay" {
// Push copy source fields
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"

// Start the process
Expand All @@ -1177,7 +1194,10 @@ func (r *ProtocolLXD) CopyContainerSnapshot(source InstanceServer, containerName
// Extract the websockets
targetSecrets := map[string]string{}
for k, v := range targetOpAPI.Metadata {
targetSecrets[k] = v.(string)
value, ok := v.(string)
if ok {
targetSecrets[k] = value
}
}

// Launch the relay
Expand All @@ -1202,7 +1222,7 @@ func (r *ProtocolLXD) CopyContainerSnapshot(source InstanceServer, containerName
}

// Pull mode migration
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "pull"
req.Source.Operation = opAPI.ID
req.Source.Websockets = sourceSecrets
Expand Down Expand Up @@ -1574,11 +1594,13 @@ func (r *ProtocolLXD) ConsoleContainer(containerName string, console api.Contain
// Parse the fds
fds := map[string]string{}

value, ok := opAPI.Metadata["fds"]
values, ok := opAPI.Metadata["fds"].(map[string]any)
if ok {
values := value.(map[string]any)
for k, v := range values {
fds[k] = v.(string)
fd, ok := v.(string)
if ok {
fds[k] = fd
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/lxd_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ func (r *ProtocolLXD) CopyImage(source ImageServer, image api.Image, args *Image
},
Fingerprint: image.Fingerprint,
Mode: "pull",
Type: "image",
Type: api.SourceTypeImage,
Project: info.Project,
},
ImagePut: api.ImagePut{
Expand Down
16 changes: 8 additions & 8 deletions client/lxd_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ func (r *ProtocolLXD) CopyInstance(source InstanceServer, instance api.Instance,
}

// Local copy source fields
req.Source.Type = "copy"
req.Source.Type = api.SourceTypeCopy
req.Source.Source = instance.Name

// Copy the instance
Expand Down Expand Up @@ -965,7 +965,7 @@ func (r *ProtocolLXD) CopyInstance(source InstanceServer, instance api.Instance,
}

// Create the instance
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"
req.Source.Refresh = args.Refresh

Expand Down Expand Up @@ -1022,7 +1022,7 @@ func (r *ProtocolLXD) CopyInstance(source InstanceServer, instance api.Instance,
// Relay mode migration
if args != nil && args.Mode == "relay" {
// Push copy source fields
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"

// Start the process
Expand Down Expand Up @@ -1066,7 +1066,7 @@ func (r *ProtocolLXD) CopyInstance(source InstanceServer, instance api.Instance,
}

// Pull mode migration
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "pull"
req.Source.Operation = opAPI.ID
req.Source.Websockets = sourceSecrets
Expand Down Expand Up @@ -2008,7 +2008,7 @@ func (r *ProtocolLXD) CopyInstanceSnapshot(source InstanceServer, instanceName s
}

// Local copy source fields
req.Source.Type = "copy"
req.Source.Type = api.SourceTypeCopy
req.Source.Source = fmt.Sprintf("%s/%s", cName, sName)

// Copy the instance
Expand Down Expand Up @@ -2060,7 +2060,7 @@ func (r *ProtocolLXD) CopyInstanceSnapshot(source InstanceServer, instanceName s
}

// Create the instance
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"

op, err := r.CreateInstance(req)
Expand Down Expand Up @@ -2116,7 +2116,7 @@ func (r *ProtocolLXD) CopyInstanceSnapshot(source InstanceServer, instanceName s
// Relay mode migration
if args != nil && args.Mode == "relay" {
// Push copy source fields
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"

// Start the process
Expand Down Expand Up @@ -2160,7 +2160,7 @@ func (r *ProtocolLXD) CopyInstanceSnapshot(source InstanceServer, instanceName s
}

// Pull mode migration
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "pull"
req.Source.Operation = opAPI.ID
req.Source.Websockets = sourceSecrets
Expand Down
23 changes: 16 additions & 7 deletions client/lxd_storage_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ func (r *ProtocolLXD) CopyStoragePoolVolume(pool string, source InstanceServer,
Type: volume.Type,
Source: api.StorageVolumeSource{
Name: volume.Name,
Type: "copy",
Type: api.SourceTypeCopy,
Pool: sourcePool,
VolumeOnly: args.VolumeOnly,
Refresh: args.Refresh,
Expand Down Expand Up @@ -692,7 +692,7 @@ func (r *ProtocolLXD) CopyStoragePoolVolume(pool string, source InstanceServer,
}

// Create the container
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"

// Send the request
Expand All @@ -708,7 +708,10 @@ func (r *ProtocolLXD) CopyStoragePoolVolume(pool string, source InstanceServer,

targetSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
targetSecrets[k] = v.(string)
value, ok := v.(string)
if ok {
targetSecrets[k] = value
}
}

// Prepare the source request
Expand Down Expand Up @@ -738,13 +741,16 @@ func (r *ProtocolLXD) CopyStoragePoolVolume(pool string, source InstanceServer,
// Prepare source server secrets for remote
sourceSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
sourceSecrets[k] = v.(string)
value, ok := v.(string)
if ok {
sourceSecrets[k] = value
}
}

// Relay mode migration
if args != nil && args.Mode == "relay" {
// Push copy source fields
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "push"

// Send the request
Expand All @@ -761,7 +767,10 @@ func (r *ProtocolLXD) CopyStoragePoolVolume(pool string, source InstanceServer,
// Extract the websockets
targetSecrets := map[string]string{}
for k, v := range targetOpAPI.Metadata {
targetSecrets[k] = v.(string)
value, ok := v.(string)
if ok {
targetSecrets[k] = value
}
}

// Launch the relay
Expand All @@ -786,7 +795,7 @@ func (r *ProtocolLXD) CopyStoragePoolVolume(pool string, source InstanceServer,
}

// Pull mode migration
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Mode = "pull"
req.Source.Operation = opAPI.ID
req.Source.Websockets = sourceSecrets
Expand Down
2 changes: 2 additions & 0 deletions lxc-to-lxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ __attribute__((constructor)) void init(void) {
import "C"

import (
"fmt"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -83,6 +84,7 @@ func main() {
// Run the main command and handle errors
err := app.Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Loading

0 comments on commit 81495c0

Please sign in to comment.