Add dry-run and envvar defaults

This commit is contained in:
Neill Cox 2023-09-22 18:15:08 +10:00
parent 4daf1b9c2f
commit f8d3f19dbb
2 changed files with 73 additions and 23 deletions

View file

@ -8,10 +8,15 @@ import json
import subprocess
def openstack_cmd(cmd):
def openstack_cmd(cmd, args):
"""Utility function to run an openstack command agains the standalone cloud"""
cmd = "OS_CLOUD=standalone openstack " + cmd
if args.dry_run:
print("dry-run specified. Not executing cmd. cmd was:")
print(cmd)
return
result = subprocess.check_output(cmd, shell=True, universal_newlines=True)
return result
@ -50,7 +55,7 @@ def destroy_project(args):
cmd = "project list -f json"
project_exists = [
x
for x in json.loads(openstack_cmd(cmd))
for x in json.loads(openstack_cmd(cmd, args))
if x["Name"] == args.project_name
]
@ -71,7 +76,7 @@ def destroy_user(args):
"""Delete the user if it exists"""
cmd = "user list -f json"
user_exists = [
x for x in json.loads(openstack_cmd(cmd)) if x["Name"] == args.username
x for x in json.loads(openstack_cmd(cmd, args)) if x["Name"] == args.username
]
if user_exists:
@ -84,7 +89,7 @@ def destroy_user(args):
f"--password {args.password} {args.username}"
)
args.user_id = json.loads(openstack_cmd(cmd))["id"]
args.user_id = json.loads(openstack_cmd(cmd, args))["id"]
print(f"User created - id: {args.user_id}")
@ -99,10 +104,10 @@ def assign_member_role(args):
cmd = f"role add --user {args.username} --project {args.project_id} member"
result = openstack_cmd(cmd)
result = openstack_cmd(cmd, args)
cmd = f"role assignment list --user {args.user_id} --role member -f json"
result = json.loads(openstack_cmd(cmd))
result = json.loads(openstack_cmd(cmd, args))
if result:
print("User has member role")