Skip to content

Commit d574606

Browse files
Kyle Dixleriwahbe
andauthored
Go SDK provider (#156)
Rewrite Command Provider using the pulumi-go-provider library. Co-authored-by: Ian Wahbe <[email protected]>
1 parent ba1097a commit d574606

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2986
-4183
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ NODE_MODULE_NAME := @pulumi/command
77
NUGET_PKG_NAME := Pulumi.Command
88

99
PROVIDER := pulumi-resource-${PACK}
10+
CODEGEN := pulumi-gen-${PACK}
1011
VERSION ?= $(shell pulumictl get version)
1112
PROVIDER_PATH := provider
1213
VERSION_PATH := ${PROVIDER_PATH}/pkg/version.Version
@@ -24,6 +25,8 @@ ensure::
2425

2526
codegen::
2627
(cd provider && VERSION=${VERSION} go generate cmd/${PROVIDER}/main.go)
28+
(cd provider && go build -o $(WORKING_DIR)/bin/${CODEGEN} -ldflags "-X ${PROJECT}/${VERSION_PATH}=${VERSION}" ${PROJECT}/${PROVIDER_PATH}/cmd/$(CODEGEN))
29+
$(WORKING_DIR)/bin/${CODEGEN} $(SCHEMA_FILE) --version ${VERSION}
2730

2831
provider::
2932
(cd provider && go build -o $(WORKING_DIR)/bin/${PROVIDER} -ldflags "-X ${PROJECT}/${VERSION_PATH}=${VERSION}" $(PROJECT)/${PROVIDER_PATH}/cmd/$(PROVIDER))

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import (
5353
func main() {
5454
pulumi.Run(func(ctx *pulumi.Context) error {
5555

56-
random, err := local.NewCommand(ctx, "my-bucket", &local.CommandArgs{
56+
random, err := local.NewCommand(ctx, "my-bucket", &local.CommandInput{
5757
Create: pulumi.String("openssl rand -hex 16"),
5858
})
5959
if err != nil {
@@ -116,7 +116,7 @@ const server = new aws.ec2.Instance("server", {
116116

117117
// Now set up a connection to the instance and run some provisioning operations on the instance.
118118

119-
const connection: types.input.remote.ConnectionArgs = {
119+
const connection: types.input.remote.ConnectionInput = {
120120
host: server.publicIp,
121121
user: "ec2-user",
122122
privateKey: privateKey,
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2016-2022, Pulumi Corporation.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"bytes"
19+
"encoding/json"
20+
"flag"
21+
"fmt"
22+
"os"
23+
24+
command "github.com/pulumi/pulumi-command/provider/pkg/provider"
25+
providerVersion "github.com/pulumi/pulumi-command/provider/pkg/version"
26+
)
27+
28+
// copied from encoding/json for use with JSONMarshal above
29+
func MarshalIndent(v any) ([]byte, error) {
30+
31+
// json.Marshal normally escapes HTML. This one doesn't
32+
// https://stackoverflow.com/questions/28595664/how-to-stop-json-marshal-from-escaping-and
33+
buffer := &bytes.Buffer{}
34+
encoder := json.NewEncoder(buffer)
35+
encoder.SetEscapeHTML(false)
36+
err := encoder.Encode(v)
37+
if err != nil {
38+
return nil, err
39+
}
40+
b := buffer.Bytes()
41+
42+
// serialize and pretty print
43+
var buf bytes.Buffer
44+
prefix, indent := "", " "
45+
err = json.Indent(&buf, b, prefix, indent)
46+
if err != nil {
47+
return nil, err
48+
}
49+
return buf.Bytes(), nil
50+
}
51+
52+
func main() {
53+
flag.Usage = func() {
54+
const usageFormat = "Usage: %s <schema-file>"
55+
fmt.Fprintf(flag.CommandLine.Output(), usageFormat, os.Args[0])
56+
flag.PrintDefaults()
57+
}
58+
59+
var version string
60+
flag.StringVar(&version, "version", providerVersion.Version, "the provider version to record in the generated code")
61+
62+
flag.Parse()
63+
args := flag.Args()
64+
if len(args) < 1 {
65+
flag.Usage()
66+
return
67+
}
68+
s, err := command.Schema(version)
69+
if err != nil {
70+
panic(err)
71+
}
72+
73+
// sort keys
74+
var arg map[string]any
75+
err = json.Unmarshal([]byte(s), &arg)
76+
if err != nil {
77+
panic(err)
78+
}
79+
80+
// remove version key
81+
delete(arg, "version")
82+
83+
// use custom marshal indent to skip html escaping
84+
out, err := MarshalIndent(arg)
85+
if err != nil {
86+
panic(err)
87+
}
88+
89+
schemaPath := args[0]
90+
err = os.WriteFile(schemaPath, out, 0600)
91+
if err != nil {
92+
panic(err)
93+
}
94+
}

provider/cmd/pulumi-resource-command/generate.go

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016-2021, Pulumi Corporation.
1+
// Copyright 2016-2022, Pulumi Corporation.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,20 +12,34 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//go:generate go run ./generate.go
16-
1715
package main
1816

1917
import (
20-
_ "embed"
18+
"fmt"
19+
"os"
20+
"strings"
21+
22+
p "github.com/pulumi/pulumi-go-provider"
2123

22-
"github.com/pulumi/pulumi-command/provider/pkg/provider"
24+
command "github.com/pulumi/pulumi-command/provider/pkg/provider"
2325
"github.com/pulumi/pulumi-command/provider/pkg/version"
2426
)
2527

26-
//go:embed schema-embed.json
27-
var pulumiSchema []byte
28-
28+
// A provider is a program that listens for requests from the Pulumi engine
29+
// to interact with cloud providers using a CRUD-based model.
2930
func main() {
30-
provider.Serve("command", version.Version, pulumiSchema)
31+
version := version.Version
32+
if strings.HasPrefix(version, "v") {
33+
version = version[1:]
34+
}
35+
36+
// This method defines the provider implemented in this repository.
37+
commandProvider := command.NewProvider()
38+
39+
// This method starts serving requests using the Command provider.
40+
err := p.RunProvider("command", version, commandProvider)
41+
if err != nil {
42+
fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
43+
os.Exit(1)
44+
}
3145
}

0 commit comments

Comments
 (0)