Skip to content

Commit

Permalink
Add pulumi-converter-yaml (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frassle authored Aug 28, 2023
1 parent e501760 commit 2760742
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .goreleaser.prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ builds:
- -w
- -X github.com/pulumi/pulumi-yaml/pkg/version.Version={{.Tag}}
main: ./cmd/pulumi-language-yaml/
- id: pulumi-converter-yaml
binary: pulumi-converter-yaml
goarch:
- amd64
- arm64
goos:
- darwin
- windows
- linux
ldflags:
- -s
- -w
- -X github.com/pulumi/pulumi-yaml/pkg/version.Version={{.Tag}}
main: ./cmd/pulumi-converter-yaml/
14 changes: 14 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ builds:
- -w
- -X github.com/pulumi/pulumi-yaml/pkg/version.Version={{.Tag}}
main: ./cmd/pulumi-language-yaml/
- id: pulumi-converter-yaml
binary: pulumi-converter-yaml
goarch:
- amd64
- arm64
goos:
- darwin
- windows
- linux
ldflags:
- -s
- -w
- -X github.com/pulumi/pulumi-yaml/pkg/version.Version={{.Tag}}
main: ./cmd/pulumi-converter-yaml/
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
- Update pulumi/pulumi to v3.78.1
[#493](https://github.com/pulumi/pulumi-yaml/pull/493)

- Publish pulumi-converter-yaml.

### Bug Fixes
116 changes: 116 additions & 0 deletions cmd/pulumi-converter-yaml/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2023, 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.

// pulumi-converter-yaml is the "language converter" for Pulumi programs written in YAML or JSON. It is
// responsible for translating JSON/YAML templates into PCL.
package main

import (
"context"
"errors"
"fmt"
"log"

yamlgen "github.com/pulumi/pulumi-yaml/pkg/pulumiyaml/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/encoding"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"github.com/spf13/afero"
"google.golang.org/grpc"
)

type yamlConverter struct {
}

func (*yamlConverter) Close() error {
return nil
}

func (*yamlConverter) ConvertState(ctx context.Context,
req *plugin.ConvertStateRequest,
) (*plugin.ConvertStateResponse, error) {
return nil, errors.New("not implemented")
}

// writeProgram writes a project and pcl program to the given filesystem
func writeProgram(fs afero.Fs, proj *workspace.Project, program *pcl.Program) error {
contract.Requiref(fs != nil, "fs", "must not be nil")
contract.Requiref(proj != nil, "proj", "must not be nil")
contract.Requiref(program != nil, "program", "must not be nil")

err := program.WriteSource(fs)
if err != nil {
return fmt.Errorf("writing program: %w", err)
}

projBytes, err := encoding.YAML.Marshal(proj)
if err != nil {
return fmt.Errorf("marshaling project: %w", err)
}

err = afero.WriteFile(fs, "Pulumi.yaml", projBytes, 0o644)
if err != nil {
return fmt.Errorf("writing project: %w", err)
}

return nil
}

func (*yamlConverter) ConvertProgram(ctx context.Context,
req *plugin.ConvertProgramRequest,
) (*plugin.ConvertProgramResponse, error) {
loader, err := schema.NewLoaderClient(req.LoaderTarget)
if err != nil {
return nil, err
}
proj, program, err := yamlgen.Eject(req.SourceDirectory, loader)
if err != nil {
return nil, fmt.Errorf("load yaml program: %w", err)
}
fs := afero.NewBasePathFs(afero.NewOsFs(), req.TargetDirectory)
err = writeProgram(fs, proj, program)
if err != nil {
return nil, fmt.Errorf("write program to intermediate directory: %w", err)
}

return &plugin.ConvertProgramResponse{}, nil
}

// Launches the converter RPC endpoint
func main() {
// Fire up a gRPC server, letting the kernel choose a free port for us.
handle, err := rpcutil.ServeWithOptions(rpcutil.ServeOptions{
Init: func(srv *grpc.Server) error {
pulumirpc.RegisterConverterServer(srv, plugin.NewConverterServer(&yamlConverter{}))
return nil
},
Options: rpcutil.OpenTracingServerInterceptorOptions(nil),
})
if err != nil {
log.Fatalf("fatal: %v", err)
}

// The converter protocol requires that we now write out the port we have chosen to listen on.
fmt.Printf("%d\n", handle.Port)

// Finally, wait for the server to stop serving.
if err := <-handle.Done; err != nil {
log.Fatalf("fatal: %v", err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/pulumi/pulumi/pkg/v3 v3.78.1
github.com/pulumi/pulumi/sdk/v3 v3.78.1
github.com/spf13/afero v1.9.5
github.com/stretchr/testify v1.8.3
github.com/zclconf/go-cty v1.13.2
google.golang.org/grpc v1.57.0
Expand Down Expand Up @@ -169,7 +170,6 @@ require (
github.com/sergi/go-diff v1.3.1 // indirect
github.com/shurcooL/go-goon v0.0.0-20210110234559-7585751d9a17 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down

0 comments on commit 2760742

Please sign in to comment.