-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcfenv.py
76 lines (62 loc) · 2.59 KB
/
cfenv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import json
import os
from typing import Any
from attrs import field, mutable, validators
from glom import Path, glom
from .cloudfoundry import default_vcap_application, default_vcap_services
@mutable
class CFenv:
vcap_service_prefix: str = field(
default=os.getenv("VCAP_SERVICE_PREFIX", "p-config-server"),
validator=validators.instance_of(str),
)
vcap_application: dict = field(
default=json.loads(os.getenv("VCAP_APPLICATION", default_vcap_application)),
validator=validators.instance_of(dict),
)
vcap_services: dict = field(
default=json.loads(os.getenv("VCAP_SERVICES", default_vcap_services)),
validator=validators.instance_of(dict),
)
def __attrs_post_init__(self) -> None:
if self.vcap_service_prefix not in self.vcap_services.keys():
vcap_services_copy = self.vcap_services.copy()
vcap_services_copy[self.vcap_service_prefix] = vcap_services_copy.pop(
"p-config-server"
)
self.vcap_services = vcap_services_copy
@property
def space_name(self) -> Any:
return glom(self.vcap_application, "space_name", default="")
@property
def organization_name(self) -> Any:
return glom(self.vcap_application, "organization_name", default="")
@property
def application_name(self) -> Any:
return glom(self.vcap_application, "application_name", default="")
@property
def uris(self) -> Any:
return glom(self.vcap_application, "uris", default=[])
def configserver_uri(
self, vcap_path: str = "0.credentials.uri", default: Any = ""
) -> Any:
path = self._format_vcap_path(vcap_path)
return glom(self.vcap_services, path, default=default)
def configserver_access_token_uri(
self, vcap_path: str = "0.credentials.access_token_uri", default: Any = ""
) -> Any:
path = self._format_vcap_path(vcap_path)
return glom(self.vcap_services, path, default=default)
def configserver_client_id(
self, vcap_path: str = "0.credentials.client_id", default: Any = ""
) -> Any:
path = self._format_vcap_path(vcap_path)
return glom(self.vcap_services, path, default=default)
def configserver_client_secret(
self, vcap_path: str = "0.credentials.client_secret", default: Any = ""
) -> Any:
path = self._format_vcap_path(vcap_path)
return glom(self.vcap_services, path, default=default)
def _format_vcap_path(self, path: str) -> Path:
subpath = path.split(".")
return Path(Path(self.vcap_service_prefix), *subpath)