Ejemplo n.º 1
0
    def test_update(self):
        request_body = {
            "keypair": {
                "is_default": "true"
            }
        }
        expected = {
            "keypair": {
                "keypair_id": KEYPAIR_ID,
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "nova_keypair_id": "test_keypair",
                "name": "test_keypair",
                "private_key": PRIVATE_KEY,
                "is_default": True,
                "status": "ACTIVE"
            }
        }

        url = "/v1/groups/" + GID + "/keypairs/" + KEYPAIR_ID
        req = get_request(url, 'PUT', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 200)
        for key in request_body["keypair"]:
            self.assertEqual(body["keypair"][key], expected["keypair"][key])
Ejemplo n.º 2
0
    def test_create_group_description_length_zero(self):
        request_body = {
            "group": {
                "name": "test_group",
                "description": ""
            }
        }
        expected = {
            "group": {
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "name": "test_group",
                "description": "",
                "status": "ACTIVE"
            }
        }

        url = '/v1/groups'
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 201)
        for key in expected["group"]:
            self.assertEqual(body["group"][key], expected["group"][key])
Ejemplo n.º 3
0
    def test_create_securitygroup_name_with_leading_trailing_whitespace(self):
        request_body = {"securitygroup": {"name": " test_securitygroup "}}

        scheduler_rpcapi.SchedulerAPI.select_destinations(
            IsA(context.RequestContext), request_spec={}, filter_properties={}
        ).AndReturn({"host": "fake_host"})
        operator_rpcapi.ResourceOperatorAPI.securitygroup_create(
            IsA(context.RequestContext),
            "fake_host",
            gid=GID,
            securitygroup_id=IsA(unicode),
            name="test_securitygroup",
            securitygrouprules=[],
        )
        self.mox.ReplayAll()

        expected = {
            "securitygroup": {
                "securitygroup_id": SECURITYGROUP_ID,
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "name": "test_securitygroup",
                "is_default": False,
                "status": "BUILDING",
            }
        }

        url = get_base_url(GID)
        req = get_request(url, "POST", request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 202)
        for key in expected["securitygroup"]:
            self.assertEqual(body["securitygroup"][key], expected["securitygroup"][key])
Ejemplo n.º 4
0
    def test_show(self):
        self.stubs.Set(
            db, "network_get_by_network_id", fake_network_get_by_network_id)

        url = '/v1/groups/' + GID + '/networks'
        req = get_request(url + "/" + NETWORK_ID1, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)

        expected_body = {"network":
                         {
                             "network_id": NETWORK_ID1,
                             "neutron_network_id": None,
                             "gid": GID,
                             "user_id": "fake",
                             "project_id": "fake",
                             "name": "net-45212048-abc3-43cc-89b3-377341426ac",
                             "is_admin": "True",
                             "cidr": "10.0.0.0/24",
                             "ext_router_id": "91212048-abc3-43cc-89b3-377341"
                             "426aca",
                             "status": "BUILDING"
                         }
                         }
        self.assertEqual(body, expected_body)
        self.assertEqual(res.status_code, 200)
Ejemplo n.º 5
0
 def test_index(self):
     url = "/v1/groups/" + GID + "/keypairs"
     req = get_request(url, 'GET')
     res = req.get_response(self.app)
     body = jsonutils.loads(res.body)
     expected = [
         {
             "keypair_id": KEYPAIR_ID1,
             "nova_keypair_id": "fake_key1",
             "gid": GID,
             "user_id": "fake",
             "project_id": "fake",
             "name": "fake_key1",
             "private_key": PRIVATE_KEY,
             "is_default": False,
             "status": "ACTIVE"
         },
         {
             "keypair_id": KEYPAIR_ID2,
             "nova_keypair_id": "fake_key2",
             "gid": GID,
             "user_id": "fake",
             "project_id": "fake",
             "name": "fake_key2",
             "private_key": PRIVATE_KEY,
             "is_default": False,
             "status": "ACTIVE"
         },
     ]
     self.assertEqual(res.status_code, 200)
     self.assertEqual(body["keypairs"], expected)
Ejemplo n.º 6
0
    def test_index(self):
        self.stubs.Set(db, "network_get_all", fake_network_get_all)
        self.mox.StubOutWithMock(manager.ResourceOperator, "network_list")
        manager.ResourceOperator.network_list(
            IsA(context.RequestContext),
            IsA(list)).AndReturn(_return_base_network_list(self.context, GID))
        self.mox.ReplayAll()

        expect = _return_base_network_list(self.context, GID)
        expect[0].update(ext_router_id="11212048-abc3-43cc-89b3-377341426aca")
        expect[1].update(ext_router_id="21212048-abc3-43cc-89b3-377341426aca")
        expect[0].update(name="net-45212048-abc3-43cc-89b3-377341426ac")
        expect[1].update(name="net-13092048-abc3-43cc-89b3-377341426ac")
        expect[0].update(cidr="10.0.0.0/24")
        expect[1].update(cidr="10.0.1.0/24")

        url = "/v1/groups/" + GID + "/networks"
        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)

        self.assertEqual(200, res.status_code)
        for i in range(len(body["networks"])):
            for key in body["networks"][i]:
                self.assertEqual(expect[i][key], body["networks"][i][key])
