def GET(self, req, *parts): """ Handle GET Container (List Objects) request """ env = copy.copy(req.environ) env['SCRIPT_NAME'] = self.os_version env['PATH_INFO'] = self.os_path # we will always use json format to get Nova information env['HTTP_ACCEPT'] = 'application/json' # need to remove this header, otherwise, it will always take the # original request accept content type if env.has_key('nova.best_content_type'): env.pop('nova.best_content_type') new_req = Request(env) res = new_req.get_response(self.app) if res.status_int == 200: content = json.loads(res.body) body = {} body['resourceURI'] = concat(self.uri_prefix, self.entity_uri) body['id'] = concat(self.tenant_id, '/', self.entity_uri) body['entries'] = [] flavors = content.get('flavors',[]) for flavor in flavors: entry = {} entry['resourceURI'] = concat(self.uri_prefix, self.entity_uri, 'Entry') entry['machineConfiguration'] = \ {'href': concat(self.tenant_id, '/', 'MachineConfiguration/', flavor['id'])} entry['id'] = concat(self.tenant_id, '/', self.entity_uri, 'Entry/', flavor['id']) body['entries'].append(entry) if self.res_content_type == 'application/xml': response_data = {'Collection': body} else: response_data = body new_content = make_response_data(response_data, self.res_content_type, self.metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers['Content-Type'] = self.res_content_type resp.status = 200 resp.body = new_content return resp else: return res
def _make_entry(key, source_key, entries): adds = {} match_up(adds, data, 'addr', source_key) if adds.get('addr'): entry = {} name = 'MachineNetworkInterfacesCollectionEntry' entry['id'] = concat(self.tenant_id, '/', name, '/', id, '/', key) entry['resourceURI'] = concat(self.uri_prefix, '/', name,) name = 'MachineNetworkInterfaceAddressesCollection' entry['addresses'] = {'href': concat(self.tenant_id, '/', name, '/', id, '/', key)} entries.append(entry)
def GET(self, req, *parts): """ Handle GET Container (List Objects) request """ env = copy.copy(req.environ) env["SCRIPT_NAME"] = self.os_version env["PATH_INFO"] = self.os_path # we will always use json format to get Nova information env["HTTP_ACCEPT"] = "application/json" # need to remove this header, otherwise, it will always take the # original request accept content type if env.has_key("nova.best_content_type"): env.pop("nova.best_content_type") new_req = Request(env) res = new_req.get_response(self.app) if res.status_int == 200: content = json.loads(res.body) body = {} body["resourceURI"] = concat(self.uri_prefix, self.entity_uri) body["id"] = concat(self.tenant_id, "/", self.entity_uri) body["entries"] = [] images = content.get("images", []) for image in images: entry = {} entry["resourceURI"] = concat(self.uri_prefix, self.entity_uri, "Entry") entry["machineImage"] = {"href": concat(self.tenant_id, "/", "MachineImage/", image["id"])} entry["id"] = concat(self.tenant_id, "/", self.entity_uri, "Entry/", image["id"]) body["entries"].append(entry) if self.res_content_type == "application/xml": response_data = {"Collection": body} else: response_data = body new_content = make_response_data(response_data, self.res_content_type, self.metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers["Content-Type"] = self.res_content_type resp.status = 200 resp.body = new_content return resp else: return res
def GET(self, req, *parts): """ Handle GET machine request """ new_req = self._fresh_request(req) res = new_req.get_response(self.app) if res.status_int == 200: content = json.loads(res.body) body = {} body['resourceURI'] = concat(self.uri_prefix, self.entity_uri) body['id'] = concat(self.tenant_id, '/', self.entity_uri) body['entries'] = [] machines = content.get('servers',[]) for machine in machines: entry = {} entry['resourceURI'] = concat(self.uri_prefix, self.entity_uri, 'Entry') entry['machine'] = {'href': concat(self.tenant_id, '/', 'Machine/', machine['id'])} entry['id'] = concat(self.tenant_id, '/', self.entity_uri, 'Entry/', machine['id']) body['entries'].append(entry) if self.res_content_type == 'application/xml': response_data = {'Collection': body} else: response_data = body new_content = make_response_data(response_data, self.res_content_type, self.metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers['Content-Type'] = self.res_content_type resp.status = 200 resp.body = new_content return resp else: return res
def GET(self, req, *parts): """ Handle GET Container (List Objects) request """ env = self._fresh_env(req) env['PATH_INFO'] = concat(self.os_path, '/', '/'.join(parts)) new_req = Request(env) res = new_req.get_response(self.app) if res.status_int == 200: flavor = json.loads(res.body).get('flavor') if flavor: body = {} body['resourceURI'] = concat(self.uri_prefix, self.entity_uri) body['id'] = concat(self.tenant_id, '/', self.entity_uri, '/', self.config_id) match_up(body, flavor, 'name', 'name') match_up(body, flavor, 'cpu', 'vcpus') match_up(body, flavor, 'memory', 'ram') body['disks'] = [] body['disks'].append({'capacity': flavor.get('disk'), 'units': 'gb'}) if self.res_content_type == 'application/xml': body.pop('resourceURI') response_data = {self.entity_uri: body} else: response_data = body new_content = make_response_data(response_data, self.res_content_type, self.metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers['Content-Type'] = self.res_content_type resp.status = 200 resp.body = new_content return resp else: return res return res
def GET(self, req, *parts): """ Handle GET machine request """ env = self._fresh_env(req) env['PATH_INFO'] = concat(self.os_path, '/', parts[0]) new_req = Request(env) res = new_req.get_response(self.app) if res.status_int == 200: data = json.loads(res.body).get('server') body = {} body['id'] = concat(self.tenant_id, '/', self.entity_uri, '/', self.machine_id, '/', self.address_key, '/', self.machine_ip) adds = {} match_up(adds, data, 'addr', 'addresses/'+self.address_key) ips = adds.get('addr') if ips: for ip in ips: if self.machine_ip == ip.get('addr'): body['ip'] = self.machine_ip if self.res_content_type == 'application/xml': response_data = {'Address': body} else: response_data = body new_content = make_response_data(response_data, self.res_content_type, self.metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers['Content-Type'] = self.res_content_type resp.status = 200 resp.body = new_content return resp else: return res
def _make_entry(entries): adds = {} match_up(adds, data, 'addr', 'addresses/'+self.address_key) if adds.get('addr'): ips = adds.get('addr') for addr in ips: entry = {} name = 'MachineNetworkInterfacesAddressesCollectionEntry' entry['id'] = concat(self.tenant_id, '/', name, '/', self.machine_id, '/', self.address_key, '/', addr.get('addr')) entry['resourceURI'] = concat(self.uri_prefix, '/', name,) name = 'MachineNetworkInterfaceAddress' entry['address'] = {'href': concat(self.tenant_id, '/', name, '/', self.machine_id, '/', self.address_key, '/', addr.get('addr'))} entries.append(entry)
def _delete(self, req, request_data, *parts): """ Handle the stop machine request """ env = self._fresh_env(req) env['PATH_INFO'] = concat(self.os_path, '/', parts[0]) env['REQUEST_METHOD'] = 'DELETE' new_req = Request(env) res = new_req.get_response(self.app) return res
def __init__(self, conf, app, req, tenant_id, *args): super(MachineCtrler, self).__init__(conf, app, req, tenant_id, *args) self.os_path = '/%s/servers' % (tenant_id) self.entity_uri = 'Machine' self.metadata = {'attributes': {'memory': ['quantity', 'units'], 'property': 'key', 'volumes': 'href', 'networkInterfaces': 'href', 'Entry': 'resourceURI', 'operation': ['rel', 'href']}, 'plurals': {'entries': 'Entry'}, 'sequence': {'Machine': ['id', 'name', 'description', 'created', 'updated', 'property', 'state', 'cpu', 'memory', 'disks', 'networkInterfaces', 'operations']}} self.actions = {concat(self.uri_prefix, 'action/restart'): 'reboot', concat(self.uri_prefix, 'action/stop'): 'delete'}
def GET(self, req, *parts): """ Handle GET machine request """ env = self._fresh_env(req) env['PATH_INFO'] = concat(self.os_path, '/', parts[0]) new_req = Request(env) res = new_req.get_response(self.app) if res.status_int == 200: data = json.loads(res.body).get('server') body = {} body['id'] = concat(self.tenant_id, '/', self.entity_uri, '/', self.machine_id, '/', self.address_key) body['resourceURI'] = concat(self.uri_prefix, self.entity_uri) body['entries'] = self._get_entry(data) if self.res_content_type == 'application/xml': response_data = {'Collection': body} else: response_data = body new_content = make_response_data(response_data, self.res_content_type, self.metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers['Content-Type'] = self.res_content_type resp.status = 200 resp.body = new_content return resp else: return res
def GET(self, req, *parts): """ Handle GET Container (List Objects) request """ env = self._fresh_env(req) env["PATH_INFO"] = concat(self.os_path, "/", self.image_id) new_req = Request(env) res = new_req.get_response(self.app) if res.status_int == 200: image = json.loads(res.body).get("image") if image: body = {} body["id"] = concat(self.tenant_id, "/", self.entity_uri, "/", self.image_id) match_up(body, image, "name", "name") match_up(body, image, "created", "created") match_up(body, image, "updated", "updated") body["imageLocation"] = body["id"] if self.res_content_type == "application/xml": response_data = {self.entity_uri: body} else: body["resourceURI"] = concat(self.uri_prefix, self.entity_uri) response_data = body new_content = make_response_data(response_data, self.res_content_type, self.metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers["Content-Type"] = self.res_content_type resp.status = 200 resp.body = new_content return resp else: return res return res
def _reboot(self, req, request_data, *parts): """ Handle the machine reboot request """ data = {} force = request_data.get('force', False) if isinstance(force, str): force = 'HARD' if force.lower() == 'true' else 'SOFT' else: force = 'HARD' if force else 'SOFT' data['reboot'] = {'type': force} env = self._fresh_env(req) env['PATH_INFO'] = concat(self.os_path, '/', parts[0], '/action') env['CONTENT_TYPE'] = 'application/json' new_req = Request(env) new_req.body = json.dumps(data) res = new_req.get_response(self.app) return res
def GET(self, req, *parts): """ Handle GET Container (List Objects) request """ body = {} body['id'] = concat(self.tenant_id, '/', self.entity_uri) body['name'] = self.entity_uri body['description'] = 'Cloud Entry Point' body['baseURI'] = concat(req.host_url, self.request_prefix, '/') body['machineConfigs'] = \ {'href': concat(self.tenant_id, '/', 'MachineConfigurationCollection')} body['machineImages'] = \ {'href': concat(self.tenant_id, '/', 'MachineImageCollection')} body['machines'] = \ {'href': concat(self.tenant_id, '/', 'MachineCollection')} if self.res_content_type == 'application/xml': response_data = {'CloudEntryPoint': body} else: body['resourceURI'] = concat(self.uri_prefix, self.entity_uri) response_data = body new_content = make_response_data(response_data, self.res_content_type, self.metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers['Content-Type'] = self.res_content_type resp.status = 200 resp.body = new_content return resp
def GET(self, req, *parts): """ Handle GET Container (List Objects) request """ env = self._fresh_env(req) env['PATH_INFO'] = concat(self.os_path, '/', '/'.join(parts)) new_req = Request(env) res = new_req.get_response(self.app) if res.status_int == 200: data = json.loads(res.body).get('server') body = {} body['id'] = concat(self.tenant_id, '/machine/', parts[0]) match_up(body, data, 'name', 'name') match_up(body, data, 'created', 'created') match_up(body, data, 'updated', 'updated') match_up(body, data, 'state', 'status') body['networkInterfaces'] = {'href': concat(self.tenant_id, '/networkInterfacesCollection/', parts[0])} # Send a request to get the details on flavor env = self._fresh_env(req) env['PATH_INFO'] = '/%s/flavors/%s' % (self.tenant_id, data['flavor']['id']) new_req = Request(env) res = new_req.get_response(self.app) if res.status_int == 200: flavor = json.loads(res.body).get('flavor') match_up(body, flavor, 'cpu', 'vcpus') ram = {} match_up(ram, flavor, 'quantity', 'ram') ram['units'] = 'MB' body['memory'] = ram disks = [] disks.append({'capacity': int(flavor.get('disk')) * 1000}) body['disks'] = disks # deal with machine operations operations = [] name = concat(self.uri_prefix, 'action/stop') operations.append(self._create_op(name, parts[0])) name = concat(self.uri_prefix, 'action/restart') operations.append(self._create_op(name, parts[0])) body['operations'] = operations if self.res_content_type == 'application/xml': response_data = {'Machine': body} else: body['resourceURI'] = concat(self.uri_prefix, self.entity_uri) response_data = body new_content = make_response_data(response_data, self.res_content_type, self.metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers['Content-Type'] = self.res_content_type resp.status = 200 resp.body = new_content return resp else: return res
def _create_op(self, name, id): entry = {} entry['rel'] = name entry['href'] = concat(self.tenant_id, '/machine/', id) return entry
def POST(self, req, *parts): """ Handle POST machine request which will create a machine """ try: request_data = get_request_data(req.body, self.req_content_type) except Exception as error: return get_err_response('MalformedBody') if request_data: data = request_data.get('body').get('MachineCreate') if not data: data = request_data.get('body') if data: new_body = {} match_up(new_body, data, 'name', 'name') keys = {'/cimiv1': '/v2', 'machineConfig': 'flavors', 'machineImage': 'images'} match_up(new_body, data, 'imageRef', 'machineTemplate/machineImage/href') new_body['imageRef'] = sub_path(new_body.get('imageRef'), keys) match_up(new_body, data, 'flavorRef', 'machineTemplate/machineConfig/href') new_body['flavorRef'] = sub_path(new_body.get('flavorRef'), keys) new_req = self._fresh_request(req) adminPass = data.get('credentials', {}).get('password') if adminPass: new_body['adminPass'] = adminPass new_req.body = json.dumps({'server':new_body}) resp = new_req.get_response(self.app) if resp.status_int == 201: # resource created successfully, we redirect the request # to query machine resp_data = json.loads(resp.body) id = resp_data.get('server').get('id') env = self._fresh_env(req) env['PATH_INFO'] = concat(self.request_prefix, '/', self.tenant_id, '/servers/', id) env['REQUEST_METHOD'] = 'GET' new_req = Request(env) resp = new_req.get_response(self.app) resp.status = 201 elif resp.status_int == 202: resp_body_data = json.loads(resp.body).get('server') id = resp_body_data.get('id') resp_data = {} match_up(resp_data, data, 'name', 'name') match_up(resp_data, data, 'description', 'description') resp_data['id'] = concat(self.tenant_id, '/machine/', id) resp_data['credentials'] = {'userName': '******', 'password': resp_body_data.get('adminPass')} if self.res_content_type == 'application/xml': response_data = {'Machine': resp_data} else: response_data = resp_data new_content = make_response_data(response_data, self.res_content_type, self.machine_metadata, self.uri_prefix) resp = Response() self._fixup_cimi_header(resp) resp.headers['Content-Type'] = self.res_content_type resp.status = 202 resp.body = new_content return resp else: return get_err_response('BadRequest') else: return get_err_response('BadRequest')