Exemplo n.º 1
0
    def __init__(self,
                 metadata: dict = {},
                 transaction: Transaction = None,
                 **kwargs):
        self.func_name = None
        self.is_with_statement = False

        if transaction:
            self.transaction = transaction
        else:
            self.transaction = Transaction(metadata)

        if config.get_global('SET_LOGGING', True):
            set_logger(transaction=self.transaction)

        self.locator = Locator(self.transaction)
        self.handler = {
            'authentication': {
                'handlers': [],
                'methods': []
            },
            'authorization': {
                'handlers': [],
                'methods': []
            },
            'mutation': {
                'handlers': [],
                'methods': []
            },
            'event': {
                'handlers': [],
                'methods': []
            },
        }
Exemplo n.º 2
0
 def __init__(self, single_task: dict):
     self.locator = single_task['locator']
     self.name = single_task['name']
     self.metadata = single_task['metadata']
     self.method = single_task['method']
     self.params = single_task['params']
     transaction = Transaction(meta=self.metadata)
     self._locator = Locator(transaction)
Exemplo n.º 3
0
    def __init__(self, transaction: Transaction = None):

        if transaction:
            self.transaction = transaction
        else:
            self.transaction = Transaction()

        self.locator = Locator(self.transaction)
Exemplo n.º 4
0
 def __init__(self, queue, interval):
     super().__init__(queue, interval)
     self.count = self._init_count()
     self.locator = Locator()
     self.TOKEN = self._update_token()
     self.domain_id = _get_domain_id_from_token(self.TOKEN)
     self.schedule_info = {}
     self.idx = 0
Exemplo n.º 5
0
 def __init__(self,
              experimental_non_blocking=True,
              experimental_thread_pool=None):
     super().__init__(experimental_non_blocking, experimental_thread_pool)
     locator = Locator()
     self.actuator = locator.get_actuator('Health')
     self.actuator.add_health_update(self)
     self.actuator.check()
Exemplo n.º 6
0
    def __init__(
        self,
        interval=1,
    ):
        self.config = self.parse_config(interval)

        self.count = self._init_count()
        self.locator = Locator()
        self.TOKEN = self._update_token()
        self.domain_id = _get_domain_id_from_token(self.TOKEN)
Exemplo n.º 7
0
    def __init__(self, transaction=None, **kwargs):

        if transaction:
            self.transaction = transaction
        else:
            self.transaction = Transaction()

        self.locator = Locator(self.transaction)

        for k, v in kwargs.items():
            setattr(self, k, v)
Exemplo n.º 8
0
async def webhook(webhook_id: str, access_key: str, request: Request):
    locator = Locator()
    data = await request.json()

    event_service = locator.get_service('EventService')
    event_service.create({
        'webhook_id': webhook_id,
        'access_key': access_key,
        'data': data or {}
    })

    return {}
Exemplo n.º 9
0
    def __init__(self, *args, **kwargs):
        self.transaction = Transaction()
        self.locator = Locator(self.transaction)
        if hasattr(current_app.conf, "spaceone_scheduler_service"):
            self.service_name = current_app.conf.get("spaceone_scheduler_service")
        else:
            raise SpaceOneSchedulerError("can not find CELERY.spaceone_scheduler_service config")

        self.Service = self.locator.get_service(self.service_name, metadata=self.metadata)
        self._schedule = {}
        self._last_updated = None

        Scheduler.__init__(self, *args, **kwargs)
        self.max_interval = (kwargs.get('max_interval')
                             or self.app.conf.CELERYBEAT_MAX_LOOP_INTERVAL or 5)
Exemplo n.º 10
0
    def __init__(self, metadata={}, transaction=None):
        self.func_name = None
        self.is_with_statement = False

        if transaction:
            self.transaction = transaction
        else:
            self.transaction = Transaction(metadata)

        set_logger(transaction=self.transaction)
        self.locator = Locator(self.transaction)
        self.handler = {
            'authentication': {'handlers': [], 'methods': []},
            'authorization': {'handlers': [], 'methods': []},
            'event': {'handlers': [], 'methods': []},
        }