Ejemplo n.º 7
0
    def test_update(self):
        request_body = {
            "group": {
                "name": "My_Group_updated",
                "description": "This is my group updated.",
            }
        }
        expected = {
            "group": {
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "name": "test",
                "description": "test",
                "status": "ACTIVE"
            }
        }

        url = '/v1/groups/' + GID
        req = get_request(url, 'PUT', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 200)
        for key in request_body["group"]:
            self.assertEqual(body["group"][key], expected["group"][key])
Ejemplo n.º 8
0
    def test_show(self):
        def _mock_data():
            return {
                "keypair_id": KEYPAIR_ID1,
                "nova_keypair_id": "fake_key1",
                "gid": GID,
                "user_id": "noauth",
                "project_id": "noauth",
                "display_name": "fake_key1",
                "private_key": PRIVATE_KEY,
                "is_default": False,
                "status": "ACTIVE"
            }

        self.mox.StubOutWithMock(db, "keypair_get_by_keypair_id")
        self.mox.StubOutWithMock(manager.ResourceOperator, "keypair_show")
        db.keypair_get_by_keypair_id(IsA(context.RequestContext), GID,
                                     KEYPAIR_ID1).AndReturn(_mock_data())
        manager.ResourceOperator.keypair_show(IsA(context.RequestContext),
                                              IsA(dict)).AndReturn(
                                                  _mock_data())

        self.mox.ReplayAll()

        url = "/v1/groups/" + GID + "/keypairs/" + KEYPAIR_ID1
        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        expect = _mock_data()
        expect.update(name="fake_key1")

        self.assertEqual(res.status_code, 200)
        for key in body["keypair"]:
            self.assertEqual(expect[key], body["keypair"][key])
Ejemplo n.º 9
0
 def test_delete_invalid_format_gid(self):
     url = '/v1/groups/' + GID + "err"
     req = get_request(url, 'DELETE')
     res = req.get_response(self.app)
     self.assertEqual(res.status_code, 404)
     body = jsonutils.loads(res.body)
     print(body)
Ejemplo n.º 10
0
    def test_update(self):
        request_body = {
            "group": {
                "name": "My_Group_updated",
                "description": "This is my group updated.",
            }
        }
        expected = {
            "group": {
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "name": "test",
                "description": "test",
                "status": "ACTIVE"
            }
        }

        url = '/v1/groups/' + GID
        req = get_request(url, 'PUT', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 200)
        for key in request_body["group"]:
            self.assertEqual(body["group"][key], expected["group"][key])
Ejemplo n.º 11
0
    def test_create_group_name_with_leading_trailing_whitespace(self):
        request_body = {
            "group": {
                "name": " test_group ",
                "description": "This is test group"
            }
        }
        expected = {
            "group": {
                "gid": GID,
                "user_id": "noauth",
                "project_id": "noauth",
                "name": "test_group",
                "description": "This is test group",
                "status": "ACTIVE"
            }
        }

        url = '/v1/groups'
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 201)
        for key in expected["group"]:
            self.assertEqual(body["group"][key], expected["group"][key])
Ejemplo n.º 12
0
 def test_show(self):
     self.stubs.Set(db, "securitygroup_get_by_securitygroup_id",
                    fake_securitygroup_get_by_securitygroup_id)
     self.mox.StubOutWithMock(manager.ResourceOperator,
                              "securitygroup_show")
     securitygroup = {
         "securitygroup_id": SECURITYGROUP_ID1,
         "neutron_securitygroup_id": "fake_key1",
         "gid": GID,
         "user_id": "fake",
         "project_id": "fake",
         "display_name": "fake_key1",
         "is_default": False,
         "status": "Exist"
     }
     manager.ResourceOperator.securitygroup_show(
         IsA(context.RequestContext), IsA(object)).AndReturn(securitygroup)
     self.mox.ReplayAll()
     url = get_base_url(GID) + "/" + SECURITYGROUP_ID1
     req = get_request(url, 'GET')
     res = req.get_response(self.app)
     body = jsonutils.loads(res.body)
     expected = {
         "securitygroup_id": SECURITYGROUP_ID1,
         "neutron_securitygroup_id": "fake_key1",
         "gid": GID,
         "user_id": "fake",
         "project_id": "fake",
         "name": "fake_key1",
         "is_default": False,
         "status": "Exist"
     }
     self.assertEqual(res.status_code, 200)
     self.assertEqual(body["securitygroup"], expected)
Ejemplo n.º 13
0
 def test_index(self):
     url = get_base_url(GID)
     req = get_request(url, "GET")
     res = req.get_response(self.app)
     body = jsonutils.loads(res.body)
     expected = [
         {
             "securitygroup_id": SECURITYGROUP_ID1,
             "neutron_securitygroup_id": "fake_key1",
             "gid": GID,
             "user_id": "fake",
             "project_id": "fake",
             "name": "fake_key1",
             "is_default": False,
             "status": "ACTIVE",
         },
         {
             "securitygroup_id": SECURITYGROUP_ID2,
             "neutron_securitygroup_id": "fake_key2",
             "gid": GID,
             "user_id": "fake",
             "project_id": "fake",
             "name": "fake_key2",
             "is_default": False,
             "status": "ACTIVE",
         },
     ]
     self.assertEqual(res.status_code, 200)
     self.assertEqual(body["securitygroups"], expected)
