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
def set_metadata(self, key, value, domain=MetadataDomain.GENERAL, visibility=MetadataVisibility.READ_WRITE, metadata_value_type=MetadataValueType.STRING): """Add a metadata entry to the org vdc network. Only admins can perform this operation. If an entry with the same key exists, it will be updated with the new value. :param str key: an arbitrary key name. Length cannot exceed 256 UTF-8 characters. :param str value: value of the metadata entry :param client.MetadataDomain domain: domain where the new entry would be put. :param client.MetadataVisibility visibility: visibility of the metadata entry. :param client.MetadataValueType metadata_value_type: :return: an object of type EntityType.TASK XML which represents the asynchronous task that is updating the metadata on the org vdc network. :rtype: lxml.objectify.ObjectifiedElement """ metadata = Metadata(client=self.client, resource=self.get_all_metadata()) return metadata.set_metadata(key=key, value=value, domain=domain, visibility=visibility, metadata_value_type=metadata_value_type, use_admin_endpoint=True)
def set_multiple_metadata(self, key_value_dict, domain=MetadataDomain.GENERAL, visibility=MetadataVisibility.READ_WRITE, metadata_value_type=MetadataValueType.STRING): """Add multiple new metadata entries to the vApp. If an entry with the same key exists, it will be updated with the new value. All entries must have the same value type and will be written to the same domain with identical visibility. :param dict key_value_dict: a dict containing key-value pairs to be added/updated. :param client.MetadataDomain domain: domain where the new entries would be put. :param client.MetadataVisibility visibility: visibility of the metadata entries. :param client.MetadataValueType metadata_value_type: :return: an object of type EntityType.TASK XML which represents the asynchronous task that is updating the metadata on the vApp. """ metadata = Metadata(client=self.client, resource=self.get_metadata()) return metadata.set_multiple_metadata( key_value_dict=key_value_dict, domain=MetadataDomain(domain), visibility=MetadataVisibility(visibility), metadata_value_type=MetadataValueType(metadata_value_type), use_admin_endpoint=False)
def set_metadata(self, domain, visibility, key, value, metadata_type=MetadataValueType.STRING.value): """Add a new metadata entry to the vApp. If an entry with the same key exists, it will be updated with the new value. :param str domain: a value of SYSTEM places this MetadataEntry in the SYSTEM domain. Omit or leave empty to place this MetadataEntry in the GENERAL domain. :param str visibility: must be one of the values specified in MetadataVisibility enum. :param str key: an arbitrary key name. Length cannot exceed 256 UTF-8 characters. :param str value: value of the metadata entry. :param str metadata_type: one of the types specified in client.MetadataValueType enum. :return: an object of type EntityType.TASK XML which represents the asynchronous task that is updating the metadata on the vApp. """ metadata = Metadata(client=self.client, resource=self.get_metadata()) return metadata.set_metadata( key=key, value=value, domain=MetadataDomain(domain), visibility=MetadataVisibility(visibility), metadata_value_type=MetadataValueType(metadata_type), use_admin_endpoint=False)
def get_metadata_value(self, key, domain=MetadataDomain.GENERAL): """Fetch the metadata value identified by the domain and key. :param str key: key of the metadata to be fetched. :param client.MetadataDomain domain: domain of the value to be fetched. :return: an object containing EntityType.METADATA_VALUE XML data which represents the metadata value. :rtype: lxml.objectify.ObjectifiedElement """ metadata = Metadata(client=self.client, resource=self.get_all_metadata()) return metadata.get_metadata_value(key, domain)
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)
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