Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added environment_nested plugin #51

Merged
merged 1 commit into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/plugins/environment_nested.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Environment Nested Plugin
If you add `environment_nested` to your list of data sources in `common.yaml`, you'll be able to create or overwrite nested configuration variables.

```yaml
data_sources: [ "defaults", "file", "environment_nested" ]
template_sources: [ "file" ]

defaults:
global:
restapi:
server:
port: 80
```

Setting environment variable `restapi_server_port=8080` will overwrite value `80` from defaults.

To overwrite large structures it's probably easier to use `environment_json`.

3 changes: 2 additions & 1 deletion docs/plugins/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ In addition to specifying values in YAML environment files, there are other plug
* [Defaults](defaults.md) : Make use of default values across your environments and templates - this can help avoid repeated definitions and makes for more efficient configuration.
* [Environment variables](environment.md) : Make use of environment variables in your templates.
* [JSON environment variables](environment_json.md) : Use complex JSON data structures from the environment in your templates. See [http://www.markround.com/blog/2014/10/17/building-dynamic-docker-images-with-json-and-tiller-0-dot-1-4/](http://www.markround.com/blog/2014/10/17/building-dynamic-docker-images-with-json-and-tiller-0-dot-1-4/) for some practical examples.
* [Nested environment variables](environment_nested.md) : Use environment variables `a_b_c=value` to build nested variables like `a['b']['c']`.
* [External files](external_file.md) : Load external JSON or YAML files, and use their contents in your templates.
* [HTTP plugins](http.md) : These plugins let you retrieve your templates and values from a HTTP server
* [Random data](random.md) : Simple wrapper to provide random values to your templates.
Expand Down Expand Up @@ -45,4 +46,4 @@ data_sources:
- environment
```

So, to summarise: A template value will take priority over a global value, and a value from a plugin loaded later will take priority over any previously loaded plugins.
So, to summarise: A template value will take priority over a global value, and a value from a plugin loaded later will take priority over any previously loaded plugins.
29 changes: 29 additions & 0 deletions features/environment_nested.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Feature: Configure tiller with nested environment variables

Scenario: Import environment variable using string data type
Given I use a fixture named "environment_nested"
And I set the environment variables to:
| variable | value |
| my_nested_var | http://www.somecompany.com |
When I successfully run `tiller -b . -v -n`
Then a file named "test.txt" should exist
And the file "test.txt" should contain "test_var: http://www.somecompany.com"

Scenario: Import environment variable using numeric data type
Given I use a fixture named "environment_nested"
And I set the environment variables to:
| variable | value |
| my_nested_var | 1234 |
When I successfully run `tiller -b . -v -n`
Then a file named "test.txt" should exist
And the file "test.txt" should contain "test_var: 1234"

Scenario: Import environment variable using boolean data type
Given I use a fixture named "environment_nested"
And I set the environment variables to:
| variable | value |
| my_nested_var | true |
When I successfully run `tiller -b . -v -n`
Then a file named "test.txt" should exist
And the file "test.txt" should contain "test_var: true"

8 changes: 8 additions & 0 deletions features/fixtures/environment_nested/common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
data_sources: [ "defaults", "environment_nested" ]
template_sources: [ "file" ]

defaults:

test.erb:
target: test.txt
1 change: 1 addition & 0 deletions features/fixtures/environment_nested/templates/test.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_var: <%= my['nested']['var'] %>
18 changes: 18 additions & 0 deletions lib/tiller/data/environment_nested.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'tiller/datasource'

class EnvironmentNestedDataSource < Tiller::DataSource

def global_values
values = Hash.new
ENV.each do |k, v|
begin
v = YAML.load(v) # helper to get real data type instead of string
values.deep_merge!(k.split('_').reverse.inject(v) { |a, n| { n => a } })
rescue
Tiller::log.debug("Environment variable #{k} with value #{v} could not be unfolded (ignored)")
end
end
values
end

end
1 change: 1 addition & 0 deletions tiller.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |s|
lib/tiller/data/http.rb
lib/tiller/data/environment.rb
lib/tiller/data/environment_json.rb
lib/tiller/data/environment_nested.rb
lib/tiller/data/external_file.rb
lib/tiller/data/random.rb
lib/tiller/data/defaults.rb
Expand Down