1 # Copyright ETSI Contributors and Others.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
17 from osmclient
.common
.exceptions
import ClientException
18 from osmclient
.cli_commands
import utils
19 from prettytable
import PrettyTable
23 from typing
import Any
, Dict
26 logger
= logging
.getLogger("osmclient")
29 @click.command(name
="vca-add", short_help
="adds a VCA (Juju controller) to OSM")
30 @click.argument("name")
34 help="Comma-separated list of IP or hostnames of the Juju controller",
36 @click.option("--user", prompt
=True, help="Username with admin priviledges")
37 @click.option("--secret", prompt
=True, help="Password of the specified username")
38 @click.option("--cacert", prompt
=True, help="CA certificate")
42 help="Name of the cloud that will be used for LXD containers (LXD proxy charms)",
47 help="Name of the cloud credentialsto be used for the LXD cloud",
52 help="Name of the cloud that will be used for K8s containers (K8s proxy charms)",
57 help="Name of the cloud credentialsto be used for the K8s cloud",
62 help="Configuration options for the models",
64 @click.option("--description", default
=None, help="human readable description")
85 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
88 vca
["endpoints"] = endpoints
.split(",")
90 vca
["secret"] = secret
91 vca
["cacert"] = cacert
92 vca
["lxd-cloud"] = lxd_cloud
93 vca
["lxd-credentials"] = lxd_credentials
94 vca
["k8s-cloud"] = k8s_cloud
95 vca
["k8s-credentials"] = k8s_credentials
97 vca
["description"] = description
99 model_config
= load(model_config
)
100 vca
["model-config"] = model_config
101 ctx
.obj
.vca
.create(name
, vca
)
106 if os
.path
.isfile(data
):
107 return load_file(data
)
110 return json
.loads(data
)
111 except ValueError as e
:
112 raise ClientException(e
)
115 def load_file(file_path
: str) -> Dict
:
118 with
open(file_path
, "r") as f
:
121 return yaml
.safe_load(content
)
122 except yaml
.scanner
.ScannerError
:
125 return json
.loads(content
)
128 raise ClientException(f
"{file_path} must be a valid yaml or json file")
131 @click.command(name
="vca-update", short_help
="updates a VCA")
132 @click.argument("name")
134 "--endpoints", help="Comma-separated list of IP or hostnames of the Juju controller"
136 @click.option("--user", help="Username with admin priviledges")
137 @click.option("--secret", help="Password of the specified username")
138 @click.option("--cacert", help="CA certificate")
141 help="Name of the cloud that will be used for LXD containers (LXD proxy charms)",
145 help="Name of the cloud credentialsto be used for the LXD cloud",
149 help="Name of the cloud that will be used for K8s containers (K8s proxy charms)",
153 help="Name of the cloud credentialsto be used for the K8s cloud",
157 help="Configuration options for the models",
159 @click.option("--description", default
=None, help="human readable description")
177 NAME: name or ID of the VCA
180 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
184 vca
["endpoints"] = endpoints
.split(",")
188 vca
["secret"] = secret
190 vca
["cacert"] = cacert
192 vca
["lxd-cloud"] = lxd_cloud
194 vca
["lxd-credentials"] = lxd_credentials
196 vca
["k8s-cloud"] = k8s_cloud
198 vca
["k8s-credentials"] = k8s_credentials
200 vca
["description"] = description
202 model_config
= load(model_config
)
203 vca
["model-config"] = model_config
204 ctx
.obj
.vca
.update(name
, vca
)
207 @click.command(name
="vca-delete", short_help
="deletes a VCA")
208 @click.argument("name")
210 "--force", is_flag
=True, help="forces the deletion from the DB (not recommended)"
213 def vca_delete(ctx
, name
, force
):
216 NAME: name or ID of the VCA to be deleted
219 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
220 ctx
.obj
.vca
.delete(name
, force
=force
)
223 @click.command(name
="vca-list")
228 help="restricts the list to the VCAs matching the filter",
230 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
231 @click.option("--long", is_flag
=True, help="get more details")
233 def vca_list(ctx
, filter, literal
, long):
236 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
238 filter = "&".join(filter)
239 resp
= ctx
.obj
.vca
.list(filter)
241 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
245 ["Name", "Id", "Project", "Operational State", "Detailed Status"]
247 project_list
= ctx
.obj
.project
.list()
249 table
= PrettyTable(["Name", "Id", "Operational State"])
251 logger
.debug("VCA details: {}".format(yaml
.safe_dump(vca
)))
253 project_id
, project_name
= utils
.get_project(project_list
, vca
)
254 detailed_status
= vca
.get("_admin", {}).get("detailed-status", "-")
260 vca
.get("_admin", {}).get("operationalState", "-"),
261 utils
.wrap_text(text
=detailed_status
, width
=40),
269 vca
.get("_admin", {}).get("operationalState", "-"),
276 @click.command(name
="vca-show", short_help
="shows the details of a VCA")
277 @click.argument("name")
278 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
280 def vca_show(ctx
, name
, literal
):
281 """shows the details of a VCA
283 NAME: name or ID of the VCA
286 resp
= ctx
.obj
.vca
.get(name
)
288 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
290 table
= PrettyTable(["key", "attribute"])
291 for k
, v
in list(resp
.items()):
292 table
.add_row([k
, utils
.wrap_text(text
=json
.dumps(v
, indent
=2), width
=100)])