def create_user(auth, region): json_body = json.loads(frequest.data) if 'default_project_id' not in json_body['user']: msg = 'default_project_id is needed when create user' raise BadRequest(description=msg) kwargs = {'json': json_body} resp = httprequest.httpclient( 'POST', config.os_auth_url + '/v3/users', auth[0], kwargs=kwargs) resp_json = resp.json() if resp.status_code < 300: assignment_resp = httprequest.httpclient( 'PUT', config.os_auth_url + '/v3/projects/%s/users/%s/roles/%s' % ( json_body['user']['default_project_id'], resp_json['user']['id'], config.admin_role_id), auth[0]) if assignment_resp.status_code > 300: resp = httprequest.httpclient( 'DELETE', config.os_auth_url + '/v3/users/%s' % resp_json['user']['id'], auth[0]) msg = 'Fail to create user' raise BadRequest(description=msg) return make_response(json.dumps(resp_json), resp.status_code)
def get_port_monitor(auth, region, port_id): floating_url = auth[2][0] + '/floatingips/' + port_id port_url = auth[2][0] + '/ports?device_id=' + port_id floating_resp = httprequest.httpclient( 'GET', floating_url, auth[0]) port_resp = httprequest.httpclient( 'GET', port_url, auth[0]) port_data = json.loads(port_resp.content) floating_data = json.loads(floating_resp.content) if not floating_data.get('floatingip', '').get('id', None): resp = {'code': 404, 'message': 'Port Not Found'} return make_response(json.dumps(resp), floating_resp.status_code) resp = {} port_data = port_data['ports'][0] floating_data = floating_data['floatingip'] resp['port_id'] = port_id resp['ip_address'] = floating_data['floating_ip_address'] resp['status'] = floating_data['status'].lower() resp['mac_address'] = port_data.get('mac_address', '') resp['inbound_rate'] = 0 resp['outbound_rate'] = 0 resp['health_status'] = True return make_response(json.dumps({'port_monitor': resp}), floating_resp.status_code)
def get_port_monitor(auth, region, port_id): floating_url = auth[2][0] + '/floatingips/' + port_id port_url = auth[2][0] + '/ports?device_id=' + port_id floating_resp = httprequest.httpclient('GET', floating_url, auth[0]) port_resp = httprequest.httpclient('GET', port_url, auth[0]) port_data = json.loads(port_resp.content) floating_data = json.loads(floating_resp.content) if not floating_data.get('floatingip', '').get('id', None): resp = {'code': 404, 'message': 'Port Not Found'} return make_response(json.dumps(resp), floating_resp.status_code) resp = {} port_data = port_data['ports'][0] floating_data = floating_data['floatingip'] resp['port_id'] = port_id resp['ip_address'] = floating_data['floating_ip_address'] resp['status'] = floating_data['status'].lower() resp['mac_address'] = port_data.get('mac_address', '') resp['inbound_rate'] = 0 resp['outbound_rate'] = 0 resp['health_status'] = True return make_response(json.dumps({'port_monitor': resp}), floating_resp.status_code)
def list_servers(auth, region, project_id): kwargs = {'headers': {'X-Openstack-Region': region}} kwargs['params'] = request.args resp = httprequest.httpclient( 'GET', auth[1][0] + '/servers/detail', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def list_snapshots(auth, region, project_id): _check_cinder_project(project_id, auth) kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient( 'GET', auth[3][0] + '/snapshots/detail', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def get_server(auth, region, project_id, server_id): _check_nova_project(project_id, auth) kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient( 'GET', auth[1][0] + '/servers/%s' % server_id, auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def get_resources(auth, region): kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient('GET', config.os_auth_url + '/v3/resources', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def delete_user(auth, region, user_id): resp = httprequest.httpclient( 'DELETE', config.os_auth_url + '/v3/users/%s' % user_id, auth[0]) if resp.status_code == 204: return make_response('', resp.status_code) else: return make_response(json.dumps(resp.json()), resp.status_code)
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)
def create_project(auth, region): json_body = json.loads(frequest.data) kwargs = {'json': json_body} resp = httprequest.httpclient( 'POST', config.os_auth_url + '/v3/projects', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def update_user(auth, region, user_id): json_body = json.loads(frequest.data) kwargs = {'json': json_body} resp = httprequest.httpclient( 'PATCH', config.os_auth_url + '/v3/users/%s' % user_id, auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def list_images(auth, region): kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient('GET', auth[4][0] + '/v1/images/detail', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def get_volume(auth, region, project_id, volume_id): kwargs = {'headers': {'X-Openstack-Region': region}} kwargs['params'] = request.args resp = httprequest.httpclient( 'GET', auth[3][0] + '/volumes/%s' % volume_id, auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def list_snapshots(auth, region, project_id): _check_cinder_project(project_id, auth) kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient('GET', auth[3][0] + '/snapshots/detail', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def update_quotas(auth, region, project_id): json_body = json.loads(frequest.data) kwargs = {'headers': {'X-Openstack-Region': region}} kwargs['json'] = json_body resp = httprequest.httpclient( 'PUT', config.os_auth_url + '/v3/%s/quotas' % project_id, auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def get_server(auth, region, project_id, server_id): _check_nova_project(project_id, auth) kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient('GET', auth[1][0] + '/servers/%s' % server_id, auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def get_volume(auth, region, project_id, volume_id): _check_cinder_project(project_id, auth) kwargs = {'headers': {'X-Openstack-Region': region}} kwargs['params'] = request.args resp = httprequest.httpclient('GET', auth[3][0] + '/volumes/%s' % volume_id, auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
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)
def list_servers(auth, region, project_id): _check_nova_project(project_id, auth) kwargs = {'headers': {'X-Openstack-Region': region}} kwargs['params'] = request.args resp = httprequest.httpclient('GET', auth[1][0] + '/servers/detail', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
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)
def get_quotas(auth, region, project_id): kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient( 'GET', config.os_auth_url + '/v3/%s/quotas' % project_id, auth[0], kwargs=kwargs) resp_json = resp.json() if resp.status_code < 300: resp_json['quota'].update( {'wafs': 0, 'ips': 0, 'anti_virus': 0, 'wafs_used': 0, 'ips_used': 0, 'anti_virus': 0}) return make_response(json.dumps(resp_json), resp.status_code)
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)
def update_quotas(auth, region, project_id): json_body = json.loads(frequest.data) kwargs = {'headers': {'X-Openstack-Region': region}} kwargs['json'] = json_body resp = httprequest.httpclient('PUT', config.os_auth_url + '/v3/%s/quotas' % project_id, auth[0], kwargs=kwargs) if resp.status_code == 204: return make_response('', resp.status_code) else: return make_response(json.dumps(resp.json()), resp.status_code)
def create_user(auth, region): try: json_body = json.loads(frequest.data) if not json_body.get('user'): msg = 'Bad request data in creating v3 user: %r' % frequest.data log.error(msg) resp = {'code': 400, 'message': msg} return make_response(json.dumps(resp), 400) if 'default_project_id' not in json_body['user']: msg = 'default_project_id is needed when create user' raise BadRequest(description=msg) kwargs = {'json': json_body} resp = httprequest.httpclient( 'POST', config.os_auth_url + '/v3/users', auth[0], kwargs=kwargs) resp_json = resp.json() if resp.status_code < 300: assignment_resp = httprequest.httpclient( 'PUT', config.os_auth_url + '/v3/projects/%s/users/%s/roles/%s' % ( json_body['user']['default_project_id'], resp_json['user']['id'], config.member_role_id), auth[0]) if assignment_resp.status_code > 300: resp = httprequest.httpclient( 'DELETE', config.os_auth_url + '/v3/users/%s' % resp_json['user']['id'], auth[0]) msg = 'Fail to create user' raise BadRequest(description=msg) except Exception as e: message = 'Failed to create v3 user: %r' % e log.error(message) resp = {'code': 500, 'messeage': message} return make_response(json.dumps(resp), 500) return make_response(json.dumps(resp_json), resp.status_code)
def create_user(auth, region): try: json_body = json.loads(frequest.data) if not json_body.get('user'): msg = 'Bad request data in creating v3 user: %r' % frequest.data log.error(msg) resp = {'code': 400, 'message': msg} return make_response(json.dumps(resp), 400) if 'default_project_id' not in json_body['user']: msg = 'default_project_id is needed when create user' raise BadRequest(description=msg) kwargs = {'json': json_body} resp = httprequest.httpclient('POST', config.os_auth_url + '/v3/users', auth[0], kwargs=kwargs) resp_json = resp.json() if resp.status_code < 300: assignment_resp = httprequest.httpclient( 'PUT', config.os_auth_url + '/v3/projects/%s/users/%s/roles/%s' % (json_body['user']['default_project_id'], resp_json['user']['id'], config.member_role_id), auth[0]) if assignment_resp.status_code > 300: resp = httprequest.httpclient( 'DELETE', config.os_auth_url + '/v3/users/%s' % resp_json['user']['id'], auth[0]) msg = 'Fail to create user' raise BadRequest(description=msg) except Exception as e: message = 'Failed to create v3 user: %r' % e log.error(message) resp = {'code': 500, 'messeage': message} return make_response(json.dumps(resp), 500) return make_response(json.dumps(resp_json), resp.status_code)
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)
def get_quotas(auth, region, project_id): kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient('GET', config.os_auth_url + '/v3/%s/quotas' % project_id, auth[0], kwargs=kwargs) resp_json = resp.json() if resp.status_code < 300: resp_json['quota'].update({ 'wafs': 0, 'ips': 0, 'anti_virus': 0, 'wafs_used': 0, 'ips_used': 0, 'anti_virus': 0 }) return make_response(json.dumps(resp_json), resp.status_code)
def create_project(auth, region): try: json_body = json.loads(frequest.data) if not json_body.get('project'): msg = 'Bad request data in creating v3 project: %r' % frequest.data log.error(msg) resp = {'code': 400, 'message': msg} return make_response(json.dumps(resp), 400) kwargs = {'json': json_body} resp = httprequest.httpclient( 'POST', config.os_auth_url + '/v3/projects', auth[0], kwargs=kwargs) except Exception as e: message = 'Failed to create project: %r' % e log.error(message) resp = {'code': 500, 'messeage': message} return make_response(json.dumps(resp), 500) return make_response(json.dumps(resp.json()), resp.status_code)
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)
def update_user(auth, region, user_id): try: json_body = json.loads(frequest.data) if not json_body.get('user'): msg = 'Wrong request data in update v3 user: %r' % frequest.data log.error(msg) resp = {'code': 400, 'message': msg} return make_response(json.dumps(resp), 400) kwargs = {'json': json_body} resp = httprequest.httpclient( 'PATCH', config.os_auth_url + '/v3/users/%s' % user_id, auth[0], kwargs=kwargs) except Exception as e: message = 'Failed to update v3 user: %r' % e log.error(message) resp = {'code': 500, 'messeage': message} return make_response(json.dumps(resp), 500) return make_response(json.dumps(resp.json()), resp.status_code)
def create_project(auth, region): try: json_body = json.loads(frequest.data) if not json_body.get('project'): msg = 'Bad request data in creating v3 project: %r' % frequest.data log.error(msg) resp = {'code': 400, 'message': msg} return make_response(json.dumps(resp), 400) kwargs = {'json': json_body} resp = httprequest.httpclient('POST', config.os_auth_url + '/v3/projects', auth[0], kwargs=kwargs) except Exception as e: message = 'Failed to create project: %r' % e log.error(message) resp = {'code': 500, 'messeage': message} return make_response(json.dumps(resp), 500) return make_response(json.dumps(resp.json()), resp.status_code)
def update_user(auth, region, user_id): try: json_body = json.loads(frequest.data) if not json_body.get('user'): msg = 'Wrong request data in update v3 user: %r' % frequest.data log.error(msg) resp = {'code': 400, 'message': msg} return make_response(json.dumps(resp), 400) kwargs = {'json': json_body} resp = httprequest.httpclient('PATCH', config.os_auth_url + '/v3/users/%s' % user_id, auth[0], kwargs=kwargs) except Exception as e: message = 'Failed to update v3 user: %r' % e log.error(message) resp = {'code': 500, 'messeage': message} return make_response(json.dumps(resp), 500) return make_response(json.dumps(resp.json()), resp.status_code)
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)
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)
def get_server_monitor(auth, region, server_id): conn = getDBConn() try: with conn.cursor() as cursor: cursor.callproc('sp_get_server_monitor', (server_id, )) resultSet = cursor.fetchall() server_monitor = {} if len(resultSet) > 0: server_monitor['server_id'] = server_id server_monitor['cpu_usage'] = '-9999' server_monitor['mem_usage'] = '-9999' server_monitor['status'] = '' server_monitor['running_time'] = '-9999' server_monitor['health_status'] = 'false' server_monitor['nics'] = [] server_monitor['disks'] = [] try: kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient( 'GET', auth[1][0] + '/servers/%s' % server_id, auth[0], kwargs=kwargs) if resp.status_code == 200: result = resp.json() vm_server = result.get('server') if vm_server != None: vm_status = vm_server.get('status', '').lower() if vm_status == 'active': server_monitor['status'] = 'active' elif vm_status == '': server_monitor['status'] = '' else: server_monitor['status'] = 'down' except Exception as exc_inside: log.error(exc_inside) for result in resultSet: name = result.get('name', '').lower() output = result.get('output', '').lower() if output == '': continue if name == 'uptime': if output.find('uptime=') >= 0: server_monitor['running_time'] =\ output.split('=', 1)[1] elif name == 'cpu usage': if output.find('cpuusage=') >= 0: server_monitor['cpu_usage'] =\ output.split('=', 1)[1][:-1] elif name == 'memory usage': if output.find('memoryusage=') >= 0: server_monitor['mem_usage'] =\ output.split('=', 1)[1][:-1] elif name == 'health_status': server_monitor['health_status'] = output elif name.startswith('eth'): if output.find('name=') >= 0: server_monitor['nics'].append( getNicsInfo(output)) elif name.startswith('disk'): if output.find('name=') >= 0: server_monitor['disks'].append( getDisksInfo(output)) except Exception as exc: log.error(exc) return make_response(json.dumps(exc.message), 500) finally: conn.close() return make_response( json.dumps({'server_monitor': server_monitor}), 200)
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)
def get_quotas(auth, region, project_id): kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient( 'GET', config.os_auth_url + '/v3/%s/quotas' % project_id, auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def get_user(auth, region, user_id): resp = httprequest.httpclient( 'GET', config.os_auth_url + '/v3/users/%s' % user_id, auth[0]) return make_response(json.dumps(resp.json()), resp.status_code)
def get_project(auth, region, project_id): resp = httprequest.httpclient( 'GET', config.os_auth_url + '/v3/projects/%s' % project_id, auth[0]) return make_response(json.dumps(resp.json()), resp.status_code)
def list_images(auth, region): kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient( 'GET', auth[4][0] + '/images', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def get_server_monitor(auth, region, server_id): conn = getDBConn() try: with conn.cursor() as cursor: cursor.callproc('sp_get_server_monitor', (server_id, )) resultSet = cursor.fetchall() server_monitor = {} if len(resultSet) > 0: server_monitor['server_id'] = server_id server_monitor['cpu_usage'] = '-9999' server_monitor['mem_usage'] = '-9999' server_monitor['status'] = '' server_monitor['running_time'] = '-9999' server_monitor['health_status'] = 'false' server_monitor['nics'] = [] server_monitor['disks'] = [] try: kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient('GET', auth[1][0] + '/servers/%s' % server_id, auth[0], kwargs=kwargs) if resp.status_code == 200: result = resp.json() vm_server = result.get('server') if vm_server != None: vm_status = vm_server.get('status', '').lower() if vm_status == 'active': server_monitor['status'] = 'active' elif vm_status == '': server_monitor['status'] = '' else: server_monitor['status'] = 'down' except Exception as exc_inside: log.error(exc_inside) for result in resultSet: name = result.get('name', '').lower() output = result.get('output', '').lower() if output == '': continue if name == 'uptime': if output.find('uptime=') >= 0: server_monitor['running_time'] =\ output.split('=', 1)[1] elif name == 'cpu usage': if output.find('cpuusage=') >= 0: server_monitor['cpu_usage'] =\ output.split('=', 1)[1][:-1] elif name == 'memory usage': if output.find('memoryusage=') >= 0: server_monitor['mem_usage'] =\ output.split('=', 1)[1][:-1] elif name == 'health_status': server_monitor['health_status'] = output elif name.startswith('eth'): if output.find('name=') >= 0: server_monitor['nics'].append(getNicsInfo(output)) elif name.startswith('disk'): if output.find('name=') >= 0: server_monitor['disks'].append( getDisksInfo(output)) except Exception as exc: log.error(exc) return make_response(json.dumps(exc.message), 500) finally: conn.close() return make_response(json.dumps({'server_monitor': server_monitor}), 200)
def get_service(auth, region): resp = httprequest.httpclient('GET', config.os_auth_url + '/v3/services', auth[0]) return make_response(json.dumps(resp.json()), resp.status_code)
def get_service(auth, region): resp = httprequest.httpclient( 'GET', config.os_auth_url + '/v3/services', auth[0]) return make_response(json.dumps(resp.json()), resp.status_code)
def get_resources(auth, region): kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient( 'GET', config.os_auth_url + '/v3/resources', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)
def list_volumes(auth, region, project_id): kwargs = {'headers': {'X-Openstack-Region': region}} resp = httprequest.httpclient( 'GET', auth[3][0] + '/volumes/detail', auth[0], kwargs=kwargs) return make_response(json.dumps(resp.json()), resp.status_code)