-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommand_instance.py
More file actions
173 lines (145 loc) · 4.85 KB
/
command_instance.py
File metadata and controls
173 lines (145 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import sys
from typing import get_args
from urllib import parse
import click
from tabulate import tabulate
from yaspin import yaspin
from proxygen_cli.lib import output, proxygen_api, spec, version
from proxygen_cli.lib.constants import LITERAL_ENVS
from proxygen_cli.lib.settings import SETTINGS
CHOICE_OF_ENVS = click.Choice(get_args(LITERAL_ENVS))
@click.group()
@click.option(
"--api", default=SETTINGS.api, help="Override the default API", show_default=True
)
@click.pass_context
def instance(ctx, api):
"""
Create/Update/Delete instances of your API.
"""
version.validate_cli_version()
ctx.ensure_object(dict)
ctx.obj["api"] = api
if api is None:
raise click.UsageError(
"You must set the API before using this command: see `proxygen settings`"
)
@instance.command()
@click.pass_context
@click.option(
"--env", type=CHOICE_OF_ENVS, help="Only print instances in the choice environment."
)
def list(ctx, env):
api = ctx.obj["api"]
if env:
objects = proxygen_api.get_instances(api, env)
else:
objects = proxygen_api.get_resources(api, _type="instance")
output.print_table(objects)
@instance.command()
@click.argument("env", type=CHOICE_OF_ENVS)
@click.argument("base_path")
@click.argument("spec_file")
@click.option(
"--no-confirm",
is_flag=True,
show_default=True,
help="Do not prompt for confirmation.",
)
@click.option(
"--quiet",
is_flag=True,
show_default=True,
help="Suppress spinner output.",
)
@click.pass_context
def deploy(ctx, env, base_path, spec_file, no_confirm, quiet):
"""
Deploy <SPEC_FILE> to <ENV> under <BASE_PATH>
Your instance is deployed at
https://<ENV>.api.service.nhs.uk/<BASE_PATH> unless <ENV> is
"prod", then it is deployed to
https://api.service.nhs.uk/<BASE_PATH>.
"""
if "/" in base_path:
raise click.ClickException(
"Multipart base paths must include '_' instead of '/'. "
"This is to ensure a path-safe version. "
"Proxygen will convert these underscores back in to '/' for the proxy."
)
api = ctx.obj["api"]
paas_open_api = spec.resolve(spec_file)
_url = spec.url(env, base_path)
# Overwrite the servers object to point to the values provided from the cli
server_url = _url.replace("_", "/")
paas_open_api["servers"] = [{"url": server_url}]
if not no_confirm:
output.print_spec(paas_open_api)
if not click.confirm(f"Deploy this spec to {_url}?"): # pragma: no cover
raise click.Abort()
with yaspin(stream=open(os.devnull, 'w') if quiet else sys.stdout) as sp:
sp.text = f"Deploying {_url}"
instance = parse.quote(base_path)
try:
result = proxygen_api.put_instance(api, env, instance, paas_open_api)
if result is None:
raise click.ClickException(f"Invalid instance {_url}")
sp.ok("✔")
except Exception as e:
sp.fail("✘")
raise click.ClickException(
f"Failed to deploy instance {_url}. Error: {str(e)}"
)
@instance.command()
@click.argument("env", type=CHOICE_OF_ENVS)
@click.argument("base_path")
@click.pass_context
def get(ctx, env, base_path):
"""
Get the spec used to deploy the instance in environment <ENV> with base path <BASE_PATH>.
"""
api = ctx.obj["api"]
instance = parse.quote(base_path)
result = proxygen_api.get_instance(api, env, instance)
output.print_spec(result)
@instance.command()
@click.argument("env", type=CHOICE_OF_ENVS)
@click.argument("base_path")
@click.option(
"--no-confirm",
is_flag=True,
show_default=True,
help="Do not prompt for confirmation.",
)
@click.option(
"--quiet",
is_flag=True,
show_default=True,
help="Suppress spinner output.",
)
@click.pass_context
def delete(ctx, env, base_path, no_confirm, quiet):
"""
Delete the instance deployed on <BASE_PATH> in environment <ENV>.
"""
api = ctx.obj["api"]
instance = parse.quote(base_path)
_url = spec.url(env, base_path)
if not no_confirm:
result = proxygen_api.get_instance(api, env, instance)
if not result:
raise click.ClickException(f"No such instance {_url}")
output.print_spec(result)
if not click.confirm(f"Delete the instance at {_url}?"):
raise click.Abort()
with yaspin(stream=open(os.devnull, 'w') if quiet else sys.stdout) as sp:
sp.text = f"Deleting {_url}"
try:
result = proxygen_api.delete_instance(api, env, instance)
if result is None:
raise click.ClickException(f"No such instance {_url}")
sp.ok("✔")
except Exception as e:
sp.fail("✘")
raise click.ClickException(f"Failed to deploy instance {_url}. Error: {str(e)}")