Skip to content

Commit

Permalink
Merge pull request #839 from rpunjani/issue-811
Browse files Browse the repository at this point in the history
Add get_project instance placement scriptlet function
  • Loading branch information
stgraber authored May 6, 2024
2 parents 20b2a64 + cef77b6 commit dabd5a6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/api-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions doc/explanation/clustering.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions internal/server/scriptlet/instance_placement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions internal/server/scriptlet/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func InstancePlacementCompile(src string) (*starlark.Program, error) {
"get_instance_resources",
"get_instances",
"get_cluster_members",
"get_project",
},
name)
}
Expand Down
1 change: 1 addition & 0 deletions internal/version/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down

0 comments on commit dabd5a6

Please sign in to comment.