alignak_backend_client/backend_client.py
def delete_resource(self, resource_name, name):
        """Delete a specific resource by name"""
        try:
            logger.info("Trying to get %s: '%s'", resource_name, name)

            if name is None:
                # No name is defined, delete all the resources...
                if not self.dry_run:
                    headers = {
                        'Content-Type': 'application/json'
                    }
                    logger.info("-> deleting all %s", resource_name)
                    self.backend.delete(resource_name, headers)
                    logger.info("-> deleted all %s", resource_name)
                else:
                    response = {'_id': '_fake', '_etag': '_fake'}
                    logger.info("Dry-run mode: should have deleted all %s", resource_name)
            else:
                params = {'where': json.dumps({'name': name})}
                if resource_name in ['host', 'service', 'user']:
                    params = {'where': json.dumps({'name': name, '_is_template': self.model})}
                if resource_name == 'service' and '/' in name:
                    splitted_name = name.split('/')
                    name = splitted_name[0] + '_' + splitted_name[1]

                    # Get host from name
                    response2 = self.backend.get(
                        'host', params={'where': json.dumps({'name': splitted_name[0]})})
                    if response2['_items']:
                        host = response2['_items'][0]
                        logger.info("Got host '%s' for the service '%s'",
                                    splitted_name[0], splitted_name[1])
                    else:
                        logger.warning("Not found host '%s'!", splitted_name[0])
                        return False

                    if splitted_name[1] == '*':
                        params = {'where': json.dumps({'host': host['_id']})}
                    else:
                        params = {'where': json.dumps({'name': splitted_name[1],
                                                       'host': host['_id']})}

                response = self.backend.get_all(resource_name, params=params)
                if response['_items']:
                    logger.info("-> found %d matching %s", len(response['_items']), resource_name)
                    for item in response['_items']:
                        logger.info("-> found %s '%s': %s", resource_name, name, item['name'])

                        # Exists in the backend, we must delete the element...
                        if not self.dry_run:
                            headers = {
                                'Content-Type': 'application/json',
                                'If-Match': item['_etag']
                            }
                            logger.info("-> deleting %s: %s", resource_name, item['name'])
                            self.backend.delete(resource_name + '/' + item['_id'], headers)
                            logger.info("-> deleted %s: %s", resource_name, item['name'])
                        else:
                            response = {'_id': '_fake', '_etag': '_fake'}
                            logger.info("Dry-run mode: should have deleted an %s '%s'",
                                        resource_name, name)
                        logger.info("-> deleted: '%s': %s",
                                    resource_name, item['_id'])
                else:
                    logger.warning("-> %s item '%s' not found", resource_name, name)
                    return False
        except BackendException as exp:  # pragma: no cover, should never happen
            logger.exception("Exception: %s", exp)
            logger.error("Response: %s", exp.response)
            print("Deletion error for  '%s' : %s" % (resource_name, name))
            print("~~~~~~~~~~~~~~~~~~~~~~~~~~")
            print("Exiting with error code: 5")
            return False

        return True
def delete_resource(self, resource_name, name):
        """Delete a specific resource by name"""
        try:
            logger.info("Trying to get %s: '%s'", resource_name, name)

            if name is None:
                # No name is defined, delete all the resources...
                if not self.dry_run:
                    headers = {
                        'Content-Type': 'application/json'
                    }
                    logger.info("-> deleting all %s", resource_name)
                    self.backend.delete(resource_name, headers)
                    logger.info("-> deleted all %s", resource_name)
                else:
                    response = {'_id': '_fake', '_etag': '_fake'}
                    logger.info("Dry-run mode: should have deleted all %s", resource_name)
            else:
                params = {'where': json.dumps({'name': name})}
                if resource_name in ['host', 'service', 'user']:
                    params = {'where': json.dumps({'name': name, '_is_template': self.model})}
                if resource_name == 'service' and '/' in name:
                    splitted_name = name.split('/')
                    name = splitted_name[0] + '_' + splitted_name[1]

                    # Get host from name
                    response2 = self.backend.get(
                        'host', params={'where': json.dumps({'name': splitted_name[0]})})
                    if response2['_items']:
                        host = response2['_items'][0]
                        logger.info("Got host '%s' for the service '%s'",
                                    splitted_name[0], splitted_name[1])
                    else:
                        logger.warning("Not found host '%s'!", splitted_name[0])
                        return False

                    if splitted_name[1] == '*':
                        params = {'where': json.dumps({'host': host['_id']})}
                    else:
                        params = {'where': json.dumps({'name': splitted_name[1],
                                                       'host': host['_id']})}

                response = self.backend.get_all(resource_name, params=params)
                if response['_items']:
                    logger.info("-> found %d matching %s", len(response['_items']), resource_name)
                    for item in response['_items']:
                        logger.info("-> found %s '%s': %s", resource_name, name, item['name'])

                        # Exists in the backend, we must delete the element...
                        if not self.dry_run:
                            headers = {
                                'Content-Type': 'application/json',
                                'If-Match': item['_etag']
                            }
                            logger.info("-> deleting %s: %s", resource_name, item['name'])
                            self.backend.delete(resource_name + '/' + item['_id'], headers)
                            logger.info("-> deleted %s: %s", resource_name, item['name'])
                        else:
                            response = {'_id': '_fake', '_etag': '_fake'}
                            logger.info("Dry-run mode: should have deleted an %s '%s'",
                                        resource_name, name)
                        logger.info("-> deleted: '%s': %s",
                                    resource_name, item['_id'])
                else:
                    logger.warning("-> %s item '%s' not found", resource_name, name)
                    return False
        except BackendException as exp:  # pragma: no cover, should never happen
            logger.exception("Exception: %s", exp)
            logger.error("Response: %s", exp.response)
            print("Deletion error for  '%s' : %s" % (resource_name, name))
            print("~~~~~~~~~~~~~~~~~~~~~~~~~~")
            print("Exiting with error code: 5")
            return False

        return True
