Skip to content

Commit

Permalink
Fix panic when environment uses computed values (#414)
Browse files Browse the repository at this point in the history
Fixes #411

This is related to
#399
  • Loading branch information
seanyeh authored Sep 25, 2024
1 parent 2236a45 commit b011017
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
### Bug Fixes

- Fixes failing pulumi refresh on org tokens with a slash in its name [#388](https://github.com/pulumi/pulumi-pulumiservice/issues/388)
- Fix panic when using computed values in environment definition [#411](https://github.com/pulumi/pulumi-pulumiservice/issues/411)

### Miscellaneous
10 changes: 7 additions & 3 deletions provider/pkg/provider/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,14 @@ func (st *PulumiServiceEnvironmentResource) Check(req *pulumirpc.CheckRequest) (
}
}

yamlBytes, err := getBytesFromAsset(inputMap["yaml"].AssetValue())
if err != nil {
return nil, err
yamlBytes := []byte{}
if !inputMap["yaml"].IsComputed() {
yamlBytes, err = getBytesFromAsset(inputMap["yaml"].AssetValue())
if err != nil {
return nil, err
}
}

stringYaml := string(yamlBytes)
trimmedYaml := strings.TrimSpace(stringYaml)
inputMap["yaml"] = resource.MakeSecret(resource.NewStringProperty(trimmedYaml))
Expand Down
59 changes: 59 additions & 0 deletions provider/pkg/provider/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/pulumi/esc"
"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"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
Expand Down Expand Up @@ -169,6 +171,63 @@ func buildEscClientMock(getEnvironmentFunc getEnvironmentFunc, getEnvironmentRev
}
}

func TestEnvironmentCheck(t *testing.T) {
mockedClient := buildEscClientMock(
func(ctx context.Context, orgName string, envName string, version string, decrypt bool) (yaml []byte, etag string, revision int, err error) {
return nil, "", 0, fmt.Errorf("not found")
},
func(ctx context.Context, orgName, envName, tagName string) (*client.EnvironmentRevisionTag, error) {
return nil, nil
},
)
provider := PulumiServiceEnvironmentResource{
client: mockedClient,
}

envDef := `values:
foo: bar
`
propertyMap := resource.PropertyMap{}
propertyMap["organization"] = resource.NewPropertyValue("org")
propertyMap["project"] = resource.NewPropertyValue("project")
propertyMap["name"] = resource.NewPropertyValue("env")
propertyMap["yaml"] = resource.NewAssetProperty(&asset.Asset{Text: envDef})

t.Run("Check", func(t *testing.T) {
properties, _ := plugin.MarshalProperties(
propertyMap,
plugin.MarshalOptions{
KeepUnknowns: true,
SkipNulls: true,
},
)
req := pulumirpc.CheckRequest{
News: properties,
}

_, err := provider.Check(&req)
assert.NoError(t, err)
})

t.Run("Check when yaml contains computed resource", func(t *testing.T) {
propertyMap["yaml"] = resource.NewComputedProperty(resource.Computed{Element: resource.NewStringProperty("")})

properties, _ := plugin.MarshalProperties(
propertyMap,
plugin.MarshalOptions{
KeepUnknowns: true,
SkipNulls: true,
},
)
req := pulumirpc.CheckRequest{
News: properties,
}

_, err := provider.Check(&req)
assert.NoError(t, err)
})
}

func TestEnvironment(t *testing.T) {
t.Run("Read when the resource is not found", func(t *testing.T) {
mockedClient := buildEscClientMock(
Expand Down

0 comments on commit b011017

Please sign in to comment.