Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic YAML support to python SDK #499

Merged
merged 6 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@

### Major changes

- Add basic YAML support to Python SDK (https://github.com/pulumi/pulumi-kubernetes/pull/499)

### Improvements

- None

### Bug fixes

- None

## 0.22.0 (March 25, 2019)

### Major changes

- Add support for Kubernetes v1.14.0 (https://github.com/pulumi/pulumi-kubernetes/pull/371)

### Improvements

- None
Expand Down
60 changes: 28 additions & 32 deletions cmd/pulumi-gen-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,41 +140,37 @@ func writeNodeJSClient(data map[string]interface{}, outdir, templateDir string)
}

func writePythonClient(data map[string]interface{}, outdir, templateDir string) {
err := gen.PythonClient(
data,
templateDir,
func(initPy string) error {
return ioutil.WriteFile(
fmt.Sprintf("%s/pulumi_kubernetes/__init__.py", outdir), []byte(initPy), 0777)
},
func(group, initPy string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/%s", outdir, group)

err := os.MkdirAll(path, 0700)
if err != nil {
return err
}
err := gen.PythonClient(data, templateDir, func(initPy string) error {
return ioutil.WriteFile(
fmt.Sprintf("%s/pulumi_kubernetes/__init__.py", outdir), []byte(initPy), 0777)
}, func(group, initPy string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/%s", outdir, group)

return ioutil.WriteFile(fmt.Sprintf("%s/__init__.py", path), []byte(initPy), 0777)
},
func(group, version, initPy string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/%s/%s", outdir, group, version)
err := os.MkdirAll(path, 0700)
if err != nil {
return err
}

err := os.MkdirAll(path, 0700)
if err != nil {
return err
}
return ioutil.WriteFile(fmt.Sprintf("%s/__init__.py", path), []byte(initPy), 0777)
}, func(group, version, initPy string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/%s/%s", outdir, group, version)

err := os.MkdirAll(path, 0700)
if err != nil {
return err
}

return ioutil.WriteFile(fmt.Sprintf("%s/__init__.py", path), []byte(initPy), 0777)
},
func(group, version, kind, kindPy string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/%s/%s/%s.py", outdir, group, version, kind)
return ioutil.WriteFile(path, []byte(kindPy), 0777)
},
func(casingPy string) error {
return ioutil.WriteFile(
fmt.Sprintf("%s/pulumi_kubernetes/tables.py", outdir), []byte(casingPy), 0777)
})
return ioutil.WriteFile(fmt.Sprintf("%s/__init__.py", path), []byte(initPy), 0777)
}, func(group, version, kind, kindPy string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/%s/%s/%s.py", outdir, group, version, kind)
return ioutil.WriteFile(path, []byte(kindPy), 0777)
}, func(casingPy string) error {
return ioutil.WriteFile(
fmt.Sprintf("%s/pulumi_kubernetes/tables.py", outdir), []byte(casingPy), 0777)
}, func(yamlPy string) error {
return ioutil.WriteFile(
fmt.Sprintf("%s/pulumi_kubernetes/yaml.py", outdir), []byte(yamlPy), 0777)
})
if err != nil {
panic(err)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/gen/python-templates/root__init__.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ __all__ = [

# Expose the provider directly.
from .provider import Provider

from .yaml import ConfigFile
131 changes: 131 additions & 0 deletions pkg/gen/python-templates/yaml.py.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# *** 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 json
from copy import deepcopy
from typing import Optional

import pulumi.runtime
import requests
import yaml
{{#Groups}}
{{#Versions}}
from pulumi_kubernetes.{{Group}}.{{Version}} import *
{{/Versions}}
{{/Groups}}

from . import tables


class ConfigFile(pulumi.ComponentResource):
"""
ConfigFile creates a set of Kubernetes resources from a Kubernetes YAML file. If `config.name`
is not specified, `ConfigFile` assumes the argument `name` is the filename.
lblackstone marked this conversation as resolved.
Show resolved Hide resolved

:param str name: A name for a resource.
:param str file_id: Path or a URL that uniquely identifies a file.
:param ResourceOptions opts: A bag of optional settings that control a resource's behavior.
"""

def __init__(self, name: str, file_id: str, opts: Optional[pulumi.ResourceOptions] = None):
if not name:
raise TypeError('Missing resource name argument (for URN creation)')
if not isinstance(name, str):
raise TypeError('Expected resource name to be a string')
if opts and not isinstance(opts, pulumi.ResourceOptions):
raise TypeError('Expected resource options to be a ResourceOptions instance')

__props__ = dict()

super(ConfigFile, self).__init__(
"kubernetes:yaml:ConfigFile",
name,
__props__,
opts)

if file_id.startswith('http://') or file_id.startswith('https://'):
text = read_url(file_id)
else:
text = read_file(file_id)

# TODO: transformation support
if opts is not None:
_opts = deepcopy(opts)
_opts.parent = self
else:
_opts = pulumi.ResourceOptions(parent=self)

self.register_outputs(parse_yaml_document(yaml.safe_load_all(text), _opts))

def translate_output_property(self, prop: str) -> str:
return tables._CASING_FORWARD_TABLE.get(prop) or prop

def translate_input_property(self, prop: str) -> str:
return tables._CASING_BACKWARD_TABLE.get(prop) or prop


def read_url(url: str) -> str:
response = requests.get(url)
response.raise_for_status()

return response.text


def read_file(path: str) -> str:
with open(path, 'r') as file:
data = file.read()

return data


def parse_yaml_document(objects, opts: Optional[pulumi.ResourceOptions] = None):
resources = {}
for obj in objects:
# TODO: transformation support
file_objects = parse_yaml_object(obj, opts)
for key, value in file_objects:
resources[key] = value

return resources


def parse_yaml_object(obj, opts: Optional[pulumi.ResourceOptions] = None):
if not obj:
return []

# TODO: transformation support

if "kind" not in obj or "apiVersion" not in obj:
raise Exception("Kubernetes resources require a kind and apiVersion: {}".format(json.dumps(obj)))

api_version = obj["apiVersion"]
kind = obj["kind"]

if kind.endswith("List"):
objs = []
if "items" in obj:
for item in obj["items"]:
objs = objs + parse_yaml_object(item, opts)
return objs

if "metadata" not in obj or "name" not in obj["metadata"]:
raise Exception("YAML object does not have a .metadata.name: {}/{} {}".format(
api_version, kind, json.dumps(obj)))

metadata = obj["metadata"]
spec = obj.get("spec")
identifier = metadata["name"]
if "namespace" in metadata:
identifier = "{}/{}".format(metadata["namespace"], metadata["name"])

gvk = f"{api_version}/{kind}"
{{#Groups}}
{{#Versions}}
{{#Kinds}}
if gvk == "{{RawAPIVersion}}/{{Kind}}":
return [(f"{{RawAPIVersion}}/{{Kind}}:{identifier}",
{{Kind}}(identifier, opts, metadata, spec))]
{{/Kinds}}
{{/Versions}}
{{/Groups}}
raise Exception(f"Unsupported gvk: {gvk}")
17 changes: 16 additions & 1 deletion pkg/gen/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import (

// PythonClient will generate a Pulumi Kubernetes provider client SDK for nodejs.
func PythonClient(
swagger map[string]interface{}, templateDir string,
swagger map[string]interface{},
templateDir string,
rootInit func(initPy string) error,
groupInit func(group, initPy string) error,
versionInit func(group, version, initPy string) error,
kindFile func(group, version, kind, kindPy string) error,
casingFile func(casingPy string) error,
yamlFile func(yamlPy string) error,
) error {
definitions := swagger["definitions"].(map[string]interface{})

Expand All @@ -59,6 +61,19 @@ func PythonClient(

groupsSlice := createGroups(definitions, pythonProvider())

yamlPy, err := mustache.RenderFile(
fmt.Sprintf("%s/yaml.py.mustache", templateDir),
map[string]interface{}{
"Groups": groupsSlice,
})
if err != nil {
return err
}
err = yamlFile(yamlPy)
if err != nil {
return err
}

rootInitPy, err := mustache.RenderFile(
fmt.Sprintf("%s/root__init__.py.mustache", templateDir),
map[string]interface{}{
Expand Down
2 changes: 2 additions & 0 deletions sdk/python/pulumi_kubernetes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@

# Expose the provider directly.
from .provider import Provider

from .yaml import ConfigFile
Loading