Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Environment Resource #271

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### Improvements

- Add TeamEnvironmentPermission resource [#179](https://github.com/pulumi/pulumi-pulumiservice/issues/179)
- Add support for Environment resource [#255](https://github.com/pulumi/pulumi-pulumiservice/issues/255)

### Bug Fixes

Expand Down
41 changes: 41 additions & 0 deletions provider/cmd/pulumi-resource-pulumiservice/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,47 @@
"timestamp"
]
},
"pulumiservice:index:Environment": {
"description": "An ESC Environment.",
"properties": {
"organization": {
"description": "Organization name.",
"type": "string"
},
"name": {
"description": "Environment name.",
"type": "string"
},
"yaml": {
"description": "Environment's yaml file.",
"$ref": "pulumi.json#/Asset"
Comment on lines +1280 to +1282
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should make absolutely certain that assets work with output properties. Not 100% sure about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed it works with output using Random and below snippet of code:

var random = new RandomString("rand", new RandomStringArgs
    {
        Length = 5,
        Special = false,
    });

    Output<AssetOrArchive> asset = random.Result.Apply(res => {
        String yaml = """
            imports:
              - dev-stacks
            values:
              aws:
                secrets:
                  fn::open::aws-secrets:
                    region: us-west-2
                    login: ${aws.creds}
                    get:
                      allSecrets:
                        secretId: iaro-dev-stack/pulumi-service
              secrets:
                fn::fromJSON: ${aws.secrets.allSecrets}
              pulumiConfig:
                gitHubOAuthID: ${secrets.gitHubOAuthID}
                gitHubOAuthSecret: ${secrets.gitHubOAuthSecret}
                randomString: 
        """ + res;
        AssetOrArchive asset = new StringAsset(yaml);
        return asset;
        }
    );

    var environ = new Pulumi.PulumiService.Environment(
        "Iaro's environment",
        new EnvironmentArgs {
            Organization = "IaroslavTitov",
            Name = "IaroEnv",
            Yaml = asset
        }
    );

When I go to see the environment in console it has a random blurb zbhrh and the same in Random's resource properties.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I should clarify--I meant that I'm not sure that the output property Environment.Yaml will work with assets. I'd try passing the Yaml output of one environment to the Yaml input of another.

Copy link
Contributor Author

@IaroslavTitov IaroslavTitov May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, gotcha, didn't understand properly
Confirmed that this works, creating 2 environments with identical yaml:

var environ = new Pulumi.PulumiService.Environment(
        "Iaro's environment",
        new EnvironmentArgs {
            Organization = "IaroslavTitov",
            Name = "IaroEnv",
            Yaml = asset
        }
    );

    var environ2 = new Pulumi.PulumiService.Environment(
        "Iaro's environment 2",
        new EnvironmentArgs {
            Organization = "IaroslavTitov",
            Name = "IaroEnv2",
            Yaml = environ.Yaml.Apply(yaml => yaml)
        }
    );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW @IaroslavTitov you shouldn't have to do this:

Yaml = environ.Yaml.Apply(yaml => yaml)

It can just be:

Yaml = environ.Yaml

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, makes sense, got stuck in the Apply mindset

}
},
"required": [
"organization",
"name",
komalali marked this conversation as resolved.
Show resolved Hide resolved
"yaml"
],
"inputProperties": {
"organization": {
"description": "Organization name.",
"type": "string"
},
"name": {
"description": "Environment name.",
"type": "string"
},
"yaml": {
"description": "Environment's yaml file.",
"$ref": "pulumi.json#/Asset"
}
},
"requiredInputs": [
"organization",
"name",
"yaml"
]
},
"pulumiservice:index:TeamEnvironmentPermission": {
"description": "A permission for a team to use an environment.",
"properties": {
Expand Down
2 changes: 1 addition & 1 deletion provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/pulumi/pulumi-pulumiservice/provider
go 1.21.0

require (
github.com/pulumi/esc v0.6.2
github.com/pulumi/esc v0.8.3
github.com/pulumi/pulumi/pkg/v3 v3.112.0
github.com/pulumi/pulumi/sdk/v3 v3.115.3-0.20240507143413-cffdfd1fa489
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions provider/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pulumi/appdash v0.0.0-20231130102222-75f619a67231 h1:vkHw5I/plNdTr435cARxCW6q9gc0S/Yxz7Mkd38pOb0=
github.com/pulumi/appdash v0.0.0-20231130102222-75f619a67231/go.mod h1:murToZ2N9hNJzewjHBgfFdXhZKjY3z5cYC1VXk+lbFE=
github.com/pulumi/esc v0.6.2 h1:+z+l8cuwIauLSwXQS0uoI3rqB+YG4SzsZYtHfNoXBvw=
github.com/pulumi/esc v0.6.2/go.mod h1:jNnYNjzsOgVTjCp0LL24NsCk8ZJxq4IoLQdCT0X7l8k=
github.com/pulumi/esc v0.8.3 h1:myeDL6dD/mz34zZjCL8s7d/tWHBJYxfMxDCL11MHoqc=
github.com/pulumi/esc v0.8.3/go.mod h1:v5VAPxYDa9DRwvubbzKt4ZYf5y0esWC2ccSp/AT923I=
github.com/pulumi/pulumi/pkg/v3 v3.112.0 h1:vhoM6sx1eegJntIeUZENtck3VeMtK1zBiQ2E3EPOnHw=
github.com/pulumi/pulumi/pkg/v3 v3.112.0/go.mod h1:GQhNr0v5E8TACF8j0p6UQqyr7mZreUpoMfVjLeu6eY0=
github.com/pulumi/pulumi/sdk/v3 v3.115.3-0.20240507143413-cffdfd1fa489 h1:e7J2I8veUe9mSpzWN9kPREp7YhHUwnQz0aP0k5R45V8=
Expand Down
275 changes: 275 additions & 0 deletions provider/pkg/provider/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
package provider

import (
"context"
"fmt"
"path"
"strings"

esc_client "github.com/pulumi/esc/cmd/esc/cli/client"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/asset"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
pbempty "google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/structpb"
)

type PulumiServiceEnvironmentResource struct {
client esc_client.Client
}

type PulumiServiceEnvironmentInput struct {
OrgName string
EnvName string
Yaml []byte
}

func (i *PulumiServiceEnvironmentInput) ToPropertyMap() (resource.PropertyMap, error) {
propertyMap := resource.PropertyMap{}
propertyMap["organization"] = resource.NewPropertyValue(i.OrgName)
propertyMap["name"] = resource.NewPropertyValue(i.EnvName)

yamlAsset, err := asset.FromText(strings.TrimSuffix(string(i.Yaml), "\n"))
if err != nil {
return nil, err
}
propertyMap["yaml"] = resource.MakeSecret(resource.NewAssetProperty(yamlAsset))

return propertyMap, nil
}

func ToPulumiServiceEnvironmentInput(properties *structpb.Struct) (*PulumiServiceEnvironmentInput, error) {
inputMap, err := plugin.UnmarshalProperties(properties, plugin.MarshalOptions{KeepUnknowns: true, SkipNulls: true})
if err != nil {
return nil, err
}

input := PulumiServiceEnvironmentInput{}
if inputMap["organization"].HasValue() && inputMap["organization"].IsString() {
input.OrgName = inputMap["organization"].StringValue()
} else {
return nil, fmt.Errorf("failed to unmarshal organization value from properties: %s", inputMap)
}
if inputMap["name"].HasValue() && inputMap["name"].IsString() {
input.EnvName = inputMap["name"].StringValue()
} else {
return nil, fmt.Errorf("failed to unmarshal environment name value from properties: %s", inputMap)
}
if inputMap["yaml"].HasValue() && inputMap["yaml"].IsAsset() {
input.Yaml = []byte(inputMap["yaml"].AssetValue().Text)
} else {
return nil, fmt.Errorf("failed to unmarshal yaml value from properties: %s", inputMap)
}

return &input, nil
}

func (st *PulumiServiceEnvironmentResource) Diff(req *pulumirpc.DiffRequest) (*pulumirpc.DiffResponse, error) {
olds, err := plugin.UnmarshalProperties(req.GetOlds(), plugin.MarshalOptions{})
if err != nil {
return nil, err
}

news, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{})
if err != nil {
return nil, err
}

diffs := olds.Diff(news)
if diffs == nil {
return &pulumirpc.DiffResponse{
Changes: pulumirpc.DiffResponse_DIFF_NONE,
}, nil
}

dd := plugin.NewDetailedDiffFromObjectDiff(diffs, false)

detailedDiffs := map[string]*pulumirpc.PropertyDiff{}
replaces := []string(nil)
replaceProperties := map[string]bool{
"organization": true,
"name": true,
}
for k, v := range dd {
if _, ok := replaceProperties[k]; ok {
v.Kind = v.Kind.AsReplace()
replaces = append(replaces, k)
}
detailedDiffs[k] = &pulumirpc.PropertyDiff{
Kind: pulumirpc.PropertyDiff_Kind(v.Kind),
InputDiff: v.InputDiff,
}
}

changes := pulumirpc.DiffResponse_DIFF_NONE
if len(detailedDiffs) > 0 {
changes = pulumirpc.DiffResponse_DIFF_SOME
}
return &pulumirpc.DiffResponse{
Changes: changes,
Replaces: replaces,
DetailedDiff: detailedDiffs,
HasDetailedDiff: true,
DeleteBeforeReplace: len(replaces) > 0,
}, nil
}

func (st *PulumiServiceEnvironmentResource) Delete(req *pulumirpc.DeleteRequest) (*pbempty.Empty, error) {
input, err := ToPulumiServiceEnvironmentInput(req.GetProperties())
if err != nil {
return nil, err
}

err = st.client.DeleteEnvironment(context.Background(), input.OrgName, input.EnvName)
if err != nil {
return nil, err
}
return &pbempty.Empty{}, nil
}

func (st *PulumiServiceEnvironmentResource) Create(req *pulumirpc.CreateRequest) (*pulumirpc.CreateResponse, error) {
input, err := ToPulumiServiceEnvironmentInput(req.GetProperties())
if err != nil {
return nil, err
}

// First check if yaml is valid
_, diagnostics, err := st.client.CheckYAMLEnvironment(context.Background(), input.OrgName, input.Yaml)
if err != nil {
return nil, err
}
if diagnostics != nil {
return nil, fmt.Errorf("failed to create environment, yaml code failed following checks: %+v", diagnostics)
}

// Then create environment, and update it with yaml provided. ESC API architecture doesn't let you do it in one call
err = st.client.CreateEnvironment(context.Background(), input.OrgName, input.EnvName)
if err != nil {
return nil, err
}
diagnostics, err = st.client.UpdateEnvironment(context.Background(), input.OrgName, input.EnvName, input.Yaml, "")
if err != nil {
return nil, err
}
if diagnostics != nil {
return nil, fmt.Errorf("failed to update brand new environment with pre-checked yaml, due to failing the following checks: %+v \n"+
"This should never happen, if you're seeing this message there's likely a bug in ESC APIs", diagnostics)
}

propertyMap, err := input.ToPropertyMap()
if err != nil {
return nil, err
}
outputProperties, err := plugin.MarshalProperties(
propertyMap,
plugin.MarshalOptions{
KeepSecrets: true,
},
)
if err != nil {
return nil, err
}

return &pulumirpc.CreateResponse{
Id: path.Join(input.OrgName, input.EnvName),
Properties: outputProperties,
}, nil
}

func (st *PulumiServiceEnvironmentResource) Check(req *pulumirpc.CheckRequest) (*pulumirpc.CheckResponse, error) {
inputMap, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{})
if err != nil {
return nil, err
}

var failures []*pulumirpc.CheckFailure
for _, p := range []resource.PropertyKey{"organization", "name", "yaml"} {
if !inputMap[(p)].HasValue() {
failures = append(failures, &pulumirpc.CheckFailure{
Reason: fmt.Sprintf("missing required property '%s'", p),
Property: string(p),
})
}
}

return &pulumirpc.CheckResponse{Inputs: req.GetNews(), Failures: failures}, nil
}

func (st *PulumiServiceEnvironmentResource) Update(req *pulumirpc.UpdateRequest) (*pulumirpc.UpdateResponse, error) {
input, err := ToPulumiServiceEnvironmentInput(req.GetNews())
if err != nil {
return nil, err
}

diagnostics, err := st.client.UpdateEnvironment(context.Background(), input.OrgName, input.EnvName, input.Yaml, "")
if err != nil {
return nil, err
}
if diagnostics != nil {
return nil, fmt.Errorf("failed to update environment, yaml code failed following checks: %+v", diagnostics)
}

propertyMap, err := input.ToPropertyMap()
if err != nil {
return nil, err
}
outputProperties, err := plugin.MarshalProperties(
propertyMap,
plugin.MarshalOptions{
KeepSecrets: true,
},
)
if err != nil {
return nil, err
}

return &pulumirpc.UpdateResponse{
Properties: outputProperties,
}, nil
}

func (st *PulumiServiceEnvironmentResource) Read(req *pulumirpc.ReadRequest) (*pulumirpc.ReadResponse, error) {
input, err := ToPulumiServiceEnvironmentInput(req.GetProperties())
if err != nil {
return nil, err
}

retrievedYaml, _, err := st.client.GetEnvironment(context.Background(), input.OrgName, input.EnvName, false)
if err != nil {
return &pulumirpc.ReadResponse{Id: "", Properties: nil}, nil
}

result := PulumiServiceEnvironmentInput{
OrgName: input.OrgName,
EnvName: input.EnvName,
Yaml: retrievedYaml,
}

propertyMap, err := result.ToPropertyMap()
if err != nil {
return nil, err
}
properties, err := plugin.MarshalProperties(
propertyMap,
plugin.MarshalOptions{
KeepSecrets: true,
},
)
if err != nil {
return nil, err
}

return &pulumirpc.ReadResponse{
Id: req.Id,
Properties: properties,
Inputs: properties,
}, nil
}

func (st *PulumiServiceEnvironmentResource) Name() string {
return "pulumiservice:index:Environment"
}

func (st *PulumiServiceEnvironmentResource) Configure(_ PulumiServiceConfig) {
}
Loading
Loading