Added support for custom naming of dynamic provider resource (#7633)

Now there is not possible to change a name of dynamic provider resource without copying a code of the `pulumi.sdk.python.lib.pulumi.dynamic.dynamic.Resource` and changing the hard-coded name `"pulumi-python:dynamic:Resource"`. I successfully uses this proposal to make it possible.

Usage:
```python
class CustomResource(
    Resource, name="my-custom-provider:CustomResource"
):
   ...
```

Co-authored-by: Pat Gavlin <pgavlin@gmail.com>
This commit is contained in:
Jan Češpivo 2021-08-17 23:15:53 +02:00 committed by GitHub
parent 2223c6b8b9
commit e67334db1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 2 deletions

View file

@ -1,5 +1,8 @@
### Improvements
- [sdk/python] - Add support for custom naming of dynamic provider resource.
[#7633](https://github.com/pulumi/pulumi/pull/7633)
### Bug Fixes
- [sdk/dotnet] - Fix an exception when passing an unknown `Output` to

View file

@ -15,7 +15,7 @@
import asyncio
import base64
import pickle
from typing import Any, Optional, List, TYPE_CHECKING, no_type_check, cast
from typing import Any, ClassVar, Optional, List, TYPE_CHECKING, no_type_check, cast
import dill
from .. import CustomResource, ResourceOptions
@ -242,6 +242,13 @@ class Resource(CustomResource):
Resource represents a Pulumi Resource that incorporates an inline implementation of the Resource's CRUD operations.
"""
_resource_type_name: ClassVar[str]
def __init_subclass__(cls, module: str = '', name: str = 'Resource'):
if module:
module = f'/{module}'
cls._resource_type_name = f'dynamic{module}:{name}'
def __init__(self,
provider: ResourceProvider,
name: str,
@ -261,4 +268,4 @@ class Resource(CustomResource):
props = cast(dict, props)
props[PROVIDER_KEY] = serialize_provider(provider)
super().__init__("pulumi-python:dynamic:Resource", name, props, opts)
super().__init__(f"pulumi-python:{self._resource_type_name}", name, props, opts)

View file

@ -0,0 +1,3 @@
name: dynamic_resource_type_name_py
description: A simple Python program that uses dynamic providers with custom resource type name.
runtime: python

View file

@ -0,0 +1,21 @@
# Copyright 2016-2021, Pulumi Corporation. All rights reserved.
from pulumi import export
from pulumi.dynamic import CreateResult, Resource, ResourceProvider
class CustomResource(
Resource, module="custom-provider", name="CustomResource"
):
def __init__(self, name, opts=None):
super().__init__(DummyResourceProvider(), name, {}, opts)
class DummyResourceProvider(ResourceProvider):
def create(self, props):
return CreateResult("resource-id", {})
resource = CustomResource("resource-name")
export("urn", resource.urn)

View file

@ -416,6 +416,24 @@ func TestDynamicPythonNonMain(t *testing.T) {
})
}
// Tests custom resource type name of dynamic provider in Python.
func TestCustomResourceTypeNameDynamicPython(t *testing.T) {
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: filepath.Join("dynamic", "python-resource-type-name"),
Dependencies: []string{
filepath.Join("..", "..", "sdk", "python", "env", "src"),
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
urnOut := stack.Outputs["urn"].(string)
urn := resource.URN(urnOut)
typ := urn.Type().String()
assert.Equal(t, "pulumi-python:dynamic/custom-provider:CustomResource", typ)
},
})
}
func TestPartialValuesPython(t *testing.T) {
if runtime.GOOS == WindowsOS {
t.Skip("Temporarily skipping test on Windows - pulumi/pulumi#3811")