Skip to content

Instantly share code, notes, and snippets.

@LouisAmon
Created February 26, 2020 07:15
Show Gist options
  • Save LouisAmon/b3b999bcaf0500f04a9f3d811f73ee16 to your computer and use it in GitHub Desktop.
Save LouisAmon/b3b999bcaf0500f04a9f3d811f73ee16 to your computer and use it in GitHub Desktop.
Pulumi Dynamic Provider (Python)
# This simple example illustrates how Nuage recommends you should build
# a Dynamic Provider in Pulumi
#
# The example is based on the official documentation:
# https://www.pulumi.com/docs/intro/concepts/programming-model/#example-github-labels-rest-api
#
# It defines a Github Label programmatically via Pulumi.
#
# The recommended directory structure is the following:
#
# .
# ├── __main__.py
# ├── Pulumi.yaml
# └── dynamic
# ├── __init__.py
# └── github
# ├── __init__.py
# └── label.py <--- this Gist is here
from typing import Optional
from pulumi import Output
from pulumi.dynamic import ResourceProvider, Resource, CreateResult
from github import Github, GithubObject
token = config.require_secret("github_token")
g = Github(auth)
class Label(Resource):
name: Input[str]
color: Input[str]
url: Input[str]
description: Input[str]
def __init__(
self,
name,
color,
owner,
repo,
description=None,
opts=None
):
def __init__(self, name, color, owner, repo, description=None):
self.owner = owner
self.repo = repo
self.name = Output.from_input(name)
self.color = Output.from_input(color)
self.description = description
super().__init__(GithubLabelProvider(), name, full_args, opts)
class LabelProvider(ResourceProvider):
def create(self, props):
l = g.get_user(props["owner"]).get_repo(props["repo"]).create_label(
name=props["name"],
color=props["color"],
description=props.get("description", GithubObject.NotSet))
return CreateResult(l.name, {**props, **l.raw_data})
def update(self, id, _olds, props):
l = g.get_user(props["owner"]).get_repo(props["repo"]).get_label(id)
l.edit(name=props["name"],
color=props["color"],
description=props.get("description", GithubObject.NotSet))
return UpdateResult({**props, **l.raw_data})
def delete(self, id, props):
l = g.get_user(props["owner"]).get_repo(props["repo"]).get_label(id)
l.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment