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("--newname", help="New name for the VCA")
137 @click.option("--user", help="Username with admin priviledges")
138 @click.option("--secret", help="Password of the specified username")
139 @click.option("--cacert", help="CA certificate")
142 help="Name of the cloud that will be used for LXD containers (LXD proxy charms)",
146 help="Name of the cloud credentialsto be used for the LXD cloud",
150 help="Name of the cloud that will be used for K8s containers (K8s proxy charms)",
154 help="Name of the cloud credentialsto be used for the K8s cloud",
158 help="Configuration options for the models",
160 @click.option("--description", default
=None, help="human readable description")
179 NAME: name or ID of the VCA
182 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
185 vca
["name"] = newname
187 vca
["endpoints"] = endpoints
.split(",")
191 vca
["secret"] = secret
193 vca
["cacert"] = cacert
195 vca
["lxd-cloud"] = lxd_cloud
197 vca
["lxd-credentials"] = lxd_credentials
199 vca
["k8s-cloud"] = k8s_cloud
201 vca
["k8s-credentials"] = k8s_credentials
203 vca
["description"] = description
205 model_config
= load(model_config
)
206 vca
["model-config"] = model_config
207 ctx
.obj
.vca
.update(name
, vca
)
210 @click.command(name
="vca-delete", short_help
="deletes a VCA")
211 @click.argument("name")
213 "--force", is_flag
=True, help="forces the deletion from the DB (not recommended)"
216 def vca_delete(ctx
, name
, force
):
219 NAME: name or ID of the VCA to be deleted
222 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
223 ctx
.obj
.vca
.delete(name
, force
=force
)
226 @click.command(name
="vca-list")
231 help="restricts the list to the VCAs matching the filter",
233 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
234 @click.option("--long", is_flag
=True, help="get more details")
236 def vca_list(ctx
, filter, literal
, long):
239 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
241 filter = "&".join(filter)
242 resp
= ctx
.obj
.vca
.list(filter)
244 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
248 ["Name", "Id", "Project", "Operational State", "Detailed Status"]
250 project_list
= ctx
.obj
.project
.list()
252 table
= PrettyTable(["Name", "Id", "Operational State"])
254 logger
.debug("VCA details: {}".format(yaml
.safe_dump(vca
)))
256 project_id
, project_name
= utils
.get_project(project_list
, vca
)
257 detailed_status
= vca
.get("_admin", {}).get("detailed-status", "-")
263 vca
.get("_admin", {}).get("operationalState", "-"),
264 utils
.wrap_text(text
=detailed_status
, width
=40),
272 vca
.get("_admin", {}).get("operationalState", "-"),
279 @click.command(name
="vca-show", short_help
="shows the details of a VCA")
280 @click.argument("name")
281 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
283 def vca_show(ctx
, name
, literal
):
284 """shows the details of a VCA
286 NAME: name or ID of the VCA
289 resp
= ctx
.obj
.vca
.get(name
)
291 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
293 table
= PrettyTable(["key", "attribute"])
294 for k
, v
in list(resp
.items()):
295 table
.add_row([k
, utils
.wrap_text(text
=json
.dumps(v
, indent
=2), width
=100)])