Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Implement 'vdc list-compute-policies'. #577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions docs/vcd_vdc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
51 changes: 51 additions & 0 deletions vcd_cli/vdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
#

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
from pyvcloud.vcd.exceptions import OperationNotSupportedException
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
Expand Down Expand Up @@ -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='<vdc-name>')
@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)