From 85fb783d5adf04f0368fa28a43a15ba507f47ded Mon Sep 17 00:00:00 2001 From: Olivier DRAGHI Date: Wed, 5 Oct 2022 03:55:57 +0200 Subject: [PATCH] Implement 'vdc list-compute-policies'. Option '--show-id' to show compute policy id. The command show compute policies of the VDC and some details: - is_default - is_sizing - is_vm_placement Tech note: Notice that I have used a private function _do_request_prim(). We should use class VcdClient and method client.call_api() but rehydrate_from_token() need to be surgarged to work proprely. I have a pending pull request to fix that in pyvcloud. Signed-off-by: Olivier DRAGHI --- docs/vcd_vdc.md | 17 +++++++++-------- vcd_cli/vdc.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/docs/vcd_vdc.md b/docs/vcd_vdc.md index be45c1be..b6a598c7 100644 --- a/docs/vcd_vdc.md +++ b/docs/vcd_vdc.md @@ -31,13 +31,14 @@ Options: -h, --help Show this message and exit. Commands: - acl work with vdc acl - create create a virtual datacenter - delete delete a virtual datacenter - disable disable a virtual datacenter - enable enable a virtual datacenter - info show virtual datacenter details - list list of virtual datacenters - use set active virtual datacenter + acl work with vdc acl + create create a virtual datacenter + delete delete a virtual datacenter + disable disable a virtual datacenter + enable enable a virtual datacenter + info show virtual datacenter details + list list of virtual datacenters + list-compute-policies list compute policies available in a VDC + use set active virtual datacenter ``` diff --git a/vcd_cli/vdc.py b/vcd_cli/vdc.py index a01a34f7..5f265386 100644 --- a/vcd_cli/vdc.py +++ b/vcd_cli/vdc.py @@ -13,6 +13,9 @@ # import click +import requests + +from pyvcloud.vcd.api_helper import ApiHelper from pyvcloud.vcd.client import ApiVersion from pyvcloud.vcd.client import EntityType from pyvcloud.vcd.client import get_links @@ -20,6 +23,7 @@ from pyvcloud.vcd.org import Org from pyvcloud.vcd.utils import access_settings_to_dict from pyvcloud.vcd.utils import vdc_to_dict +from pyvcloud.vcd.utils import extract_id from pyvcloud.vcd.vdc import VDC from vcd_cli.utils import access_settings_to_list @@ -540,3 +544,50 @@ def list_disk(ctx, vdc_name): stdout(disk_list, ctx) except Exception as e: stderr(e, ctx) + + +@vdc.command('list-compute-policies', short_help='list compute policies available in a VDC') +@click.pass_context +@click.argument('vdc-name', metavar='') +@click.option( + '--show-id', + 'show_id', + required=False, + is_flag=True, + show_default=True, + default=False, + help='show id') +def list_compute_policies(ctx, vdc_name, show_id): + try: + api_helper=ApiHelper() + restore_session(ctx) + client = ctx.obj['client'] + in_use_org_href = ctx.obj['profiles'].get('org_href') + org = Org(client, in_use_org_href) + vdc_resource = org.get_vdc(vdc_name) + vdc = VDC(client, resource=vdc_resource) + defaut_policy=vdc.resource.DefaultComputePolicy.get('id') + + policies_list = vdc.list_compute_policies() + result = [] + for policy_ref in policies_list: + response=client._do_request_prim( + method='GET', + uri=policy_ref.get('href'), + session=client._session, + accept_type='application/json') + sc = response.status_code + if sc is requests.codes.ok: + policy=api_helper.deserialize(response, response_type='object') + result.append({ + 'id': extract_id(policy.get('id')), + 'name': policy.get('name'), + 'is_sizing': policy.get('isSizingOnly'), + 'is_vm_placement': not policy.get('isSizingOnly'), + 'is_default': defaut_policy == policy.get('id') + }) + else: + raise Exception('invalid http response status code %s' % sc ) + stdout(result, ctx,show_id=show_id) + except Exception as e: + stderr(e, ctx) \ No newline at end of file