Skip to content

Commit

Permalink
Enable l2-simple
Browse files Browse the repository at this point in the history
  • Loading branch information
Frassle committed Nov 14, 2024
1 parent 58cc134 commit 899ba63
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 19 deletions.
22 changes: 11 additions & 11 deletions cmd/pulumi-language-yaml/language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,16 @@ var expectedFailures = map[string]string{
"l2-invoke-simple": "TODO",
"l2-plain": "TODO",
"l2-ref-ref": "TODO",
"l2-resource-simple": "TODO",
"l2-failed-create-continue-on-error": "TODO",
"l2-invoke-variants": "TODO",
"l2-primitive-ref": "TODO",
"l2-resource-alpha": "TODO",
"l2-target-up-with-new-dependency": "TODO",
"l2-invoke-dependencies": "TODO",
"l2-large-string": "TODO",
"l2-provider-grpc-config": "TODO",
"l2-resource-asset-archive": "TODO",
//"l2-resource-simple": "TODO",
"l2-failed-create-continue-on-error": "TODO",
"l2-invoke-variants": "TODO",
"l2-primitive-ref": "TODO",
"l2-resource-alpha": "TODO",
"l2-target-up-with-new-dependency": "TODO",
"l2-invoke-dependencies": "TODO",
"l2-large-string": "TODO",
"l2-provider-grpc-config": "TODO",
"l2-resource-asset-archive": "TODO",
}

