def copy_products_to_local_account()

in resources/sc-autopilot-importer.py [0:0]


def copy_products_to_local_account(local_port,shared_port,launch_constraint,sc_client):
    print("Copying products to local account for portfolio {}...".format(local_port['DisplayName']))
    try:
        print("Copying shared products to local account...")
        #Get list of products in portfolio and copy them if they don't exist
        shared_products = sc_client.search_products_as_admin(PortfolioId= shared_port['Id'])
        print("List of products shared with this account... {}".format(shared_products['ProductViewDetails']))
        #Get a list of products pre-existing in the account
        local_products = sc_client.search_products_as_admin()
        #Compare existing products with shared products
        for y in shared_products['ProductViewDetails']:
            exists = False
            for x in local_products['ProductViewDetails']:
                if(x['ProductViewSummary']['Name'] == y['ProductViewSummary']['Name']):
                    exists = True
                    print("Product {} exists, will not be copied!".format(y['ProductViewSummary']['Name']))
                    associate_products_to_local_portfolio(x['ProductViewSummary']['Name'],local_port,launch_constraint,sc_client)
           #if shared product does not exist in current account, then copy
            if(exists==False):
                response = sc_client.copy_product(
                    AcceptLanguage='en',
                    SourceProductArn=y['ProductARN'],
                    CopyOptions=['CopyTags',]
                )
                print("Copied product {} to account successfully. Response: {}".format(y['ProductARN'],response))
                time.sleep(20)
                associate_products_to_local_portfolio(y['ProductViewSummary']['Name'],local_port,launch_constraint,sc_client)

    except botocore.exceptions.ClientError as e:
        print(e)
        print("Failed In the Create PortfolioShare for. Error : {}".format(e))
        sys.exit(1)
    return "Success"