Skip to main content

Small helper to access Platform.sh environment variables.

Project description

Platform.sh Config Reader (Python)

CircleCI Status

This library provides a streamlined and easy to use way to interact with a Platform.sh environment. It offers utility methods to access routes and relationships more cleanly than reading the raw environment variables yourself.

This library requires Python 3.5 or later.

Install

pip install platformshconfig

Usage Example

Example:

import sys
import pysolr

from platformshconfig import Config


config = Config()

if not config.is_valid_platform():
    sys.exit("Not in a Platform.sh Environment.")

credentials = config.credentials('solr')

formatted = config.formatted_credentials('solr', 'pysolr')

conn = pysolr.Solr(formatted)

# Do stuff with the conn here.

API Reference

Create a config object

from platformshconfig import Config

config = Config()

config is now a Config object that provides access to the Platform.sh environment.

The is_valid_platform() method returns True if the code is running in a context that has Platform.sh environment variables defined. If it returns False then most other functions will throw exceptions if used.

Inspect the environment

The following methods return True or False to help determine in what context the code is running:

config.in_build()

config.in_runtime()

config.on_dedicated()

config.on_production()

Note:

Platform.sh will no longer refer to its 99.99% uptime SLA product as "Enterprise", but rather as "Dedicated". Configuration Reader libraries have in turn been updated to include an on_dedicated method to replace on_enterprise. For now on_enterprise remains available. It now calls the new method and no breaking changes have been introduced.

It is recommended that you update your projects to use on_dedicated as soon as possible, as on_enterprise will be removed in a future version of this library.

Read environment variables

The following magic properties return the corresponding environment variable value. See the Platform.sh documentation for a description of each.

The following are available both in Build and at Runtime:

config.applicationName

config.appDir

config.project

config.treeID

config.projectEntropy

The following are available only if in_runtime() returned True:

config.branch

condig.documentRoot

config.smtpHost

config.environment

config.socket

config.port

Reading service credentials

Platform.sh services are defined in a services.yaml file, and exposed to an application by listing a relationship to that service in the application's .platform.app.yaml file. User, password, host, etc. information is then exposed to the running application in the PLATFORM_RELATIONSHIPS environment variable, which is a base64-encoded JSON string. The following method allows easier access to credential information than decoding the environment variable yourself.

creds = config.credentials('database')

The return value of credentials() is a dictionary matching the relationship JSON object, which includes the appropriate user, password, host, database name, and other pertinent information. See the Service documentation for your service for the exact structure and meaning of each property. In most cases that information can be passed directly to whatever other client library is being used to connect to the service.

Formatting service credentials

In some cases the library being used to connect to a service wants its credentials formatted in a specific way; it could be a DSN string of some sort or it needs certain values concatenated to the database name, etc. For those cases you can use "Credential Formatters". A Credential Formatter is any callable (function, anonymous function, object method, etc.) that takes a credentials array and returns any type, since the library may want different types.

Credential Formatters can be registered on the configuration object, and a few are included out of the box. That allows 3rd party libraries to ship their own formatters that can be easily integrated into the Config object to allow easier use.

def format_my_service(credentials):
    return "some string based on 'credentials'."

# Call this in setup
config.register_formatter('my_service', format_my_service)

# Then call this method to get the formatted version
formatted = config.formatted_credentials('database', 'my_service')

The first parameter is the name of a relationship defined in .platform.app.yaml. The second is a formatter that was previously registered with register_formatter(). If either the service or formatter is missing an exception will be thrown. The type of formatted will depend on the formatter function and can be safely passed directly to the client library.

Three formatters are included out of the box:

  • pymongo returns a DSN appropriate for using pymongo to connect to MongoDB. Note that pymongo will still need the username and password from the credentials dictionary passed as separate parameters.
  • pysolr returns a DSN appropriate for using pysolr to connect to Apache Solr.
  • postgresql_dsn returns a DSN appropriate for postgresql connection.

Reading Platform.sh variables

Platform.sh allows you to define arbitrary variables that may be available at build time, runtime, or both. They are stored in the PLATFORM_VARIABLES environment variable, which is a base64-encoded JSON string.

The following two methods allow access to those values from your code without having to bother decoding the values yourself:

config.variables()

This method returns a dictionary of all variables defined. Usually this method is not necessary and config.variable() is preferred.

config.variable("foo", "default")

This method looks for the "foo" variable. If found, it is returned. If not, the optional second parameter is returned as a default.

Reading Routes

Routes on Platform.sh define how a project will handle incoming requests; that primarily means what application container will serve the request, but it also includes cache configuration, TLS settings, etc. Routes may also have an optional ID, which is the preferred way to access them.

config.get_route("main")

The get_route() method takes a single string for the route ID ("main" in this case) and returns the corresponding route array. If the route is not found it will throw an exception.

To access all routes, or to search for a route that has no ID, the routes() method returns an dictionary of routes keyed by their URL. That mirrors the structure of the PLATFORM_ROUTES environment variable.

If called in the build phase an exception is thrown.

Changelog

[2.3.1] - 2019-11-04

Added

  • CHANGELOG added.
  • on_dedicated method that determines if the current environment is a Platform.sh Dedicated environment. Replaces deprecated on_enterprise method.

Changed

  • Deprecates on_enterprise method - which is for now made to wrap around the added on_dedicated method. on_enterprise will be removed in a future release, so update your projects to use on_dedicated instead as soon as possible.

[2.3.0] - 2019-09-19

Added

  • get_primary_route method for accessing routes marked "primary" in routes.yaml.
  • get_upstream_routes method returns an object map that includes only those routes that point to a valid upstream.

[2.2.3] - 2019-04-30

Changed

  • Removes guard on variables() method.

[2.2.2] - 2019-04-29

Changed

  • Refactors dynamic property access to be more permissive.

[2.2.1] - 2019-04-25

Changed

  • More permissive check for relationships.

[2.2.0] - 2019-04-24

Added

  • postgresql_dsn credential formatter; returns a DSN appropriate for PostgreSQL connection.

[2.1.1] - 2019-03-22

Changed

  • Fixes build issues in has_relationship() and routes() methods.

[2.1.0] - 2019-03-22

Added

  • has_relationship method to determine if a relationship is defined, and thus has credentials available.

Changed

  • Fixes routes method.

[2.0.4] - 2019-03-06

Added

  • CircleCI configuration

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

platformshconfig-2.3.1.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

platformshconfig-2.3.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file platformshconfig-2.3.1.tar.gz.

File metadata

  • Download URL: platformshconfig-2.3.1.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.6.4

File hashes

Hashes for platformshconfig-2.3.1.tar.gz
Algorithm Hash digest
SHA256 7c44af99c8bce6c4b0fc4780f5156d2109500d2ef78f558c96622f77e71a0744
MD5 615faff074ddbe6357d5c1b97b4a9e8f
BLAKE2b-256 b1a292d625da9b0a4c29bc096c0ebe3fd0db53cb2ef9f5e4d299329810824d4f

See more details on using hashes here.

File details

Details for the file platformshconfig-2.3.1-py3-none-any.whl.

File metadata

  • Download URL: platformshconfig-2.3.1-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.6.4

File hashes

Hashes for platformshconfig-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7a7fccc1eaebb255527a5875e85ac71c6f85a67576a9bd0f69df8078d1094319
MD5 8c34c758b89659547e5ef431f4868de1
BLAKE2b-256 3654976b3941b3f6e09cb59ddcb712dfac1f470fd425e4fc76a0c77736c2bd8e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page