Skip to content

Commit

Permalink
Test and fix support for local plugins (#619)
Browse files Browse the repository at this point in the history
This updates the pulumi dependency to
[d9df4771815894ab6ddaf17b78f6f023c91b7148](pulumi/pulumi@d9df477)
which has a number of fixes for plugin and schema loading.

This allows YAML to correctly load schemas for plugins defined in the
Pulumi.yaml file itself. We often use this for integegration tests, but we
expect users to increasinly make use of this feature in the future as an
alternative to dynamic providers.
  • Loading branch information
Frassle authored Sep 3, 2024
1 parent f20ccea commit ef1fb05
Show file tree
Hide file tree
Showing 15 changed files with 1,108 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@

- Parse the items property on config type declarations to prevent diagnostic messages about
unknown fields [#615](https://github.com/pulumi/pulumi-yaml/pull/615)

- Fix usage of local plugins (those defined in the Pulumi.yaml file plugins section) [#619](https://github.com/pulumi/pulumi-yaml/pull/619)
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ require (
github.com/hexops/autogold v1.3.0
github.com/iancoleman/strcase v0.2.0
github.com/pkg/errors v0.9.1
github.com/pulumi/pulumi/pkg/v3 v3.129.1-0.20240819035819-d75251c49f8a
github.com/pulumi/pulumi/sdk/v3 v3.129.1-0.20240819035819-d75251c49f8a
github.com/pulumi/pulumi/pkg/v3 v3.130.1-0.20240831132520-d9df47718158
github.com/pulumi/pulumi/sdk/v3 v3.130.1-0.20240831132520-d9df47718158
github.com/spf13/afero v1.9.5
github.com/stretchr/testify v1.9.0
github.com/zclconf/go-cty v1.13.2
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.33.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down Expand Up @@ -186,7 +187,6 @@ require (
google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
lukechampine.com/frand v1.4.2 // indirect
mvdan.cc/gofumpt v0.5.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ github.com/pulumi/esc v0.9.1 h1:HH5eEv8sgyxSpY5a8yePyqFXzA8cvBvapfH8457+mIs=
github.com/pulumi/esc v0.9.1/go.mod h1:oEJ6bOsjYlQUpjf70GiX+CXn3VBmpwFDxUTlmtUN84c=
github.com/pulumi/inflector v0.1.1 h1:dvlxlWtXwOJTUUtcYDvwnl6Mpg33prhK+7mzeF+SobA=
github.com/pulumi/inflector v0.1.1/go.mod h1:HUFCjcPTz96YtTuUlwG3i3EZG4WlniBvR9bd+iJxCUY=
github.com/pulumi/pulumi/pkg/v3 v3.129.1-0.20240819035819-d75251c49f8a h1:0+5MrobQVNIJRXxeeVEgNsnS8jVYxwm390K5wuboS+Y=
github.com/pulumi/pulumi/pkg/v3 v3.129.1-0.20240819035819-d75251c49f8a/go.mod h1:W3c7JgO064kUH5IfyQMCgmsAr5iwr3PO6KEeIQiO0dY=
github.com/pulumi/pulumi/sdk/v3 v3.129.1-0.20240819035819-d75251c49f8a h1:1k4pn6Ef3Ts5C6UMKpQGcVzs6GmfHOJWW76wVaAUiKE=
github.com/pulumi/pulumi/sdk/v3 v3.129.1-0.20240819035819-d75251c49f8a/go.mod h1:p1U24en3zt51agx+WlNboSOV8eLlPWYAkxMzVEXKbnY=
github.com/pulumi/pulumi/pkg/v3 v3.130.1-0.20240831132520-d9df47718158 h1:X7NKRRMROkOjou/WYdhH5JhwqFm0QUI98eVBfO5y0zs=
github.com/pulumi/pulumi/pkg/v3 v3.130.1-0.20240831132520-d9df47718158/go.mod h1:8Wt6MyH3CO7ssKjYOuhmZHjV/H8oCeSCKe8jjVskuDo=
github.com/pulumi/pulumi/sdk/v3 v3.130.1-0.20240831132520-d9df47718158 h1:FF8TMAxHXhLPL+ug2M3gulInEULONmg9UL9ajRbdVIc=
github.com/pulumi/pulumi/sdk/v3 v3.130.1-0.20240831132520-d9df47718158/go.mod h1:p1U24en3zt51agx+WlNboSOV8eLlPWYAkxMzVEXKbnY=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
Expand Down
9 changes: 5 additions & 4 deletions pkg/pulumiyaml/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)

type ResourceTypeToken string
Expand Down Expand Up @@ -86,8 +87,8 @@ func (l packageLoader) Close() {
}
}

func NewPackageLoader() (PackageLoader, error) {
host, err := newResourcePackageHost()
func NewPackageLoader(plugins *workspace.Plugins) (PackageLoader, error) {
host, err := newResourcePackageHost(plugins)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -466,15 +467,15 @@ func getResourceConstants(props []*schema.Property) map[string]interface{} {
return constantProps
}

func newResourcePackageHost() (plugin.Host, error) {
func newResourcePackageHost(plugins *workspace.Plugins) (plugin.Host, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
sink := diag.DefaultSink(os.Stderr, os.Stderr, diag.FormatOptions{
Color: cmdutil.GetGlobalColorization(),
})
pluginCtx, err := plugin.NewContext(sink, sink, nil, nil, cwd, nil, true, nil)
pluginCtx, err := plugin.NewContextWithRoot(sink, sink, nil, cwd, cwd, nil, true, nil, plugins, nil, nil)
if err != nil {
return nil, err
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/version"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"

Expand Down Expand Up @@ -135,6 +136,15 @@ func (host *yamlLanguageHost) Run(ctx context.Context, req *pulumirpc.RunRequest
fmt.Sprintf(`PULUMI_CONFIG=%s`, jsonConfigValue),
}

projPath, err := workspace.DetectProjectPathFrom(req.Info.RootDirectory)
if err != nil {
return nil, err
}
proj, err := workspace.LoadProject(projPath)
if err != nil {
return nil, err
}

template, diags, err := host.loadTemplate(compilerEnv)
if err != nil {
return &pulumirpc.RunResponse{Error: err.Error()}, nil
Expand Down Expand Up @@ -166,7 +176,7 @@ func (host *yamlLanguageHost) Run(ctx context.Context, req *pulumirpc.RunRequest
ConfigSecretKeys: req.GetConfigSecretKeys(),
ConfigPropertyMap: confPropMap,
Organization: req.Organization,
Parallel: int(req.GetParallel()),
Parallel: req.GetParallel(),
DryRun: req.GetDryRun(),
MonitorAddr: req.GetMonitorAddress(),
EngineAddr: host.engineAddress,
Expand All @@ -177,7 +187,7 @@ func (host *yamlLanguageHost) Run(ctx context.Context, req *pulumirpc.RunRequest
defer pctx.Close()
// Now instruct the Pulumi Go SDK to run the pulumi YAML interpreter.
if err := pulumi.RunWithContext(pctx, func(ctx *pulumi.Context) error {
loader, err := pulumiyaml.NewPackageLoader()
loader, err := pulumiyaml.NewPackageLoader(proj.Plugins)
if err != nil {
return err
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,15 @@ func TestEnvVarsKeepConflictingValues(t *testing.T) {
}
integration.ProgramTest(t, &testOptions)
}

// Test a local provider plugin.
//
//nolint:paralleltest // ProgramTest calls t.Parallel()
func TestLocalPlugin(t *testing.T) {
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: filepath.Join("testdata", "local"),
LocalProviders: []integration.LocalDependency{
{Package: "testprovider", Path: "testprovider"},
},
})
}
10 changes: 10 additions & 0 deletions pkg/tests/testdata/local/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: local
scription: An integration test showing the use of a a local plugin
runtime: yaml
resources:
res1:
type: testprovider:index:Random
properties:
length: 10
res2:
type: testprovider:index:Echo
2 changes: 2 additions & 0 deletions pkg/tests/testprovider/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pulumi-resource-testprovider
schema-testprovider.json
1 change: 1 addition & 0 deletions pkg/tests/testprovider/PulumiPlugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtime: go
101 changes: 101 additions & 0 deletions pkg/tests/testprovider/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !all
// +build !all

package main

import (
"errors"
"fmt"
"reflect"

"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

type Random struct {
pulumi.CustomResourceState

Length pulumi.IntOutput `pulumi:"length"`
Result pulumi.StringOutput `pulumi:"result"`
}

func NewRandom(ctx *pulumi.Context,
name string, args *RandomArgs, opts ...pulumi.ResourceOption,
) (*Random, error) {
if args == nil || args.Length == nil {
return nil, errors.New("missing required argument 'Length'")
}
var resource Random
err := ctx.RegisterResource("testprovider:index:Random", name, args, &resource, opts...)
if err != nil {
return nil, err
}
return &resource, nil
}

type randomArgs struct {
Length int `pulumi:"length"`
Prefix string `pulumi:"prefix"`
}

type RandomArgs struct {
Length pulumi.IntInput
Prefix pulumi.StringInput
}

func (RandomArgs) ElementType() reflect.Type {
return reflect.TypeOf((*randomArgs)(nil)).Elem()
}

type Component struct {
pulumi.ResourceState

ChildID pulumi.IDOutput `pulumi:"childId"`
}

type ComponentArgs struct {
Length int `pulumi:"length"`
}

func NewComponent(ctx *pulumi.Context, name string, args *ComponentArgs,
opts ...pulumi.ResourceOption,
) (*Component, error) {
if args == nil {
return nil, errors.New("args is required")
}

component := &Component{}
err := ctx.RegisterComponentResource("testprovider:index:Component", name, component, opts...)
if err != nil {
return nil, err
}

res, err := NewRandom(ctx, fmt.Sprintf("child-%s", name), &RandomArgs{
Length: pulumi.Int(args.Length),
}, pulumi.Parent(component))
if err != nil {
return nil, err
}

component.ChildID = res.ID()

if err := ctx.RegisterResourceOutputs(component, pulumi.Map{
"childId": component.ChildID,
}); err != nil {
return nil, err
}

return component, nil
}
Loading

0 comments on commit ef1fb05

Please sign in to comment.