Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #118 from msabramo/dynamic_config_tenant
Browse files Browse the repository at this point in the history
Support dynamic config w/ OS_TENANT_NAME
  • Loading branch information
major committed Jul 7, 2016
2 parents 67c9621 + 3ad132b commit 0259ee5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
17 changes: 15 additions & 2 deletions docs/configuring.md
Expand Up @@ -92,7 +92,7 @@ When supernova runs, it will take the configuration options and pass them direct

For accounts where you would like to utilize the same configuration, it is possible to use the same entry.

In the configuration, you can separate the regions by a semicolon::
In the configuration, you can separate the regions by a semicolon:

[personal]
..snip..
Expand All @@ -101,11 +101,24 @@ In the configuration, you can separate the regions by a semicolon::
OS_USERNAME=username
OS_PASSWORD=somelongapikey


This will create the super group "personal" as well as the individual groups "personal-DFW" and "personal-ORD"::

> supernova -l | grep personal
-- personal-DFW -------------------------------------------------------------
SUPERNOVA_GROUP : personal
-- personal-ORD -------------------------------------------------------------
SUPERNOVA_GROUP : personal

You can also use dynamic configuration with tenants (in the `OS_TENANT_NAME` setting):

[personal]
..snip..
OS_TENANT_NAME=dev;prod
OS_USERNAME=username
OS_PASSWORD=somelongapikey

> supernova -l | grep personal
-- personal-dev -------------------------------------------------------------
SUPERNOVA_GROUP : personal
-- personal-prod -------------------------------------------------------------
SUPERNOVA_GROUP : personal
34 changes: 20 additions & 14 deletions supernova/config.py
Expand Up @@ -77,32 +77,38 @@ def get_config_file(override_files=False):
raise Exception("Couldn't find a valid configuration file to parse")


def create_dynamic_configs(config, region_name='OS_REGION_NAME',
def create_dynamic_configs(config,
dynamic_attrs=('OS_REGION_NAME', 'OS_TENANT_NAME'),
delimiter=';'):
if not isinstance(config, ConfigObj):
raise ValueError("config should be ConfigObj, not %s" % type(config))

sections = copy.copy(config.sections)

for section in sections:
delete_original_section = False

# Check to see if we should generate new sections.
if delimiter in config[section].get(region_name, ''):
for new_section_arg in config[section][region_name].split(
delimiter):
for dynamic_attr in dynamic_attrs:
# Check to see if we should generate new sections.
if delimiter in config[section].get(dynamic_attr, ''):
for new_section_arg in config[section][dynamic_attr].split(
delimiter):

new_section = section + '-' + new_section_arg
new_section = section + '-' + new_section_arg

# Use default section
config[new_section] = {}
# Use default section
config[new_section] = {}

# Copy the existing section config.
config[new_section].update(config[section])
config[new_section][region_name] = new_section_arg
# Copy the existing section config.
config[new_section].update(config[section])
config[new_section][dynamic_attr] = new_section_arg

# We are eventually going to delete the old section.
# Lets use it as a supernova group
config[new_section]['SUPERNOVA_GROUP'] = section
# We are eventually going to delete the old section.
# Lets use it as a supernova group
config[new_section]['SUPERNOVA_GROUP'] = section

delete_original_section = True

if delete_original_section:
# We are done, lets remove the original section
del config[section]
23 changes: 23 additions & 0 deletions tests/test_config.py
Expand Up @@ -78,3 +78,26 @@ def test_dynamic_sections_without_default(self):
result['dynamic-section-ORD'].get('OS_REGION_NAME')
assert 'DFW' == \
result['dynamic-section-DFW'].get('OS_REGION_NAME')

def test_dynamic_sections_using_tenant(self):
result = config.load_config()
result['dynamic-section'] = {'OS_TENANT_NAME': "dev;prod"}
config.create_dynamic_configs(result)
# The new sections exist
assert 'dynamic-section-dev' in result.sections
assert 'dynamic-section-prod' in result.sections

# The default section is no longer provided
assert 'dynamic-section' not in result.sections

# The super group is set up correctly
assert 'dynamic-section' in \
result['dynamic-section-dev'].get('SUPERNOVA_GROUP')
assert 'dynamic-section' == \
result['dynamic-section-prod'].get('SUPERNOVA_GROUP')

# The regions are correct
assert 'dev' == \
result['dynamic-section-dev'].get('OS_TENANT_NAME')
assert 'prod' == \
result['dynamic-section-prod'].get('OS_TENANT_NAME')

0 comments on commit 0259ee5

Please sign in to comment.