Skip to content

Instantly share code, notes, and snippets.

@kerin
Created November 23, 2017 10:53
Show Gist options
  • Save kerin/4b3a155fc527870e9afa246b3ffaa551 to your computer and use it in GitHub Desktop.
Save kerin/4b3a155fc527870e9afa246b3ffaa551 to your computer and use it in GitHub Desktop.
def sync_service_catalog(s3, artifact):
""" Pseudo logic as follows
1. Extract S3 Zip file
2. Iterate through all the sub-folders in the portfolio directory
3. Read mapping.yaml in each such folder. Refer Readme for more details on syntax
4. If portfolio name matches, update the products by creating a new version
5. If portfolio name does not matches, create a new one.
6. Share the portfolio with list of accounts mentioned in the mapping.yaml
7. Give access to the principals mentioned in the mapping.yaml
8. Tag Portfolio as mentioned in mapping.yaml
:param s3: S3 Boto3 client
:param artifact: Artifact object sent by codepipeline
:return: None
"""
bucket = artifact['location']['s3Location']['bucketName']
key = artifact['location']['s3Location']['objectKey']
revisionID = artifact['revision']
tmp_file = '/tmp/' + str(uuid.uuid4())
logger.info('Downloading objects from S3, {}/{} -> {}'
.format(bucket, key, tmp_file))
s3.download_file(bucket, key, tmp_file)
portfolios_directory = '/tmp/portfolios/'
template_directory = '/tmp/templates'
with zipfile.ZipFile(tmp_file, 'r') as zip:
zip.extractall('/tmp/')
logger.info('Extract Complete')
if os.path.isdir(portfolios_directory):
for folder in os.listdir(portfolios_directory):
logger.info('Checking object = ' + folder)
if os.path.isdir(portfolios_directory + folder):
logger.info('Found ' + folder + ' portfolio')
for mappingfile in os.listdir(portfolios_directory + folder):
logger.info('Found ' + mappingfile + ' inside folder ' + folder)
if str(mappingfile).endswith('mapping.yaml'):
logger.info('Working with ' + mappingfile + ' inside folder ' + folder)
with open((portfolios_directory + str(folder) + "/" + str(mappingfile)), 'r') as stream:
objfile = yaml.load(stream)
# objfile = json.loads(portfolios_directory+folder+"/"+mappingfile)
logger.info('Loaded JSON=' + str(objfile))
lst_portfolio = list_portfolios()
lst_portfolio_name = []
obj_portfolio = {}
for portfolio in lst_portfolio:
if portfolio['DisplayName'] not in lst_portfolio_name:
lst_portfolio_name.append(portfolio['DisplayName'])
if objfile['name'] in lst_portfolio_name:
logger.info('Portfolio %s found. Updating!' % (objfile['name']))
for item in lst_portfolio:
if item['DisplayName'] == objfile['name']:
portfolio_id = item['Id']
obj_portfolio = item
update_portfolio(obj_portfolio, objfile, bucket)
remove_principal_with_portfolio(obj_portfolio['Id'])
associate_principal_with_portfolio(obj_portfolio, objfile)
lst_products = list_products_for_portfolio(portfolio_id)
lst_products_name = []
for products in lst_products:
lst_products_name.append(products['Name'])
for productsInFile in objfile['products']:
s3key = 'sc-templates/' + objfile['name'] + '/' + productsInFile['name'] + '/' + productsInFile['name']\
+ '.yaml'
if productsInFile['name'] in lst_products_name:
logger.info('Product %s found. Updating!' % (productsInFile['name']))
for ids in lst_products:
if ids['Name'] == productsInFile['name']:
productid = ids['ProductId']
s3.upload_file(
template_directory + "/" + productsInFile['template'],
bucket, s3key)
cleanup_product_versions(productid)
create_provisioning_artifact(productsInFile, productid, bucket + "/" + s3key, revisionID)
else:
logger.info('Product %s not found. Creating!' % (productsInFile['name']))
s3.upload_file(
template_directory + "/" + productsInFile['template'],
bucket,
s3key)
create_product(productsInFile, portfolio_id, bucket + "/" + s3key)
else:
logger.info('Portfolio %s not found. Creating!' % (objfile['name']))
create_portfolio_response = create_portfolio(objfile, bucket)
portfolioid = create_portfolio_response['PortfolioDetail']['Id']
associate_principal_with_portfolio(create_portfolio_response['PortfolioDetail'], objfile)
for productsInFile in objfile['products']:
s3key = 'sc-templates/' + objfile['name'] + '/' + productsInFile['name'] + '/' + productsInFile['name'] \
+ '.yaml'
s3.upload_file(
template_directory + "/" + productsInFile['template'], bucket,
s3key)
create_product(productsInFile, portfolioid, bucket + "/" + s3key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment