Beispiel #1
0
def loadbalance_resrc(auth, region, sub_res, tenant_id=None):
    res_url = get_url(auth, "lb", sub_res, tenant_id)
    try:
        resp = httprequest.httpclient("GET", res_url, auth[0])
        log.info("RESP:" + str(resp.json()))
    except Exception as exc:
        resp = {"code": 404, "message": "RESOURCE NOT FOUND"}
        return make_response(str(resp), 404)
    return make_response(json.dumps(resp.json()), resp.status_code)
Beispiel #2
0
def loadbalance_resrc(auth, region, sub_res, resource_id=None):
    res_url = get_url(auth, 'lb', sub_res, resource_id)
    try:
        resp = httprequest.httpclient('GET', res_url, auth[0])
        log.info('RESP:' + str(resp.json()))
    except Exception as e:
        log(e)
        resp = {'code': 404, 'message': 'RESOURCE NOT FOUND'}
        return make_response(json.dumps(resp), 404)
    return make_response(json.dumps(resp.json()), resp.status_code)
Beispiel #3
0
def loadbalance_resrc(auth, region, sub_res, resource_id=None):
    res_url = get_url(auth, 'lb', sub_res, resource_id)
    try:
        resp = httprequest.httpclient(
            'GET', res_url, auth[0])
        log.info('RESP:' + str(resp.json()))
    except Exception as e:
        log(e)
        resp = {'code': 404, 'message': 'RESOURCE NOT FOUND'}
        return make_response(json.dumps(resp), 404)
    return make_response(json.dumps(resp.json()),
                         resp.status_code)
Beispiel #4
0
def floatingip_resrc(auth, region, resource_id=None):
    res_url = get_url(auth, 'floatingips', resource_id)
    resp = httprequest.httpclient(
        'GET', res_url, auth[0])
    log.info('RESP:' + str(resp.json()))
    return make_response(json.dumps(resp.json()),
                         resp.status_code)
Beispiel #5
0
def firewall_resrc(auth, region, sub_res, tenant_id=None):
    sub_resources = ("firewall_policies", "firewalls", "firewall_rules")
    if sub_res not in sub_resources:
        resp = {"code": 404, "message": "RESOURCE NOT FOUND"}
        return make_response(str(resp), 404)

    res_url = get_url(auth, "fw", sub_res, tenant_id)
    try:
        resp = httprequest.httpclient("GET", res_url, auth[0])
        log.info("RESP:" + str(resp.json()))
        return make_response(json.dumps(resp.json()), resp.status_code)
    except Exception as exc:
        if not tenant_id:
            resp = firewall_default_data(sub_res, is_list=True)
        else:
            resp = firewall_default_data(sub_res, is_list=False)
        return make_response(str(resp), 404)
Beispiel #6
0
def firewall_resrc(auth, region, sub_res, resource_id=None):
    sub_resources = ('firewall_policies', 'firewalls', 'firewall_rules')
    if sub_res not in sub_resources:
        resp = {'code': 404, 'message': 'RESOURCE NOT FOUND'}
        return make_response(json.dumps(resp), 404)

    res_url = get_url(auth, 'fw', sub_res, resource_id)
    try:
        resp = httprequest.httpclient('GET', res_url, auth[0])
        log.info('RESP:' + str(resp.json()))
        if resp.status_code == 404:
            raise Exception('RESOURCE NOT FOUND')
        return make_response(json.dumps(resp.json()), resp.status_code)
    except Exception:
        if not resource_id:
            resp = firewall_default_data(sub_res, is_list=True)
        else:
            resp = firewall_default_data(sub_res, is_list=False)
        return make_response(json.dumps(resp), 200)