Ejemplo n.º 14
0
 def test_delete_invalid_format_gid(self):
     url = '/v1/groups/' + GID + "err"
     req = get_request(url, 'DELETE')
     res = req.get_response(self.app)
     self.assertEqual(res.status_code, 404)
     body = jsonutils.loads(res.body)
     print(body)
Ejemplo n.º 15
0
    def test_index(self):
        processes = _base_processes(GID)
        expect = get_base_processes_response_body(processes)

        url = get_base_url(GID)
        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(body, expect)
Ejemplo n.º 16
0
    def __call__(self, model):
        # this part deals with subviews that were already serialized
        cpy = copy.deepcopy(model)
        for key, valstr in model.items():
            if getattr(valstr, '__is_json__', False):
                cpy[key] = json.loads(valstr)

        res = utils.StringWithAttrs(json.dumps(cpy.data))
        res.__is_json__ = True
        return res
Ejemplo n.º 17
0
    def __call__(self, model):
        # this part deals with subviews that were already serialized
        cpy = copy.deepcopy(model)
        for key, valstr in model.items():
            if getattr(valstr, "__is_json__", False):
                cpy[key] = json.loads(valstr)

        res = utils.StringWithAttrs(json.dumps(cpy.data))
        res.__is_json__ = True
        return res
Ejemplo n.º 18
0
    def load_json(cls, data, default_rule=None):
        """
        Allow loading of JSON rule data.
        """

        # Suck in the JSON data and parse the rules
        rules = dict((k, parse_rule(v)) for k, v in
                     jsonutils.loads(data).items())

        return cls(rules, default_rule)
Ejemplo n.º 19
0
    def test_show(self):
        process = _base_process1(GID, PID1)
        expect = get_base_process_response_body(process)

        url = get_base_url(GID) + "/" + PID1
        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(body, expect)
Ejemplo n.º 20
0
    def test_create_by_name_blank(self):
        self.stubs.Set(db, "network_create", fake_create_db)
        self.stubs.Set(db, "group_get_by_gid", fake_group_get_by_gid)

        self.mox.StubOutWithMock(uuid, "uuid4")
        self.mox.StubOutWithMock(manager.ResourceOperator, "network_create")

        mock_uuid = NETWORK_ID1
        uuid.uuid4().AndReturn(mock_uuid)
        uuid.uuid4().AndReturn(mock_uuid)

        network_value = {
            "name": "network-" + mock_uuid,
            "cidr": "10.0.0.0/24",
            "gateway": "10.0.0.254",
            "dns_nameservers": ["8.8.8.8", "8.8.4.4"],
            "ext_router": "91212048-abc3-43cc-89b3-377341426aca"}
        manager.ResourceOperator.network_create(
            IsA(context.RequestContext), **network_value).AndReturn(
                {"neutron_network_id": "neutron-id-data"})
        self.mox.ReplayAll()

        request_body = {
            "network": {
                "name": None,
                "is_admin": "True",
                "cidr": "10.0.0.0/24",
                "gateway": "10.0.0.254",
                "dns_nameservers": ["8.8.8.8", "8.8.4.4"],
                "ext_router_id": "91212048-abc3-43cc-89b3-377341426aca"
            }
        }

        expect = {
            "network": {
                "network_id": mock_uuid,
                "neutron_network_id": "neutron-id-data",
                "gid": GID,
                "user_id": "noauth",
                "project_id": "noauth",
                "name": "network-" + mock_uuid,
                "is_admin": True,
                "cidr": "10.0.0.0/24",
                "ext_router_id": "91212048-abc3-43cc-89b3-377341426aca"
            }
        }

        url = '/v1/groups/' + GID + '/networks'
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        for key in body["network"]:
            self.assertEqual(
                expect["network"][key], body["network"][key])
        self.assertEqual(res.status_code, 201)
Ejemplo n.º 21
0
    def test_create(self):
        self.stubs.Set(db, "network_create", fake_create_db)
        self.stubs.Set(db, "group_get_by_gid", fake_group_get_by_gid)
        request_spec = {}
        filter_properties = {}
        self.mox.StubOutWithMock(
            scheduler_rpcapi.SchedulerAPI, "select_destinations")
        scheduler_rpcapi.SchedulerAPI.select_destinations(
            mox.IsA(context.RequestContext),
            request_spec,
            filter_properties)\
            .AndReturn({"host": RO_HOST_NAME})

        self.mox.StubOutWithMock(
            resourceoperator_rpcapi.ResourceOperatorAPI, "network_create")
        resourceoperator_rpcapi.ResourceOperatorAPI.network_create(
            mox.IsA(context.RequestContext),
            RO_HOST_NAME,
            mox.IsA(dict))
        self.mox.ReplayAll()

        request_body = {
            "network": {
                "is_admin": "True",
                "name": "network-test",
                "cidr": "10.0.0.0/24",
                "gateway": "10.0.0.254",
                "dns_nameservers": ["8.8.8.8", "8.8.4.4"],
                "ext_router_id": "91212048-abc3-43cc-89b3-377341426aca"
            }
        }

        expected_body = {
            "network": {
                "network_id": NETWORK_ID1,
                "neutron_network_id": None,
                "name": "network-test",
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "is_admin": True,
                "cidr": "10.0.0.0/24",
                "ext_router_id": "91212048-abc3-43cc-89b3-377341426aca",
                "status": "BUILDING"
            }
        }
        url = '/v1/groups/' + GID + '/networks'
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        for key in expected_body["network"]:
            self.assertEqual(
                body["network"][key], expected_body["network"][key])
        self.assertEqual(res.status_code, 202)
