#
# Copyright 2019 XEBIALABS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

from xlr_xldeploy.XLDeployClientUtil import XLDeployClientUtil

xld_client = XLDeployClientUtil.create_xldeploy_client(xldeployServer,
                                                       username, password)

propertiesXml = ''
for key in properties.keys():
    propertiesXml = '%s<%s>%s</%s>' % (propertiesXml, key, properties[key],
                                       key)

xld_client.create_ci(ciID, ciType, propertiesXml)

if addToEnvironment:
    xld_client.add_ci_to_environment(envID, ciID, ciType)
#
# Copyright 2018 XEBIALABS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

from xlr_xldeploy.XLDeployClientUtil import XLDeployClientUtil


xld_client = XLDeployClientUtil.create_xldeploy_client(xldeployServer, username, password)

applicationId = xld_client.get_latest_deployed_version(environmentId, applicationName)

 def __init__(self, xldeployServer, username, password):
     self.xldClient = XLDeployClientUtil.create_xldeploy_client(
         xldeployServer, username, password)
    parent = path.rpartition('/')[0]
    if parent and not xld_destination_client.check_ci_exist(parent):
        create_path(parent)
    xld_destination_client.create_directory(path)

def get_username():
    if username:
        return username
    return xldeployServer['username']

def get_password():
    if username:
        return password
    return xldeployServer['password']

xld_source_client = XLDeployClientUtil.create_xldeploy_client(xldeployServer, username, password)
xld_destination_client = XLDeployClientUtil.create_xldeploy_client(destinationXLDeployServer, destinationUsername, destinationPassword)

if autoCreatePath:
    app_path = deploymentPackage.rpartition('/')[0]
    if not xld_destination_client.check_ci_exist(app_path):
        parent = app_path.rpartition('/')[0]
        if not xld_destination_client.check_ci_exist(parent):
            create_path(parent)
        xld_destination_client.create_application(app_path)

if xld_destination_client.check_ci_exist(deploymentPackage):
    if idempotent:
        xld_destination_client.delete_ci(deploymentPackage)
    else:
        raise Exception("[%s] already exists on destination server!" % deploymentPackage)
    parent = path.rpartition('/')[0]
    if parent and not xld_destination_client.check_ci_exist(parent):
        create_path(parent)
    xld_destination_client.create_directory(path)

def get_username():
    if username:
        return username
    return xldeployServer['username']

def get_password():
    if username:
        return password
    return xldeployServer['password']

xld_source_client = XLDeployClientUtil.create_xldeploy_client(xldeployServer, username, password)
xld_destination_client = XLDeployClientUtil.create_xldeploy_client(destinationXLDeployServer, destinationUsername, destinationPassword)

if autoCreatePath:
    app_path = deploymentPackage.rpartition('/')[0]
    if not xld_destination_client.check_ci_exist(app_path):
        parent = app_path.rpartition('/')[0]
        if not xld_destination_client.check_ci_exist(parent):
            create_path(parent)
        xld_destination_client.create_application(app_path)

if xld_destination_client.check_ci_exist(deploymentPackage):
    if idempotent:
        xld_destination_client.delete_ci(deploymentPackage)
    else:
        raise Exception("[%s] already exists on destination server!" % deploymentPackage)
 def __init__(self,  xldeployServer, username, password):
     self.xldClient = XLDeployClientUtil.create_xldeploy_client(xldeployServer, username, password)