Skip to content

Commit 6772de7

Browse files
authored
Merge pull request #159 from dandersonsw/features/add_roles_manager
Implement Roles in v3 API
2 parents 5f53211 + f7738ef commit 6772de7

6 files changed

Lines changed: 168 additions & 0 deletions

File tree

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ Available managers on API V3 are:
249249
- ``organizations``
250250
- ``organization_quotas``
251251
- ``processes``
252+
- ``roles``
252253
- ``security_groups``
253254
- ``service_brokers``
254255
- ``service_credential_bindings``

main/cloudfoundry_client/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from cloudfoundry_client.v3.organization_quotas import OrganizationQuotaManager
3333
from cloudfoundry_client.v3.processes import ProcessManager
3434
from cloudfoundry_client.v3.organizations import OrganizationManager
35+
from cloudfoundry_client.v3.roles import RoleManager
3536
from cloudfoundry_client.v3.security_groups import SecurityGroupManager
3637
from cloudfoundry_client.v3.service_brokers import ServiceBrokerManager
3738
from cloudfoundry_client.v3.service_credential_bindings import ServiceCredentialBindingManager
@@ -106,6 +107,7 @@ def __init__(self, target_endpoint: str, credential_manager: "CloudFoundryClient
106107
self.organizations = OrganizationManager(target_endpoint, credential_manager)
107108
self.organization_quotas = OrganizationQuotaManager(target_endpoint, credential_manager)
108109
self.processes = ProcessManager(target_endpoint, credential_manager)
110+
self.roles = RoleManager(target_endpoint, credential_manager)
109111
self.security_groups = SecurityGroupManager(target_endpoint, credential_manager)
110112
self.service_brokers = ServiceBrokerManager(target_endpoint, credential_manager)
111113
self.service_credential_bindings = ServiceCredentialBindingManager(target_endpoint, credential_manager)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import TYPE_CHECKING
2+
3+
from cloudfoundry_client.v3.entities import EntityManager
4+
5+
if TYPE_CHECKING:
6+
from cloudfoundry_client.client import CloudFoundryClient
7+
8+
9+
class RoleManager(EntityManager):
10+
def __init__(self, target_endpoint: str, client: "CloudFoundryClient"):
11+
super(RoleManager, self).__init__(target_endpoint, client, "/v3/roles")
12+
13+
def remove(self, role_guid: str):
14+
super(RoleManager, self)._remove(role_guid)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"pagination": {
3+
"total_results": 3,
4+
"total_pages": 2,
5+
"first": {
6+
"href": "https://api.example.org/v3/roles?page=1&per_page=2"
7+
},
8+
"last": {
9+
"href": "https://api.example.org/v3/roles?page=2&per_page=2"
10+
},
11+
"next": {
12+
"href": null
13+
},
14+
"previous": null
15+
},
16+
"resources": [
17+
{
18+
"guid": "40557c70-d1bd-4976-a2ab-a85f5e882418",
19+
"created_at": "2019-10-10T17:19:12Z",
20+
"updated_at": "2019-10-10T17:19:12Z",
21+
"type": "organization_auditor",
22+
"relationships": {
23+
"user": {
24+
"data": {
25+
"guid": "59eadb5f-fc13-414f-84ba-77a35e239cc8"
26+
}
27+
},
28+
"organization": {
29+
"data": {
30+
"guid": "05c5da3b-6cbc-421c-87c3-20bb3c41ab7c"
31+
}
32+
},
33+
"space": {
34+
"data": null
35+
}
36+
},
37+
"links": {
38+
"self": {
39+
"href": "https://api.example.org/v3/roles/40557c70-d1bd-4976-a2ab-a85f5e882418"
40+
},
41+
"user": {
42+
"href": "https://api.example.org/v3/users/59eadb5f-fc13-414f-84ba-77a35e239cc8"
43+
},
44+
"organization": {
45+
"href": "https://api.example.org/v3/organizations/05c5da3b-6cbc-421c-87c3-20bb3c41ab7c"
46+
}
47+
}
48+
},
49+
{
50+
"guid": "12347c70-d1bd-4976-a2ab-a85f5e882418",
51+
"created_at": "2047-11-10T17:19:12Z",
52+
"updated_at": "2047-11-10T17:19:12Z",
53+
"type": "space_auditor",
54+
"relationships": {
55+
"user": {
56+
"data": {
57+
"guid": "47eadb5f-fc13-414f-84ba-47a35e239cc8"
58+
}
59+
},
60+
"organization": {
61+
"data": null
62+
},
63+
"space": {
64+
"data": {
65+
"guid": "47c5da3b-6cbc-421c-87c3-20bb3c41ab7c"
66+
}
67+
}
68+
},
69+
"links": {
70+
"self": {
71+
"href": "https://api.example.org/v3/roles/12347c70-d1bd-4976-a2ab-a85f5e882418"
72+
},
73+
"user": {
74+
"href": "https://api.example.org/v3/users/47eadb5f-fc13-414f-84ba-77a35e239cc8"
75+
},
76+
"space": {
77+
"href": "https://api.example.org/v3/spaces/47c5da3b-6cbc-421c-87c3-20bb3c41ab7c"
78+
}
79+
}
80+
}
81+
]
82+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"guid": "role_id",
3+
"created_at": "2019-10-10T17:19:12Z",
4+
"updated_at": "2019-10-10T17:19:12Z",
5+
"type": "organization_auditor",
6+
"relationships": {
7+
"user": {
8+
"data": {
9+
"guid": "59eadb5f-fc13-414f-84ba-77a35e239cc8"
10+
}
11+
},
12+
"organization": {
13+
"data": {
14+
"guid": "05c5da3b-6cbc-421c-87c3-20bb3c41ab7c"
15+
}
16+
},
17+
"space": {
18+
"data": null
19+
}
20+
},
21+
"links": {
22+
"self": {
23+
"href": "https://api.example.org/v3/roles/40557c70-d1bd-4976-a2ab-a85f5e882418"
24+
},
25+
"user": {
26+
"href": "https://api.example.org/v3/users/59eadb5f-fc13-414f-84ba-77a35e239cc8"
27+
},
28+
"organization": {
29+
"href": "https://api.example.org/v3/organizations/05c5da3b-6cbc-421c-87c3-20bb3c41ab7c"
30+
}
31+
}
32+
}

