Skip to content

Commit 7084bcd

Browse files
authored
Merge pull request #17 from ovv/postgresql-dsn-formatter
Add postgresql DSN formatter
2 parents 15d482e + a894bec commit 7084bcd

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,19 @@ def format_my_service(credentials):
120120
return "some string based on 'credentials'."
121121

122122
# Call this in setup
123-
config.register_formatter('my_service', 'format_my_service')
123+
config.register_formatter('my_service', format_my_service)
124124

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

129129
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.
130130

131-
Two formatters are included out of the box:
131+
Three formatters are included out of the box:
132132

133133
* `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.
134-
* `pysolr` returns a DSN appropriate for using `pysolr` to connect to Apache Solr.
134+
* `pysolr` returns a DSN appropriate for using `pysolr` to connect to Apache Solr.
135+
* `postgresql_dsn` returns a DSN appropriate for postgresql connection.
135136

136137
### Reading Platform.sh variables
137138

platformshconfig/config.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def __init__(self, environment_variables=None, env_prefix='PLATFORM_'):
150150

151151
self.register_formatter('pymongo', pymongo_formatter)
152152
self.register_formatter('pysolr', pysolr_formatter)
153+
self.register_formatter('postgresql_dsn', posgresql_dsn_formatter)
153154

154155
if self['VARIABLES']:
155156
variables = self['VARIABLES']
@@ -577,6 +578,24 @@ def pysolr_formatter(credentials):
577578
credentials['path'])
578579

579580

581+
def posgresql_dsn_formatter(credentials):
582+
"""
583+
Returns formatted Posgresql credentials as DSN.
584+
585+
Args:
586+
credentials (dict):
587+
The credentials dictionary from the relationships.
588+
589+
Returns:
590+
(string) A formatted postgresql DSN.
591+
"""
592+
593+
return "postgresql://{0}:{1}@{2}:{3}/{4}".format(credentials["username"],
594+
credentials["password"],
595+
credentials["host"],
596+
credentials["port"],
597+
credentials["path"])
598+
580599
class BuildTimeVariableAccessException(RuntimeError):
581600
pass
582601

0 commit comments

Comments
 (0)