Ejemplo n.º 1
0
def search(ctx, resource_type, query_filter):
    """Search for resources in vCloud Director.

\b
    Description
        Search for resources of the provided type. Resource type is not case
        sensitive. When invoked without a resource type, list the available
        types to search for. Admin types are only allowed when the user is
        the system administrator.
\b
        Filters can be applied to the search.
\b
    Examples
        vcd search
            lists available resource types.
\b
        vcd search task
            Search for all tasks in current organization.
\b
        vcd search task --filter 'status==running'
            Search for running tasks in current organization.
\b
        vcd search admintask --filter 'status==running'
            Search for running tasks in all organizations, system administrator only.
\b
        vcd search task --filter 'id==ffb96443-d7f3-4200-825d-0f297388ebc0'
            Search for a task by id
\b
        vcd search vapp
            Search for vApps.
\b
        vcd search vapp -f 'metadata:cse.node.type==STRING:master'
            Search for vApps by metadata.
\b
        vcd search vm
            Search for virtual machines.
    """  # NOQA
    try:
        if resource_type is None:
            click.secho(ctx.get_help())
            click.echo('\nAvailable resource types:')
            click.echo(tabulate(tabulate_names(RESOURCE_TYPES, 4)))
            return
        restore_session(ctx)
        client = ctx.obj['client']
        result = []
        resource_type_cc = to_camel_case(resource_type, RESOURCE_TYPES)
        q = client.get_typed_query(resource_type_cc,
                                   query_result_format=QueryResultFormat.
                                   ID_RECORDS,
                                   qfilter=query_filter)
        records = list(q.execute())
        if len(records) == 0:
            result = 'not found'
        else:
            for r in records:
                result.append(to_dict(r, resource_type=resource_type_cc))
        stdout(result, ctx, show_id=True)
    except Exception as e:
        stderr(e, ctx)
Ejemplo n.º 2
0
def info(ctx, resource_type, resource_id):
    """Display details of a resource in vCloud Director.

\b
    Description
        The info command displays details of a resource with the provided type
        and id. Resource type is not case sensitive. When invoked without a
        resource type, list the available types. Admin resources are only
        allowed when the user is the system administrator.
\b
    Examples
        vcd info task ffb96443-d7f3-4200-825d-0f297388ebc0
            Get details of task by id.
\b
        vcd info vapp c48a4e1a-7bd9-4177-9c67-4c330016b99f
            Get details of vApp by id.
\b
        vcd catalog info my-catalog my-item
            Get details of an item listed in the previous command.
\b
        vcd info catalogitem f653b137-0d14-4ea9-8f14-dcd2b7914110
            Get details of the catalog item based on the 'id' listed in the
            previous command.
\b
        vcd info vapptemplate 53b83b27-1f2b-488e-9020-a27aee8cb640
            Get details of the vApp tepmlate based on the 'template-id' listed
            in the previous command.
\b
    See Also
        Several 'vcd' commands provide the id of a resource, including the
        'vcd search' command.
    """

    try:
        if resource_type is None or resource_id is None:
            click.secho(ctx.get_help())
            click.echo('\nAvailable resource types:')
            click.echo(tabulate(tabulate_names(RESOURCE_TYPES, 4)))
            return
        restore_session(ctx)
        client = ctx.obj['client']
        q = client.get_typed_query(
            to_camel_case(resource_type, RESOURCE_TYPES),
            query_result_format=QueryResultFormat.REFERENCES,
            qfilter='id==%s' % resource_id)
        records = list(q.execute())
        if len(records) == 0:
            raise Exception('not found')
        elif len(records) > 1:
            raise Exception('multiple found')
        resource = client.get_resource(records[0].get('href'))
        stdout_xml(resource, ctx.find_root().params['is_colorized'])
    except Exception as e:
        import traceback
        traceback.print_exc()
        stderr(e, ctx)