func TestLanguage(t *testing.T) {
Expand All @@ -212,7 +212,7 @@ func TestLanguage(t *testing.T) {
// Run the language plugin
handle, err := rpcutil.ServeWithOptions(rpcutil.ServeOptions{
Init: func(srv *grpc.Server) error {
host := server.NewLanguageHost(engineAddress, "", "")
host := server.NewLanguageHost(engineAddress, "", "", true /* useRPCLoader */)
pulumirpc.RegisterLanguageRuntimeServer(srv, host)
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/pulumi-language-yaml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
// Fire up a gRPC server, letting the kernel choose a free port.
port, done, err := rpcutil.Serve(0, cancelChannel, []func(*grpc.Server) error{
func(srv *grpc.Server) error {
host := server.NewLanguageHost(engineAddress, tracing, compiler)
host := server.NewLanguageHost(engineAddress, tracing, compiler, false /* useRPCLoader */)
pulumirpc.RegisterLanguageRuntimeServer(srv, host)
return nil
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resources:
res:
type: simple:Resource
properties:
value: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: l2-resource-simple
runtime: yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packageDeclarationVersion: 1
name: simple
version: 2.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packageDeclarationVersion: 1
name: simple
version: 2.0.0
11 changes: 10 additions & 1 deletion pkg/pulumiyaml/codegen/gen_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
enc "github.com/pulumi/pulumi/sdk/v3/go/common/encoding"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/fsutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"

"github.com/pulumi/pulumi-yaml/pkg/pulumiyaml/ast"
Expand Down Expand Up @@ -56,7 +57,7 @@ func GenerateProgram(program *pcl.Program) (map[string][]byte, hcl.Diagnostics,
return map[string][]byte{"Main.yaml": w.Bytes()}, g.diags, err
}

func GenerateProject(directory string, project workspace.Project, program *pcl.Program) error {
func GenerateProject(directory string, project workspace.Project, program *pcl.Program, localDependencies map[string]string) error {
files, diagnostics, err := GenerateProgram(program)
if err != nil {
return err
Expand Down Expand Up @@ -94,6 +95,14 @@ func GenerateProject(directory string, project workspace.Project, program *pcl.P
}
}

for name, content := range localDependencies {
outPath := path.Join(directory, "sdks", name+".yaml")
err := fsutil.CopyFile(outPath, content, nil)
if err != nil {
return fmt.Errorf("copy local dependency: %w", err)
}
}

return nil
}

Expand Down
136 changes: 136 additions & 0 deletions pkg/pulumiyaml/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/blang/semver"
"github.com/iancoleman/strcase"
"github.com/pulumi/pulumi-yaml/pkg/pulumiyaml/ast"
"github.com/pulumi/pulumi-yaml/pkg/pulumiyaml/packages"
"github.com/pulumi/pulumi-yaml/pkg/pulumiyaml/syntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
Expand Down Expand Up @@ -121,6 +122,30 @@ type pluginEntry struct {
func GetReferencedPlugins(tmpl *ast.TemplateDecl) ([]Plugin, syntax.Diagnostics) {
pluginMap := map[string]*pluginEntry{}

// Iterate over the package declarations
for _, pkg := range tmpl.Packages {
name := pkg.Name
version := pkg.Version
if pkg.Parameterization != nil {
name = pkg.Parameterization.Name
version = pkg.Parameterization.Version
}

if entry, found := pluginMap[name]; found {
if entry.version == "" {
entry.version = version
}
if entry.pluginDownloadURL == "" {
entry.pluginDownloadURL = pkg.DownloadURL
}
} else {
pluginMap[name] = &pluginEntry{
version: version,
pluginDownloadURL: pkg.DownloadURL,
}
}
}

acceptType := func(r *Runner, typeName string, version, pluginDownloadURL *ast.StringExpr) {
pkg := ResolvePkgName(typeName)
if entry, found := pluginMap[pkg]; found {
Expand Down Expand Up @@ -197,6 +222,117 @@ func GetReferencedPlugins(tmpl *ast.TemplateDecl) ([]Plugin, syntax.Diagnostics)
return plugins, nil
}

// GetReferencedPlugins returns the packages and (if provided) versions for each referenced package
// used in the program.
func GetReferencedPackages(tmpl *ast.TemplateDecl) ([]packages.PackageDecl, syntax.Diagnostics) {
packageMap := map[string]*packages.PackageDecl{}

// Iterate over the package declarations
for _, pkg := range tmpl.Packages {
name := pkg.Name
version := pkg.Version
if pkg.Parameterization != nil {
name = pkg.Parameterization.Name
version = pkg.Parameterization.Version
}

if entry, found := packageMap[name]; found {
if entry.Version == "" {
entry.Version = version
}
if entry.DownloadURL == "" {
entry.DownloadURL = pkg.DownloadURL
}
} else {
packageMap[name] = &pkg
}
}

acceptType := func(r *Runner, typeName string, version, pluginDownloadURL *ast.StringExpr) {
pkg := ResolvePkgName(typeName)
if entry, found := packageMap[pkg]; found {
if v := version.GetValue(); v != "" && entry.Version != v {
if entry.Version == "" {
entry.Version = v
} else {
r.sdiags.Extend(ast.ExprError(version, fmt.Sprintf("Package %v already declared with a conflicting version: %v", pkg, entry.Version), ""))
}
}
if url := pluginDownloadURL.GetValue(); url != "" && entry.DownloadURL != url {
if entry.DownloadURL == "" {
entry.DownloadURL = url
} else {
r.sdiags.Extend(ast.ExprError(pluginDownloadURL, fmt.Sprintf("Package %v already declared with a conflicting plugin download URL: %v", pkg, entry.DownloadURL), ""))
}
}
} else {
packageMap[pkg] = &packages.PackageDecl{
Name: pkg,
Version: version.GetValue(),
DownloadURL: pluginDownloadURL.GetValue(),
}
}
}

diags := newRunner(tmpl, nil).Run(walker{
VisitResource: func(r *Runner, node resourceNode) bool {
res := node.Value

if res.Type == nil {
r.sdiags.Extend(syntax.NodeError(node.Value.Syntax(), fmt.Sprintf("Resource declared without a 'type': %q", node.Key.Value), ""))
return true
}
acceptType(r, res.Type.Value, res.Options.Version, res.Options.PluginDownloadURL)

return true
},
VisitExpr: func(ctx *evalContext, expr ast.Expr) bool {
if expr, ok := expr.(*ast.InvokeExpr); ok {
if expr.Token == nil {
ctx.Runner.sdiags.Extend(syntax.NodeError(expr.Syntax(), "Invoke declared without a 'function' type", ""))
return true
}
acceptType(ctx.Runner, expr.Token.GetValue(), expr.CallOpts.Version, expr.CallOpts.PluginDownloadURL)
}
return true
},
})

if diags.HasErrors() {
return nil, diags
}

var packages []packages.PackageDecl
for _, pkg := range packageMap {
packages = append(packages, *pkg)
}

sort.Slice(packages, func(i, j int) bool {
pI, pJ := packages[i], packages[j]
if pI.Name != pJ.Name {
return pI.Name < pJ.Name
}
if pI.Version != pJ.Version {
return pI.Version < pJ.Version
}
if pI.Parameterization == nil && pJ.Parameterization == nil {
return pI.DownloadURL < pJ.DownloadURL
}
if pI.Parameterization == nil {
return true
}
if pJ.Parameterization == nil {
return false
}
if pI.Parameterization.Name != pJ.Parameterization.Name {
return pI.Parameterization.Name < pJ.Parameterization.Name
}
return pI.Parameterization.Version < pJ.Parameterization.Version
})

return packages, nil
}

func ResolvePkgName(typeString string) string {
typeParts := strings.Split(typeString, ":")

Expand Down
Loading

0 comments on commit 899ba63

Please sign in to comment.