From 08edef3b9897b63268b2f02ac17788f564142eb2 Mon Sep 17 00:00:00 2001 From: Rohit Punjani Date: Sat, 4 May 2024 18:10:17 -0500 Subject: [PATCH 1/3] api: instances_scriptlet_get_project Signed-off-by: Rohit Punjani --- doc/api-extensions.md | 4 ++++ internal/version/api.go | 1 + 2 files changed, 5 insertions(+) diff --git a/doc/api-extensions.md b/doc/api-extensions.md index e857360c790..b64f30bafcd 100644 --- a/doc/api-extensions.md +++ b/doc/api-extensions.md @@ -2469,6 +2469,10 @@ This allows the instance scriptlet to fetch a list of instances given an optiona This allows the instance scriptlet to fetch a list of cluster members given an optional cluster group. +## `instances_scriptlet_get_project` + +This allows the instance scriptlet to fetch a project given name of a project. + ## `network_acl_stateless` This adds support for stateless rules in network ACLs. diff --git a/internal/version/api.go b/internal/version/api.go index 7f29688c324..52fa24b66c1 100644 --- a/internal/version/api.go +++ b/internal/version/api.go @@ -414,6 +414,7 @@ var APIExtensions = []string{ "profiles_all_projects", "instances_scriptlet_get_instances", "instances_scriptlet_get_cluster_members", + "instances_scriptlet_get_project", "network_acl_stateless", "instance_state_started_at", } From 7a82233562322fa94df8497b85085e3bfb055463 Mon Sep 17 00:00:00 2001 From: Rohit Punjani Date: Sat, 4 May 2024 18:37:54 -0500 Subject: [PATCH 2/3] doc/instances/scriptlet: Add get_project Signed-off-by: Rohit Punjani --- doc/explanation/clustering.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/explanation/clustering.md b/doc/explanation/clustering.md index 92da8fab566..381ae207416 100644 --- a/doc/explanation/clustering.md +++ b/doc/explanation/clustering.md @@ -203,6 +203,7 @@ The following functions are available to the scriptlet (in addition to those pro - `get_instance_resources()`: Get information about the resources the instance will require. Returns an object with the resource information in the form of [`scriptlet.InstanceResources`](https://pkg.go.dev/github.com/lxc/incus/shared/api/scriptlet/#InstanceResources). - `get_instances(location, project)`: Get a list of instances based on project and/or location filters. Returns the list of instances in the form of [`[]api.Instance`](https://pkg.go.dev/github.com/lxc/incus/shared/api#Instance). - `get_cluster_members(group)`: Get a list of cluster members based on the cluster group. Returns the list of cluster members in the form of [`[]api.ClusterMember`](https://pkg.go.dev/github.com/lxc/incus/shared/api#ClusterMember). +- `get_project(name)`: Get a project object based on the project name. Returns a project object in the form of [`api.Project`](https://pkg.go.dev/github.com/lxc/incus/shared/api#Project). ```{note} Field names in the object types are equivalent to the JSON field names in the associated Go types. From cef77b6d0a02d3aaecc42b410bb8b41c1604ae23 Mon Sep 17 00:00:00 2001 From: Rohit Punjani Date: Sun, 5 May 2024 17:42:31 -0500 Subject: [PATCH 3/3] incusd/scriptlet: Add get_project Closes #811 Signed-off-by: Rohit Punjani --- .../server/scriptlet/instance_placement.go | 36 +++++++++++++++++++ internal/server/scriptlet/load/load.go | 1 + 2 files changed, 37 insertions(+) diff --git a/internal/server/scriptlet/instance_placement.go b/internal/server/scriptlet/instance_placement.go index 15eb406453b..b00faf60b9b 100644 --- a/internal/server/scriptlet/instance_placement.go +++ b/internal/server/scriptlet/instance_placement.go @@ -394,6 +394,41 @@ func InstancePlacementRun(ctx context.Context, l logger.Logger, s *state.State, return rv, nil } + getProjectFunc := func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var name string + + err := starlark.UnpackArgs(b.Name(), args, kwargs, "name??", &name) + if err != nil { + return nil, err + } + + var p *api.Project + + err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { + dbProject, err := dbCluster.GetProject(ctx, tx.Tx(), name) + if err != nil { + return err + } + + p, err = dbProject.ToAPI(ctx, tx.Tx()) + if err != nil { + return err + } + + return nil + }) + if err != nil { + return nil, err + } + + rv, err := StarlarkMarshal(p) + if err != nil { + return nil, fmt.Errorf("Marshalling instance resources failed: %w", err) + } + + return rv, nil + } + var err error var raftNodes []db.RaftNode err = s.DB.Node.Transaction(ctx, func(ctx context.Context, tx *db.NodeTx) error { @@ -461,6 +496,7 @@ func InstancePlacementRun(ctx context.Context, l logger.Logger, s *state.State, "get_instance_resources": starlark.NewBuiltin("get_instance_resources", getInstanceResourcesFunc), "get_instances": starlark.NewBuiltin("get_instances", getInstancesFunc), "get_cluster_members": starlark.NewBuiltin("get_cluster_members", getClusterMembersFunc), + "get_project": starlark.NewBuiltin("get_project", getProjectFunc), } prog, thread, err := scriptletLoad.InstancePlacementProgram() diff --git a/internal/server/scriptlet/load/load.go b/internal/server/scriptlet/load/load.go index 894c16830ad..d6cb86998d4 100644 --- a/internal/server/scriptlet/load/load.go +++ b/internal/server/scriptlet/load/load.go @@ -24,6 +24,7 @@ func InstancePlacementCompile(src string) (*starlark.Program, error) { "get_instance_resources", "get_instances", "get_cluster_members", + "get_project", }, name) }