Skip to content

Commit

Permalink
Fix parsing provider config defaultTags (#1393)
Browse files Browse the repository at this point in the history
Fix the provider to parse the format as defined by the schema.

Keep a fallback in place to allow it parsing the default tags as a
simple map for backward compatibility.
  • Loading branch information
danielrbradley authored Mar 11, 2024
1 parent 6d802c5 commit dce8812
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
8 changes: 2 additions & 6 deletions examples/examples_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ func TestWriteOnlyGo(t *testing.T) {
With(integration.ProgramTestOptions{
ExpectRefreshChanges: false,
Dir: filepath.Join(getCwd(t), "write-only-go"),
Config: map[string]string{
"aws-native:defaultTags": `{
"defaultTag": "defaultTagValue"
}`,
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
// We should see both the default tag and the local tag in the log group tags.
// If the default tag is missing, it's because there was a missing output from the provider and the SDK filled in the value with the original input.
// If the default tag is missing, it's because there was a missing output from the provider
// because write-only properties aren't returned in the read result and the SDK filled in the value with the original input.
tags := stack.Outputs["tags"].(map[string]interface{})
assert.Equal(t, map[string]interface{}{
"defaultTag": "defaultTagValue",
Expand Down
4 changes: 3 additions & 1 deletion examples/examples_py_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func TestDefaultTagsPython(t *testing.T) {
Dir: filepath.Join(getCwd(t), "default-tags-py"),
Config: map[string]string{
"aws-native:defaultTags": `{
"defaultTag": "defaultTagValue"
"tags": {
"defaultTag": "defaultTagValue"
}
}`,
},
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
Expand Down
4 changes: 2 additions & 2 deletions examples/write-only-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/pulumi/pulumi-aws-native/examples/simple-go
module github.com/pulumi/pulumi-aws-native/examples/write-only-go

go 1.21

Expand Down Expand Up @@ -64,6 +64,7 @@ require (
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/skeema/knownhosts v1.2.1 // 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
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
Expand All @@ -88,5 +89,4 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/frand v1.4.2 // indirect
pgregory.net/rapid v0.6.1 // indirect
)
6 changes: 4 additions & 2 deletions examples/write-only-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasO
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
Expand Down Expand Up @@ -530,6 +532,6 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
lukechampine.com/frand v1.4.2 h1:RzFIpOvkMXuPMBb9maa4ND4wjBn71E1Jpf8BzJHMaVw=
lukechampine.com/frand v1.4.2/go.mod h1:4S/TM2ZgrKejMcKMbeLjISpJMO+/eZ1zu3vYX9dtj3s=
pgregory.net/rapid v0.6.1 h1:4eyrDxyht86tT4Ztm+kvlyNBLIk071gR+ZQdhphc9dQ=
pgregory.net/rapid v0.6.1/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
pgregory.net/rapid v0.5.5 h1:jkgx1TjbQPD/feRoK+S/mXw9e1uj6WilpHrXJowi6oA=
pgregory.net/rapid v0.5.5/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
16 changes: 15 additions & 1 deletion examples/write-only-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,33 @@
package main

import (
awsNative "github.com/pulumi/pulumi-aws-native/sdk/go/aws"
ssm "github.com/pulumi/pulumi-aws-native/sdk/go/aws/ssm"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
awsNativeConfig := config.New(ctx, "aws-native")
awsNativeProvider, err := awsNative.NewProvider(ctx, "aws-native", &awsNative.ProviderArgs{
Region: pulumi.String(awsNativeConfig.Require("region")),
DefaultTags: awsNative.ProviderDefaultTagsArgs{
Tags: pulumi.StringMap{
"defaultTag": pulumi.String("defaultTagValue"),
},
},
})
if err != nil {
return err
}
logGroup, err := ssm.NewParameter(ctx, "log-test", &ssm.ParameterArgs{
Type: ssm.ParameterTypeString,
Value: pulumi.String("test"),
Tags: pulumi.StringMap{
"localTag": pulumi.String("localTagValue"),
},
})
}, pulumi.Provider(awsNativeProvider))
if err != nil {
return err
}
Expand Down
17 changes: 13 additions & 4 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,21 @@ func (p *cfnProvider) Configure(ctx context.Context, req *pulumirpc.ConfigureReq
}

if defaultTagsJson, ok := vars["aws-native:config:defaultTags"]; ok {
var defaultTags map[string]string
type DefaultTags struct {
Tags map[string]string `json:"tags"`
}
var defaultTags DefaultTags
err := json.Unmarshal([]byte(defaultTagsJson), &defaultTags)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal 'skipCredentialsValidation' config: %w", err)
if err == nil {
p.defaultTags = defaultTags.Tags
} else {
// As a fallback, we also try to unmarshal the default tags as a simple map[string]string
// as this was originally supported when implementing defaultTags.
fallbackErr := json.Unmarshal([]byte(defaultTagsJson), &p.defaultTags)
if fallbackErr != nil {
return nil, fmt.Errorf("failed to unmarshal 'defaultTags' config: %w", err)
}
}
p.defaultTags = defaultTags
} else {
p.defaultTags = nil
}
Expand Down

0 comments on commit dce8812

Please sign in to comment.