Beispiel #7
0
def vpn_res_list(auth, region, project_id, sub_res):
    vpn_resources = []
    sub_resources = {
        'vpnservices': 'vpnservices',
        'ikepolicies': 'ikepolicies',
        'ipsecpolicies': 'ipsecpolicies',
        'ipsec-site-connections': 'ipsec_site_connections'
    }
    if sub_res not in sub_resources.keys():
        resp = {'code': 404, 'message': 'RESOURCE NOT FOUND'}
        return make_response(json.dumps(resp), 404)

    try:
        conn = get_database_conn()
        with conn.cursor() as cursor:
            sql = 'select * from ' + VPN_DB_TABLE_DICT[sub_res] + \
                ' where tenant_id=\'%s\'' % project_id
            result_num = cursor.execute(sql)
            for result in cursor:
                if sub_res in ['ikepolicies', 'ipsecpolicies']:
                    units = result.pop('lifetime_units')
                    value = result.pop('lifetime_value')
                    result['lifetime'] = {'units': units, 'value': value}
                if sub_res == 'ipsec-site-connections':
                    action = result.pop('dpd_action')
                    interval = result.pop('dpd_interval')
                    timeout = result.pop('dpd_timeout')
                    result['dpd'] = {
                        'action': action,
                        'interval': interval,
                        'timeout': timeout
                    }
                    result['peer_cidrs'] = result['peer_cidrs'].split(',')
                vpn_resources.append(result)
    except Exception as e:
        message = 'Failed to get %s list: %r' % (sub_res, e)
        log.error(message)
        response = {'code': 500, 'message': message}
        return make_response(json.dumps(response), 500)
    else:
        body = {sub_resources[sub_res]: vpn_resources}
        return make_response(json.dumps(body), 200)
Beispiel #8
0
def vpn_res_list(auth, region, project_id, sub_res):
    vpn_resources = []
    sub_resources = {'vpnservices': 'vpnservices',
                     'ikepolicies': 'ikepolicies',
                     'ipsecpolicies': 'ipsecpolicies',
                     'ipsec-site-connections': 'ipsec_site_connections'}
    if sub_res not in sub_resources.keys():
        resp = {'code': 404, 'message': 'RESOURCE NOT FOUND'}
        return make_response(json.dumps(resp), 404)

    try:
        conn = get_database_conn()
        with conn.cursor() as cursor:
            sql = 'select * from ' + VPN_DB_TABLE_DICT[sub_res] + \
                ' where tenant_id=\'%s\'' % project_id
            result_num = cursor.execute(sql)
            for result in cursor:
                if sub_res in ['ikepolicies', 'ipsecpolicies']:
                    units = result.pop('lifetime_units')
                    value = result.pop('lifetime_value')
                    result['lifetime'] = {'units': units,
                                          'value': value}
                if sub_res == 'ipsec-site-connections':
                    action = result.pop('dpd_action')
                    interval = result.pop('dpd_interval')
                    timeout = result.pop('dpd_timeout')
                    result['dpd'] = {'action': action,
                                     'interval': interval,
                                     'timeout': timeout}
                    result['peer_cidrs'] = result['peer_cidrs'].split(',')
                vpn_resources.append(result)
    except Exception as e:
        message = 'Failed to get %s list: %r' % (sub_res, e)
        log.error(message)
        response = {'code': 500, 'message': message}
        return make_response(json.dumps(response), 500)
    else:
        body = {sub_resources[sub_res]: vpn_resources}
        return make_response(json.dumps(body), 200)
Beispiel #9
0
def firewall_resrc(auth, region, sub_res, resource_id=None):
    sub_resources = ('firewall_policies',
                     'firewalls', 'firewall_rules')
    if sub_res not in sub_resources:
        resp = {'code': 404, 'message': 'RESOURCE NOT FOUND'}
        return make_response(json.dumps(resp), 404)

    res_url = get_url(auth, 'fw', sub_res, resource_id)
    try:
        resp = httprequest.httpclient(
            'GET', res_url, auth[0])
        log.info('RESP:' + str(resp.json()))
	if resp.status_code == 404:
	    raise Exception('RESOURCE NOT FOUND')
        return make_response(json.dumps(resp.json()),
                             resp.status_code)
    except Exception:
        if not resource_id:
            resp = firewall_default_data(sub_res, is_list=True)
        else:
            resp = firewall_default_data(sub_res, is_list=False)
        return make_response(json.dumps(resp), 200)
