Created
August 26, 2019 14:44
-
-
Save haizaar/c6e0bb8efd0a3396cce613bf7f5043f2 to your computer and use it in GitHub Desktop.
A script to create multi-file GCP Deployment Manager composite type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import time | |
import click | |
import googleapiclient.discovery | |
import googleapiclient.errors | |
@click.command("create") | |
@click.argument("name") | |
@click.argument("template", type=click.File("rt")) | |
@click.option("--imports", "-i", multiple=True, type=click.File("rt"), | |
help="Additional file(s) that the template uses (imports)") | |
@click.option("--interpreter", default="PYTHON") | |
@click.option("--project-id", required=True) | |
@click.option("--replace", default=False, is_flag=True, show_default=True, | |
help="Replace existing composite type") | |
@click.pass_context | |
def create(ctx, name, template, imports, interpreter, project_id, replace): | |
""" | |
Create new Deployment Manager Composite Type | |
from given template file and optional additional import files. | |
We have this script because gcloud tool does not currently support | |
additional imports | |
""" | |
body = { | |
"name": name, | |
"templateContents": { | |
"template": template.read(), | |
"interpreter": interpreter, | |
"schema": "", | |
} | |
} | |
if imports: | |
import_contents = [] | |
for i in imports: | |
import_contents.append({ | |
"name": i.name, | |
"content": i.read(), | |
}) | |
body["templateContents"]["imports"] = import_contents | |
service = googleapiclient.discovery.build("deploymentmanager", "v2beta") | |
deletor = service.compositeTypes().delete(project=project_id, compositeType=name) | |
creator = service.compositeTypes().insert(project=project_id, body=body) | |
# Update API call can't update template contents, therefore is quite useless in our case | |
try: | |
rv = creator.execute() | |
except googleapiclient.errors.HttpError as e: | |
conflict = 409 | |
if e.resp.status != conflict: | |
raise | |
if replace: | |
click.echo("Deleting current Composite Type") | |
deletor.execute() | |
time.sleep(1) # Too lazy to write a proper wait-delete-to-complete routine | |
click.echo("Creating new Composite Type") | |
rv = creator.execute() | |
else: | |
click.echo("Composite Type {} already exists. Use --replace to update it.".format(name)) | |
ctx.abort() | |
print(json.dumps(rv, indent=2)) | |
if __name__ == "__main__": | |
create() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment