CFEnv v1.0.0 CFEnv behaviour View Source
The CFEnv behavior creates a process that stores, parses, and processes your CloudFoundry environment variables and application information.
We can define a module that represents your service bindings.
defmodule MyApp.Env do
use CFEnv,
otp_app: :my_app,
json_engine: Jason
...
Configuration can be stored in application configuration, or as runtime configutation
Application config example
config :my_app, MyApp.Env
json_engine: Jason,
default_services: %{
"some_db" => %{
"username" => System.get_env("TEST_USER"),
"password" => System.get_env("TEST_PASSWORD")
}
}
...
Runtime config Example
defmodule MyApp.Supervisor do
# Automatically defines child_spec/1
use Supervisor
def start_link(arg) do
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
end
@impl true
def init(_arg) do
cf_options = [
json_engine: Jason,
default_services: %{
"some_db" => %{
"username" => System.get_env("TEST_USER"),
"password" => System.get_env("TEST_PASSWORD")
}
}]
children = [
# For Elixir v1.5 and later
{MyApp.Env, [ cf_options ]},
# For Elixir v1.4 and earlier
worker(MyApp.Env, [cf_options])
]
Supervisor.init(children, strategy: :one_for_one)
end
end
Once started, you can fetch your services anywhere you want.
defmodule MyApp.Kafka do
def start_link() do
creds = MyApp.Env.service_credentials("kafka_service")
GenServer_start_link(__MODULE__, creds, name: __MODULE__)
end
...
end
Link to this section Summary
Callbacks
Gets the current parsed VCAP_APPLICATION as a map
Gets the value of the cf_api
property for the current application. Location
of the Cloud Controller API for the CF Deployment where the app runs
Gets the value of the application_id
property for the current application.
This is GUID identifying the application
Gets the value of the limits
property for the current application
Gets the value of the application_name
property for the current application.
This is the name assigned to the application when it was pushed
Gets the value of the space_id
property for the current application. This is
GUID identifying the application’s space
Gets the value of the space_name
property for the current application. This
is the Human-readable name of the space where the app is deployed
Returns the value of the start
property for the current application.
Human-readable timestamp for the time the instance was started. Not provided
on Diego Cells
Gets the value of the application_uris
property for the current application.
These are the URIs assigned to the application
Gets the value of the application_version
property for the current application.
This is a GUID identifying a version of the application. Each time an
application is pushed or restarted, this value is updated
Get a service, as a map
Gets the credentials
property for a service
Gets the label
property for a service
Gets the name
property for a service
Gets the plan
property for a service
Gets the tags
property for a service
Get the parsed VCAP_SERVICES
env variable, as a map
Starts the environment store
Link to this section Types
Link to this section Callbacks
Gets the current parsed VCAP_APPLICATION as a map.
Examples
iex> MyApp.app()
%{
"name" => "my-app",
"users" => nil,
"application_name" => "my-app",
"application_uris" => ["my-app.192.0.2.34.xip.io"],
"application_version" => "fb8fbcc6-8d58-479e-bcc7-3b4ce5a7f0ca",
"limits" => %{"disk" => 1024, "fds" => 16384, "mem" => 256},
"uris" => ["my-app.192.0.2.34.xip.io"],
"version" => "fb8fbcc6-8d58-479e-bcc7-3b4ce5a7f0ca",
"application_id" => "fa05c1a9-0fc1-4fbd-bae1-139850dec7a3",
"cf_api" => "https://api.example.com",
"space_id" => "06450c72-4669-4dc6-8096-45f9777db68a",
"space_name" => "my-space",
"start" => "2013-08-12 00:05:29+0000"
}
Gets the value of the cf_api
property for the current application. Location
of the Cloud Controller API for the CF Deployment where the app runs.
Examples
iex> MyApp.app_cf_api()
"https://api.example.com"
Gets the value of the application_id
property for the current application.
This is GUID identifying the application.
Examples
iex> MyApp.app_id()
"fa05c1a9-0fc1-4fbd-bae1-139850dec7a3"
Gets the value of the limits
property for the current application.
Examples
The limits to disk space, number of files, and memory permitted to the app. Memory and disk space limits are supplied when the application is deployed, either on the command line or in the application manifest. The number of files allowed is operator-defined.
iex> MyApp.app_limits
%{“disk” => 1024, “fds” => 16384, “mem” => 256}
Gets the value of the application_name
property for the current application.
This is the name assigned to the application when it was pushed.
Examples
iex> MyApp.app_name()
"my-app"
Gets the value of the space_id
property for the current application. This is
GUID identifying the application’s space.
Examples
iex> MyApp.app_space_id()
“06450c72-4669-4dc6-8096-45f9777db68a”
Gets the value of the space_name
property for the current application. This
is the Human-readable name of the space where the app is deployed.
Examples
iex> MyApp.app_space_name()
"my-space"
Returns the value of the start
property for the current application.
Human-readable timestamp for the time the instance was started. Not provided
on Diego Cells.
Examples
iex> MyApp.app_start()
“2013-08-12 00:05:29+0000”
Gets the value of the application_uris
property for the current application.
These are the URIs assigned to the application.
Examples
iex> MyApp.app_uris()
["my-app.192.0.2.34.xip.io"]
Gets the value of the application_version
property for the current application.
This is a GUID identifying a version of the application. Each time an
application is pushed or restarted, this value is updated.
Examples
iex> MyApp.app_version()
"fb8fbcc6-8d58-479e-bcc7-3b4ce5a7f0ca"
Get a service, as a map.
Examples
iex> MyApp.Env.service("cf-env-test")
%{
"credentials" => %{
"database" => "database",
"password" => "passw0rd",
"url" => "https://example.com/",
"username" => "userid"
},
"label" => "user-provided",
"name" => "cf-env-test",
"syslog_drain_url" => "http://example.com/syslog",
"tags" => []
}
service_credentials(service_name()) :: map()
Gets the credentials
property for a service.
Examples
iex> MyApp.Env.service_credentials("dynamo-db")
%{"database" => "database", "password" => "passw0rd",
"url" => "https://example.com/", "username" => "userid"}
service_label(service_name()) :: String.t() | nil
Gets the label
property for a service.
Examples
iex> MyApp.service_label("cf-env-test")
"user-provided"
service_name(service_name()) :: String.t() | nil
Gets the name
property for a service.
Examples
iex> MyApp.service_name("cf-env-test")
"cf-env-test"
(Given that the name is required to invoke, this is only useful when using an alias.)
service_plan(service_name()) :: String.t() | nil
Gets the plan
property for a service.
Examples
iex> MyApp.service_plan("cf-env-test")
"free"
service_tags(service_name()) :: [String.t()]
Gets the tags
property for a service.
Examples
iex> MyApp.service_tags()
["smtp", "email"]
Get the parsed VCAP_SERVICES
env variable, as a map.
Examples
iex> MyApp.Env.services()
%{
"cf-env-test" => %{
"credentials" => %{
"database" => "database",
"password" => "passw0rd",
"url" => "https://example.com/",
"username" => "userid"
},
"label" => "user-provided",
"name" => "cf-env-test",
"syslog_drain_url" => "http://example.com/syslog",
"tags" => []
}
}
Starts the environment store.
Examples
takes a list of options that determines how JSON should be parsed
default_services
a map of services names, and credentialas to use as defaults.json_engine
- The module CFEnv will use for decoding JSON. seeCFEnv.Adapters.JSON
for details.
iex> {:ok, #PID<0.43.0>} = MyApp.Env.start_link([
json_engine: Jason,
default_services: %{
"some_db" => %{
"username" => System.get_env("TEST_USER"),
"password" => System.get_env("TEST_PASSWORD")
}
}])