def test_delete_parent_child_relation(self): self.stubs.Set(db, "process_delete", fake_delete) self.mox.StubOutWithMock(db, 'process_get_all') self.mox.StubOutWithMock( scheduler_rpcapi.SchedulerAPI, "select_destinations") self.mox.StubOutWithMock( operator_rpcapi.ResourceOperatorAPI, "process_delete") self._set_mox_resource_operator_process_delete() self._set_mox_resource_operator_process_delete() db.process_get_all( IsA(context.RequestContext), GID, {"ppid": PID1})\ .AndReturn([{"pid": PID2}]) db.process_get_all( IsA(context.RequestContext), GID, {"ppid": PID2}).AndReturn([{}]) self.mox.ReplayAll() url = get_base_url(GID) + "/" + PID1 req = get_request(url, "DELETE") res = req.get_response(self.app) self.assertEqual(res.status_code, 204)
def test_delete_raise_exception_keypair_inuse(self): db.process_get_all(IsA(context.RequestContext), GID, filters={"keypair_id": KEYPAIR_ID})\ .AndRaise(exception.keypairInUse(keypair_id=KEYPAIR_ID)) self.mox.ReplayAll() url = "/v1/groups/" + GID + "/keypairs/" + KEYPAIR_ID req = get_request(url, "DELETE") res = req.get_response(self.app) self.assertEqual(res.status_code, 409)
def test_delete_raise_exception_keypair_inuse(self): self.mox.StubOutWithMock(db, "process_get_all") db.process_get_all(IsA(context.RequestContext), GID, filters={ "keypair_id": KEYPAIR_ID }).AndReturn([{}]) self.mox.ReplayAll() url = "/v1/groups/" + GID + "/keypairs/" + KEYPAIR_ID req = get_request(url, "DELETE") res = req.get_response(self.app) self.assertEqual(res.status_code, 409)
def test_delete_not_found_exception(self): self.mox.StubOutWithMock(db, 'process_get_all') self.mox.StubOutWithMock(db, 'process_delete') db.process_get_all( IsA(context.RequestContext), GID, {"ppid": PID1}).AndReturn([{}]) db.process_delete(IsA(context.RequestContext), GID, PID1).AndRaise( exception.ProcessNotFound(pid=PID1)) self.mox.ReplayAll() url = get_base_url(GID) + "/" + PID1 req = get_request(url, "DELETE") res = req.get_response(self.app) self.assertEqual(res.status_code, 404)
def test_delete_keypair_not_found(self): db.process_get_all(IsA(context.RequestContext), GID, filters={"keypair_id": KEYPAIR_ID})\ .AndReturn([]) self.mox.StubOutWithMock(db, "keypair_delete") db.keypair_delete(IsA(context.RequestContext), GID, KEYPAIR_ID)\ .AndRaise(exception.KeypairNotFound(keypair_id=KEYPAIR_ID)) self.mox.ReplayAll() url = "/v1/groups/" + GID + "/keypairs/" + KEYPAIR_ID req = get_request(url, "DELETE") res = req.get_response(self.app) self.assertEqual(res.status_code, 404)
def _delete_children(context, gid, pid): processes = db.process_get_all(context, gid, {"ppid": pid}) for process in processes: _delete_children(context, gid, process["pid"]) _delete(context, gid, process["pid"], process["nova_instance_id"]) return
def _validate(body, gid): if not uuidutils.is_uuid_like(gid): raise exception.GroupNotFound(gid=gid) if not self.is_valid_body(body, 'proxy'): msg = _("Invalid request body") raise exception.InvalidInput(reason=msg) proxy = db.process_get_all( context, gid, {"is_proxy": True, "status": "ACTIVE"}) if len(proxy) == 0: msg = _("Proxy instance not exists in this group") raise exception.ProxyNotFound(msg) values = body["proxy"] app_status = values.get("app_status") if not app_status: msg = _("app_status is required") raise exception.InvalidInput(reason=msg) valid_values = {} if values.get("shm_endpoint"): valid_values["shm_endpoint"] = values.get("shm_endpoint") if values.get("ipc_endpoint"): valid_values["ipc_endpoint"] = values.get("ipc_endpoint") if values.get("fs_endpoint"): valid_values["fs_endpoint"] = values.get("fs_endpoint") valid_values["app_status"] = app_status valid_values["pid"] = proxy[0]["pid"] valid_values return valid_values
def test_delete_raise_exception_by_scheduler_rpcapi(self): db.process_get_all(IsA(context.RequestContext), GID, filters={"keypair_id": KEYPAIR_ID})\ .AndReturn([]) scheduler_rpcapi.SchedulerAPI.select_destinations( IsA(context.RequestContext), request_spec={}, filter_properties={})\ .AndRaise(Exception()) self.mox.ReplayAll() url = '/v1/groups/' + GID + '/keypairs/' + KEYPAIR_ID req = get_request(url, "DELETE") res = req.get_response(self.app) self.assertEqual(res.status_code, 500)
def test_process_get_all(self): processes = self._create_process(self.gid, 3) res_processes = db.process_get_all(context, self.gid) ignored_keys = ['deleted_at', 'updated_at', 'created_at'] self.assertEqual(len(res_processes), len(processes)) for i in range(0, len(res_processes)): self._assertEqualObjects( res_processes[i], processes[i], ignored_keys)
def _get_child_pid(context, gid, pid): processes = db.process_get_all(context, gid, {"ppid": pid}) targets = [] for process in processes: if "pid" in process: targets.append(process["pid"]) targets.extend( _get_child_pid(context, gid, process["pid"])) return targets
def index(self, req, gid): try: self._uuid_check(gid) except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) context = req.environ['rack.context'] process_list = db.process_get_all(context, gid) process_list = self.manager.process_list(context, process_list) return self._view_builder.index(process_list)
def test_delete(self): db.process_get_all(IsA(context.RequestContext), GID, filters={"keypair_id": KEYPAIR_ID})\ .AndReturn([]) scheduler_rpcapi.SchedulerAPI.select_destinations( IsA(context.RequestContext), request_spec={}, filter_properties={})\ .AndReturn({"host": "fake_host"}) operator_rpcapi.ResourceOperatorAPI.keypair_delete( IsA(context.RequestContext), "fake_host", nova_keypair_id=IsA(str)) self.mox.ReplayAll() url = "/v1/groups/" + GID + "/keypairs/" + KEYPAIR_ID req = get_request(url, "DELETE") res = req.get_response(self.app) self.assertEqual(res.status_code, 204)
def test_delete_keypair_not_found(self): self.mox.StubOutWithMock(db, "process_get_all") self.mox.StubOutWithMock(db, "keypair_get_by_keypair_id") self.mox.StubOutWithMock(manager.ResourceOperator, "keypair_delete") db.process_get_all(IsA(context.RequestContext), GID, filters={ "keypair_id": KEYPAIR_ID }).AndReturn([]) db.keypair_get_by_keypair_id(IsA(context.RequestContext), GID, KEYPAIR_ID).AndReturn( {"nova_keypair_id": KEYPAIR_ID}) manager.ResourceOperator.keypair_delete(IsA( context.RequestContext), KEYPAIR_ID).AndRaise(exception.NotFound()) self.mox.ReplayAll() url = "/v1/groups/" + GID + "/keypairs/" + KEYPAIR_ID req = get_request(url, "DELETE") res = req.get_response(self.app) self.assertEqual(res.status_code, 404)
def _validate(context, body, gid): self._uuid_check(gid) process = db.process_get_all(context, gid, filters={"is_proxy": True}) if not process: msg = _("Proxy process does not exist in the group %s" % gid) raise exception.InvalidInput(reason=msg) if not self.is_valid_body(body, 'proxy'): msg = _("Invalid request body") raise exception.InvalidInput(reason=msg) values = body["proxy"] app_status = values.get("app_status") ipc_endpoint = values.get("ipc_endpoint") shm_endpoint = values.get("shm_endpoint") fs_endpoint = values.get("fs_endpoint") valid_values = {} if app_status: utils.check_string_length(app_status, 'app_status', min_length=1, max_length=255) valid_values["app_status"] = app_status if ipc_endpoint: utils.check_string_length(ipc_endpoint, 'ipc_endpoint', min_length=1, max_length=255) valid_values["ipc_endpoint"] = ipc_endpoint if shm_endpoint: utils.check_string_length(shm_endpoint, 'shm_endpoint', min_length=1, max_length=255) valid_values["shm_endpoint"] = shm_endpoint if fs_endpoint: utils.check_string_length(fs_endpoint, 'fs_endpoint', min_length=1, max_length=255) valid_values["fs_endpoint"] = fs_endpoint if not valid_values: msg = _("No keyword is provided.") raise exception.InvalidInput(reason=msg) return process[0]["pid"], valid_values
def show_proxy(self, req, gid): try: self._uuid_check(gid) context = req.environ['rack.context'] process = db.process_get_all(context, gid, filters={"is_proxy": True}) if not process: msg = _("Proxy process does not exist in the group %s" % gid) raise webob.exc.HTTPBadRequest(explanation=msg) self.manager.process_show(context, process[0]) except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return self._view_builder.show_proxy(process[0])
def index(self, req, gid): def _validate(gid): if not uuidutils.is_uuid_like(gid): raise exception.GroupNotFound(gid=gid) try: _validate(gid) except exception.ProcessNotFound: msg = _("Process could not be found") raise webob.exc.HTTPNotFound(explanation=msg) filters = {} pid = req.params.get('pid') ppid = req.params.get('ppid') name = req.params.get('name') status = req.params.get('status') glance_image_id = req.params.get('glance_image_id') nova_flavor_id = req.params.get('nova_flavor_id') securitygroup_id = req.params.get('securitygroup_id') network_id = req.params.get('network_id') keypair_id = req.params.get('keypair_id') if pid: filters['pid'] = pid if ppid: filters['ppid'] = ppid if name: filters['name'] = name if status: filters['status'] = status if glance_image_id: filters['glance_image_id'] = glance_image_id if nova_flavor_id: filters['nova_flavor_id'] = nova_flavor_id if securitygroup_id: filters['securitygroup_id'] = securitygroup_id if network_id: filters['network_id'] = network_id if keypair_id: filters['keypair_id'] = keypair_id context = req.environ['rack.context'] process_list = db.process_get_all(context, gid, filters) return self._view_builder.index(process_list)
def delete(self, req, gid, keypair_id): context = req.environ['rack.context'] try: self._uuid_check(gid=gid, keypair_id=keypair_id) filters = {"keypair_id": keypair_id} processes = db.process_get_all(context, gid, filters=filters) if processes: raise exception.keypairInUse(keypair_id=keypair_id) keypair = db.keypair_get_by_keypair_id(context, gid, keypair_id) self.manager.keypair_delete(context, keypair["nova_keypair_id"]) db.keypair_delete(context, gid, keypair_id) except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.keypairInUse as e: raise webob.exc.HTTPConflict(explanation=e.format_message())
def delete(self, req, gid, keypair_id): context = req.environ["rack.context"] try: self._uuid_check(gid=gid, keypair_id=keypair_id) filters = {"keypair_id": keypair_id} processes = db.process_get_all(context, gid, filters=filters) if processes: raise exception.keypairInUse(keypair_id=keypair_id) keypair = db.keypair_delete(context, gid, keypair_id) host = self.scheduler_rpcapi.select_destinations(context, request_spec={}, filter_properties={}) self.operator_rpcapi.keypair_delete(context, host["host"], nova_keypair_id=keypair["nova_keypair_id"]) except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.keypairInUse as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except Exception as e: LOG.warn(e) raise exception.KeypairDeleteFailed()
def show(self, req, gid): def _validate(gid): if not uuidutils.is_uuid_like(gid): raise exception.GroupNotFound(gid=gid) try: _validate(gid) context = req.environ['rack.context'] proxy = db.process_get_all( context, gid, {"is_proxy": True, "status": "ACTIVE"}) if len(proxy) == 0: msg = _("Proxy instance not exists in this group") raise exception.ProxyNotFound(msg) except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return self._view_builder.show(proxy[0])
def delete(self, req, gid): def _validate(gid): if not uuidutils.is_uuid_like(gid): raise exception.GroupNotFound(gid=gid) try: _validate(gid) context = req.environ['rack.context'] keypairs = db.keypair_get_all(context, gid) if keypairs: raise exception.GroupInUse(gid=gid) securitygroups = db.securitygroup_get_all(context, gid) if securitygroups: raise exception.GroupInUse(gid=gid) networks = db.network_get_all(context, gid) if networks: raise exception.GroupInUse(gid=gid) processes = db.process_get_all(context, gid) if processes: raise exception.GroupInUse(gid=gid) db.group_delete(context, gid) except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.GroupInUse as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except Exception as e: LOG.warn(e) raise exception.GroupDeleteFailed()
def test_process_get_all_empty(self): res_processes = db.process_get_all(context, self.gid) expected = [] self.assertEqual(res_processes, expected)
def _validate(context, body, gid, is_proxy=False): proxy = db.process_get_all(context, gid, filters={"is_proxy": True}) if is_proxy: if len(proxy) > 0: msg = _("Proxy process already exists in the group %s" % gid) raise exception.InvalidInput(reason=msg) else: if len(proxy) != 1: msg = _("Proxy process does not exist in the group %s" % gid) raise webob.exc.HTTPBadRequest(explanation=msg) keyname = "proxy" if is_proxy else "process" if not self.is_valid_body(body, keyname): msg = _("Invalid request body") raise exception.InvalidInput(reason=msg) values = body[keyname] ppid = values.get("ppid") name = values.get("name") keypair_id = values.get("keypair_id") securitygroup_ids = values.get("securitygroup_ids") glance_image_id = values.get("glance_image_id") nova_flavor_id = values.get("nova_flavor_id") userdata = values.get("userdata") args = values.get("args") self._uuid_check(gid, ppid, keypair_id) pid = unicode(uuid.uuid4()) if not name: prefix = "proxy-" if is_proxy else "process-" name = prefix + pid if ppid: parent_process = db.process_get_by_pid(context, gid, ppid) nova_keypair_id = None if keypair_id: keypair = db.keypair_get_by_keypair_id(context, gid, keypair_id) nova_keypair_id = keypair["nova_keypair_id"] elif ppid: keypair_id = parent_process.get("keypair_id") if keypair_id: keypair = db.keypair_get_by_keypair_id( context, gid, keypair_id) nova_keypair_id = keypair["nova_keypair_id"] else: default_keypair = db.keypair_get_all( context, gid, filters={"is_default": True}) if default_keypair: keypair_id = default_keypair[0]["keypair_id"] nova_keypair_id = default_keypair[0]["nova_keypair_id"] if securitygroup_ids is not None and\ not isinstance(securitygroup_ids, list): msg = _("securitygroupids must be a list") raise exception.InvalidInput(reason=msg) elif securitygroup_ids: neutron_securitygroup_ids = [] for id in securitygroup_ids: self._uuid_check(securitygroup_id=id) securitygroup = db.securitygroup_get_by_securitygroup_id( context, gid, id) neutron_securitygroup_ids.append( securitygroup["neutron_securitygroup_id"]) elif ppid: securitygroups = parent_process.get("securitygroups") securitygroup_ids =\ [securitygroup["securitygroup_id"] for securitygroup in securitygroups] neutron_securitygroup_ids =\ [securitygroup["neutron_securitygroup_id"] for securitygroup in securitygroups] else: default_securitygroups = db.securitygroup_get_all( context, gid, filters={"is_default": True}) if default_securitygroups: securitygroup_ids =\ [securitygroup["securitygroup_id"] for securitygroup in default_securitygroups] neutron_securitygroup_ids =\ [securitygroup["neutron_securitygroup_id"] for securitygroup in default_securitygroups] else: msg = _("securitygroup_ids is required. Default \ securitygroup_ids are not registered.") raise exception.InvalidInput(reason=msg) if not glance_image_id and ppid: glance_image_id = parent_process.get("glance_image_id") if not nova_flavor_id and ppid: nova_flavor_id = parent_process.get("nova_flavor_id") if userdata: try: base64.b64decode(userdata) except TypeError: msg = _("userdadta must be a base64 encoded value.") raise exception.InvalidInput(reason=msg) networks = db.network_get_all(context, gid) if not networks: msg = _("Netwoks does not exist in the group %s" % gid) raise webob.exc.HTTPBadRequest(explanation=msg) network_ids =\ [network["network_id"] for network in networks] neutron_network_ids =\ [network["neutron_network_id"] for network in networks] nics = [] for id in neutron_network_ids: nics.append({"net-id": id}) if args is None: args = {} elif args is not None and\ not isinstance(args, dict): msg = _("args must be a dict.") raise exception.InvalidInput(reason=msg) else: for key in args.keys(): args[key] = str(args[key]) default_args = { "gid": gid, "pid": pid, } if ppid: default_args["ppid"] = ppid if is_proxy: default_args["rackapi_ip"] = cfg.CONF.my_ip default_args["os_username"] = cfg.CONF.os_username default_args["os_password"] = cfg.CONF.os_password default_args["os_tenant_name"] = cfg.CONF.os_tenant_name default_args["os_auth_url"] = cfg.CONF.os_auth_url default_args["os_region_name"] = cfg.CONF.os_region_name else: proxy_instance_id = proxy[0]["nova_instance_id"] default_args["proxy_ip"] = self.manager.get_process_address( context, proxy_instance_id) args.update(default_args) valid_values = {} valid_values["gid"] = gid valid_values["ppid"] = ppid valid_values["pid"] = pid valid_values["display_name"] = name valid_values["keypair_id"] = keypair_id valid_values["securitygroup_ids"] = securitygroup_ids valid_values["glance_image_id"] = glance_image_id valid_values["nova_flavor_id"] = nova_flavor_id valid_values["userdata"] = userdata valid_values["args"] = json.dumps(args) valid_values["is_proxy"] = True if is_proxy else False valid_values["network_ids"] = network_ids if is_proxy: ipc_endpoint = values.get("ipc_endpoint") shm_endpoint = values.get("shm_endpoint") fs_endpoint = values.get("fs_endpoint") if ipc_endpoint: utils.check_string_length(ipc_endpoint, 'ipc_endpoint', min_length=1, max_length=255) if shm_endpoint: utils.check_string_length(shm_endpoint, 'shm_endpoint', min_length=1, max_length=255) if fs_endpoint: utils.check_string_length(fs_endpoint, 'fs_endpoint', min_length=1, max_length=255) valid_values["ipc_endpoint"] = ipc_endpoint valid_values["shm_endpoint"] = shm_endpoint valid_values["fs_endpoint"] = fs_endpoint boot_values = {} boot_values["name"] = name boot_values["key_name"] = nova_keypair_id boot_values["security_groups"] = neutron_securitygroup_ids boot_values["image"] = glance_image_id boot_values["flavor"] = nova_flavor_id boot_values["userdata"] = userdata boot_values["meta"] = args boot_values["nics"] = nics return valid_values, boot_values