Esempio n. 1
0
 def test_keypair_get_keypair_not_found(self):
     gid = "12345678-1234-5678-9123-123456789012"
     self._create_group(gid)
     values = self._get_base_values(gid)
     db.keypair_create(self.user_ctxt, values)
     self.assertRaises(exception.KeypairNotFound,
                       db.keypair_get_by_keypair_id,
                       self.user_ctxt, gid, "aaaaa")
Esempio n. 2
0
    def test_keypair_create(self):
        gid = "12345678-1234-5678-9123-123456789012"
        self._create_group(gid)

        values = self._get_base_values(gid)
        keypair = db.keypair_create(self.user_ctxt, values)
        ignored_keys = ['deleted', 'deleted_at', 'updated_at',
                        'created_at']
        self._assertEqualObjects(keypair, values, ignored_keys)
Esempio n. 3
0
 def test_keypair_delete(self):
     gid = "12345678-1234-5678-9123-123456789012"
     self._create_group(gid)
     values_before = self._get_base_values(gid)
     keypair = db.keypair_create(self.user_ctxt, values_before)
     keypair_after = db.keypair_delete(
         self.user_ctxt, gid, keypair["keypair_id"])
     self.assertEqual("DELETING", keypair_after["status"])
     self.assertEqual(1, keypair_after["deleted"])
     self.assertIsNotNone(keypair_after.get("deleted_at"))
Esempio n. 4
0
    def create(self, req, body, gid):
        def _validate(context, body, gid):
            if not self.is_valid_body(body, 'keypair'):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            self._uuid_check(gid)
            values = body["keypair"]
            name = values.get("name")
            is_default = values.get("is_default")

            if is_default:
                try:
                    is_default = strutils.bool_from_string(is_default,
                                                           strict=True)
                    keypairs = db.keypair_get_all(context,
                                                  gid,
                                                  filters={"is_default": True})
                    if keypairs:
                        msg = _("Default keypair already exists in the "
                                "group %s" % gid)
                        raise exception.InvalidInput(reason=msg)
                except ValueError:
                    msg = _("is_default must be a boolean")
                    raise exception.InvalidInput(reason=msg)
            else:
                is_default = False

            valid_values = {}
            valid_values["gid"] = gid
            valid_values["display_name"] = name
            valid_values["is_default"] = is_default
            return valid_values

        try:
            context = req.environ['rack.context']
            values = _validate(context, body, gid)
            db.group_get_by_gid(context, gid)
            values["keypair_id"] = unicode(uuid.uuid4())
            if not values["display_name"]:
                values["display_name"] = "keypair-" + values["keypair_id"]
            result_value = self.manager.keypair_create(context,
                                                       values["display_name"])
            values.update(result_value)
            values["user_id"] = context.user_id
            values["project_id"] = context.project_id
            keypair = db.keypair_create(context, values)
        except exception.InvalidInput as e:
            raise webob.exc.HTTPBadRequest(explanation=e.format_message())
        except exception.GroupNotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())

        return self._view_builder.create(keypair)
Esempio n. 5
0
 def _create_keypair(self, gid):
     values = {
         "gid": gid,
         "keypair_id": unicode(uuid.uuid4()),
         "private_key": "test",
         "user_id": self.user_ctxt.user_id,
         "project_id": self.user_ctxt.project_id,
         "display_name": "test_keypair",
         "is_default": False,
         "status": "ACTIVE",
         "deleted": 0
     }
     return db.keypair_create(self.user_ctxt, values)
Esempio n. 6
0
 def test_keypair_update(self):
     gid = "12345678-1234-5678-9123-123456789012"
     self._create_group(gid)
     values_before = self._get_base_values(gid)
     keypair = db.keypair_create(self.user_ctxt, values_before)
     values = {
         "is_default": False,
         "status": "ACTIVE",
     }
     keypair_after = db.keypair_update(
         self.user_ctxt, gid, keypair["keypair_id"], values)
     self.assertEqual(keypair_after["is_default"], False)
     self.assertEqual(keypair_after["status"], "ACTIVE")
Esempio n. 7
0
 def _create_keypair(self, gid, values):
     v = self._get_base_values(gid)
     v.update(values)
     return db.keypair_create(self.user_ctxt, v)
Esempio n. 8
0
    def create(self, req, body, gid):
        def _validate(body, gid):
            if not self.is_valid_body(body, "keypair"):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            self._uuid_check(gid)
            values = body["keypair"]
            name = values.get("name")
            is_default = values.get("is_default")

            if name:
                if isinstance(name, six.string_types):
                    name = name.strip()
                utils.check_string_length(name, "name", min_length=1, max_length=255)

            if is_default:
                try:
                    is_default = strutils.bool_from_string(is_default, strict=True)
                except ValueError:
                    msg = _("is_default must be a boolean")
                    raise exception.InvalidInput(reason=msg)
            else:
                is_default = False

            valid_values = {}
            valid_values["gid"] = gid
            valid_values["display_name"] = name
            valid_values["is_default"] = is_default
            return valid_values

        try:
            values = _validate(body, gid)
        except exception.InvalidInput as e:
            raise webob.exc.HTTPBadRequest(explanation=e.format_message())
        except exception.GroupNotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())

        context = req.environ["rack.context"]
        values["keypair_id"] = unicode(uuid.uuid4())
        if not values["display_name"]:
            values["display_name"] = "keypair-" + values["keypair_id"]
        values["user_id"] = context.user_id
        values["project_id"] = context.project_id
        values["status"] = "BUILDING"

        try:
            db.group_get_by_gid(context, gid)
            keypair = db.keypair_create(context, values)
            host = self.scheduler_rpcapi.select_destinations(context, request_spec={}, filter_properties={})
            self.operator_rpcapi.keypair_create(
                context, host["host"], gid=gid, keypair_id=values["keypair_id"], name=values["display_name"]
            )
        except exception.GroupNotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())
        except Exception:
            keypair_id = values["keypair_id"]
            db.keypair_update(context, gid, keypair_id, {"status": "ERROR"})
            raise exception.KeypairCreateFailed()

        return self._view_builder.create(keypair)