def list_stack_instances()

in source/state_machine_handler.py [0:0]


    def list_stack_instances(self):
        """Set values for AccountList, RegionList, LoopFlag, etc.
           that will be used by step functions as input to determine
           its operations: create, update or delete stackset or
           stack instances

        Returns:
            event

        Raises:
        """
        self.logger.info("Executing: " + self.__class__.__name__ + "/"
                         + inspect.stack()[0][3])
        self.logger.info(self.params)

        if 'ParameterOverrides' in self.params.keys():
            self.logger.info("Override parameters found in the event")
            self.event.update({'OverrideParametersExist': 'yes'})
        else:
            self.logger.info("Override parameters NOT found in the event")
            self.event.update({'OverrideParametersExist': 'no'})

        # Check if stack instances exist
        stack_set = StackSet(self.logger)

        # if account list is not present then only create StackSet
        # and skip stack instance creation
        if type(self.params.get('AccountList')) is not list or \
                not self.params.get('AccountList'):
            self._set_skip_stack_instance_operation()
            return self.event
        else:  # proceed if account list exists
            account_id = self.params.get('AccountList')[0]

            # if this is 2nd round, fetch one of the existing accounts
            # that hasn't been processed in the first round
            if self.event.get('ActiveAccountList') is not None  \
                and self.event.get('ActiveRegionList') is not None  \
                and self.params.get('AccountList') !=  \
                    self.event.get('ActiveAccountList'):
                account_id = self._add_list(self.params.get('AccountList'),
                                            self.event.get('ActiveAccountList')
                                            )[0]

            self.logger.info("Account Id for list stack instance: {}"
                             .format(account_id))

            if self.event.get('NextToken') is not None and  \
                    self.event.get('NextToken') != 'Complete':

                self.logger.info('Found next token')
                response = stack_set.list_stack_instances(
                    StackSetName=self.params.get('StackSetName'),
                    StackInstanceAccount=account_id,
                    MaxResults=20,
                    NextToken=self.event.get('NextToken')
                    )
            else:
                self.logger.info('Next token not found.')
                response = stack_set.list_stack_instances(
                    StackSetName=self.params.get('StackSetName'),
                    StackInstanceAccount=account_id,
                    MaxResults=20)
            self.logger.info("List Stack Instance Response"
                             " for account: {}".format(account_id))
            self.logger.info(response)

            if response is not None:
                # If no stack instances are found for new accounts
                # in manifest file entered by user AND no other
                # existing stack instances, then only create stack
                # instance operation is needed.
                # Therefore here set values as input for step functions
                # to trigger create operation accordingly.
                if not response.get('Summaries') and \
                        self.event.get('StackInstanceAccountList') is None:

                    self._set_only_create_stack_instance_operation()
                    return self.event

                # If there are stack instances, follow the route below
                # to determine what operations (create, update, delete)
                # that step functions should perform.
                else:
                    existing_region_list = [] \
                        if self.event.get('ExistingRegionList') is None \
                        else self.event.get('ExistingRegionList')
                    existing_account_list = [] \
                        if self.event.get('StackInstanceAccountList') \
                        is None \
                        else self.event.get('StackInstanceAccountList')

                    if response.get('Summaries'):
                        self.logger.info("Found existing stack instance for "
                                         "AccountList.")
                        self.event.update({'InstanceExist': 'yes'})
                        existing_region_list = \
                            self._get_existing_stack_instance_info(
                                    response.get('Summaries'),
                                    existing_region_list)
                    # If there are no stack instances for new account list
                    # but there are some for existing accounts that are
                    # not in the new account list, get the info about
                    # those stack instances.
                    elif self.event.get('StackInstanceAccountList') \
                            is not None and len(existing_region_list) == 0:
                        account_id = self.event.get(
                                        'StackInstanceAccountList')[0]
                        response = stack_set.list_stack_instances(
                            StackSetName=self.params.get('StackSetName'),
                            StackInstanceAccount=account_id,
                            MaxResults=20)
                        self.logger.info("List Stack Instance Response for"
                                         " StackInstanceAccountList")
                        self.logger.info(response)

                        if response.get('Summaries'):
                            self.logger.info("Found existing stack instances "
                                             "for StackInstanceAccountList.")
                            self.event.update({'InstanceExist': 'yes'})
                            existing_region_list =  \
                                self._get_existing_stack_instance_info(
                                        response.get('Summaries'),
                                        existing_region_list)
                        else:
                            existing_region_list =  \
                                self.params.get('RegionList')

                    self.logger.info("Updated existing region List: {}"
                                     .format(existing_region_list))

                    self.logger.info("Next Token Returned: {}"
                                     .format(response.get('NextToken')))

                    if response.get('NextToken') is None:

                        add_region_list, delete_region_list, add_account_list,\
                            delete_account_list = \
                            self._get_add_delete_region_account_list(
                                existing_region_list,
                                existing_account_list)
                        self._set_loop_flag(add_region_list,
                                            delete_region_list,
                                            add_account_list,
                                            delete_account_list)
                        self._update_event_for_add(add_account_list,
                                                   add_region_list)
                        self._update_event_for_delete(delete_account_list,
                                                      delete_region_list)
                        self.event.update({'ExistingRegionList':
                                           existing_region_list})
                    else:
                        self.event.update({'NextToken':
                                           response.get('NextToken')})
                        # Update the self.event with existing_region_list
                        self.event.update({'ExistingRegionList':
                                           existing_region_list})
                    return self.event
        return self.event