Skip to content

Commit

Permalink
Improve CustomResource for Python SDK (#700)
Browse files Browse the repository at this point in the history
- Standardize arg names with the rest of the SDK
- Add typing
- Add .get method
- Add test
  • Loading branch information
lblackstone committed Aug 23, 2019
1 parent e8c9372 commit 5a5747e
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- Document await logic in the SDKs. (https://github.com/pulumi/pulumi-kubernetes/pull/711).
- Document await timeouts and how to override. (https://github.com/pulumi/pulumi-kubernetes/pull/718).
- Improve CustomResource for Python SDK. (https://github.com/pulumi/pulumi-kubernetes/pull/700).
- Don't populate `.status` in input types (https://github.com/pulumi/pulumi-kubernetes/pull/635).

## 1.0.0-beta.1 (August 13, 2019)
Expand Down
66 changes: 49 additions & 17 deletions pkg/gen/python-templates/CustomResource.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# *** WARNING: this file was generated by the Pulumi Kubernetes codegen tool. ***
# *** Do not edit by hand unless you're certain you know what you are doing! ***

import warnings

import pulumi
import pulumi.runtime
from pulumi import ResourceOptions

from .. import tables, version

Expand All @@ -15,37 +18,66 @@ class CustomResource(pulumi.CustomResource):
`ServiceMonitor` resource definition as an argument.
"""

def __init__(self, __name__, __opts__=None, api_version=None, kind=None, metadata=None, spec=None):
if not __name__:
def __init__(self, resource_name, api_version, kind, spec=None, metadata=None, opts=None,
__name__=None, __opts__=None):
"""
:param str resource_name: _Unique_ name used to register this resource with Pulumi.
:param str api_version: The API version of the apiExtensions.CustomResource we
wish to select, as specified by the CustomResourceDefinition that defines it on the
API server.
:param str kind: The kind of the apiextensions.CustomResource we wish to select,
as specified by the CustomResourceDefinition that defines it on the API server.
:param pulumi.Input[Any] spec: Specification of the CustomResource.
:param Optional[pulumi.Input[Any]] metadata: Standard object metadata; More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata.
:param Optional[pulumi.ResourceOptions] opts: A bag of options that control this
resource's behavior.
"""
if __name__ is not None:
warnings.warn("explicit use of __name__ is deprecated", DeprecationWarning)
resource_name = __name__
if __opts__ is not None:
warnings.warn("explicit use of __opts__ is deprecated, use 'opts' instead", DeprecationWarning)
opts = __opts__
if not resource_name:
raise TypeError('Missing resource name argument (for URN creation)')
if not isinstance(__name__, str):
if not isinstance(resource_name, str):
raise TypeError('Expected resource name to be a string')
if __opts__ and not isinstance(__opts__, pulumi.ResourceOptions):
if opts and not isinstance(opts, pulumi.ResourceOptions):
raise TypeError('Expected resource options to be a ResourceOptions instance')

__props__ = dict()

if not api_version:
raise TypeError('Missing required property apiVersion')
__props__['apiVersion'] = api_version
if not kind:
raise TypeError('Missing required property kind')
__props__['kind'] = kind
if spec is None:
raise TypeError('Missing required property spec')
__props__['spec'] = spec
__props__['metadata'] = metadata

if __opts__ is None:
__opts__ = pulumi.ResourceOptions()
if __opts__.version is None:
__opts__.version = version.get_version()
opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(version=version.get_version()))

super(CustomResource, self).__init__(
"kubernetes:%s:%s" % (api_version, kind),
__name__,
f"kubernetes:{api_version}:{kind}",
resource_name,
__props__,
__opts__)
opts)

@staticmethod
def get(resource_name, api_version, kind, id, opts=None):
"""
:param str resource_name: _Unique_ name used to register this resource with Pulumi.
:param str api_version: The API version of the apiExtensions.CustomResource we
wish to select, as specified by the CustomResourceDefinition that defines it on the
API server.
:param str kind: The kind of the apiextensions.CustomResource we wish to select,
as specified by the CustomResourceDefinition that defines it on the API server.
:param pulumi.Input[str] id: An ID for the Kubernetes resource to retrieve.
Takes the form <namespace>/<name> or <name>.
:param Optional[pulumi.ResourceOptions] opts: A bag of options that control this
resource's behavior.
"""

opts = ResourceOptions.merge(opts, pulumi.ResourceOptions(id=id))
return CustomResource(resource_name=resource_name, api_version=api_version, kind=kind, opts=opts)

def translate_output_property(self, prop: str) -> str:
return tables._CASING_FORWARD_TABLE.get(prop) or prop
Expand Down
2 changes: 1 addition & 1 deletion pkg/gen/python-templates/yaml.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,5 @@ def _parse_yaml_object(
{{/Groups}}
return [identifier.apply(
lambda x: (f"{gvk}:{x}",
CustomResource(f"{x}", opts, api_version, kind, metadata, spec)))]
CustomResource(f"{x}", api_version, kind, spec, metadata, opts)))]

66 changes: 49 additions & 17 deletions sdk/python/pulumi_kubernetes/apiextensions/CustomResource.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# *** WARNING: this file was generated by the Pulumi Kubernetes codegen tool. ***
# *** Do not edit by hand unless you're certain you know what you are doing! ***

import warnings

import pulumi
import pulumi.runtime
from pulumi import ResourceOptions

from .. import tables, version

Expand All @@ -15,37 +18,66 @@ class CustomResource(pulumi.CustomResource):
`ServiceMonitor` resource definition as an argument.
"""

def __init__(self, __name__, __opts__=None, api_version=None, kind=None, metadata=None, spec=None):
if not __name__:
def __init__(self, resource_name, api_version, kind, spec=None, metadata=None, opts=None,
__name__=None, __opts__=None):
"""
:param str resource_name: _Unique_ name used to register this resource with Pulumi.
:param str api_version: The API version of the apiExtensions.CustomResource we
wish to select, as specified by the CustomResourceDefinition that defines it on the
API server.
:param str kind: The kind of the apiextensions.CustomResource we wish to select,
as specified by the CustomResourceDefinition that defines it on the API server.
:param pulumi.Input[Any] spec: Specification of the CustomResource.
:param Optional[pulumi.Input[Any]] metadata: Standard object metadata; More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata.
:param Optional[pulumi.ResourceOptions] opts: A bag of options that control this
resource's behavior.
"""
if __name__ is not None:
warnings.warn("explicit use of __name__ is deprecated", DeprecationWarning)
resource_name = __name__
if __opts__ is not None:
warnings.warn("explicit use of __opts__ is deprecated, use 'opts' instead", DeprecationWarning)
opts = __opts__
if not resource_name:
raise TypeError('Missing resource name argument (for URN creation)')
if not isinstance(__name__, str):
if not isinstance(resource_name, str):
raise TypeError('Expected resource name to be a string')
if __opts__ and not isinstance(__opts__, pulumi.ResourceOptions):
if opts and not isinstance(opts, pulumi.ResourceOptions):
raise TypeError('Expected resource options to be a ResourceOptions instance')

__props__ = dict()

if not api_version:
raise TypeError('Missing required property apiVersion')
__props__['apiVersion'] = api_version
if not kind:
raise TypeError('Missing required property kind')
__props__['kind'] = kind
if spec is None:
raise TypeError('Missing required property spec')
__props__['spec'] = spec
__props__['metadata'] = metadata

if __opts__ is None:
__opts__ = pulumi.ResourceOptions()
if __opts__.version is None:
__opts__.version = version.get_version()
opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(version=version.get_version()))

super(CustomResource, self).__init__(
"kubernetes:%s:%s" % (api_version, kind),
__name__,
f"kubernetes:{api_version}:{kind}",
resource_name,
__props__,
__opts__)
opts)

@staticmethod
def get(resource_name, api_version, kind, id, opts=None):
"""
:param str resource_name: _Unique_ name used to register this resource with Pulumi.
:param str api_version: The API version of the apiExtensions.CustomResource we
wish to select, as specified by the CustomResourceDefinition that defines it on the
API server.
:param str kind: The kind of the apiextensions.CustomResource we wish to select,
as specified by the CustomResourceDefinition that defines it on the API server.
:param pulumi.Input[str] id: An ID for the Kubernetes resource to retrieve.
Takes the form <namespace>/<name> or <name>.
:param Optional[pulumi.ResourceOptions] opts: A bag of options that control this
resource's behavior.
"""

opts = ResourceOptions.merge(opts, pulumi.ResourceOptions(id=id))
return CustomResource(resource_name=resource_name, api_version=api_version, kind=kind, opts=opts)

def translate_output_property(self, prop: str) -> str:
return tables._CASING_FORWARD_TABLE.get(prop) or prop
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/pulumi_kubernetes/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,5 +904,5 @@ def _parse_yaml_object(
VolumeAttachmentList(f"{x}", opts, **obj)))]
return [identifier.apply(
lambda x: (f"{gvk}:{x}",
CustomResource(f"{x}", opts, api_version, kind, metadata, spec)))]
CustomResource(f"{x}", api_version, kind, spec, metadata, opts)))]

