Skip to content

Commit fe1123f

Browse files
authored
Merge pull request #167 from cloudfoundry-community/features/implement_service_credential_bindings_creations
implement service credential bindings creations
2 parents e626a46 + 11a8beb commit fe1123f

12 files changed

Lines changed: 191 additions & 28 deletions

File tree

main/cloudfoundry_client/v3/apps.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING
1+
from typing import TYPE_CHECKING, Optional
22

33
from cloudfoundry_client.json_object import JsonObject
44
from cloudfoundry_client.v3.entities import EntityManager
@@ -16,8 +16,8 @@ def restart(self, application_guid: str):
1616
self.entity_uri,
1717
application_guid))
1818

19-
def remove(self, application_guid: str):
20-
super(AppManager, self)._remove(application_guid)
19+
def remove(self, application_guid: str, asynchronous: bool = True) -> Optional[str]:
20+
return super(AppManager, self)._remove(application_guid, asynchronous)
2121

2222
def get_env(self, application_guid: str) -> JsonObject:
2323
return super(AppManager, self)._get("%s%s/%s/env" % (self.target_endpoint, self.entity_uri, application_guid))

main/cloudfoundry_client/v3/buildpacks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def create(
3030
}
3131
return super(BuildpackManager, self)._create(data)
3232

33-
def remove(self, buildpack_guid: str):
34-
super(BuildpackManager, self)._remove(buildpack_guid)
33+
def remove(self, buildpack_guid: str, asynchronous: bool = True) -> Optional[str]:
34+
return super(BuildpackManager, self)._remove(buildpack_guid, asynchronous)
3535