Exemplo n.º 11
0
async def create_event(webhook_id: str, access_key: str, request: Request):
    locator = Locator()
    try:
        try:
            data = await request.json()
        except Exception as e:
            _LOGGER.debug(f'JSON Parsing Error: {e}')
            raise ERROR_UNKNOWN(message='JSON Parsing Error: Request body requires JSON format.')

        event_service: EventService = locator.get_service('EventService')
        event_service.create({
            'webhook_id': webhook_id,
            'access_key': access_key,
            'data': data or {}
        })
        return {}
    except ERROR_BASE as e:
        raise HTTPException(status_code=500, detail=e.message)
    except Exception as e:
        raise HTTPException(status_code=500, detail=f'Unknown Error: {str(e)}')
Exemplo n.º 12
0
    def _change_secret_id_filter(condition):
        value = condition.get('v') or condition.get('value')
        operator = condition.get('o') or condition.get('operator')

        map_query = {'filter': [{'k': 'secret_id', 'v': value, 'o': operator}]}

        locator = Locator()
        secret_group_map_model = locator.get_model('SecretGroupMap')
        map_vos, total_count = secret_group_map_model.query(**map_query)

        return {
            'k':
            'secret_group_id',
            'v':
            list(
                map(lambda map_vo: map_vo.secret_group.secret_group_id,
                    map_vos)),
            'o':
            'in'
        }
Exemplo n.º 13
0
def _update_alert_state(alert_id, access_key, state):
    locator = Locator()
    try:

        alert_service: AlertService = locator.get_service('AlertService')
        alert_vo: Alert = alert_service.update_state({
            'alert_id': alert_id,
            'access_key': access_key,
            'state': state
        })

        return {
            'alert_number': alert_vo.alert_number,
            'alert_id': alert_vo.alert_id,
            'title': alert_vo.title,
            'state': alert_vo.state,
            'assignee': alert_vo.assignee,
            'urgency': alert_vo.urgency
        }

    except ERROR_BASE as e:
        raise HTTPException(status_code=500, detail=e.message)
    except Exception as e:
        raise HTTPException(status_code=500, detail=f'Unknown Error: {str(e)}')
Exemplo n.º 14
0
 def __init__(self, *args, **kwargs):
     transaction = Transaction()
     self.locator = Locator(transaction)
Exemplo n.º 15
0
 def __init__(self):
     locator = Locator()
     self.actuator = locator.get_actuator('ServerInfo')
Exemplo n.º 16
0
 def __init__(self, queue, **kwargs):
     BaseWorker.__init__(self, queue, **kwargs)
     self.locator = Locator()
Exemplo n.º 17
0
 def __init__(self, queue, interval, minute=':00'):
     super().__init__(queue, interval, minute)
     self.count = self._init_count()
     self.locator = Locator()
     self.TOKEN = self._update_token()
     self.domain_id = _get_domain_id_from_token(self.TOKEN)
 def __init__(self, queue, interval):
     super().__init__(queue, interval)
     self.locator = Locator()
     self._init_config()
     self._create_metadata()
Exemplo n.º 19
0
    with open(config_path, 'r') as f:
        _set_file_config(f)


def print_schedules(schedules: List[SpaceoneTaskData]):
    print(f"id | schedule | total_run_count | last_run")
    for sch in schedules:
        print(
            f"{sch.schedule_id} | {sch.schedule_info} | {sch.total_run_count} | {sch.last_run_at}"
        )
    print('\n\n')


