Skip to content

Commit

Permalink
lxd/*: Replace hardcoded source types with constants
Browse files Browse the repository at this point in the history
Signed-off-by: Din Music <[email protected]>
  • Loading branch information
MusicDin committed Dec 5, 2024
1 parent 632b358 commit 310d18e
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lxd/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ func imagesPost(d *Daemon, r *http.Request) response.Response {
/* Processing image upload */
info, err = getImgPostInfo(s, r, builddir, projectName, post, imageMetadata)
} else {
if req.Source.Type == "image" {
if req.Source.Type == api.SourceTypeImage {
/* Processing image copy from remote */
info, err = imgPostRemoteInfo(s, r, req, op, projectName, budget)
} else if req.Source.Type == "url" {
Expand Down
10 changes: 5 additions & 5 deletions lxd/instance/instance_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func ResolveImage(ctx context.Context, tx *db.ClusterTx, projectName string, sou
// A nil list indicates that we can't tell at this stage, typically for private images.
func SuitableArchitectures(ctx context.Context, s *state.State, tx *db.ClusterTx, projectName string, sourceInst *cluster.Instance, sourceImageRef string, req api.InstancesPost) ([]int, error) {
// Handle cases where the architecture is already provided.
if shared.ValueInSlice(req.Source.Type, []string{"conversion", "migration", "none"}) && req.Architecture != "" {
if shared.ValueInSlice(req.Source.Type, []string{api.SourceTypeConversion, api.SourceTypeMigration, api.SourceTypeNone}) && req.Architecture != "" {
id, err := osarch.ArchitectureId(req.Architecture)
if err != nil {
return nil, err
Expand All @@ -557,22 +557,22 @@ func SuitableArchitectures(ctx context.Context, s *state.State, tx *db.ClusterTx
}

// For migration and conversion, an architecture must be specified in the req.
if shared.ValueInSlice(req.Source.Type, []string{"conversion", "migration"}) && req.Architecture == "" {
if shared.ValueInSlice(req.Source.Type, []string{api.SourceTypeConversion, api.SourceTypeMigration}) && req.Architecture == "" {
return nil, api.StatusErrorf(http.StatusBadRequest, "An architecture must be specified in migration or conversion requests")
}

// For none, allow any architecture.
if req.Source.Type == "none" {
if req.Source.Type == api.SourceTypeNone {
return []int{}, nil
}

// For copy, always use the source architecture.
if req.Source.Type == "copy" {
if req.Source.Type == api.SourceTypeCopy {
return []int{sourceInst.Architecture}, nil
}

// For image, things get a bit more complicated.
if req.Source.Type == "image" {
if req.Source.Type == api.SourceTypeImage {
// Handle local images.
if req.Source.Server == "" {
_, img, err := tx.GetImageByFingerprintPrefix(ctx, sourceImageRef, cluster.ImageFilter{Project: &projectName})
Expand Down
2 changes: 1 addition & 1 deletion lxd/instance_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func instancePostClusteringMigrate(s *state.State, r *http.Request, srcPool stor
InstancePut: srcInstInfo.Writable(),
Type: api.InstanceType(srcInstInfo.Type),
Source: api.InstanceSource{
Type: "migration",
Type: api.SourceTypeMigration,
Mode: "pull",
Operation: fmt.Sprintf("https://%s%s", srcMember.Address, srcOp.URL()),
Websockets: sourceSecrets,
Expand Down
4 changes: 2 additions & 2 deletions lxd/instance_rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func instanceRebuildPost(d *Daemon, r *http.Request) response.Response {
return fmt.Errorf("Failed loading instance: %w", err)
}

if req.Source.Type != "none" {
if req.Source.Type != api.SourceTypeNone {
sourceImage, err = getSourceImageFromInstanceSource(ctx, s, tx, targetProject.Name, req.Source, &sourceImageRef, dbInst.Type.String())
if err != nil && !api.StatusErrorCheck(err, http.StatusNotFound) {
return err
Expand All @@ -136,7 +136,7 @@ func instanceRebuildPost(d *Daemon, r *http.Request) response.Response {
}

run := func(op *operations.Operation) error {
if req.Source.Type == "none" {
if req.Source.Type == api.SourceTypeNone {
return instanceRebuildFromEmpty(inst, op)
}

Expand Down
16 changes: 8 additions & 8 deletions lxd/instances_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
profileProject := project.ProfileProjectFromRecord(targetProject)

switch req.Source.Type {
case "copy":
case api.SourceTypeCopy:
if req.Source.Source == "" {
return api.StatusErrorf(http.StatusBadRequest, "Must specify a source instance")
}
Expand Down Expand Up @@ -1130,7 +1130,7 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
}
}

case "image":
case api.SourceTypeImage:
// Check if the image has an entry in the database but fail only if the error
// is different than the image not being found.
sourceImage, err = getSourceImageFromInstanceSource(ctx, s, tx, targetProject.Name, req.Source, &sourceImageRef, string(req.Type))
Expand Down Expand Up @@ -1339,15 +1339,15 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
}

switch req.Source.Type {
case "image":
case api.SourceTypeImage:
return createFromImage(s, r, *targetProject, profiles, sourceImage, sourceImageRef, &req)
case "none":
case api.SourceTypeNone:
return createFromNone(s, r, targetProjectName, profiles, &req)
case "migration":
case api.SourceTypeMigration:
return createFromMigration(s, r, targetProjectName, profiles, &req)
case "conversion":
case api.SourceTypeConversion:
return createFromConversion(s, r, targetProjectName, profiles, &req)
case "copy":
case api.SourceTypeCopy:
return createFromCopy(s, r, targetProjectName, profiles, &req)
default:
return response.BadRequest(fmt.Errorf("Unknown source type %s", req.Source.Type))
Expand Down Expand Up @@ -1507,7 +1507,7 @@ func clusterCopyContainerInternal(s *state.State, r *http.Request, source instan
}

// Reset the source for a migration
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Certificate = string(s.Endpoints.NetworkCert().PublicKey())
req.Source.Mode = "pull"
req.Source.Operation = fmt.Sprintf("https://%s/%s/operations/%s", nodeAddress, version.APIVersion, opAPI.ID)
Expand Down
2 changes: 1 addition & 1 deletion lxd/project/limits/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func AllowInstanceCreation(globalConfig *clusterConfig.Config, tx *db.ClusterTx,
// Special case restriction checks on volatile.* keys.
strip := false

if shared.ValueInSlice(req.Source.Type, []string{"copy", "migration"}) {
if shared.ValueInSlice(req.Source.Type, []string{api.SourceTypeCopy, api.SourceTypeMigration}) {
// Allow stripping volatile keys if dealing with a copy or migration.
strip = true
}
Expand Down
8 changes: 4 additions & 4 deletions lxd/storage_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,13 +1095,13 @@ func storagePoolVolumesPost(d *Daemon, r *http.Request) response.Response {
switch req.Source.Type {
case "":
return doVolumeCreateOrCopy(s, r, requestProjectName, projectName, poolName, &req)
case "copy":
case api.SourceTypeCopy:
if dbVolume != nil {
return doCustomVolumeRefresh(s, r, requestProjectName, projectName, poolName, &req)
}

return doVolumeCreateOrCopy(s, r, requestProjectName, projectName, poolName, &req)
case "migration":
case api.SourceTypeMigration:
return doVolumeMigration(s, r, requestProjectName, projectName, poolName, &req)
default:
return response.BadRequest(fmt.Errorf("Unknown source type %q", req.Source.Type))
Expand Down Expand Up @@ -1146,7 +1146,7 @@ func clusterCopyCustomVolumeInternal(s *state.State, r *http.Request, sourceAddr
}

// Reset the source for a migration
req.Source.Type = "migration"
req.Source.Type = api.SourceTypeMigration
req.Source.Certificate = string(s.Endpoints.NetworkCert().PublicKey())
req.Source.Mode = "pull"
req.Source.Operation = fmt.Sprintf("https://%s/%s/operations/%s", sourceAddress, version.APIVersion, opAPI.ID)
Expand Down Expand Up @@ -1801,7 +1801,7 @@ func storageVolumePostClusteringMigrate(s *state.State, r *http.Request, srcPool
Name: newVolumeName,
Type: "custom",
Source: api.StorageVolumeSource{
Type: "migration",
Type: api.SourceTypeMigration,
Mode: "pull",
Operation: fmt.Sprintf("https://%s%s", srcMember.Address, srcOp.URL()),
Websockets: sourceSecrets,
Expand Down

0 comments on commit 310d18e

Please sign in to comment.