Ejemplo n.º 1
0
    def remove_metadata(self, key, domain=MetadataDomain.GENERAL):
        """Remove a metadata entry from the vApp.

        :param str key: key of the metadata to be removed.
        :param client.MetadataDomain domain: domain of the entry to be removed.

        :return: an object of type EntityType.TASK XML which represents
            the asynchronous task that is deleting the metadata on the vApp.

        :rtype: lxml.objectify.ObjectifiedElement

        :raises: AccessForbiddenException: If there is no metadata entry
            corresponding to the key provided.
        """
        metadata = Metadata(client=self.client, resource=self.get_metadata())
        return metadata.remove_metadata(key=key,
                                        domain=domain,
                                        use_admin_endpoint=False)
Ejemplo n.º 2
0
class VCDOrg(VcdAnsibleModule):
    def __init__(self, **kwargs):
        super(VCDOrg, self).__init__(**kwargs)

    def manage_states(self):
        state = self.params.get('state')
        if state == "present":
            return self.create()

        if state == "absent":
            return self.delete()

        if state == "update":
            return self.update()

    def manage_operations(self):
        operation = self.params.get('operation')
        if operation == "read":
            return self.read()

        if operation == "add_rights":
            return self.add_rights()

        if operation == "remove_rights":
            return self.remove_rights()

        if operation == "list_rights":
            return self.list_rights()

        if operation == "list_roles":
            return self.list_roles()

        if operation == "list_users":
            return self.list_users()

        if operation == "list_vdcs":
            return self.list_vdcs()

        if operation == "set_metadata":
            return self.set_metadata()

    def create(self):
        org_name = self.params.get('org_name')
        full_name = self.params.get('full_name')
        is_enabled = self.params.get('is_enabled')
        response = dict()
        response['changed'] = False

        try:
            sys_admin = self.client.get_admin()
            self.system = System(self.client, admin_resource=sys_admin)
            self.system.create_org(org_name, full_name, is_enabled)
            response['msg'] = 'Org {} has been created.'.format(org_name)
            response['changed'] = True
        except BadRequestException:
            response['warnings'] = 'Org {} is already present.'.format(
                org_name)

        return response

    def read(self):
        org_name = self.params.get('org_name')
        response = dict()
        org_details = dict()
        response['changed'] = False

        resource = self.client.get_org_by_name(org_name)
        org = Org(self.client, resource=resource)
        org_admin_resource = org.client.get_resource(org.href_admin)
        org_details['org_name'] = org_name
        org_details['full_name'] = str(org_admin_resource['FullName'])
        org_details['is_enabled'] = str(org_admin_resource['IsEnabled'])
        response['msg'] = org_details

        return response

    def update(self):
        org_name = self.params.get('org_name')
        is_enabled = self.params.get('is_enabled')
        response = dict()
        response['changed'] = False

        resource = self.client.get_org_by_name(org_name)
        org = Org(self.client, resource=resource)
        org.update_org(is_enabled)
        response['msg'] = "Org {} has been updated.".format(org_name)
        response['changed'] = True

        return response

    def delete(self):
        org_name = self.params.get('org_name')
        force = self.params.get('force')
        recursive = self.params.get('recursive')
        response = dict()
        response['changed'] = False

        try:
            sys_admin = self.client.get_admin()
            self.system = System(self.client, admin_resource=sys_admin)
            delete_org_task = self.system.delete_org(org_name, force,
                                                     recursive)
            self.execute_task(delete_org_task)
            response['msg'] = "Org {} has been deleted.".format(org_name)
            response['changed'] = True
        except EntityNotFoundException:
            response['warnings'] = "Org {} is not present.".format(org_name)

        return response

    def add_rights(self):
        org_name = self.params.get('org_name')
        org_rights = self.params.get('org_rights')
        response = dict()
        response['changed'] = False

        resource = self.client.get_org_by_name(org_name)
        org = Org(self.client, resource=resource)
        org.add_rights(org_rights)
        response['msg'] = "Rights has been added to org successfully."
        response['changed'] = True

        return response

    def remove_rights(self):
        org_name = self.params.get('org_name')
        org_rights = self.params.get('org_rights')
        response = dict()
        response['changed'] = False

        resource = self.client.get_org_by_name(org_name)
        org = Org(self.client, resource=resource)
        org.remove_rights(org_rights)
        response['msg'] = "Rights has been removed to org successfully."
        response['changed'] = True

        return response

    def list_rights(self):
        org_name = self.params.get('org_name')
        response = dict()
        response['changed'] = False

        resource = self.client.get_org_by_name(org_name)
        org = Org(self.client, resource=resource)
        response['msg'] = org.list_rights_of_org()

        return response

    def list_roles(self):
        org_name = self.params.get('org_name')
        response = dict()
        response['changed'] = False

        resource = self.client.get_org_by_name(org_name)
        org = Org(self.client, resource=resource)
        response['msg'] = org.list_roles()

        return response

    def list_users(self):
        org_name = self.params.get('org_name')
        response = dict()
        org_details = dict()
        response['users'] = list()
        response['changed'] = False

        resource = self.client.get_org_by_name(org_name)
        org = Org(self.client, resource=resource)
        org_user_list = org.list_users()
        resource_type = ResourceType.USER.value
        if self.client.is_sysadmin():
            resource_type = ResourceType.ADMIN_USER.value
        for org_user in org_user_list:
            response['users'].append(
                to_dict(org_user, resource_type=resource_type, exclude=[]))

        return response

    def list_vdcs(self):
        org_name = self.params.get('org_name')
        response = dict()
        org_details = dict()
        response['vdcs'] = list()
        response['changed'] = False

        resource = self.client.get_org_by_name(org_name)
        org = Org(self.client, resource=resource)
        response['vdcs'] = org.list_vdcs()

        return response

    def set_metadata(self):
        org_name = self.params.get('org_name')
        metadata = self.params.get('metadata')
        response = dict()
        response['msg'] = ''

        if len(metadata) != 0:
            # workaround to set metadata for org as it is as of now not implemented in pyvcloud for org, vdc, e.g. - we will open a pull request to fix this in the future
            resource = self.client.get_linked_resource(
                self.client.get_org_by_name(org_name), RelationType.DOWN,
                EntityType.METADATA.value)
            self.metadata = Metadata(self.client, resource=resource)
            for md in metadata:
                domain = MetadataDomain.SYSTEM
                visibility = MetadataVisibility.READONLY
                if type(md) is dict and md.get('state', 'present') == 'absent':
                    if md.get('visibility', 'READONLY').upper() == 'READWRITE':
                        domain = MetadataDomain.GENERAL
                    self.metadata.remove_metadata(md['name'], domain)
                else:
                    if md.get('visibility', 'READONLY').upper() == 'PRIVATE':
                        visibility = MetadataVisibility.PRIVATE
                    elif md.get('visibility',
                                'READONLY').upper() == 'READWRITE':
                        domain = MetadataDomain.GENERAL
                        visibility = MetadataVisibility.READWRITE
                    value_type = MetadataValueType.STRING
                    if md.get('type', 'STRING').upper() == 'NUMBER':
                        value_type = MetadataValueType.NUMBER
                    elif md.get('type', 'STRING').upper() == 'BOOLEAN':
                        value_type = MetadataValueType.BOOLEAN
                    elif md.get('type', 'STRING').upper() == 'DATA_TIME':
                        value_type = MetadataValueType.DATA_TIME
                    self.metadata.set_metadata(md['name'], md['value'], domain,
                                               visibility, value_type, True)

        return response