Beispiel #10
0
def vpn_res_get(auth, region, project, sub_res, resource_id):
    vpn_resource = {}
    sub_resources = {'vpnservices': 'vpnservice',
                     'ikepolicies': 'ikepolicy',
                     'ipsecpolicies': 'ipsecpolicy',
                     'ipsec-site-connections': 'ipsec_site_connection'}
    if sub_res not in sub_resources.keys():
        resp = {'code': 404, 'message': 'RESOURCE NOT FOUND'}
        return make_response(json.dumps(resp), 404)
    if not verify_id(resource_id):
        message = 'Invalid %s id %s! Please check it.'\
            % (sub_res, resource_id)
        response = {'code': 401, 'message': message}
        return make_response(json.dumps(message), 401)

    try:
        conn = get_database_conn()
        with conn.cursor() as cursor:
            sql = 'select * from ' + VPN_DB_TABLE_DICT[sub_res] + \
                ' where id=\'%s\'' % resource_id
            result_num = cursor.execute(sql)
            if result_num == 1:
                result = cursor.fetchone()
                if sub_res in ['ikepolicies', 'ipsecpolicies']:
                    units = result.pop('lifetime_units')
                    value = result.pop('lifetime_value')
                    result['lifetime'] = {'units': units,
                                          'value': value}
                if sub_res == 'ipsec-site-connections':
                    action = result.pop('dpd_action')
                    interval = result.pop('dpd_interval')
                    timeout = result.pop('dpd_timeout')
                    result['dpd'] = {'action': action,
                                     'interval': interval,
                                     'timeout': timeout}
                    result['peer_cidrs'] = result['peer_cidrs'].split(',')
                vpn_resource = result
            elif result_num == 0:
                message = 'Unable to find ' + sub_res + \
                    ' with id ' + resource_id
                log.debug(message)
                response = {'code': 404, 'message': message}
                return make_response(json.dumps(response), 404)
            else:
                message = 'Unknown error.'
                log.error(message)
                response = {'code': 500, 'message': message}
                return make_response(json.dumps(response), 500)

    except Exception as e:
        message = 'Failed to get %s by id %s: %r:' \
            % (sub_res, resource_id, e)
        log.error(message)
        response = {'code': 500, 'message': message}
        return make_response(json.dumps(response), 500)
    else:
        body = {sub_resources[sub_res]: vpn_resource}
        return make_response(json.dumps(body), 200)
Beispiel #11
0
def floatingip_resrc(auth, region, resource_id=None):
    res_url = get_url(auth, 'floatingips', resource_id)
    resp = httprequest.httpclient('GET', res_url, auth[0])
    log.info('RESP:' + str(resp.json()))
    return make_response(json.dumps(resp.json()), resp.status_code)
Beispiel #12
0
def vpn_res_get(auth, region, project, sub_res, resource_id):
    vpn_resource = {}
    sub_resources = {
        'vpnservices': 'vpnservice',
        'ikepolicies': 'ikepolicy',
        'ipsecpolicies': 'ipsecpolicy',
        'ipsec-site-connections': 'ipsec_site_connection'
    }
    if sub_res not in sub_resources.keys():
        resp = {'code': 404, 'message': 'RESOURCE NOT FOUND'}
        return make_response(json.dumps(resp), 404)
    if not verify_id(resource_id):
        message = 'Invalid %s id %s! Please check it.'\
            % (sub_res, resource_id)
        response = {'code': 401, 'message': message}
        return make_response(json.dumps(message), 401)

    try:
        conn = get_database_conn()
        with conn.cursor() as cursor:
            sql = 'select * from ' + VPN_DB_TABLE_DICT[sub_res] + \
                ' where id=\'%s\'' % resource_id
            result_num = cursor.execute(sql)
            if result_num == 1:
                result = cursor.fetchone()
                if sub_res in ['ikepolicies', 'ipsecpolicies']:
                    units = result.pop('lifetime_units')
                    value = result.pop('lifetime_value')
                    result['lifetime'] = {'units': units, 'value': value}
                if sub_res == 'ipsec-site-connections':
                    action = result.pop('dpd_action')
                    interval = result.pop('dpd_interval')
                    timeout = result.pop('dpd_timeout')
                    result['dpd'] = {
                        'action': action,
                        'interval': interval,
                        'timeout': timeout
                    }
                    result['peer_cidrs'] = result['peer_cidrs'].split(',')
                vpn_resource = result
            elif result_num == 0:
                message = 'Unable to find ' + sub_res + \
                    ' with id ' + resource_id
                log.debug(message)
                response = {'code': 404, 'message': message}
                return make_response(json.dumps(response), 404)
            else:
                message = 'Unknown error.'
                log.error(message)
                response = {'code': 500, 'message': message}
                return make_response(json.dumps(response), 500)

    except Exception as e:
        message = 'Failed to get %s by id %s: %r:' \
            % (sub_res, resource_id, e)
        log.error(message)
        response = {'code': 500, 'message': message}
        return make_response(json.dumps(response), 500)
    else:
        body = {sub_resources[sub_res]: vpn_resource}
        return make_response(json.dumps(body), 200)
Beispiel #13
0
def floatingip_resrc(auth, region, tenant_id=None):
    res_url = get_url(auth, "floatingips", tenant_id)
    resp = httprequest.httpclient("GET", res_url, auth[0])
    log.info("RESP:" + str(resp.json()))
    return make_response(json.dumps(resp.json()), resp.status_code)