def setup_portfolios()

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


def setup_portfolios(sc_iam_role,launch_constraint,share_type):
    print("Looking for shared portfolios in this account...")
    sc_client = boto3.client('servicecatalog')
    try:
        #List portfolio shares
        shared_portfolios = sc_client.list_accepted_portfolio_shares(PortfolioShareType=share_type)
        print("The portfolio share type is {} ".format(share_type))
        print("The following portfolios are shared with this account: {}".format(shared_portfolios['PortfolioDetails']))

        #Look for local copy of shared portfolio. If not, create one.
        local_portfolios = sc_client.list_portfolios()
        shared_port_details = ''
        for shared_port in shared_portfolios['PortfolioDetails']:
            exists = False
            shared_port_details = sc_client.describe_portfolio(Id=shared_port['Id'])
            for local_port in local_portfolios['PortfolioDetails']:
                if(local_port['DisplayName'] == shared_port['DisplayName']):
                    exists = True
                    print("Portfolio {} exists, will not be created!".format(shared_port['DisplayName']))
                    #associate iam to portfolio
                    associate_iam_principal(local_port,sc_iam_role,sc_client)
                    #logic to copy products and associate with local portfolio
                    copy_products_to_local_account(local_port,shared_port,launch_constraint,sc_client)

            #if a local copy of portfolio does not exist, create a local portfolio
            if(exists==False):
                response = sc_client.create_portfolio(
                    AcceptLanguage='en',
                    DisplayName=shared_port.get('DisplayName', ''),
                    Description=shared_port.get('Description', ''),
                    ProviderName=shared_port.get('ProviderName', ''),
                    Tags=[
                        {
                            'Key': 'PortfolioType',
                            'Value': 'Inherited'
                        },
                    ]
                )
                print("Created local portfolio successfully. Response: {}".format(response))
                associate_iam_principal(response['PortfolioDetail'], sc_iam_role,sc_client)
                copy_products_to_local_account(response['PortfolioDetail'],shared_port,launch_constraint,sc_client)
        return "Success"
    except botocore.exceptions.ClientError as e:
        print(e)
        print("Failed In the Create PortfolioShare for. Error : {}".format(e))
        sys.exit(1)
    return "Success"