Support base64 encoded credentials for VIM creation 61/14561/1 master
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 13 Sep 2024 17:28:41 +0000 (19:28 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 13 Sep 2024 17:28:41 +0000 (19:28 +0200)
Change-Id: I719d76ba089047358b9447a2df64929600fd08d5
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osmclient/cli_commands/vim.py

index bb7622d..c3377a9 100755 (executable)
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import base64
 import click
 from osmclient.common import print_output
+from osmclient.common.exceptions import ClientException
 from osmclient.cli_commands import utils
 from prettytable import PrettyTable
 import yaml
@@ -52,7 +54,11 @@ def _check_ca_cert(vim_config: dict) -> None:
     default=None,
     help="VIM specific config parameters in YAML or JSON file",
 )
-@click.option("--account_type", default="openstack", help="VIM type")
+@click.option(
+    "--account_type",
+    default="openstack",
+    help="VIM type (openstack, aws, azure, gcp, vmware)",
+)
 @click.option("--description", default=None, help="human readable description")
 @click.option(
     "--sdn_controller",
@@ -139,8 +145,18 @@ def vim_create(
     vim_config = utils.create_config(config_file, config)
     _check_ca_cert(vim_config)
     if creds:
-        with open(creds, "r") as cf:
-            vim_config["credentials"] = yaml.safe_load(cf.read())
+        with open(creds, "rb") as cf:
+            creds_content = cf.read()
+        vim_config["credentials_base64"] = base64.b64encode(creds_content).decode(
+            "utf-8"
+        )
+        if account_type != "aws":
+            try:
+                # Convert binary content to text before loading as YAML
+                creds_text = creds_content.decode("utf-8")
+                vim_config["credentials"] = yaml.safe_load(creds_text)
+            except (UnicodeDecodeError, yaml.YAMLError) as e:
+                raise ClientException(f"Error decoding credentials file: {e}")
     ctx.obj.vim.create(
         name, vim, vim_config, sdn_controller, sdn_port_mapping, wait=wait
     )
@@ -159,7 +175,7 @@ def vim_create(
     default=None,
     help="VIM specific config parameters in YAML or JSON file",
 )
-@click.option("--account_type", help="VIM type")
+@click.option("--account_type", help="VIM type (openstack, aws, azure, gcp, vmware)")
 @click.option("--description", help="human readable description")
 @click.option(
     "--sdn_controller",
@@ -245,8 +261,18 @@ def vim_update(
         vim_config = utils.create_config(config_file, config)
         _check_ca_cert(vim_config)
     if creds:
-        with open(creds, "r") as cf:
-            vim_config["credentials"] = yaml.safe_load(cf.read())
+        with open(creds, "rb") as cf:
+            creds_content = cf.read()
+        vim_config["credentials_base64"] = base64.b64encode(creds_content).decode(
+            "utf-8"
+        )
+        if account_type != "aws":
+            try:
+                # Convert binary content to text before loading as YAML
+                creds_text = creds_content.decode("utf-8")
+                vim_config["credentials"] = yaml.safe_load(creds_text)
+            except (UnicodeDecodeError, yaml.YAMLError) as e:
+                raise ClientException(f"Error decoding credentials file: {e}")
     prometheus_config = {}
     if prometheus_url:
         prometheus_config["prometheus-url"] = prometheus_url