if __name__ == '__main__':
    config_server()
    locator = Locator()
    svc = locator.get_service('ScheduleService')
    print('list all schedules')
    print_schedules(svc.list())

    print('add schedule')
    sch_name = f"test_sche_{randint(1, 1000)}"
    svc.add({
        'domain_id': "sample",
        'enabled': True,
        'task': 'spaceone.core.celery.tasks.test_task',
        'name': sch_name,
        'interval': {
            'period': 'seconds',
            'every': randint(6, 12)
        },
Exemplo n.º 20
0
class BaseAPI(object):
    locator = Locator()

    def __init__(self):
        self._check_variables()
        self._set_grpc_method()

    @property
    def name(self):
        return self.__class__.__name__

    @property
    def pb2_grpc_module(self):
        return self.pb2_grpc

    @property
    def service_name(self):
        return self.pb2.DESCRIPTOR.services_by_name[
            self.__class__.__name__].full_name

    def _check_variables(self):
        if not hasattr(self, 'pb2'):
            raise Exception(
                f'gRPC Servicer has not set <pb2> variable. (servicer={self.__class__.__name__})'
            )

        if not hasattr(self, 'pb2_grpc'):
            raise Exception(
                f'gRPC Servicer has not set <pb2_grpc> variable. (servicer={self.__class__.__name__})'
            )

    def _get_grpc_servicer(self):
        grpc_servicer = None
        for base_class in self.__class__.__bases__:
            if base_class.__module__ == self.pb2_grpc.__name__:
                grpc_servicer = base_class

        if grpc_servicer is None:
            raise Exception(
                f'gRPC servicer is not set. (servicer={self.__class__.__name__})'
            )

        return grpc_servicer

    def _set_grpc_method(self):
        grpc_servicer = self._get_grpc_servicer()

        for f_name, f_object in inspect.getmembers(
                self.__class__, predicate=inspect.isfunction):
            if hasattr(grpc_servicer, f_name):
                setattr(self, f_name,
                        self._grpc_method(f_object, config.get_service()))

    @staticmethod
    def get_minimal(params):
        return params.get('query', {}).get('minimal', False)

    @staticmethod
    def _error_method(error, context):
        is_logging = False
        if not isinstance(error, ERROR_BASE):
            error = ERROR_UNKNOWN(message=error)
            is_logging = True
        elif error.meta.get('type') == 'service':
            is_logging = True

        if is_logging:
            _LOGGER.error(f'(Error) => {error.message} {error}',
                          extra={
                              'error_code': error.error_code,
                              'error_message': error.message,
                              'traceback': traceback.format_exc()
                          })

        details = f'{error.error_code}: {error.message}'
        context.abort(grpc.StatusCode[error.status_code], details)

    def _generate_response(self, response_iterator, context):
        try:
            for response in response_iterator:
                yield response

        except Exception as e:
            self._error_method(e, context)

    def _grpc_method(self, func, service_name):
        def wrapper(request_or_iterator, context):
            try:
                context.api_info = {
                    'service': service_name,
                    'resource': self.__class__.__name__,
                    'verb': func.__name__
                }

                response_or_iterator = func(self, request_or_iterator, context)

                if isinstance(response_or_iterator, types.GeneratorType):
                    return self._generate_response(response_or_iterator,
                                                   context)
                else:
                    return response_or_iterator

            except Exception as e:
                self._error_method(e, context)

        return wrapper

    @staticmethod
    def _convert_message(request):
        return MessageToDict(request, preserving_proto_field_name=True)

    @staticmethod
    def _get_metadata(context):
        metadata = {}
        for key, value in context.invocation_metadata():
            metadata[key.strip()] = value.strip()

        metadata.update(context.api_info)

        # TODO: This is experimental log. Please confirm peer information is useful on k8s.
        metadata.update({'peer': context.peer()})

        return metadata

    def _generate_message(self, request_iterator):
        for request in request_iterator:
            yield self._convert_message(request)

    def parse_request(self, request_or_iterator, context):
        if isinstance(request_or_iterator, collections.Iterable):
            return self._generate_message(
                request_or_iterator), self._get_metadata(context)
        else:
            return self._convert_message(
                request_or_iterator), self._get_metadata(context)