Skip to content

Commit

Permalink
Merge pull request #1504 from stgraber/cluster
Browse files Browse the repository at this point in the history
incusd/instance_post: Expand profiles in scriptlet context
  • Loading branch information
stgraber authored Dec 13, 2024
2 parents dfe8f19 + 98099c3 commit 0cb05c2
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions cmd/incusd/instance_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/lxc/incus/v6/internal/server/db"
dbCluster "github.com/lxc/incus/v6/internal/server/db/cluster"
"github.com/lxc/incus/v6/internal/server/db/operationtype"
deviceConfig "github.com/lxc/incus/v6/internal/server/device/config"
"github.com/lxc/incus/v6/internal/server/instance"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/operations"
Expand Down Expand Up @@ -335,13 +336,73 @@ func instancePost(d *Daemon, r *http.Request) response.Response {
return response.InternalError(err)
}

// Load profiles.
profileNames := make([]string, 0, len(inst.Profiles()))
for _, profile := range inst.Profiles() {
profileNames = append(profileNames, profile.Name)
}

profiles := make([]api.Profile, 0, len(profileNames))
if len(profileNames) > 0 {
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
profileFilters := make([]dbCluster.ProfileFilter, 0, len(profileNames))
for _, profileName := range profileNames {
profileFilters = append(profileFilters, dbCluster.ProfileFilter{
Project: &instProject,
Name: &profileName,
})
}

dbProfiles, err := dbCluster.GetProfiles(ctx, tx.Tx(), profileFilters...)
if err != nil {
return err
}

dbProfileConfigs, err := dbCluster.GetConfig(ctx, tx.Tx(), "profile")
if err != nil {
return err
}

dbProfileDevices, err := dbCluster.GetDevices(ctx, tx.Tx(), "profile")
if err != nil {
return err
}

profilesByName := make(map[string]dbCluster.Profile, len(dbProfiles))
for _, dbProfile := range dbProfiles {
profilesByName[dbProfile.Name] = dbProfile
}

for _, profileName := range profileNames {
profile, found := profilesByName[profileName]
if !found {
return fmt.Errorf("Requested profile %q doesn't exist", profileName)
}

apiProfile, err := profile.ToAPI(ctx, tx.Tx(), dbProfileConfigs, dbProfileDevices)
if err != nil {
return err
}

profiles = append(profiles, *apiProfile)
}

return nil
})
if err != nil {
return response.SmartError(err)
}
}

// Prepare the placement scriptlet context.
req := apiScriptlet.InstancePlacement{
InstancesPost: api.InstancesPost{
Name: name,
Type: api.InstanceType(instanceType.String()),
InstancePut: api.InstancePut{
Config: inst.ExpandedConfig(),
Devices: inst.ExpandedDevices().CloneNative(),
Config: db.ExpandInstanceConfig(inst.LocalConfig(), profiles),
Devices: db.ExpandInstanceDevices(deviceConfig.NewDevices(inst.LocalDevices().CloneNative()), profiles).CloneNative(),
Profiles: profileNames,
},
},
Project: instProject,
Expand Down

0 comments on commit 0cb05c2

Please sign in to comment.