Ejemplo n.º 22
0
 def test_index(self):
     url = '/v1/groups'
     req = get_request(url, 'GET')
     res = req.get_response(self.app)
     body = jsonutils.loads(res.body)
     expected = copy.deepcopy(FAKE_GROUPS)
     for group in expected["groups"]:
         group["name"] = group.pop("display_name")
         group["description"] = group.pop("display_description")
     self.assertEqual(res.status_code, 200)
     self.assertEqual(body, expected)
Ejemplo n.º 23
0
 def test_index(self):
     url = '/v1/groups'
     req = get_request(url, 'GET')
     res = req.get_response(self.app)
     body = jsonutils.loads(res.body)
     expected = copy.deepcopy(FAKE_GROUPS)
     for group in expected["groups"]:
         group["name"] = group.pop("display_name")
         group["description"] = group.pop("display_description")
     self.assertEqual(res.status_code, 200)
     self.assertEqual(body, expected)
Ejemplo n.º 24
0
    def test_index_return_empty_list(self):
        self.stubs.Set(
            db, "network_get_all", fake_network_get_all_empty_list)

        url = '/v1/groups/' + GID + '/networks'
        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        expected_body = {"networks": []}

        self.assertEqual(body, expected_body)
        self.assertEqual(res.status_code, 200)
Ejemplo n.º 25
0
    def test_index_return_empty_list(self):
        self.stubs.Set(db, "network_get_all", fake_network_get_all_empty_list)
        self.mox.StubOutWithMock(manager.ResourceOperator, "network_list")
        manager.ResourceOperator.network_list(
            IsA(context.RequestContext), []).AndReturn([])
        self.mox.ReplayAll()

        url = '/v1/groups/' + GID + '/networks'
        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        expected_body = {"networks": []}

        self.assertEqual(expected_body, body)
        self.assertEqual(res.status_code, 200)
Ejemplo n.º 26
0
    def test_show(self):
        url = '/v1/groups/00000000-0000-0000-0000-000000000010'
        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        expected = {"group": {
                    "gid": "00000000-0000-0000-0000-000000000010",
                    "user_id": "a4362182a2ac425c9b0b0826ad187d31",
                    "project_id": "a43621849823764c9b0b0826ad187d31t",
                    "name": "my_group",
                    "description": "This is my group.",
                    "status": "ACTIVE"
                    }}

        self.assertEqual(res.status_code, 200)
        self.assertEqual(body, expected)
Ejemplo n.º 27
0
    def test_create(self):
        self._set_mox_resource_operator_process_create()
        self.mox.ReplayAll()

        process = _base_process1(GID, PID1)
        request_body = get_base_request_body1(process)
        expect = get_base_process_response_body(process)

        url = get_base_url(GID)
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 202)
        expect["process"]["userdata"] = USER_DATA_B64_DEC
        for key in body["process"]:
            self.assertEqual(body["process"][key], expect["process"][key])
Ejemplo n.º 28
0
def action_peek_json(body):
    """Determine action to invoke."""

    try:
        decoded = jsonutils.loads(body)
    except ValueError:
        msg = _("cannot understand JSON")
        raise exception.MalformedRequestBody(reason=msg)

    # Make sure there's exactly one key...
    if len(decoded) != 1:
        msg = _("too many body keys")
        raise exception.MalformedRequestBody(reason=msg)

    # Return the action and the decoded body...
    return decoded.keys()[0]
Ejemplo n.º 29
0
def action_peek_json(body):
    """Determine action to invoke."""

    try:
        decoded = jsonutils.loads(body)
    except ValueError:
        msg = _("cannot understand JSON")
        raise exception.MalformedRequestBody(reason=msg)

    # Make sure there's exactly one key...
    if len(decoded) != 1:
        msg = _("too many body keys")
        raise exception.MalformedRequestBody(reason=msg)

    # Return the action and the decoded body...
    return decoded.keys()[0]
Ejemplo n.º 30
0
    def test_create_with_no_name(self):
        self.stubs.Set(db, "group_get_by_gid", fake_group_get_by_id)
        self.mox.StubOutWithMock(manager.ResourceOperator,
                                 "securitygroup_create")
        result_value = {"neutron_securitygroup_id": "fake_id"}
        manager.ResourceOperator.securitygroup_create(
            IsA(context.RequestContext), IsA(unicode),
            IsA(list)).AndReturn(result_value)
        self.mox.StubOutWithMock(db, "securitygroup_create")
        name = "securitygroup-" + SECURITYGROUP_ID
        db.securitygroup_create(IsA(context.RequestContext),
                                IsA(dict))\
            .AndReturn({"securitygroup_id": SECURITYGROUP_ID,
                        "neutron_securitygroup_id": "fake_id",
                        "gid": GID,
                        "user_id": "noauth",
                        "project_id": "noauth",
                        "display_name": name,
                        "is_default": False})
        self.mox.ReplayAll()

        request_body = {
            "securitygroup": {
                "is_default": "false",
            }
        }

        expected = {
            "securitygroup": {
                "securitygroup_id": SECURITYGROUP_ID,
                "neutron_securitygroup_id": "fake_id",
                "gid": GID,
                "user_id": "noauth",
                "project_id": "noauth",
                "name": name,
                "is_default": False,
            }
        }

        url = get_base_url(GID)
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 201)
        for key in body["securitygroup"]:
            self.assertEqual(body["securitygroup"][key],
                             expected["securitygroup"][key])
