generated from pulumi/pulumi-provider-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
When I go to see the environment in console it has a random blurb
zbhrh
and the same in Random's resource properties.There was a problem hiding this comment.
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 theYaml
output of one environment to theYaml
input of another.There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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:
It can just be:
There was a problem hiding this comment.
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