From 6645ace47d10e7606c541f4503008afdfbf16061 Mon Sep 17 00:00:00 2001 From: Jiri Kyjovsky Date: Tue, 3 Oct 2023 15:18:40 +0200 Subject: [PATCH] provide subnets ids directly to the tool via argument --- resalloc_ibm_cloud/argparsers.py | 10 +++++++--- resalloc_ibm_cloud/ibm_cloud_vm.py | 12 +++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/resalloc_ibm_cloud/argparsers.py b/resalloc_ibm_cloud/argparsers.py index e3aaf0c..97062bd 100644 --- a/resalloc_ibm_cloud/argparsers.py +++ b/resalloc_ibm_cloud/argparsers.py @@ -4,6 +4,7 @@ import argparse + def default_arg_parser(): """ The part that every resalloc-ibm-cloud utility needs @@ -45,10 +46,13 @@ def vm_arg_parser(): "--no-floating-ip", action="store_true", help="Don't use floating IPs (for VPN)" ) parser_create.add_argument( - "--zones", + "--subnets-ids", + type=str, + nargs="+", help=( - "Path to json file with zones as keys and subnet id as value." - 'content of file will look like: {"jp-tok-1": "secret-subnet-id-123-abcd", ...}' + "Space separated list of subnets ids. In case zones are different than a" + " basic zone, then specify subnet id with zone colon separated like this:" + " zone-1:subnet-id-1 zone-2:subnet-id-2" ), required=True, ) diff --git a/resalloc_ibm_cloud/ibm_cloud_vm.py b/resalloc_ibm_cloud/ibm_cloud_vm.py index 9614154..d8f966e 100755 --- a/resalloc_ibm_cloud/ibm_cloud_vm.py +++ b/resalloc_ibm_cloud/ibm_cloud_vm.py @@ -3,7 +3,6 @@ """ -import json import logging import pipes import os @@ -132,8 +131,7 @@ def create_instance(service, instance_name, opts): """ log = opts.log - with open(opts.zones, "r", encoding="utf-8") as zone_file: - zones = json.load(zone_file) + zone, subnet_id = _get_zone_and_subnet_id(opts) instance_prototype_model = { "keys": [{"id": opts.ssh_key_id}], @@ -155,14 +153,14 @@ def create_instance(service, instance_name, opts): "primary_network_interface": { "name": "primary-network-interface", "subnet": { - "id": zones[opts.zone], + "id": subnet_id, }, "security_groups": [ {"id": opts.security_group_id}, ], }, "zone": { - "name": urlparse(opts.se), + "name": zone, }, "volume_attachments": [ { @@ -344,10 +342,6 @@ def main(): opts.instance = "production" if "-prod-" in name else "devel" if opts.subparser == "create": - with open(opts.zones, "r", encoding="utf-8") as zone_file: - allowed_zones = list(json.load(zone_file).keys()) - - opts.zone = random.choice(allowed_zones) # detect_floating_ip_name(opts) create_instance(service, name, opts) elif opts.subparser == "delete":