Ejemplo n.º 31
0
    def __call__(self, req):
        user_id = req.headers.get('X_USER')
        user_id = req.headers.get('X_USER_ID', user_id)
        if user_id is None:
            LOG.debug("Neither X_USER_ID nor X_USER found in request")
            return webob.exc.HTTPUnauthorized()

        roles = self._get_roles(req)

        if 'X_TENANT_ID' in req.headers:
            # This is the new header since Keystone went to ID/Name
            project_id = req.headers['X_TENANT_ID']
        else:
            # This is for legacy compatibility
            project_id = req.headers['X_TENANT']
        project_name = req.headers.get('X_TENANT_NAME')
        user_name = req.headers.get('X_USER_NAME')

        # Get the auth token
        auth_token = req.headers.get('X_AUTH_TOKEN',
                                     req.headers.get('X_STORAGE_TOKEN'))

        # Build a context, including the auth_token...
        remote_address = req.remote_addr
        if CONF.use_forwarded_for:
            remote_address = req.headers.get('X-Forwarded-For', remote_address)

        service_catalog = None
        if req.headers.get('X_SERVICE_CATALOG') is not None:
            try:
                catalog_header = req.headers.get('X_SERVICE_CATALOG')
                service_catalog = jsonutils.loads(catalog_header)
            except ValueError:
                raise webob.exc.HTTPInternalServerError(
                    _('Invalid service catalog json.'))

        ctx = context.RequestContext(user_id,
                                     project_id,
                                     user_name=user_name,
                                     project_name=project_name,
                                     roles=roles,
                                     auth_token=auth_token,
                                     remote_address=remote_address,
                                     service_catalog=service_catalog)

        req.environ['rack.context'] = ctx
        return self.application
Ejemplo n.º 32
0
    def __call__(self, req):
        user_id = req.headers.get('X_USER')
        user_id = req.headers.get('X_USER_ID', user_id)
        if user_id is None:
            LOG.debug("Neither X_USER_ID nor X_USER found in request")
            return webob.exc.HTTPUnauthorized()

        roles = self._get_roles(req)

        if 'X_TENANT_ID' in req.headers:
            # This is the new header since Keystone went to ID/Name
            project_id = req.headers['X_TENANT_ID']
        else:
            # This is for legacy compatibility
            project_id = req.headers['X_TENANT']
        project_name = req.headers.get('X_TENANT_NAME')
        user_name = req.headers.get('X_USER_NAME')

        # Get the auth token
        auth_token = req.headers.get('X_AUTH_TOKEN',
                                     req.headers.get('X_STORAGE_TOKEN'))

        # Build a context, including the auth_token...
        remote_address = req.remote_addr
        if CONF.use_forwarded_for:
            remote_address = req.headers.get('X-Forwarded-For', remote_address)

        service_catalog = None
        if req.headers.get('X_SERVICE_CATALOG') is not None:
            try:
                catalog_header = req.headers.get('X_SERVICE_CATALOG')
                service_catalog = jsonutils.loads(catalog_header)
            except ValueError:
                raise webob.exc.HTTPInternalServerError(
                    _('Invalid service catalog json.'))

        ctx = context.RequestContext(user_id,
                                     project_id,
                                     user_name=user_name,
                                     project_name=project_name,
                                     roles=roles,
                                     auth_token=auth_token,
                                     remote_address=remote_address,
                                     service_catalog=service_catalog)

        req.environ['rack.context'] = ctx
        return self.application
Ejemplo n.º 33
0
    def test_index_filters(self):
        url = '/v1/groups?project_id=PID&name=NAME&status=STATUS'

        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)

        expected = {"groups": [
            {"gid": "fake",
             "user_id": "fake",
             "project_id": "PID",
             "name": "NAME",
             "description": "fake",
             "status": "STATUS"}
        ]}
        self.assertEqual(res.status_code, 200)
        self.assertEqual(body, expected)
Ejemplo n.º 34
0
    def test_create(self):
        request_body = {
            "keypair": {
                "name": "test_keypair",
                "is_default": "true",
            }
        }

        self.mox.StubOutWithMock(db, "keypair_get_all")
        self.mox.StubOutWithMock(manager.ResourceOperator, "keypair_create")
        self.mox.StubOutWithMock(uuid, 'uuid4')
        mock_id = "1234-5678"
        uuid.uuid4().AndReturn(mock_id)
        uuid.uuid4().AndReturn(mock_id)
        db.keypair_get_all(IsA(context.RequestContext), GID,
                           filters=IsA(dict)).AndReturn([])
        manager.ResourceOperator.keypair_create(IsA(context.RequestContext),
                                                IsA(unicode)).AndReturn({
                                                    "nova_keypair_id":
                                                    "keypair-" + mock_id,
                                                    "private_key":
                                                    "private-key-1234"
                                                })
        self.mox.ReplayAll()

        expect = {
            "keypair": {
                "keypair_id": mock_id,
                "nova_keypair_id": "keypair-" + mock_id,
                "user_id": "noauth",
                "project_id": "noauth",
                "gid": GID,
                "name": "test_keypair",
                "private_key": "private-key-1234",
                "is_default": True
            }
        }

        url = '/v1/groups/' + GID + '/keypairs'
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        for key in body["keypair"]:
            self.assertEqual(expect["keypair"][key], body["keypair"][key])
        self.assertEqual(res.status_code, 201)
