From 27f14f3144ba733507847a942e2faf8cbaad231a 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. --- CHANGELOG.md | 5 ++++ pkg/gen/python-templates/helm/v2/helm.py | 27 ++++++++++++++++++-- pkg/gen/python-templates/yaml.py.mustache | 27 ++++++++++++++++++-- sdk/python/pulumi_kubernetes/helm/v2/helm.py | 27 ++++++++++++++++++-- sdk/python/pulumi_kubernetes/yaml.py | 27 ++++++++++++++++++-- 5 files changed, 105 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92cdf19f86..c59c13d70a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,11 @@ ### Bug fixes +- Fix name collisions in the Charts/YAML Python packages + (https://github.com/pulumi/pulumi-kubernetes/pull/771). +- Implement `{ConfigFile, ConfigGroup, Chart}#get_resource` + (https://github.com/pulumi/pulumi-kubernetes/pull/771). + ## 1.0.0-rc.1 (August 28, 2019) ### Supported Kubernetes versions diff --git a/pkg/gen/python-templates/helm/v2/helm.py b/pkg/gen/python-templates/helm/v2/helm.py index 9d03c50ab4..38e4f3a0d0 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. @@ -440,5 +445,23 @@ 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, group_version_kind, name, namespace=None) -> pulumi.Output[pulumi.CustomResource]: + """ + get_resource returns a resource defined by a built-in Kubernetes group/version/kind and + name. For example: `get_resource("apps/v1/Deployment", "nginx")` + + :param str group_version_kind: Group/Version/Kind of the resource, e.g., `apps/v1/Deployment` + :param str name: Name of the resource to retrieve + :param str namespace: Optional namespace of the resource to retrieve + """ + + # `id` will either be `${name}` or `${namespace}/${name}`. + id = name + if namespace != None: + id = pulumi.Output.concat(namespace, '/', name) + + resource_id = id.apply(lambda x: f'{group_version_kind}:{x}') + return resource_id.apply(lambda x: self.resources[x]) diff --git a/pkg/gen/python-templates/yaml.py.mustache b/pkg/gen/python-templates/yaml.py.mustache index b5ef0296b1..1fb6801f05 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. @@ -57,8 +63,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 @@ -66,6 +72,23 @@ class ConfigFile(pulumi.ComponentResource): def translate_input_property(self, prop: str) -> str: return tables._CASING_BACKWARD_TABLE.get(prop) or prop + def get_resource(self, group_version_kind, name, namespace=None) -> pulumi.Output[pulumi.CustomResource]: + """ + get_resource returns a resource defined by a built-in Kubernetes group/version/kind and + name. For example: `get_resource("apps/v1/Deployment", "nginx")` + + :param str group_version_kind: Group/Version/Kind of the resource, e.g., `apps/v1/Deployment` + :param str name: Name of the resource to retrieve + :param str namespace: Optional namespace of the resource to retrieve + """ + + # `id` will either be `${name}` or `${namespace}/${name}`. + id = name + if namespace != None: + id = pulumi.Output.concat(namespace, '/', name) + + resource_id = id.apply(lambda x: f'{group_version_kind}:{x}') + return resource_id.apply(lambda x: self.resources[x]) def _read_url(url: str) -> str: response = requests.get(url) diff --git a/sdk/python/pulumi_kubernetes/helm/v2/helm.py b/sdk/python/pulumi_kubernetes/helm/v2/helm.py index 9d03c50ab4..38e4f3a0d0 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. @@ -440,5 +445,23 @@ 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, group_version_kind, name, namespace=None) -> pulumi.Output[pulumi.CustomResource]: + """ + get_resource returns a resource defined by a built-in Kubernetes group/version/kind and + name. For example: `get_resource("apps/v1/Deployment", "nginx")` + + :param str group_version_kind: Group/Version/Kind of the resource, e.g., `apps/v1/Deployment` + :param str name: Name of the resource to retrieve + :param str namespace: Optional namespace of the resource to retrieve + """ + + # `id` will either be `${name}` or `${namespace}/${name}`. + id = name + if namespace != None: + id = pulumi.Output.concat(namespace, '/', name) + + resource_id = id.apply(lambda x: f'{group_version_kind}:{x}') + return resource_id.apply(lambda x: self.resources[x]) diff --git a/sdk/python/pulumi_kubernetes/yaml.py b/sdk/python/pulumi_kubernetes/yaml.py index 6c06cad58f..ffd00dbc47 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. @@ -57,8 +63,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 @@ -66,6 +72,23 @@ def translate_output_property(self, prop: str) -> str: def translate_input_property(self, prop: str) -> str: return tables._CASING_BACKWARD_TABLE.get(prop) or prop + def get_resource(self, group_version_kind, name, namespace=None) -> pulumi.Output[pulumi.CustomResource]: + """ + get_resource returns a resource defined by a built-in Kubernetes group/version/kind and + name. For example: `get_resource("apps/v1/Deployment", "nginx")` + + :param str group_version_kind: Group/Version/Kind of the resource, e.g., `apps/v1/Deployment` + :param str name: Name of the resource to retrieve + :param str namespace: Optional namespace of the resource to retrieve + """ + + # `id` will either be `${name}` or `${namespace}/${name}`. + id = name + if namespace != None: + id = pulumi.Output.concat(namespace, '/', name) + + resource_id = id.apply(lambda x: f'{group_version_kind}:{x}') + return resource_id.apply(lambda x: self.resources[x]) def _read_url(url: str) -> str: response = requests.get(url)