test/v3/test_roles.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
from http import HTTPStatus
3+
4+
from abstract_test_case import AbstractTestCase
5+
from cloudfoundry_client.v3.entities import Entity
6+
7+
8+
class TestRoles(unittest.TestCase, AbstractTestCase):
9+
@classmethod
10+
def setUpClass(cls):
11+
cls.mock_client_class()
12+
13+
def setUp(self):
14+
self.build_client()
15+
16+
def test_list(self):
17+
self.client.get.return_value = self.mock_response("/v3/roles", HTTPStatus.OK, None, "v3", "roles",
18+
"GET_response.json")
19+
all_roles = [role for role in self.client.v3.roles.list()]
20+
self.client.get.assert_called_with(self.client.get.return_value.url)
21+
self.assertEqual(2, len(all_roles))
22+
self.assertEqual(all_roles[0]["type"], "organization_auditor")
23+
self.assertIsInstance(all_roles[0], Entity)
24+
25+
def test_get(self):
26+
self.client.get.return_value = self.mock_response(
27+
"/v3/roles/role_id", HTTPStatus.OK, None, "v3", "roles", "GET_{id}_response.json"
28+
)
29+
role = self.client.v3.roles.get("role_id")
30+
self.client.get.assert_called_with(self.client.get.return_value.url)
31+
self.assertEqual(role["type"], "organization_auditor")
32+
self.assertIsInstance(role, Entity)
33+
34+
def test_remove(self):
35+
self.client.delete.return_value = self.mock_response("/v3/roles/role_id", HTTPStatus.NO_CONTENT, None)
36+
self.client.v3.roles.remove("role_id")
37+
self.client.delete.assert_called_with(self.client.delete.return_value.url)

0 commit comments

Comments
 (0)