Skip to content

Commit

Permalink
feat:organizations_account_tags (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
eamonnfaherty committed Jun 24, 2022
1 parent 61970ae commit 3e3a081
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[tool.poetry]
name = "aws-service-catalog-puppet"
version = "0.175.0"
version = "0.176.0"
description = "Making it easier to deploy ServiceCatalog products"
classifiers = ["Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "Programming Language :: Python :: 3", "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Natural Language :: English"]
homepage = "https://service-catalog-tools-workshop.com/"
Expand Down
67 changes: 43 additions & 24 deletions servicecatalog_puppet/manifest_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,31 +220,49 @@ def expand_manifest(manifest, client):
if accounts_by_id.get(account_id) is None:
accounts_by_id[account_id] = account
else:
stored_account = accounts_by_id[account_id]
stored_account.update(account)

if stored_account.get("append"):
append = stored_account.get("append")
for tag in append.get("tags", []):
stored_account.get("tags").append(tag)
for region_enabled in append.get("regions_enabled", []):
stored_account.get("regions_enabled").append(region_enabled)
del stored_account["append"]

elif stored_account.get("overwrite"):
overwrite = stored_account.get("overwrite")
if overwrite.get("tags"):
stored_account["tags"] = overwrite.get("tags")
if overwrite.get("regions_enabled"):
stored_account["regions_enabled"] = overwrite.get("regions_enabled")
if overwrite.get("default_region"):
stored_account["default_region"] = overwrite.get("default_region")
del stored_account["overwrite"]
logger.debug("Found an already seen account")
if not (account.get("append") or account.get("overwrite")):
raise Exception(f"Account {account_id} was seen more than once without an append or overwrite")
accounts_by_id[account_id].update(account)

for stored_account_id, stored_account in accounts_by_id.items():
# Get tags from orgs if we should
logger.info(f"Last loop through: {stored_account_id}")
organizations_account_tags = stored_account.get("organizations_account_tags", "ignored")
if organizations_account_tags != "ignored":
tags = list()
paginator = client.get_paginator('list_tags_for_resource')
for page in paginator.paginate(
ResourceId=stored_account_id,
):
tags.extend(page.get('Tags', []))
tags = [f"{t['Key']}:{t['Value']}" for t in tags]

if organizations_account_tags == "append":
stored_account['tags'] = stored_account.get("tags", []) + tags
elif organizations_account_tags == "honour":
stored_account['tags'] = tags

# append or overwrite if we should
if stored_account.get("append"):
append = stored_account.get("append")
for tag in append.get("tags", []):
stored_account["tags"] = stored_account.get("tags", []) + [tag]
for region_enabled in append.get("regions_enabled", []):
stored_account["regions_enabled"] = stored_account.get("regions_enabled", []) + [region_enabled]
del stored_account["append"]

elif stored_account.get("overwrite"):
overwrite = stored_account.get("overwrite")
if overwrite.get("tags"):
stored_account["tags"] = overwrite.get("tags")
if overwrite.get("regions_enabled"):
stored_account["regions_enabled"] = overwrite.get("regions_enabled")
if overwrite.get("default_region"):
stored_account["default_region"] = overwrite.get("default_region")
del stored_account["overwrite"]


else:
raise Exception(
f"Account {account_id} has been seen twice without using append or overwrite"
)
new_manifest["accounts"] = list(accounts_by_id.values())

for section in [constants.LAUNCHES, constants.STACKS]:
Expand Down Expand Up @@ -649,6 +667,7 @@ def expand_account(account, client, account_id, manifest):
ou_from_parent = new_account["ou"]
del new_account["ou"]


account_details = response.get("Account")
if account_details.get("Status") == "ACTIVE":
if account_details.get("Name") is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Resources:
- organizations:DescribeAccount
- organizations:ListOrganizationalUnitsForParent
- organizations:ListChildren
- organizations:ListTagsForResource
Resource: "*"

AssumeRolePolicyDocument:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

setup_kwargs = {
'name': 'aws-service-catalog-puppet',
'version': '0.175.0',
'version': '0.176.0',
'description': 'Making it easier to deploy ServiceCatalog products',
'long_description': '# aws-service-catalog-puppet\n\n![logo](./docs/logo.png) \n\n## Badges\n\n[![codecov](https://codecov.io/gh/awslabs/aws-service-catalog-puppet/branch/master/graph/badge.svg?token=e8M7mdsmy0)](https://codecov.io/gh/awslabs/aws-service-catalog-puppet)\n\n\n## What is it?\nThis is a python3 framework that makes it easier to share multi region AWS Service Catalog portfolios and makes it \npossible to provision products into accounts declaratively using a metadata based rules engine.\n\nWith this framework you define your accounts in a YAML file. You give each account a set of tags, a default region and \na set of enabled regions.\n\nOnce you have done this you can define portfolios should be shared with each set of accounts using the tags and you \ncan specify which regions the shares occur in.\n\nIn addition to this, you can also define products that should be provisioned into accounts using the same tag based \napproach. The framework will assume role into the target account and provision the product on your behalf.\n\n\n## Getting started\n\nYou can read the [installation how to](https://service-catalog-tools-workshop.com/30-how-tos/10-installation/30-service-catalog-puppet.html)\nor you can read through the [every day use](https://service-catalog-tools-workshop.com/30-how-tos/50-every-day-use.html)\nguides.\n\nYou can read the [documentation](https://aws-service-catalog-puppet.readthedocs.io/en/latest/) to understand the inner \nworkings. \n\n\n## Going further\n\nThe framework is one of a pair. The other is [aws-service-catalog-factory](https://github.com/awslabs/aws-service-catalog-factory).\nWith Service Catalog Factory you can create pipelines that deploy multi region portfolios very easily. \n\n## License\n\nThis library is licensed under the Apache 2.0 License. \n \n',
'author': 'Eamonn Faherty',
Expand Down

0 comments on commit 3e3a081

Please sign in to comment.