# (c) Copyright 2017-2018 SUSE LLC
#
# 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.
from flask import Blueprint
from flask import jsonify
from flask import request
import ssl
import sys

from . import policy
from .util import ping, url_address

if sys.version_info.major < 3:
    from xmlrpclib import ServerProxy
else:
    from xmlrpc.client import ServerProxy


bp = Blueprint('suse-manager', __name__)

TIMEOUT = 2

"""
Calls to SUSE Manager
"""


def get_client(url, verify_ssl):
    context = ssl._create_default_https_context()
    if verify_ssl == 'false':
        context = ssl._create_unverified_context()

    client = ServerProxy(url, verbose=0, context=context)
    return client


@bp.route("/api/v2/sm/connection_test", methods=['POST'])
@policy.enforce('lifecycle:update_server')
def connection_test():
    context = ssl._create_default_https_context()

    verify_ssl = request.headers.get('Secured')
    if verify_ssl == 'false':
        context = ssl._create_unverified_context()

    creds = request.get_json() or {}
    port = "443"
    if 'port' in creds and creds['port'] != 0:
        port = creds['port']

    # check the host and port first before login
    try:
        ping(creds['host'], int(port))
    except Exception as e:
        return jsonify(error=str(e)), 404

    # login
    try:
        suma_url = "https://" + url_address(creds['host']) + ":" + \
            str(port) + "/rpc/api"
        suma_username = creds['username']
        suma_password = <PASSWORD>['password']
        client = ServerProxy(suma_url, verbose=0, context=context)
        key = client.auth.login(suma_username, suma_password)
        return jsonify(key)
    except Exception as e:
        if 'SSL:' in str(e) or 'doesn\'t match' in str(e):
            return jsonify(error=str(e)), 403
        else:
            return jsonify(error=str(e)), 401


@bp.route("/api/v2/sm/servers")
@policy.enforce('lifecycle:get_server')
def sm_server_list():
    try:
        key = request.headers.get('Auth-Token')
        url = request.headers.get('Suse-Manager-Url')
        verify_ssl = request.headers.get('Secured')

        client = get_client(url, verify_ssl)
        server_list = client.system.listActiveSystems(key)

        return jsonify(server_list)
    except Exception as e:
        return jsonify(error=str(e)), 400


@bp.route("/api/v2/sm/servers/<id>")
@policy.enforce('lifecycle:get_server')
def sm_server_details(id):
    try:
        key = request.headers.get('Auth-Token')
        url = request.headers.get('Suse-Manager-Url')
        verify_ssl = request.headers.get('Secured')

        client = get_client(url, verify_ssl)
        detail_list = client.system.listActiveSystemsDetails(key, int(id))

        detail = detail_list[0]
        detail.update(client.system.getDetails(key, int(id)))
        detail.update({"running_kernel": client.system.getRunningKernel(
            key, int(id))})

        return jsonify(detail)
    except Exception as e:
        return jsonify(error=str(e)), 400

# (c) Copyright 2017-2018 SUSE LLC
#
# 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.
from flask import Blueprint
from flask import jsonify
from flask import request
import ssl
import sys

from . import policy
from .util import ping, url_address

if sys.version_info.major < 3:
    from xmlrpclib import ServerProxy
else:
    from xmlrpc.client import ServerProxy


bp = Blueprint('suse-manager', __name__)

TIMEOUT = 2

"""
Calls to SUSE Manager
"""


def get_client(url, verify_ssl):
    context = ssl._create_default_https_context()
    if verify_ssl == 'false':
        context = ssl._create_unverified_context()

    client = ServerProxy(url, verbose=0, context=context)
    return client


@bp.route("/api/v2/sm/connection_test", methods=['POST'])
@policy.enforce('lifecycle:update_server')
def connection_test():
    context = ssl._create_default_https_context()

    verify_ssl = request.headers.get('Secured')
    if verify_ssl == 'false':
        context = ssl._create_unverified_context()

    creds = request.get_json() or {}
    port = "443"
    if 'port' in creds and creds['port'] != 0:
        port = creds['port']

    # check the host and port first before login
    try:
        ping(creds['host'], int(port))
    except Exception as e:
        return jsonify(error=str(e)), 404

    # login
    try:
        suma_url = "https://" + url_address(creds['host']) + ":" + \
            str(port) + "/rpc/api"
        suma_username = creds['username']
        suma_password = <PASSWORD>['password']
        client = ServerProxy(suma_url, verbose=0, context=context)
        key = client.auth.login(suma_username, suma_password)
        return jsonify(key)
    except Exception as e:
        if 'SSL:' in str(e) or 'doesn\'t match' in str(e):
            return jsonify(error=str(e)), 403
        else:
            return jsonify(error=str(e)), 401


@bp.route("/api/v2/sm/servers")
@policy.enforce('lifecycle:get_server')
def sm_server_list():
    try:
        key = request.headers.get('Auth-Token')
        url = request.headers.get('Suse-Manager-Url')
        verify_ssl = request.headers.get('Secured')

        client = get_client(url, verify_ssl)
        server_list = client.system.listActiveSystems(key)

        return jsonify(server_list)
    except Exception as e:
        return jsonify(error=str(e)), 400


@bp.route("/api/v2/sm/servers/<id>")
@policy.enforce('lifecycle:get_server')
def sm_server_details(id):
    try:
        key = request.headers.get('Auth-Token')
        url = request.headers.get('Suse-Manager-Url')
        verify_ssl = request.headers.get('Secured')

        client = get_client(url, verify_ssl)
        detail_list = client.system.listActiveSystemsDetails(key, int(id))

        detail = detail_list[0]
        detail.update(client.system.getDetails(key, int(id)))
        detail.update({"running_kernel": client.system.getRunningKernel(
            key, int(id))})

        return jsonify(detail)
    except Exception as e:
        return jsonify(error=str(e)), 400

# (c) Copyright 2017-2018 SUSE LLC # # 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. Calls to SUSE Manager # check the host and port first before login # login