Ejemplo n.º 3
0
def info(ctx, resource_type, resource_id):
    """Display details of a resource in vCloud Director.

\b
    Description
        The info command displays details of a resource with the provided type
        and id. Resource type is not case sensitive. When invoked without a
        resource type, list the available types. Admin resources are only
        allowed when the user is the system administrator.
\b
    Examples
        vcd info task ffb96443-d7f3-4200-825d-0f297388ebc0
            Get details of task by id.
\b
        vcd info vapp c48a4e1a-7bd9-4177-9c67-4c330016b99f
            Get details of vApp by id.
\b
        vcd catalog info my-catalog my-item
            Get details of an item listed in the previous command.
\b
        vcd info catalogitem f653b137-0d14-4ea9-8f14-dcd2b7914110
            Get details of the catalog item based on the 'id' listed in the
            previous command.
\b
        vcd info vapptemplate 53b83b27-1f2b-488e-9020-a27aee8cb640
            Get details of the vApp tepmlate based on the 'template-id' listed
            in the previous command.
\b
    See Also
        Several 'vcd' commands provide the id of a resource, including the
        'vcd search' command.
    """
    try:
        if resource_type is None or resource_id is None:
            click.secho(ctx.get_help())
            click.echo('\nAvailable resource types:')
            click.echo(tabulate(tabulate_names(RESOURCE_TYPES, 4)))
            return
        restore_session(ctx)
        client = ctx.obj['client']
        q = client.get_typed_query(
            to_camel_case(resource_type, RESOURCE_TYPES),
            query_result_format=QueryResultFormat.REFERENCES,
            equality_filter=('id', resource_id))
        records = list(q.execute())
        if len(records) == 0:
            raise Exception('not found')
        elif len(records) > 1:
            raise Exception('multiple found')
        resource = client.get_resource(records[0].get('href'))
        stdout_xml(resource, ctx.find_root().params['is_colorized'])
    except Exception as e:
        import traceback
        traceback.print_exc()
        stderr(e, ctx)
Ejemplo n.º 4
0
def search(ctx, resource_type, query_filter):
    """Search for resources in vCloud Director.

\b
    Description
        Search for resources of the provided type. Resource type is not case
        sensitive. When invoked without a resource type, list the available
        types to search for. Admin types are only allowed when the user is
        the system administrator.
\b
        Filters can be applied to the search.
\b
    Examples
        vcd search
            lists available resource types.
\b
        vcd search task
            Search for all tasks in current organization.
\b
        vcd search task --filter 'status==running'
            Search for running tasks in current organization.
\b
        vcd search admintask --filter 'status==running'
            Search for running tasks in all organizations,
            system administrator only.
\b
        vcd search task --filter 'id==ffb96443-d7f3-4200-825d-0f297388ebc0'
            Search for a task by id
\b
        vcd search vapp
            Search for vApps.
\b
        vcd search vapp -f 'metadata:cse.node.type==STRING:master'
            Search for vApps by metadata.
\b
        vcd search vapp -f \\
        'metadata:vapp.origin.name==STRING:photon-custom-hw11-2.0-304b817.ova'
            Search for vApps instantiated from template
\b
        vcd search vapp -f \\
        'numberOfCpus=gt=4;memoryAllocationMB=gt=10000;storageKB=gt=10000000'
            Search for resource intensive vApps
\b
        vcd search vm
            Search for virtual machines.
    """

    try:
        if resource_type is None:
            click.secho(ctx.get_help())
            click.echo('\nAvailable resource types:')
            click.echo(tabulate(tabulate_names(RESOURCE_TYPES, 4)))
            return
        restore_session(ctx)
        client = ctx.obj['client']
        result = []
        resource_type_cc = to_camel_case(resource_type, RESOURCE_TYPES)
        q = client.get_typed_query(
            resource_type_cc,
            query_result_format=QueryResultFormat.ID_RECORDS,
            qfilter=query_filter)
        records = list(q.execute())
        if len(records) == 0:
            result = 'not found'
        else:
            for r in records:
                result.append(to_dict(r, resource_type=resource_type_cc))
        stdout(result, ctx, show_id=True)
    except Exception as e:
        stderr(e, ctx)