Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas11 committed Jun 10, 2024
1 parent 51b3c70 commit 26a5278
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
39 changes: 25 additions & 14 deletions examples/assets/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ func provider() p.Provider {
})
}

// TODO,tkappler just prints failures for now, needs to actually fail the test later
func assertState(s HasAssetsArgs) {
// assertState asserts invariants about the state of the resource defined in consumer/Pulumi.yaml.
// Note on includesAssets: by default, the engine doesn't send assets to the provider for Delete
// requests. See https://github.com/pulumi/pulumi/pull/548.
func assertState(s HasAssetsArgs, includesAssets bool) {
failures := []string{}
add := func(msg string, obj any) {
failures = append(failures, fmt.Sprintf(msg, obj))
Expand All @@ -53,22 +55,31 @@ func assertState(s HasAssetsArgs) {
add("must specify either asset or archive for a2: %+v", s.A2)
}

if !s.A1.Asset.IsPath() {
add("a1 asset must be a path: %+v", s.A1.Asset)
if s.A1.Asset.Hash == "" {
add("a1 asset hash must be set: %+v", s.A1.Asset)
}
if !strings.HasSuffix(s.A1.Asset.Path, "file.txt") {
add("a1 path must have file.txt: %v", s.A1.Asset.Path)
if s.A2.Archive.Hash == "" {
add("a2 archive hash must be set: %+v", s.A2.Archive)
}

if !s.A2.Archive.IsPath() {
add("a2 archive must be a path: %+v", s.A2.Archive)
}
if !strings.HasSuffix(s.A2.Archive.Path, "file.txt.zip") {
add("a2 path must have file.txt.zip: %v", s.A2.Archive.Path)
if includesAssets {
if !s.A1.Asset.IsPath() {
add("a1 asset must be a path: %+v", s.A1.Asset)
}
if !strings.HasSuffix(s.A1.Asset.Path, "file.txt") {
add("a1 path must have file.txt: %v", s.A1.Asset.Path)
}

if !s.A2.Archive.IsPath() {
add("a2 archive must be a path: %+v", s.A2.Archive)
}
if !strings.HasSuffix(s.A2.Archive.Path, "file.txt.zip") {
add("a2 path must have file.txt.zip: %v", s.A2.Archive.Path)
}
}

if len(failures) > 0 {
fmt.Printf("INVALID state:\n %s", strings.Join(failures, "\n "))
panic(fmt.Sprintf("INVALID STATE:\n %s", strings.Join(failures, "\n ")))
}
}

Expand All @@ -78,11 +89,11 @@ func (*HasAssets) Create(ctx context.Context, name string, input HasAssetsArgs,
}

output = input
assertState(output)
assertState(output, true)
return name, output, nil
}

func (*HasAssets) Delete(ctx context.Context, id string, state HasAssetsArgs) error {
assertState(state)
assertState(state, false)
return nil
}
25 changes: 7 additions & 18 deletions infer/internal/ende/ende.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package ende

import (
"encoding/json"
"fmt"
"reflect"

"github.com/pulumi/pulumi-go-provider/infer/types"
Expand Down Expand Up @@ -191,8 +189,9 @@ func (e *ende) walk(
if typ == nil || typ.Kind() != reflect.Struct {
return e.walkMap(v, path, elemType, alignTypes)
}
// This is a scalar value, so we can return it as is. The exception is assets and archives from Pulumi's
// AssetOrArchive union type, which we translate to types.AssetOrArchive.
// This is a scalar value, so we can return it as is. The exception is assets and archives
// from Pulumi's AssetOrArchive union type, which we translate to types.AssetOrArchive.
// See #237 for more background.
default:
if typ == reflect.TypeOf(types.AssetOrArchive{}) {
// set v to a special value/property map as a signal to Encode
Expand Down Expand Up @@ -309,13 +308,6 @@ func (e *ende) walkMap(
return resource.NewObjectProperty(result)
}

func prettyPrint(v interface{}) {
b, err := json.MarshalIndent(v, "", " ")
if err == nil {
fmt.Println(string(b))
}
}

func (e *ende) Encode(src any) (resource.PropertyMap, mapper.MappingError) {
props, err := mapper.New(&mapper.Opts{
IgnoreMissing: true,
Expand All @@ -324,10 +316,10 @@ func (e *ende) Encode(src any) (resource.PropertyMap, mapper.MappingError) {
return nil, err
}

// If we see "asset" or "archive" properties who have the magic signatures inside, we assume we have an
// AssetOrArchive and need to pull the asset or archive out of the object.
// TODO,tkappler This is not safe: a user could have defined a property that looks like an
// AssetOrArchive but isn't.
// If we see the magic signatures meaning "asset" or "archive", it's an AssetOrArchive and need
// to pull the actual, inner asset or archive out of the object and discard the outer
// AssetOrArchive. See #237 for more background.
// The literal magic signatures are from pulumi/pulumi and are not exported by the SDK.
m := resource.NewPropertyValueRepl(props,
nil, // keys are not changed
func(a any) (resource.PropertyValue, bool) {
Expand Down Expand Up @@ -382,9 +374,6 @@ func (e *ende) Encode(src any) (resource.PropertyMap, mapper.MappingError) {
s.path.Set(m, s.apply(v))
}

fmt.Println("Post Encode:")
prettyPrint(m)

return m.ObjectValue(), nil
}

Expand Down
11 changes: 0 additions & 11 deletions infer/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package infer

import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -1083,8 +1082,6 @@ func (rc *derivedResourceController[R, I, O]) Create(
}
setDeps(nil, req.Properties, m)

fmt.Println("CreateResponse")
prettyPrint(m)
return p.CreateResponse{
ID: id,
Properties: m,
Expand Down Expand Up @@ -1252,19 +1249,11 @@ func (rc *derivedResourceController[R, I, O]) Delete(ctx context.Context, req p.
return err
}

prettyPrint(olds)
return del.Delete(ctx, req.ID, olds)
}
return nil
}

func prettyPrint(v interface{}) {
b, err := json.MarshalIndent(v, "", " ")
if err == nil {
fmt.Println(string(b))
}
}

// Apply dependencies to a property map, flowing secretness and computedness from input to
// output.
type setDeps func(oldInputs, input, output resource.PropertyMap)
Expand Down

0 comments on commit 26a5278

Please sign in to comment.