From 87ab5f98f4a4e6d98f41b38fd7382f79c8cca9e8 Mon Sep 17 00:00:00 2001 From: Alex Clemmer Date: Wed, 28 Aug 2019 16:04:30 -0700 Subject: [PATCH] Implement {ConfigFile, ConfigGroup, Chart}#get_resource Fixes #763. --- pkg/gen/python-templates/helm/v2/helm.py | 18 ++++++++++++++++-- pkg/gen/python-templates/yaml.py.mustache | 11 +++++++++-- sdk/python/pulumi_kubernetes/helm/v2/helm.py | 18 ++++++++++++++++-- sdk/python/pulumi_kubernetes/yaml.py | 11 +++++++++-- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/pkg/gen/python-templates/helm/v2/helm.py b/pkg/gen/python-templates/helm/v2/helm.py index 9d03c50ab4..0e132e08dc 100644 --- a/pkg/gen/python-templates/helm/v2/helm.py +++ b/pkg/gen/python-templates/helm/v2/helm.py @@ -401,6 +401,11 @@ class Chart(pulumi.ComponentResource): none of Tiller's server-side validity testing is executed. """ + resources: pulumi.Output[dict] + """ + Kubernetes resources contained in this Chart. + """ + def __init__(self, release_name, config, opts=None): """ Create an instance of the specified Helm chart. @@ -420,6 +425,7 @@ def __init__(self, release_name, config, opts=None): raise TypeError('Expected resource options to be a ResourceOptions instance') __props__ = dict() + __props__['resources'] = None if config.resource_prefix: release_name = f"{config.resource_prefix}-{release_name}" @@ -440,5 +446,13 @@ def __init__(self, release_name, config, opts=None): # Note: Unlike NodeJS, Python requires that we "pull" on our futures in order to get them scheduled for # execution. In order to do this, we leverage the engine's RegisterResourceOutputs to wait for the # resolution of all resources that this Helm chart created. - resources = all_config.apply(_parse_chart) - self.register_outputs({"output": resources}) + self.resources = all_config.apply(_parse_chart) + self.register_outputs({"resources": self.resources}) + + def get_resource(self, groupVersionKind: str, namespace=None, name=None) -> pulumi.Output[pulumi.CustomResource]: + # `id` will either be `${name}` or `${namespace}/${name}`. + id = name + if namespace != None: + id = f'{namespace}/{name}' + + return self.resources.apply(lambda rs: rs[f'{groupVersionKind}:{id}']) diff --git a/pkg/gen/python-templates/yaml.py.mustache b/pkg/gen/python-templates/yaml.py.mustache index b5ef0296b1..f0a967bdbf 100644 --- a/pkg/gen/python-templates/yaml.py.mustache +++ b/pkg/gen/python-templates/yaml.py.mustache @@ -20,6 +20,12 @@ class ConfigFile(pulumi.ComponentResource): ConfigFile creates a set of Kubernetes resources from a Kubernetes YAML file. """ + resources: pulumi.Output[dict] + """ + Kubernetes resources contained in this ConfigFile. + """ + + def __init__(self, name, file_id, opts=None, transformations=None, resource_prefix=None): """ :param str name: A name for a resource. @@ -38,6 +44,7 @@ class ConfigFile(pulumi.ComponentResource): raise TypeError('Expected resource options to be a ResourceOptions instance') __props__ = dict() + __props__['resources'] = None if resource_prefix: name = f"{resource_prefix}-{name}" @@ -57,8 +64,8 @@ class ConfigFile(pulumi.ComponentResource): # Note: Unlike NodeJS, Python requires that we "pull" on our futures in order to get them scheduled for # execution. In order to do this, we leverage the engine's RegisterResourceOutputs to wait for the # resolution of all resources that this YAML document created. - output = _parse_yaml_document(yaml.safe_load_all(text), opts, transformations, resource_prefix) - self.register_outputs({"output": output}) + self.resources = _parse_yaml_document(yaml.safe_load_all(text), opts, transformations, resource_prefix) + self.register_outputs({"resources": self.resources}) def translate_output_property(self, prop: str) -> str: return tables._CASING_FORWARD_TABLE.get(prop) or prop diff --git a/sdk/python/pulumi_kubernetes/helm/v2/helm.py b/sdk/python/pulumi_kubernetes/helm/v2/helm.py index 9d03c50ab4..0e132e08dc 100644 --- a/sdk/python/pulumi_kubernetes/helm/v2/helm.py +++ b/sdk/python/pulumi_kubernetes/helm/v2/helm.py @@ -401,6 +401,11 @@ class Chart(pulumi.ComponentResource): none of Tiller's server-side validity testing is executed. """ + resources: pulumi.Output[dict] + """ + Kubernetes resources contained in this Chart. + """ + def __init__(self, release_name, config, opts=None): """ Create an instance of the specified Helm chart. @@ -420,6 +425,7 @@ def __init__(self, release_name, config, opts=None): raise TypeError('Expected resource options to be a ResourceOptions instance') __props__ = dict() + __props__['resources'] = None if config.resource_prefix: release_name = f"{config.resource_prefix}-{release_name}" @@ -440,5 +446,13 @@ def __init__(self, release_name, config, opts=None): # Note: Unlike NodeJS, Python requires that we "pull" on our futures in order to get them scheduled for # execution. In order to do this, we leverage the engine's RegisterResourceOutputs to wait for the # resolution of all resources that this Helm chart created. - resources = all_config.apply(_parse_chart) - self.register_outputs({"output": resources}) + self.resources = all_config.apply(_parse_chart) + self.register_outputs({"resources": self.resources}) + + def get_resource(self, groupVersionKind: str, namespace=None, name=None) -> pulumi.Output[pulumi.CustomResource]: + # `id` will either be `${name}` or `${namespace}/${name}`. + id = name + if namespace != None: + id = f'{namespace}/{name}' + + return self.resources.apply(lambda rs: rs[f'{groupVersionKind}:{id}']) diff --git a/sdk/python/pulumi_kubernetes/yaml.py b/sdk/python/pulumi_kubernetes/yaml.py index 6c06cad58f..856af1cd54 100644 --- a/sdk/python/pulumi_kubernetes/yaml.py +++ b/sdk/python/pulumi_kubernetes/yaml.py @@ -20,6 +20,12 @@ class ConfigFile(pulumi.ComponentResource): ConfigFile creates a set of Kubernetes resources from a Kubernetes YAML file. """ + resources: pulumi.Output[dict] + """ + Kubernetes resources contained in this ConfigFile. + """ + + def __init__(self, name, file_id, opts=None, transformations=None, resource_prefix=None): """ :param str name: A name for a resource. @@ -38,6 +44,7 @@ def __init__(self, name, file_id, opts=None, transformations=None, resource_pref raise TypeError('Expected resource options to be a ResourceOptions instance') __props__ = dict() + __props__['resources'] = None if resource_prefix: name = f"{resource_prefix}-{name}" @@ -57,8 +64,8 @@ def __init__(self, name, file_id, opts=None, transformations=None, resource_pref # Note: Unlike NodeJS, Python requires that we "pull" on our futures in order to get them scheduled for # execution. In order to do this, we leverage the engine's RegisterResourceOutputs to wait for the # resolution of all resources that this YAML document created. - output = _parse_yaml_document(yaml.safe_load_all(text), opts, transformations, resource_prefix) - self.register_outputs({"output": output}) + self.resources = _parse_yaml_document(yaml.safe_load_all(text), opts, transformations, resource_prefix) + self.register_outputs({"resources": self.resources}) def translate_output_property(self, prop: str) -> str: return tables._CASING_FORWARD_TABLE.get(prop) or prop