Ejemplo n.º 35
0
    def test_show(self):
        url = '/v1/groups/00000000-0000-0000-0000-000000000010'
        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        expected = {
            "group": {
                "gid": "00000000-0000-0000-0000-000000000010",
                "user_id": "a4362182a2ac425c9b0b0826ad187d31",
                "project_id": "a43621849823764c9b0b0826ad187d31t",
                "name": "my_group",
                "description": "This is my group.",
                "status": "ACTIVE"
            }
        }

        self.assertEqual(res.status_code, 200)
        self.assertEqual(body, expected)
Ejemplo n.º 36
0
    def test_index(self):
        def _mock_data():
            return [{
                "keypair_id": KEYPAIR_ID1,
                "nova_keypair_id": "fake_key1",
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "display_name": "fake_key1",
                "private_key": PRIVATE_KEY,
                "is_default": False,
                "status": "ACTIVE"
            }, {
                "keypair_id": KEYPAIR_ID2,
                "nova_keypair_id": "fake_key2",
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "display_name": "fake_key2",
                "private_key": PRIVATE_KEY,
                "is_default": False,
                "status": "ACTIVE"
            }]

        self.mox.StubOutWithMock(manager.ResourceOperator, "keypair_list")
        manager.ResourceOperator.keypair_list(IsA(context.RequestContext),
                                              IsA(list)).AndReturn(
                                                  _mock_data())
        self.mox.ReplayAll()

        expect = _mock_data()
        expect[0].update(name="fake_key1")
        expect[1].update(name="fake_key2")

        url = "/v1/groups/" + GID + "/keypairs"
        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)

        self.assertEqual(200, res.status_code)
        for i in range(len(body["keypairs"])):
            for key in body["keypairs"][i]:
                self.assertEqual(expect[i][key], body["keypairs"][i][key])
Ejemplo n.º 37
0
    def test_index_filters(self):
        url = '/v1/groups?project_id=PID&name=NAME&status=STATUS'

        req = get_request(url, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)

        expected = {
            "groups": [{
                "gid": "fake",
                "user_id": "fake",
                "project_id": "PID",
                "name": "NAME",
                "description": "fake",
                "status": "STATUS"
            }]
        }
        self.assertEqual(res.status_code, 200)
        self.assertEqual(body, expected)
Ejemplo n.º 38
0
    def test_create_without_name(self):
        scheduler_rpcapi.SchedulerAPI.select_destinations(
            IsA(context.RequestContext),
            request_spec={},
            filter_properties={})\
            .AndReturn({"host": "fake_host"})
        operator_rpcapi.ResourceOperatorAPI.keypair_create(
            IsA(context.RequestContext),
            "fake_host",
            gid=GID,
            keypair_id=IsA(unicode),
            name=IsA(unicode))
        self.mox.ReplayAll()

        request_body = {
            "keypair": {
                "is_default": "true",
            }
        }
        expected = {
            "keypair": {
                "keypair_id": KEYPAIR_ID,
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "is_default": True,
                "status": "BUILDING"
            }
        }

        url = '/v1/groups/' + GID + '/keypairs'
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 202)
        for key in expected["keypair"]:
            self.assertEqual(body["keypair"][key], expected["keypair"][key])
        regex = re.compile(
            "keypair\-[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}\-"
            "[a-z0-9]{12}")
        self.assertTrue(regex.match(body["keypair"]["name"]))
Ejemplo n.º 39
0
    def test_create_without_is_default(self):
        name = "test_keypair"
        request_body = {
            "keypair": {
                "name": name,
            }
        }

        scheduler_rpcapi.SchedulerAPI.select_destinations(
            IsA(context.RequestContext),
            request_spec={},
            filter_properties={})\
            .AndReturn({"host": "fake_host"})
        operator_rpcapi.ResourceOperatorAPI.keypair_create(
            IsA(context.RequestContext),
            "fake_host",
            gid=GID,
            keypair_id=IsA(unicode),
            name=name)
        self.mox.ReplayAll()

        expected = {
            "keypair": {
                "keypair_id": KEYPAIR_ID,
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "name": "test_keypair",
                "is_default": False,
                "status": "BUILDING"
            }
        }

        url = '/v1/groups/' + GID + '/keypairs'
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 202)
        for key in expected["keypair"]:
            self.assertEqual(body["keypair"][key], expected["keypair"][key])