3636
def update(
3737
self,

main/cloudfoundry_client/v3/domains.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def update(self, domain_guid: str, meta_labels: Optional[dict] = None, meta_anno
5050
data = {"metadata": {"labels": meta_labels, "annotations": meta_annotations}}
5151
return super(DomainManager, self)._update(domain_guid, data)
5252

53-
def remove(self, domain_guid: str):
54-
super(DomainManager, self)._remove(domain_guid)
53+
def remove(self, domain_guid: str, asynchronous: bool = True) -> Optional[str]:
54+
return super(DomainManager, self)._remove(domain_guid, asynchronous)
5555

5656
def __create_shared_domain_url(self, domain_guid: str) -> str:
5757
# TODO use url parser for this

main/cloudfoundry_client/v3/entities.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,32 @@ def _patch(self, url: str, data: dict, entity_type: Optional[ENTITY_TYPE] = None
143143

144144
def _delete(self, url: str) -> Optional[str]:
145145
response = self.client.delete(url)
146+
return self._location(response)
147+
148+
@staticmethod
149+
def _location(response):
146150
try:
147151
return response.headers["Location"]
148152
except (AttributeError, KeyError):
149153
return None
150154

155+
def _remove(self, resource_id: str, asynchronous: bool = True) -> Optional[str]:
156+
url = "%s%s/%s" % (self.target_endpoint, self.entity_uri, resource_id)
157+
job_location = self._delete(url)
158+
if job_location is not None:
159+
job_guid = self._extract_job_guid(job_location)
160+
if not asynchronous:
161+
self.client.v3.jobs.wait_for_job_completion(job_guid)
162+
else:
163+
return job_guid
164+
return None
165+
166+
@staticmethod
167+
def _extract_job_guid(job_location):
168+
job_url = urlparse(job_location)
169+
job_guid = job_url.path.rsplit("/", 1)[-1]
170+
return job_guid
171+
151172
def _list(self, requested_path: str, entity_type: Optional[ENTITY_TYPE] = None, **kwargs) -> PaginateEntities:
152173
url_requested = EntityManager._get_url_with_encoded_params("%s%s" % (self.target_endpoint, requested_path), **kwargs)
153174
for element in self._paginate(url_requested, entity_type):
@@ -194,14 +215,6 @@ def _update(self, resource_id: str, data: dict) -> Entity:
194215
url = "%s%s/%s" % (self.target_endpoint, self.entity_uri, resource_id)
195216
return self._patch(url, data)
196217

197-
def _remove(self, resource_id: str, asynchronous: bool = True):
198-
url = "%s%s/%s" % (self.target_endpoint, self.entity_uri, resource_id)
199-
job_location = self._delete(url)
200-
if not asynchronous and job_location is not None:
201-
job_url = urlparse(job_location)
202-
job_guid = job_url.path.rsplit("/", 1)[-1]
203-
self.client.v3.jobs.wait_for_job_completion(job_guid)
204-
205218
def __iter__(self) -> PaginateEntities:
206219
return self.list()
207220

main/cloudfoundry_client/v3/organization_quotas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class OrganizationQuotaManager(EntityManager):
3737
def __init__(self, target_endpoint: str, client: "CloudFoundryClient"):
3838
super().__init__(target_endpoint, client, "/v3/organization_quotas")
3939

40-
def remove(self, guid: str):
41-
super()._remove(guid)
40+
def remove(self, guid: str, asynchronous: bool = True) -> Optional[str]:
41+
return super()._remove(guid, asynchronous)
4242

4343
def create(
4444
self,

main/cloudfoundry_client/v3/organizations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def update(
2727
data = {"name": name, "suspended": suspended, "metadata": {"labels": meta_labels, "annotations": meta_annotations}}
2828
return super(OrganizationManager, self)._update(guid, data)
2929

30-
def remove(self, guid: str):
31-
super(OrganizationManager, self)._remove(guid)
30+
def remove(self, guid: str, asynchronous: bool = True) -> Optional[str]:
31+
return super(OrganizationManager, self)._remove(guid, asynchronous)
3232

3333
def assign_default_isolation_segment(self, org_guid: str, iso_seg_guid: str) -> Entity:
3434
return ToOneRelationship.from_json_object(

main/cloudfoundry_client/v3/roles.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING
1+
from typing import TYPE_CHECKING, Optional
22

33
from cloudfoundry_client.v3.entities import EntityManager
44

@@ -10,5 +10,5 @@ class RoleManager(EntityManager):
1010
def __init__(self, target_endpoint: str, client: "CloudFoundryClient"):
1111
super(RoleManager, self).__init__(target_endpoint, client, "/v3/roles")
1212

13-
def remove(self, role_guid: str):
14-
super(RoleManager, self)._remove(role_guid)
13+
def remove(self, role_guid: str, asynchronous: bool = True) -> Optional[str]:
14+
return super(RoleManager, self)._remove(role_guid, asynchronous)

main/cloudfoundry_client/v3/security_groups.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def update(self,
5858
payload = self._generate_payload(name, rules, globally_enabled, staging_spaces, running_spaces)
5959
return super()._update(security_group_id, payload)
6060

61-
def remove(self, security_group_id: str):
62-
return super()._remove(security_group_id)
61+
def remove(self, security_group_id: str, asynchronous: bool = True) -> Optional[str]:
62+
return super()._remove(security_group_id, asynchronous)
6363

6464
def bind_running_security_group_to_spaces(self, security_group_id: str, space_guids: ToManyRelationship) \
6565
-> ToManyRelationship:

main/cloudfoundry_client/v3/service_brokers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ def update(
5959
payload["metadata"] = metadata
6060
return super(ServiceBrokerManager, self)._update(guid, payload)
6161

62-
def remove(self, guid: str):
63-
super(ServiceBrokerManager, self)._remove(guid)
62+
def remove(self, guid: str, asynchronous: bool = True) -> Optional[str]:
63+
return super(ServiceBrokerManager, self)._remove(guid, asynchronous)
Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import TYPE_CHECKING
1+
from typing import TYPE_CHECKING, Optional, Union
22

3-
from cloudfoundry_client.v3.entities import EntityManager
3+
from cloudfoundry_client.v3.entities import EntityManager, Entity, ToOneRelationship
44

55
if TYPE_CHECKING:
66
from cloudfoundry_client.client import CloudFoundryClient
@@ -10,3 +10,42 @@ class ServiceCredentialBindingManager(EntityManager):
1010
def __init__(self, target_endpoint: str, client: "CloudFoundryClient"):
1111
super(ServiceCredentialBindingManager, self).__init__(target_endpoint, client,
1212
"/v3/service_credential_bindings")
13+
14+
def create(
15+
self,
16+
name: str,
17+
service_credential_binding_type: str,
18+
service_instance_guid: str,
19+
application_guid: Optional[str],
20+
parameters: Optional[dict],
21+
meta_labels: Optional[dict],
22+
meta_annotations: Optional[dict],
23+
asynchronous: bool = True,
24+
) -> Union[str, Entity, None]:
25+
data = {
26+
"name": name,
27+
"type": service_credential_binding_type,
28+
"relationships": {"service_instance": ToOneRelationship(service_instance_guid)},
29+
}
30+
if application_guid:
31+
data["relationships"]["app"] = ToOneRelationship(application_guid)
32+
if parameters:
33+
data["parameters"] = parameters
34+
if meta_labels or meta_annotations:
35+
metadata = dict()
36+
if meta_labels:
37+
metadata["labels"] = meta_labels
38+
if meta_annotations:
39+
metadata["annotations"] = meta_annotations
40+
data["metadata"] = metadata
41+
url = "%s%s" % (self.target_endpoint, self.entity_uri)
42+
response = self.client.post(url, json=data)
43+
location = super(ServiceCredentialBindingManager, self)._location(response)
44+
if location:
45+
job_guid = super(ServiceCredentialBindingManager, self)._extract_job_guid(location)
46+
if asynchronous:
47+
return job_guid
48+
else:
49+
self.client.v3.jobs.wait_for_job_completion(job_guid)
50+
return None
51+
return super(ServiceCredentialBindingManager, self)._read_response(response, None)

0 commit comments

Comments
 (0)