Skip to content
This repository was archived by the owner on Sep 8, 2023. It is now read-only.

Support CMA payloads #92

Open
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ __pycache__/

# Distribution / packaging
.Python
.idea
env/
build/
develop-eggs/
Expand Down
75 changes: 75 additions & 0 deletions examples/data/cma_payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"params": {
"mlRole": "cma_role",
"mlUser": "cma-user",
"mlUserPassword": "password",
"mlForest": "cma_forest",
"mlSchemaDatabase": "cma_schema_database",
"mlDatabase": "cma_database",
"mlAppServer": "cma_appserver",
"mlGroup": "cma_group",
"mlAuth": "digest",
"mlPort": "8005"

},
"config": [{
"role": [{
"role-name": "%%mlRole%%",
"description": "A role that allows the user to read and write to rest",
"role": [
"rest-reader",
"rest-extension-user",
"rest-writer"
],
"privilege": [{
"privilege-name": "xdmp:eval",
"action": "http://marklogic.com/xdmp/privileges/xdmp-eval",
"kind": "execute"
}]
}
],
"user": [{
"user-name": "%%mlUser%%",
"description": "A user with the rest role with eval",
"password": "%%mlUserPassword%%",
"role": ["%%mlRole%%"]
}],
"forest":[
{"forest-name":"%%mlForest%%"}
],
"database": [
{
"database-name": "%%mlSchemaDatabase%%"
},
{
"database-name": "%%mlDatabase%%",
"range-element-index": [],
"triggers-database": "%%mlSchemaDatabase%%",
"triple-index": true,
"collection-lexicon": true,
"uri-lexicon": true,
"forest":["%%mlForest%%"]
}
],
"group":[
{
"group-name":"%%mlGroup%%"
}
],
"server": [{
"server-name": "%%mlAppServer%%",
"server-type": "http",
"root": "/",
"group-name": "%%mlGroup%%",
"port": "%%mlPort%%",
"modules-database": "%%mlDatabase%%",
"content-database": "%%mlDatabase%%",
"authentication": "%%mlAuth%%",
"default-error-format": "json",
"error-handler": "/MarkLogic/rest-api/error-handler.xqy",
"url-rewriter": "/MarkLogic/rest-api/rewriter.xml",
"rewrite-resolves-globally": true
}
]
}]
}
121 changes: 121 additions & 0 deletions examples/data/cma_payload.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<configuration xmlns="http://marklogic.com/manage/config">
<configs>
<config>
<params>
<param>
<name>mlAppServer</name>
<value>cma_appserver</value>
</param>
<param>
<name>mlAuth</name>
<value>digest</value>
</param>
<param>
<name>mlDatabase</name>
<value>cma_database</value>
</param>
<param>
<name>mlForest</name>
<value>cma_forest</value>
</param>
<param>
<name>mlGroup</name>
<value>cma_group</value>
</param>
<param>
<name>mlAppServer</name>
<value>cma_appserver</value>
</param>
<param>
<name>mlPort</name>
<value>8005</value>
</param>
<param>
<name>mlRole</name>
<value>cma_role</value>
</param>
<param>
<name>mlSchemaDatabase</name>
<value>cma_schema_database</value>
</param>
<param>
<name>mlUser</name>
<value>cma-user</value>
</param>
<param>
<name>mlUserPassword</name>
<value>password</value>
</param>
</params>
<roles>
<role>
<role-name>%%mlRole%%</role-name>
<description>A role that allows the user to read and write to rest</description>
<privileges>
<privilege>
<action>http://marklogic.com/xdmp/privileges/xdmp-eval</action>
<kind>execute</kind>
<privilege-name>xdmp:eval</privilege-name>
</privilege>
</privileges>
<roles>
<role>rest-reader</role>
<role>rest-extension-user</role>
<role>rest-writer</role>
</roles>
</role>
</roles>
<users>
<user>
<user-name>%%mlUser%%</user-name>
<description>A user with the rest role with eval</description>
<password>%%mlUserPassword%%</password>
<roles>
<role>%%mlRole%%</role>
</roles>
</user>
</users>
<forests>
<forest>
<forest-name>%%mlForest%%</forest-name>
</forest>
</forests>
<databases>
<database>
<database-name>%%mlSchemaDatabase%%</database-name>
</database>
<database>
<database-name>%%mlDatabase%%</database-name>
<collection-lexicon>true</collection-lexicon>
<triggers-database>%%mlSchemaDatabase%%</triggers-database>
<triple-index>true</triple-index>
<uri-lexicon>true</uri-lexicon>
<forests>
<forest>%%mlForest%%</forest>
</forests>
</database>
</databases>
<groups>
<group>
<group-name>%%mlGroup%%</group-name>
</group>
</groups>
<servers>
<server>
<server-name>%%mlAppServer%%</server-name>
<server-type>http</server-type>
<authentication>%%mlAuth%%</authentication>
<content-database>%%mlDatabase%%</content-database>
<default-error-format>json</default-error-format>
<error-handler>/MarkLogic/rest-api/error-handler.xqy</error-handler>
<group-name>%%mlGroup%%</group-name>
<modules-database>%%mlDatabase%%</modules-database>
<port>%%mlPort%%</port>
<rewrite-resolves-globally>true</rewrite-resolves-globally>
<root>/</root>
<url-rewriter>/MarkLogic/rest-api/rewriter.xml</url-rewriter>
</server>
</servers>
</config>
</configs>
</configuration>
121 changes: 121 additions & 0 deletions marklogic/client/cma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# -*- coding: utf-8 -*-
#
# Copyright 2015 MarkLogic Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
Support the CMA V3 endpoint
"""

import json, logging
import requests
from marklogic.exceptions import *

class CMA:
"""
CMA class provides methods to generate and apply application configuration.
Sample payloads in examples/data
"""

_cma_version = "v3"
_min_support_version = 9.0-5

def __init__(self, connection, params={}, scenario=None, format="json"):
self.connection = connection
self.params = params
self.scenario = scenario
self.format = format
self.logger = logging.getLogger("marklogic.client.cma")

@property
def scenario(self):
"""
Scenario property for generate config(ex. ha-local)
:return:scenario as string
"""
return self.__scenario

@scenario.setter
def scenario(self, scenario):
self.__scenario = scenario

#
@property
def params(self):
"""
Params for generate and apply config
ref: http://docs.marklogic.com/REST/configuration-management-api
:return:params as dict
"""
return self.__params

@params.setter
def params(self, params):
if isinstance(params, dict):
self.__params = params
else:
raise Exception("Params should be a dict")

@property
def format(self):
return self.__format

@format.setter
def format(self, format):
self.__format = format

def generate_config(self):
"""
Generate configuration for different scenarios
:return configuration as string(format : xml/json) or binary(format:zip)
"""

cma_url = "http://{0}:{1}/{2}/{3}" \
.format(self.connection.host, self.connection.management_port, self.connection.root, self._cma_version)

cma_url = (cma_url + "?format={}").format(self.format)

if self.scenario:
cma_url = (cma_url+"&scenario={}").format(self.scenario)
if self.params:
cma_url = (cma_url+"&params={}").format(json.dumps(self.params))

print(cma_url)

response = requests.get(cma_url, auth=self.connection.auth)

if response.status_code == 404:
return None
elif response.status_code == 200:
return response.text
else:
raise UnexpectedAPIResponse(response.text)

def apply_config(self, config, content_type):
"""
Applies the specified configuration on MarkLogic
:param config: configuration to be applied as string
:param content_type: application/json or application/xml
:return response object
"""
cma_url = "http://{0}:{1}/{2}/{3}" \
.format(self.connection.host, self.connection.management_port, self.connection.root, self._cma_version)
if self.params:
cma_url = (cma_url+"&params={}").format(self.params)
response = requests.post(cma_url, data=config, auth=self.connection.auth,
headers={'content-type': content_type})
if response.status_code > 299:
raise UnexpectedAPIResponse(response.text)
return response
Loading