Ejemplo n.º 40
0
    def test_update(self):
        request_body = {"securitygroup": {"is_default": "true"}}
        expected = {
            "securitygroup": {
                "securitygroup_id": SECURITYGROUP_ID,
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "neutron_securitygroup_id": "test_securitygroup",
                "name": "test_securitygroup",
                "is_default": True,
                "status": "ACTIVE",
            }
        }

        url = get_base_url(GID) + "/" + SECURITYGROUP_ID
        req = get_request(url, "PUT", request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 200)
        for key in request_body["securitygroup"]:
            self.assertEqual(body["securitygroup"][key], expected["securitygroup"][key])
Ejemplo n.º 41
0
    def test_show(self):

        def _mock_data():
            return {
                "network_id": NETWORK_ID1,
                "neutron_network_id": None,
                "gid": GID,
                "user_id": "noauth",
                "project_id": "noauth",
                "display_name": "net-45212048-abc3-43cc-89b3-377341426ac",
                "is_admin": "True",
                "cidr": "10.0.0.0/24",
                "ext_router": "11212048-abc3-43cc-89b3-377341426aca",
                "status": "Exist"}

        mock_data = _mock_data()
        self.mox.StubOutWithMock(db, "network_get_by_network_id")
        self.mox.StubOutWithMock(manager.ResourceOperator, "network_show")
        db.network_get_by_network_id(IsA(context.RequestContext),
                                     GID, NETWORK_ID1).AndReturn(_mock_data())
        manager.ResourceOperator.network_show(IsA(context.RequestContext),
                                              IsA(dict))

        self.mox.ReplayAll()

        url = '/v1/groups/' + GID + '/networks'
        req = get_request(url + "/" + NETWORK_ID1, 'GET')
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)

        expect = mock_data
        expect.update(ext_router_id="11212048-abc3-43cc-89b3-377341426aca")
        expect.update(name="net-45212048-abc3-43cc-89b3-377341426ac")
        expect.update(cidr="10.0.0.0/24")

        self.assertEqual(res.status_code, 200)
        for key in body["network"]:
            self.assertEqual(expect[key], body["network"][key])
Ejemplo n.º 42
0
    def test_update(self):
        request_body = {"keypair": {"is_default": "true"}}
        expected = {
            "keypair": {
                "keypair_id": KEYPAIR_ID,
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "nova_keypair_id": "test_keypair",
                "name": "test_keypair",
                "private_key": PRIVATE_KEY,
                "is_default": True,
                "status": "ACTIVE"
            }
        }

        url = "/v1/groups/" + GID + "/keypairs/" + KEYPAIR_ID
        req = get_request(url, 'PUT', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 200)
        for key in request_body["keypair"]:
            self.assertEqual(body["keypair"][key], expected["keypair"][key])
Ejemplo n.º 43
0
    def test_create_without_secgroup_keypair_id_glance_image_flavor_id(self):
        self._set_mox_resource_operator_process_create()
        self.mox.ReplayAll()

        process = _base_process1(GID, PID1)
        request_body = get_base_request_body1(process)
        request_body["process"].update(securitygroup_ids=None)
        request_body["process"].update(keypair_id=None)
        request_body["process"].update(glance_image_id=None)
        request_body["process"].update(nova_flavor_id=None)

        expect = get_base_process_response_body(process)

        request_body["process"].pop("metadata")

        url = get_base_url(GID)
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 202)
        expect["process"]["userdata"] = USER_DATA_B64_DEC
        for key in body["process"]:
            self.assertEqual(body["process"][key], expect["process"][key])
Ejemplo n.º 44
0
    def test_update(self):
        self.stubs.Set(db, "securitygroup_update", fake_update)
        request_body = {"securitygroup": {"is_default": "true"}}
        expected = {
            "securitygroup": {
                "securitygroup_id": SECURITYGROUP_ID,
                "gid": GID,
                "user_id": "noauth",
                "project_id": "noauth",
                "neutron_securitygroup_id": "test_securitygroup",
                "name": "test_securitygroup",
                "is_default": True,
            }
        }

        url = get_base_url(GID) + "/" + SECURITYGROUP_ID
        req = get_request(url, 'PUT', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)

        self.assertEqual(res.status_code, 200)
        for key in body["securitygroup"]:
            self.assertEqual(body["securitygroup"][key],
                             expected["securitygroup"][key])
