def exists(cls, connection, name, kind=None): """ Returns true if (and only if) the specified privilege exists. If the name is a structured value consisting of the kind and the name separated by a "|", as returned by the list() method, then the kind is optional. :param connection: The connection to the MarkLogic database :param name: The name of the privilege :param kind: The kind of privilege :return: The privilege """ parts = name.split("|") if len(parts) == 1: pass elif len(parts) == 2 or len(parts) == 3: if kind is not None and kind != parts[0]: raise validate_custom("Kinds must match") kind = parts[0] name = parts[1] else: raise validate_custom("Unparseable privilege name") uri = "http://{0}:{1}/manage/v2/privileges/{2}/properties?kind={3}" \ .format(connection.host, connection.management_port, name, kind) response = requests.head(uri, auth=connection.auth) if response.status_code == 200: return True elif response.status_code == 404: return False else: raise exceptions.UnexpectedManagementAPIResponse(response.text)
def list(cls, connection): """ List all the user names. :param connection: The connection to a MarkLogic server :return: A list of user names """ uri = "http://{0}:{1}/manage/v2/users" \ .format(connection.host, connection.management_port) response = requests.get(uri, auth=connection.auth, headers={'accept': 'application/json'}) if response.status_code != 200: raise exceptions.UnexpectedManagementAPIResponse(response.text) results = [] json_doc = json.loads(response.text) for item in json_doc['user-default-list']['list-items']['list-item']: results.append(item['nameref']) return results
def update(self, connection): """ Updates the User on the MarkLogic server. :param connection: The connection to a MarkLogic server :return: The User object """ uri = "http://{0}:{1}/manage/v2/users/{2}/properties" \ .format(connection.host, connection.management_port,self.name) headers = {} if self.etag is not None: headers['if-match'] = self.etag response = requests.put(uri, json=self._config, auth=connection.auth, headers=headers) if response.status_code not in [200, 204]: raise exceptions.UnexpectedManagementAPIResponse(response.text) self.name = self._config['user-name'] if 'etag' in response.headers: self.etag = response.headers['etag'] return self
def list(cls, connection): """ List all the privilege names. Privilege names are structured values, they consist of a kind and a name separated by "|". :param connection: The connection to a MarkLogic server :return: A list of Privilege names. """ uri = "http://{0}:{1}/manage/v2/privileges" \ .format(connection.host, connection.management_port) response = requests.get(uri, auth=connection.auth, headers={'accept': 'application/json'}) if response.status_code != 200: raise exceptions.UnexpectedManagementAPIResponse(response.text) results = [] json_doc = json.loads(response.text) for item in json_doc['privilege-default-list']['list-items'][ 'list-item']: results.append("{0}|{1}|{2}" \ .format(item['kind'],item['nameref'],item['action'])) return results
def create(self, connection): """ Creates the Role on the MarkLogic server. :param connection: The connection to a MarkLogic server :return: The Role object """ uri = "http://{0}:{1}/manage/v2/roles" \ .format(connection.host, connection.management_port) response = requests.post(uri, json=self._config, auth=connection.auth) if response.status_code not in [200, 201, 204]: raise exceptions.UnexpectedManagementAPIResponse(response.text) return self
def delete(self, connection): """ Deletes the Role from the MarkLogic server. :param connection: The connection to a MarkLogic server :return: The Role object """ uri = "http://{0}:{1}/manage/v2/roles/{2}" \ .format(connection.host, connection.management_port, self.name) response = requests.delete(uri, auth=connection.auth) if (response.status_code not in [200, 204] and not response.status_code == 404): raise exceptions.UnexpectedManagementAPIResponse(response.text) return self
def delete(self, connection): """ Deletes the User from the MarkLogic server. :param connection: The connection to a MarkLogic server :return: The User object """ uri = "http://{0}:{1}/manage/v2/users/{2}" \ .format(connection.host, connection.management_port, self.name) headers = {} if self.etag is not None: headers['if-match'] = self.etag response = requests.delete(uri, auth=connection.auth, headers=headers) if (response.status_code not in [200, 204] and not response.status_code == 404): raise exceptions.UnexpectedManagementAPIResponse(response.text) return self
def exists(cls, connection, name): """ Returns true if (and only if) the specified role exits. :param connection: The connection to the MarkLogic database :param name: The name of the role :return: The role """ uri = "http://{0}:{1}/manage/v2/roles/{2}/properties" \ .format(connection.host, connection.management_port, name) response = requests.head(uri, auth=connection.auth, headers={'accept': 'application/json'}) if response.status_code == 200: return True elif response.status_code == 404: return False else: raise exceptions.UnexpectedManagementAPIResponse(response.text)
def lookup(cls, connection, name, kind=None): """ Look up an individual privilege. If the name is a structured value consisting of the kind and the name separated by a "|", as returned by the list() method, then the kind is optional. :param connection: The connection to the MarkLogic database :param name: The name of the privilege :param kind: The kind of privilege :return: The privilege """ parts = name.split("|") if len(parts) == 1: pass elif len(parts) == 2 or len(parts) == 3: if kind is not None and kind != parts[0]: raise validate_custom("Kinds must match") kind = parts[0] name = parts[1] else: raise validate_custom("Unparseable privilege name") uri = "http://{0}:{1}/manage/v2/privileges/{2}/properties?kind={3}" \ .format(connection.host, connection.management_port, name, kind) response = requests.get(uri, auth=connection.auth, headers={'accept': 'application/json'}) if response.status_code == 200: result = Privilege.unmarshal(json.loads(response.text)) if 'etag' in response.headers: result.etag = response.headers['etag'] return result elif response.status_code == 404: return None else: raise exceptions.UnexpectedManagementAPIResponse(response.text)
def lookup(cls, connection, name): """ Look up an individual user. :param name: The name of the user :param connection: The connection to the MarkLogic database :return: The user """ uri = "http://{0}:{1}/manage/v2/users/{2}/properties".format( connection.host, connection.port, name) response = requests.get(uri, auth=connection.auth, headers={'accept': 'application/json'}) if response.status_code == 200: result = User.unmarshal(json.loads(response.text)) if 'etag' in response.headers: result.etag = response.headers['etag'] return result elif response.status_code == 404: return None else: raise exceptions.UnexpectedManagementAPIResponse(response.text)