Exemplo n.º 1
0
    def is_state_definition_equivalent(self):
        """
        Determines if the current state definition and the desired state definition are equivalent.

        Returns:
            bool
        """
        filtered_curr_def = pcf_util.param_filter(
            self.current_state_definition, self.UPDATE_PARAM_FILTER)

        filtered_des_def = pcf_util.param_filter(self.desired_state_definition,
                                                 self.REMOVE_PARAM_FILTER,
                                                 True)

        def_diff = pcf_util.diff_dict(filtered_curr_def, filtered_des_def)

        curr_tags = self.client.describe_tags(ResourceArns=[self.arn])

        curr_tags = curr_tags.get('TagDescriptions')[0].get('Tags', None)
        des_tags = self.desired_state_definition.get('Tags', None)

        curr_availabilty_zones = pcf_util.find_nested_dict_value(
            self.current_state_definition, ['AvailabilityZones'])
        curr_subnets_list = pcf_util.transform_list_of_dicts_to_desired_list(
            curr_availabilty_zones, 'SubnetId')

        des_subnets_list = self.desired_state_definition.get('Subnets')

        curr_listeners = [
            pcf_util.param_filter(x, {'SslPolicy'}, True)
            for x in self.get_listener_status()
        ]
        des_listeners = self.custom_config['Listeners']

        for item in des_listeners:
            item.update({'Protocol': item.get('Protocol').upper()})

        for item in curr_listeners:
            if 'ListenerArn' in item:
                item.pop('ListenerArn')

        if def_diff or self._need_update(curr_tags, des_tags) or self._need_update(curr_listeners, des_listeners) \
            or not pcf_util.is_list_equal(curr_subnets_list, des_subnets_list):
            return False
        return True
Exemplo n.º 2
0
    def _update(self):
        """
        updates the alb particle to match the desired state

        """
        filtered_curr_def = pcf_util.param_filter(
            self.current_state_definition, self.UPDATE_PARAM_FILTER)

        filtered_des_def = pcf_util.param_filter(self.desired_state_definition,
                                                 self.REMOVE_PARAM_FILTER,
                                                 True)

        diff = pcf_util.diff_dict(filtered_curr_def, filtered_des_def)

        curr_tags = self.client.describe_tags(ResourceArns=[self.arn])
        curr_tags = curr_tags.get('TagDescriptions')[0].get('Tags')
        des_tags = self.desired_state_definition.get('Tags', [{}])

        curr_listeners = [x for x in self.get_listener_status()]
        # Format current listener to compare.
        filtered_curr_listeners = [x for x in self.get_listener_status()]
        for item in filtered_curr_listeners:
            if 'ListenerArn' in item:
                item.pop('ListenerArn')

        des_listeners = self.custom_config['Listeners']

        if diff.get('SecurityGroups', None):
            self.client.set_security_groups(
                LoadBalancerArn=self.arn,
                SecurityGroups=filtered_des_def.get('SecurityGroups'))

        curr_availabilty_zones = pcf_util.find_nested_dict_value(
            self.current_state_definition, ['AvailabilityZones'])
        curr_subnets_list = pcf_util.transform_list_of_dicts_to_desired_list(
            curr_availabilty_zones, 'SubnetId')
        des_subnets_list = self.desired_state_definition.get('Subnets')

        if not pcf_util.is_list_equal(curr_subnets_list, des_subnets_list):
            update = des_subnets_list
            if update:
                self.client.set_subnets(LoadBalancerArn=self.arn,
                                        Subnets=list(update))

        if self._need_update(curr_tags, des_tags):
            add = list(
                itertools.filterfalse(lambda x: x in curr_tags, des_tags))
            remove = list(
                itertools.filterfalse(lambda x: x in des_tags, curr_tags))
            if remove:
                self.client.remove_tags(ResourceArns=[self.arn],
                                        TagKeys=[x.get('Key') for x in remove])
            if add:
                self.client.add_tags(ResourceArns=[self.arn], Tags=add)

        if self._need_update(filtered_curr_listeners, des_listeners):
            #Format current listener and desired listener to compare.
            for item in des_listeners:
                item.update({'Protocol': item.get('Protocol').upper()})

            for item in curr_listeners:
                self.client.delete_listener(ListenerArn=item['ListenerArn'])

            for item in des_listeners:
                item['LoadBalancerArn'] = self.arn
                self.create_listener(item)