pulumi/sdk/go/common/resource
Justin Van Patten 64a2288883 [engine] Fix aliasing children
There is an issue with how the engine computes the aliases when the
resource is a child and doesn't have `Parent` set on the alias spec
(and the parent doesn't have any aliases).

```python
class FooResource(pulumi.ComponentResource):
    def __init__(self, name, opts=None):
        super().__init__("my:module:FooResource", name, None, opts)

class ComponentResource(pulumi.ComponentResource):
    def __init__(self, name, opts=None):
        super().__init__("my:module:ComponentResource", name, None, opts)
        FooResource("childrenamed", pulumi.ResourceOptions(
            parent=self,
            aliases=[pulumi.Alias(name="child")]
        ))
```

In the example above, `ComponentResource` has a child `FooResource`
which was renamed from `child` to `childrenamed`.

The engine does not compute the correct alias:

```
expected: urn:pulumi:stack::project::my:module:ComponentResource$my:module:FooResource::child
  actual: urn:pulumi:stack::project::my:module:FooResource::child
```

The problem is due to:

117955ce14/pkg/resource/deploy/step_generator.go (L370-L382)

... and:

117955ce14/sdk/go/common/resource/alias.go (L24-L26)

Because the alias spec doesn't have `parent` specified, the parent type
is not being included the computed alias URN.

Existing tests such as https://github.com/pulumi/pulumi/tree/master/tests/integration/aliases/python/rename_component_and_child
didn't catch the problem because the alias specifies both the `name` and
`parent`:

117955ce14/tests/integration/aliases/python/rename_component_and_child/step2/__main__.py (L15)

In this case, specifying `parent` on the alias shouldn't be necessary.
However, even after removing `parent` from the alias spec, the test
still succeeds because the parent itself has an alias:

117955ce14/tests/integration/aliases/python/rename_component_and_child/step2/__main__.py (L18)

... and parent aliases are inherited as part of a child's aliases, so we
still get an alias that works from the inheritance.

If we change the test to make no changes to the parent such that it
doesn't have any aliases, then we get the failure as we'd expect.

A similar problem will happen when retyping a child.

**Fix**

The fix involves using the child's parent in the calculated alias URN
when `Parent` isn't specified for the alias.

As part of this, we need to properly handled `NoParent` because right
now the engine is not correctly using it. The struct representing an
alias in the engine does not have a `NoParent` field:

117955ce14/sdk/go/common/resource/alias.go (L8-L15)

And therefore does not copy it over in the gRPC request:

117955ce14/pkg/resource/deploy/source_eval.go (L1082-L1088)

Instead, the `Alias` struct has an incorrect `NoParent` method which
returns `true` if the `Parent` field has a value of `""`:

117955ce14/sdk/go/common/resource/alias.go (L24-L26)
2023-06-14 05:19:17 -07:00
..
config all: Drop //nolint:goconst 2023-03-09 11:15:21 -08:00
plugin Handle installing plugins without versions in the defaultHost 2023-06-12 12:22:08 +01:00
testing deps: Upgrade to pgregory.net/rapid v0.5 2023-02-16 12:04:07 -08:00
alias.go [engine] Fix aliasing children 2023-06-14 05:19:17 -07:00
alias_test.go feat(engine): Adds structured alias support to the engine 2022-10-11 17:56:32 -04:00
asset.go Remove outdated comments 2023-04-04 21:26:28 -07:00
asset_test.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
custom_timeouts.go move pkg/resource -> sdk/go/common/resource, but leave nested resource packages 2020-03-18 13:36:19 -07:00
errors.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
mapper_test.go Allow decoding *asset and *archive values 2022-10-17 13:28:13 -07:00
properties.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
properties_diff.go all: Fix revive issues 2023-03-21 08:55:11 -07:00
properties_diff_test.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
properties_path.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
properties_path_test.go Fix concurrent access to PropertyValue in TestPropertyPath (#9553) 2022-05-05 14:38:17 +01:00
properties_test.go ci: radical idea - what if slow tests & no stdout makes GH consider runner dead? 2022-03-06 14:52:13 -08:00
resource_goal.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
resource_id.go sdk/go: Prefer contract.Assertf over Assert 2023-02-15 10:22:43 -08:00
resource_id_test.go all: Drop //nolint:goconst 2023-03-09 11:15:21 -08:00
resource_operation.go move pkg/resource -> sdk/go/common/resource, but leave nested resource packages 2020-03-18 13:36:19 -07:00
resource_state.go This commit adds the `Created` and `Modified` timestamps to pulumi state that are optional. 2023-03-27 09:28:26 -07:00
stack.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
status.go move pkg/resource -> sdk/go/common/resource, but leave nested resource packages 2020-03-18 13:36:19 -07:00
urn.go ci: gofmt 1.18+ clean 2022-09-21 09:48:39 -07:00
urn_test.go ci: radical idea - what if slow tests & no stdout makes GH consider runner dead? 2022-03-06 14:52:13 -08:00