31 changes: 31 additions & 0 deletions tests/examples/python/get/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pulumi
from pulumi_kubernetes.apiextensions.CustomResource import CustomResource
from pulumi_kubernetes.apiextensions.v1beta1.CustomResourceDefinition import CustomResourceDefinition
from pulumi_kubernetes.core.v1 import Service
from pulumi_kubernetes.core.v1.Namespace import Namespace

service = Service.get("kube-api", "kubernetes")

crd = CustomResourceDefinition(
resource_name="foo",
metadata={"name": "gettests.python.test"},
spec={
"group": "python.test",
"version": "v1",
"scope": "Namespaced",
"names": {
"plural": "gettests",
"singular": "gettest",
"kind": "GetTest",
}
})

ns = Namespace("ns")

cr = CustomResource(
resource_name="foo",
api_version="python.test/v1",
kind="GetTest",
metadata={"namespace": ns.metadata["name"]},
spec={"foo": "bar"},
opts=pulumi.ResourceOptions(depends_on=[crd]))

cr_id = pulumi.Output.concat(ns.metadata["name"], '/', cr.metadata["name"])
cr_get = CustomResource.get(resource_name="bar", api_version="python.test/v1", kind="GetTest", id=cr_id)
2 changes: 1 addition & 1 deletion tests/examples/python/get/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pulumi>=0.17.1,<2.0.0
pulumi==1.0.0b3
3 changes: 2 additions & 1 deletion tests/examples/python/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func TestGet(t *testing.T) {
t.FailNow()
}
options := baseOptions.With(integration.ProgramTestOptions{
Dir: filepath.Join(cwd, "get"),
ExpectRefreshChanges: true, // CRD changes on refresh
Dir: filepath.Join(cwd, "get"),
})
integration.ProgramTest(t, &options)
}
Expand Down

0 comments on commit 5a5747e

Please sign in to comment.