Ejemplo n.º 45
0
    def test_create_with_rules(self):
        remote_securitygroup_id = "b755595b-3bdf-4152-8fb0-456d5e72eb01"
        name = "test_securitygroup"
        request_body = {
            "securitygroup": {
                "name": name,
                "is_default": "true",
                "securitygrouprules": [
                    {"protocol": "icmp", "remote_securitygroup_id": remote_securitygroup_id},
                    {
                        "port_range_max": "80",
                        "port_range_min": "80",
                        "protocol": "tcp",
                        "remote_securitygroup_id": remote_securitygroup_id,
                    },
                    {"protocol": "icmp", "remote_ip_prefix": "192.168.0.0/16"},
                    {
                        "port_range_max": "80",
                        "port_range_min": "80",
                        "protocol": "tcp",
                        "remote_ip_prefix": "192.168.0.0/16",
                    },
                    {
                        "port_range_max": "5000",
                        "port_range_min": "5000",
                        "protocol": "udp",
                        "remote_ip_prefix": "192.168.0.0/16",
                    },
                ],
            }
        }

        self.stubs.Set(db, "securitygroup_get_by_securitygroup_id", fake_neutron_securitygroup_id)
        scheduler_rpcapi.SchedulerAPI.select_destinations(
            IsA(context.RequestContext), request_spec={}, filter_properties={}
        ).AndReturn({"host": "fake_host"})
        operator_rpcapi.ResourceOperatorAPI.securitygroup_create(
            IsA(context.RequestContext),
            "fake_host",
            gid=GID,
            securitygroup_id=IsA(unicode),
            name=IsA(unicode),
            securitygrouprules=[
                {
                    "protocol": "icmp",
                    "port_range_max": None,
                    "port_range_min": None,
                    "remote_securitygroup_id": remote_securitygroup_id,
                    "remote_neutron_securitygroup_id": "fake_id",
                    "remote_ip_prefix": None,
                },
                {
                    "protocol": "tcp",
                    "port_range_max": "80",
                    "port_range_min": "80",
                    "remote_securitygroup_id": remote_securitygroup_id,
                    "remote_neutron_securitygroup_id": "fake_id",
                    "remote_ip_prefix": None,
                },
                {
                    "protocol": "icmp",
                    "port_range_max": None,
                    "port_range_min": None,
                    "remote_securitygroup_id": None,
                    "remote_ip_prefix": "192.168.0.0/16",
                },
                {
                    "protocol": "tcp",
                    "port_range_max": "80",
                    "port_range_min": "80",
                    "remote_securitygroup_id": None,
                    "remote_ip_prefix": "192.168.0.0/16",
                },
                {
                    "protocol": "udp",
                    "port_range_max": "5000",
                    "port_range_min": "5000",
                    "remote_securitygroup_id": None,
                    "remote_ip_prefix": "192.168.0.0/16",
                },
            ],
        )
        self.mox.ReplayAll()

        expected = {
            "securitygroup": {
                "securitygroup_id": SECURITYGROUP_ID,
                "gid": GID,
                "user_id": "fake",
                "project_id": "fake",
                "name": name,
                "is_default": True,
                "status": "BUILDING",
            }
        }

        url = get_base_url(GID)
        req = get_request(url, "POST", request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 202)
        for key in expected["securitygroup"]:
            self.assertEqual(body["securitygroup"][key], expected["securitygroup"][key])
Ejemplo n.º 46
0
 def _from_json(self, datastring):
     try:
         return jsonutils.loads(datastring)
     except ValueError:
         msg = _("cannot understand JSON")
         raise exception.MalformedRequestBody(reason=msg)
Ejemplo n.º 47
0
    def test_create_with_rules(self):
        self.stubs.Set(db, "group_get_by_gid", fake_group_get_by_id)
        self.stubs.Set(db, "securitygroup_get_by_securitygroup_id",
                       fake_securitygroup_get_by_securitygroup_id)
        self.mox.StubOutWithMock(manager.ResourceOperator,
                                 "securitygroup_create")
        name = "test_securitygroup"
        security_group = {"neutron_securitygroup_id": "fake_id"}
        manager.ResourceOperator.securitygroup_create(
            IsA(context.RequestContext), name,
            [{
                "protocol": "icmp",
                "port_range_max": None,
                "port_range_min": None,
                "remote_neutron_securitygroup_id": "fake_key1",
                "remote_ip_prefix": None
            }, {
                "protocol": "tcp",
                "port_range_max": "80",
                "port_range_min": "80",
                "remote_neutron_securitygroup_id": "fake_key1",
                "remote_ip_prefix": None
            }]).AndReturn(security_group)
        self.mox.StubOutWithMock(db, "securitygroup_create")
        db.securitygroup_create(IsA(context.RequestContext),
                                IsA(dict))\
            .AndReturn({"securitygroup_id": SECURITYGROUP_ID,
                        "neutron_securitygroup_id": "fake_id",
                        "gid": GID,
                        "user_id": "noauth",
                        "project_id": "noauth",
                        "display_name": name,
                        "is_default": True})
        self.mox.ReplayAll()

        request_body = {
            "securitygroup": {
                "name":
                name,
                "is_default":
                "true",
                "securitygrouprules": [{
                    "protocol":
                    "icmp",
                    "remote_securitygroup_id":
                    SECURITYGROUP_ID1
                }, {
                    "port_range_max":
                    "80",
                    "port_range_min":
                    "80",
                    "protocol":
                    "tcp",
                    "remote_securitygroup_id":
                    SECURITYGROUP_ID1
                }]
            }
        }

        expected = {
            "securitygroup": {
                "securitygroup_id": SECURITYGROUP_ID,
                "neutron_securitygroup_id": "fake_id",
                "gid": GID,
                "user_id": "noauth",
                "project_id": "noauth",
                "name": name,
                "is_default": True
            }
        }

        url = get_base_url(GID)
        req = get_request(url, 'POST', request_body)
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self.assertEqual(res.status_code, 201)
        for key in body["securitygroup"]:
            self.assertEqual(body["securitygroup"][key],
                             expected["securitygroup"][key])
Ejemplo n.º 48
0
 def _from_json(self, datastring):
     try:
         return jsonutils.loads(datastring)
     except ValueError:
         msg = _("cannot understand JSON")
         raise exception.MalformedRequestBody(reason=msg)