def delete_resource ( self , resource_name , name ) : try : logger . info ( "Trying to get %s: '%s'" , resource_name , name ) if name is None : # No name is defined, delete all the resources... if not self . dry_run : headers = { 'Content-Type' : 'application/json' } logger . info ( "-> deleting all %s" , resource_name ) self . backend . delete ( resource_name , headers ) logger . info ( "-> deleted all %s" , resource_name ) else : response = { '_id' : '_fake' , '_etag' : '_fake' } logger . info ( "Dry-run mode: should have deleted all %s" , resource_name ) else : params = { 'where' : json . dumps ( { 'name' : name } ) } if resource_name in [ 'host' , 'service' , 'user' ] : params = { 'where' : json . dumps ( { 'name' : name , '_is_template' : self . model } ) } if resource_name == 'service' and '/' in name : splitted_name = name . split ( '/' ) name = splitted_name [ 0 ] + '_' + splitted_name [ 1 ] # Get host from name response2 = self . backend . get ( 'host' , params = { 'where' : json . dumps ( { 'name' : splitted_name [ 0 ] } ) } ) if response2 [ '_items' ] : host = response2 [ '_items' ] [ 0 ] logger . info ( "Got host '%s' for the service '%s'" , splitted_name [ 0 ] , splitted_name [ 1 ] ) else : logger . warning ( "Not found host '%s'!" , splitted_name [ 0 ] ) return False if splitted_name [ 1 ] == '*' : params = { 'where' : json . dumps ( { 'host' : host [ '_id' ] } ) } else : params = { 'where' : json . dumps ( { 'name' : splitted_name [ 1 ] , 'host' : host [ '_id' ] } ) } response = self . backend . get_all ( resource_name , params = params ) if response [ '_items' ] : logger . info ( "-> found %d matching %s" , len ( response [ '_items' ] ) , resource_name ) for item in response [ '_items' ] : logger . info ( "-> found %s '%s': %s" , resource_name , name , item [ 'name' ] ) # Exists in the backend, we must delete the element... if not self . dry_run : headers = { 'Content-Type' : 'application/json' , 'If-Match' : item [ '_etag' ] } logger . info ( "-> deleting %s: %s" , resource_name , item [ 'name' ] ) self . backend . delete ( resource_name + '/' + item [ '_id' ] , headers ) logger . info ( "-> deleted %s: %s" , resource_name , item [ 'name' ] ) else : response = { '_id' : '_fake' , '_etag' : '_fake' } logger . info ( "Dry-run mode: should have deleted an %s '%s'" , resource_name , name ) logger . info ( "-> deleted: '%s': %s" , resource_name , item [ '_id' ] ) else : logger . warning ( "-> %s item '%s' not found" , resource_name , name ) return False except BackendException as exp : # pragma: no cover, should never happen logger . exception ( "Exception: %s" , exp ) logger . error ( "Response: %s" , exp . response ) print ( "Deletion error for  '%s' : %s" % ( resource_name , name ) ) print ( "~~~~~~~~~~~~~~~~~~~~~~~~~~" ) print ( "Exiting with error code: 5" ) return False return True
Delete a specific resource by name
Delete a specific resource by name