Exemplo n.º 1
0
    def non_updateable_object_create(self):
        log('ModuleExecutor.non_updateable_object_create()')

        attributes = self.module.params['resource']
        post_data = {self.endpoint: attributes}

        log('post data %s' % post_data)
        result = self.fetcher.post(post_data=post_data, resource=self.endpoint)
        log('post result %s' % result)

        if result['http_response_data']['status'] == 201:
            if result.get('nitro_errorcode') is not None:
                if result['nitro_errorcode'] != 0:
                    raise NitroException(
                        errorcode=result['nitro_errorcode'],
                        message=result.get('nitro_message'),
                        severity=result.get('nitro_severity'),
                    )
        elif 400 <= result['http_response_data']['status'] <= 599:
            raise NitroException(
                errorcode=result.get('nitro_errorcode'),
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
        else:
            msg = 'Did not get nitro errorcode and http status was not 201 or 4xx (%s)' % result[
                'http_response_data']['status']
            self.module.fail_json(msg=msg, **self.module_result)
Exemplo n.º 2
0
    def object_exists(self):
        log('ModuleExecutor.object_exists()')

        resource_missing_errorcode = self.module.params['workflow'].get(
            'resource_missing_errorcode')
        log('resource missing errorcode %a' % resource_missing_errorcode)

        if resource_missing_errorcode is None:
            msg = 'object lifecycle requires resource_missing_errorcode workflow parameter'
            self.module.fail_json(msg=msg, **self.module_result)

        result = self.fetcher.get(self.endpoint, self.id)

        log('get result %s' % result)
        if result['nitro_errorcode'] == 0:
            self.retrieved_object = result['data'][self.endpoint][0]
            return True
        elif result['nitro_errorcode'] == resource_missing_errorcode:
            return False
        else:
            raise NitroException(
                errorcode=result['nitro_errorcode'],
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
Exemplo n.º 3
0
    def _binding_list_item_create(self, binding):
        log('ModuleExecutor._binding_list_item_create()')

        put_data = {self.endpoint: binding}

        log('request put data: %s' % put_data)

        result = self.fetcher.put(put_data=put_data, resource=self.endpoint)

        log('result of put: %s' % result)

        if result['nitro_errorcode'] != 0:
            raise NitroException(
                errorcode=result['nitro_errorcode'],
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
Exemplo n.º 4
0
    def non_updateable_object_delete(self):
        log('ModuleExecutor.non_updateable_object_delete()')

        args = {}
        for key in self.module.params['workflow']['delete_id_attributes']:
            if key in self.configured_object:
                args[key] = self.configured_object[key]

        result = self.fetcher.delete(resource=self.endpoint,
                                     id=self.id,
                                     args=args)
        log('delete result %s' % result)

        if result['nitro_errorcode'] != 0:
            raise NitroException(
                errorcode=result['nitro_errorcode'],
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
Exemplo n.º 5
0
    def binding_create(self):
        log('ModuleExecutor.binding_create()')

        attributes = self.module.params['resource']

        put_data = {self.endpoint: attributes}

        log('request put data: %s' % put_data)

        result = self.fetcher.put(put_data=put_data, resource=self.endpoint)

        log('result of put: %s' % result)

        if result['nitro_errorcode'] != 0:
            raise NitroException(
                errorcode=result['nitro_errorcode'],
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
Exemplo n.º 6
0
    def non_updateable_object_exists(self):
        log('ModuleExecutor.non_updateable_object_exists()')

        resource_missing_errorcode = self.module.params['workflow'].get(
            'resource_missing_errorcode')
        log('resource missing errorcode %a' % resource_missing_errorcode)

        if resource_missing_errorcode is None:
            msg = 'object lifecycle requires resource_missing_errorcode workflow parameter'
            self.module.fail_json(msg=msg, **self.module_result)

        args = {}
        for key in self.module.params['workflow'].get('delete_id_attributes',
                                                      []):
            if key in self.configured_object:
                args[key] = self.configured_object[key]

        log('self.id %s' % self.id)
        result = self.fetcher.get(self.endpoint, id=self.id, args=args)
        log('get result %s' % result)

        if result['nitro_errorcode'] == 0:
            returned_list = result['data'][self.endpoint]
            if len(returned_list) > 1:
                msg = 'Found more than one existing objects'
                self.module.fail_json(msg=msg, **self.module_result)

            # Fallthrough
            self.retrieved_object = result['data'][self.endpoint][0]
            return True
        elif result['nitro_errorcode'] == resource_missing_errorcode:
            return False
        else:
            raise NitroException(
                errorcode=result['nitro_errorcode'],
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
Exemplo n.º 7
0
    def _binding_list_item_delete(self, binding):
        log('ModuleExecutor._binding_list_item_delete()')

        log('Deleting binding %s' % binding)

        # First attribute is the primary id attribute
        id_key = self.key_attributes_present[0]
        id = binding[id_key]

        args = {}
        for key in self.key_attributes_present[1:]:
            if key in binding:
                args[key] = binding[key]

        result = self.fetcher.delete(resource=self.endpoint, id=id, args=args)
        log('delete result %s' % result)

        if result['nitro_errorcode'] != 0:
            raise NitroException(
                errorcode=result['nitro_errorcode'],
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
Exemplo n.º 8
0
    def object_update(self):
        log('ModuleExecutor.object_update()')

        non_updateables_changed = list(
            frozenset(self.non_updateable_attributes)
            & frozenset(self.differing_attributes))
        if len(non_updateables_changed) > 0:
            log('Non updateables changed %s' % non_updateables_changed)
            if self.module.params['workflow']['allow_recreate']:
                self.object_delete()
                self.object_create()
            else:
                msg = (
                    'Not allowed to recreate object. Non updateable attributes changed %s'
                    % non_updateables_changed)
                self.module.fail_json(msg=msg, **self.module_result)
        else:
            attributes = self.module.params['resource']
            for attribute in self.non_updateable_attributes:
                if attribute in attributes:
                    del attributes[attribute]

            put_data = {self.endpoint: attributes}

            log('request put data: %s' % put_data)

            result = self.fetcher.put(put_data=put_data,
                                      resource=self.endpoint)

            log('result of put: %s' % result)

            if result['nitro_errorcode'] != 0:
                raise NitroException(
                    errorcode=result['nitro_errorcode'],
                    message=result.get('nitro_message'),
                    severity=result.get('nitro_severity'),
                )
Exemplo n.º 9
0
    def binding_exists(self):
        log('ModuleExecutor.binding_exists()')

        result = self.fetcher.get(self.endpoint, self.id)

        log('get result %s' % result)

        if result['nitro_errorcode'] == 0:
            if self.endpoint not in result['data']:
                return False

            objects_returned = result['data'][self.endpoint]
            matching_objects = []

            # Compare the present id attributes
            for object in objects_returned:
                if self.binding_matches_id_attributes(object):
                    matching_objects.append(object)

            if len(matching_objects) == 0:
                return False
            elif len(matching_objects) == 1:
                self.retrieved_object = matching_objects[0]
                return True
            elif len(matching_objects) > 1:
                msg = 'Found multiple matching objects for binding'
                self.module.fail_json(msg=msg, **self.module_result)
        elif result['nitro_errorcode'] == self.module.params['workflow'][
                'bound_resource_missing_errorcode']:
            return False
        else:
            raise NitroException(
                errorcode=result['nitro_errorcode'],
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )
Exemplo n.º 10
0
    def bindings_list_identical(self):
        log('ModuleExecutor.bindings_list_identical()')

        configured_bindings = self.configured_object['bindings_list']

        self.key_attributes = copy.deepcopy(
            self.module.params['workflow']['binding_workflow']
            ['delete_id_attributes'])
        self.key_attributes.insert(
            0, self.module.params['workflow']['binding_workflow']
            ['primary_id_attribute'])

        # Sanity check that at least one item is defined in bindings_list
        if len(configured_bindings) == 0:
            msg = 'Bindings list must have at least one item.'
            self.module.fail_json(msg=msg, **self.module_result)
        # Fallthrough

        # Sanity check that all bindings have uniform resource attribute keys
        key_tuples = []
        for binding in configured_bindings:
            attribute_keys_present = list(
                frozenset(binding.keys()) & frozenset(self.key_attributes))
            key_tuple = tuple(sorted(attribute_keys_present))
            key_tuples.append(key_tuple)

        key_tuple_set = frozenset(key_tuples)
        log('key_tuple_set %s' % key_tuple_set)
        if len(key_tuple_set) > 1:
            key_tuples = [item for item in key_tuple_set]
            msg = 'Bindings list key attributes are not uniform. Attribute key sets found %s' % key_tuples
            self.module.fail_json(msg=msg, **self.module_result)

        # Fallthrough

        # Sanity check that all primary ids are one and the same
        primary_id_key = self.module.params['workflow']['binding_workflow'][
            'primary_id_attribute']
        primary_ids_list = [
            item[primary_id_key] for item in configured_bindings
        ]
        primary_ids_set = frozenset(primary_ids_list)
        log('primary_ids_set %s' % primary_ids_set)
        if len(primary_ids_set) > 1:
            keys = [item for item in primary_ids_set]
            msg = 'Need to have only one primary id value. Found: %s' % keys
            self.module.fail_json(msg=msg, **self.module_result)

        # Fallthrough

        # Get existing bindings
        self.id = list(primary_ids_set)[0]
        self.endpoint = self.module.params['workflow']['binding_workflow'][
            'endpoint']

        result = self.fetcher.get(self.endpoint, self.id)

        log('get result %s' % result)

        existing_bindings = []
        if result['nitro_errorcode'] == 0:
            if self.endpoint not in result['data']:
                existing_bindings = []
            else:
                existing_bindings = result['data'][self.endpoint]

        elif result['nitro_errorcode'] == self.module.params['workflow'][
                'binding_workflow']['bound_resource_missing_errorcode']:
            existing_bindings = []
        else:
            raise NitroException(
                errorcode=result['nitro_errorcode'],
                message=result.get('nitro_message'),
                severity=result.get('nitro_severity'),
            )

        # Construct the dictionaries keyed by tuple of key attributes
        # First attribute must be the primary id attribute
        self.key_attributes_present = []
        for item in self.key_attributes:
            if item in list(key_tuple_set)[0]:
                self.key_attributes_present.append(item)

        self.configured_bindings_dict = {}
        for binding in configured_bindings:
            binding_key = self._get_binding_key_tuple(binding)

            if binding_key in self.configured_bindings_dict:
                msg = 'Found duplicate key for configured bindings %s' % (
                    binding_key, )
                self.module.fail_json(msg=msg, **self.module_result)

            log('Configured binding id %s registered to dict' %
                (binding_key, ))
            self.configured_bindings_dict[binding_key] = binding

        self.existing_bindings_dict = {}

        for binding in existing_bindings:
            binding_key = self._get_binding_key_tuple(binding)

            if binding_key in self.existing_bindings_dict:
                msg = 'Found duplicate key for existing bindings %s' % (
                    binding_key, )
                self.module.fail_json(msg=msg, **self.module_result)

            log('Existing binding id %s registered to dict' % (binding_key, ))
            self.existing_bindings_dict[binding_key] = binding

        # Calculate to delete keys
        self.to_delete_keys = []
        for existing_key in self.existing_bindings_dict:
            if existing_key not in self.configured_bindings_dict:
                log('Existing binding key marked for delete %s' %
                    (existing_key, ))
                self.to_delete_keys.append(existing_key)

        # Calculate to update keys
        self.to_update_keys = []
        for existing_key in self.existing_bindings_dict:
            if existing_key in self.configured_bindings_dict:
                configured = self.configured_bindings_dict[existing_key]
                existing = self.existing_bindings_dict[existing_key]
                if not self._binding_list_item_identical_to_configured(
                        configured, existing):
                    log('Existing binding key marked for update %s' %
                        (existing_key, ))
                    self.to_update_keys.append(existing_key)

        # Calculate to create keys
        self.to_create_keys = []
        for configured_key in self.configured_bindings_dict:
            if configured_key not in self.existing_bindings_dict:
                log('Configured binding key marked for create %s' %
                    (configured_key, ))
                self.to_create_keys.append(configured_key)

        # Calculate all changes
        all_change_keys = self.to_create_keys + self.to_update_keys + self.to_delete_keys
        if len(all_change_keys) == 0:
            return True
        else:
            return False