Skip to content

Module config.cfenv

None

None

View Source
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)

Variables

default_vcap_application
default_vcap_services

Classes

CFenv

class CFenv(
    vcap_service_prefix: str = 'p-config-server',
    vcap_application: dict = {'application_name': '', 'space_name': '', 'organization_name': '', 'uris': []},
    vcap_services: dict = {'p-config-server': [{'credentials': {'uri': '', 'access_token_uri': '', 'client_id': '', 'client_secret': ''}}]}
)
View Source
@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)

Instance variables

application_name
organization_name
space_name
uris
vcap_application
vcap_service_prefix
vcap_services

Methods

configserver_access_token_uri

def configserver_access_token_uri(
    self,
    vcap_path: str = '0.credentials.access_token_uri',
    default: Any = ''
) -> Any
View Source
    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)

configserver_client_id

def configserver_client_id(
    self,
    vcap_path: str = '0.credentials.client_id',
    default: Any = ''
) -> Any
View Source
    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)

configserver_client_secret

def configserver_client_secret(
    self,
    vcap_path: str = '0.credentials.client_secret',
    default: Any = ''
) -> Any
View Source
    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)

configserver_uri

def configserver_uri(
    self,
    vcap_path: str = '0.credentials.uri',
    default: Any = ''
) -> Any
View Source
    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)