예제 #1
0
 def __init__(self, config: Config, source_id: str):
     self.client = SchedulerServiceClient(
         api_host=config.credentials.api_host,
         auth_token=config.credentials.auth_token,
         target_org=config.org.id)
     self.source_id = source_id
     self.schedule = None
예제 #2
0
class SourceScheduleBuilder(Builder):
    def __init__(self, config: Config, source_id: str):
        self.client = SchedulerServiceClient(
            api_host=config.credentials.api_host,
            auth_token=config.credentials.auth_token,
            target_org=config.org.id)
        self.source_id = source_id
        self.schedule = None

    def from_user(self) -> 'SourceScheduleBuilder':
        try:
            self.schedule = self._from_user()
        except KeyboardInterrupt:
            print()

        return self

    def _from_user(self) -> DataSourceSchedule:
        schedule_config = self.get_input("Schedule", required=True)

        return DataSourceSchedule(source_id=self.source_id,
                                  schedule_config=schedule_config)

    def build(self, update: bool = False) -> Optional[DataSourceSchedule]:
        if self.schedule:
            if update:
                schedule = self.client.update(schedule=self.schedule)
                print("Schedule set")
            else:
                schedule = self.client.add(schedule=self.schedule)
                print("Schedule set")
            return schedule

        return None
예제 #3
0
        def __init__(self, config: Config, source: Source):
            self.config = config
            super().__init__()

            self.source = source
            self.client = SchedulerServiceClient(
                api_host=config.credentials.api_host,
                auth_token=config.credentials.auth_token,
                target_org=config.org.id)
예제 #4
0
        def __init__(self, config: Config):
            self.config = config

            super().__init__()

            self.client = SourceCatalogClient(
                api_host=config.credentials.api_host, auth_token=config.credentials.auth_token, target_org=config.org.id)
            self.scheduler_client = SchedulerServiceClient(
                api_host=config.credentials.api_host, auth_token=config.credentials.auth_token, target_org=config.org.id)
            self.oauth_client = SourceOAuthClient(
                api_host=self.config.credentials.api_host, auth_token=self.config.credentials.auth_token, target_org=self.config.org.id)
예제 #5
0
    class _SchedulesCmd(Command):
        '''Schedules - view and manage source execution schedules'''
        def __init__(self, config: Config, source: Source):
            self.config = config
            super().__init__()

            self.source = source
            self.client = SchedulerServiceClient(
                api_host=config.credentials.api_host,
                auth_token=config.credentials.auth_token,
                target_org=config.org.id)

        def emptyline(self):
            return self.do_help('')

        @builder.cmd(
            permissions=['admin:source:schedule:get', 'source:schedule:get'])
        def do_info(self, _arg):
            '''retrieve schedule details'''
            try:
                sched = self.client.get(self.source.id)
                print(sched.schedule_config)
            except:
                print("No schedule set")

        @builder.cmd(permissions=[
            'admin:source:schedule:delete', 'source:schedule:delete'
        ])
        def do_del(self, _arg):
            '''delete source schedule - disables automatic data collection'''
            if Builder.get_yes_no("Really delete schedule?"):
                self.client.delete(self.source.id)
                print(f"{source.display_name} schedule removed")

        @builder.cmd(permissions=[
            'admin:source:schedule:create', 'source:schedule:create',
            'admin:source:schedule:update', 'source:schedule:update'
        ])
        def do_set(self, _arg):
            '''Set source schedule'''
            update = False
            try:
                self.client.get(self.source.id)
                update = True
            except:
                pass

            SourceScheduleBuilder(
                self.config, self.source.id).from_user().build(update=update)
예제 #6
0
        def __init__(self, config: Config, source: Optional[Source] = None):
            self.config = config
            super().__init__()

            self.source = source
            self.client = SourceConfigClient(
                api_host=config.credentials.api_host,
                auth_token=config.credentials.auth_token,
                target_org=config.org.id)
            self.scheduler_client = SchedulerServiceClient(
                api_host=config.credentials.api_host,
                auth_token=config.credentials.auth_token,
                target_org=config.org.id)

            catalog_client = SourceCatalogClient(
                api_host=config.credentials.api_host,
                auth_token=config.credentials.auth_token,
                target_org=config.org.id)
            self.sources_by_id = {
                self.source.id: self.source
            } if self.source else {
                source.id: source
                for source in catalog_client.list()
            }
예제 #7
0
    class _SourcesCmd(CommandWithList):
        '''Sources - work with available integration sources available in the source catalog.'''

        def __init__(self, config: Config):
            self.config = config

            super().__init__()

            self.client = SourceCatalogClient(
                api_host=config.credentials.api_host, auth_token=config.credentials.auth_token, target_org=config.org.id)
            self.scheduler_client = SchedulerServiceClient(
                api_host=config.credentials.api_host, auth_token=config.credentials.auth_token, target_org=config.org.id)
            self.oauth_client = SourceOAuthClient(
                api_host=self.config.credentials.api_host, auth_token=self.config.credentials.auth_token, target_org=self.config.org.id)


        def default(self, line: str) -> Union[bool, CmdResponse]:
            try:
                idx = int(line)
                return self._do_select(idx)
            except IndexError as e:
                print(str(e))
                return False
            except ValueError:
                pass

            return super().default(line)

        def get_things(self):
            return self.client.list()

        def things_header(self):
            return [("Source", 40)]

        def format_thing(self, source: Source) -> str:
            return source.display_name.rjust(40)

        @builder.empty_cmd()
        def _do_list(self):
            '''list available sources'''
            return self._list()

        @builder.empty_cmd()
        def _do_select(self, idx: int) -> CmdResponse:
            '''change scope into source [idx]'''
            selected = self.get_thing_by_index(idx)

            return NewScopeResponse(SourceScope(self.config, selected))

        @builder.cmd(permissions=['admin:source:config:write', 'source:config:write'])
        def do_config(self, idx):
            '''configure source [idx]'''
            selected: Source = self.get_thing_by_index(self.arg_as_idx(idx))
            builder = source_config = SourceConfigBuilder(self.config, selected.id)
            source_config = builder.from_user().build()
            if source_config:
                assert source_config.id

                if builder.schema.auth['title'] == 'oauth2':
                    url = self.oauth_client.initiate(source_config_id=source_config.id)
                    print("Browse to the following URL to initiate the OAuth workflow:")
                    print(url)

                try:
                    self.scheduler_client.execute(source_config_id=source_config.id)
                except Exception as e:
                    print(f"Unable to schedule immediate execution: {e}")

        @builder.cmd(permissions=['admin:source:create'])
        def do_add(self, _arg):
            '''add new source'''
            print("Source Configuration")
            source = SourceBuilder(config=self.config).from_user().build()
            if source:
                print("Source Schema Configuration")
                schema = SourceSchemaBuilder(config=self.config,
                                             source_id=source.id).from_user().build()
                if schema:
                    print("Source Schedule")
                    SourceScheduleBuilder(config=self.config,
                                          source_id=source.id).from_user().build()
예제 #8
0
    class SourceConfigsCmd(CommandWithList):
        '''Configs - manage source configs'''
        def __init__(self, config: Config, source: Optional[Source] = None):
            self.config = config
            super().__init__()

            self.source = source
            self.client = SourceConfigClient(
                api_host=config.credentials.api_host,
                auth_token=config.credentials.auth_token,
                target_org=config.org.id)
            self.scheduler_client = SchedulerServiceClient(
                api_host=config.credentials.api_host,
                auth_token=config.credentials.auth_token,
                target_org=config.org.id)

            catalog_client = SourceCatalogClient(
                api_host=config.credentials.api_host,
                auth_token=config.credentials.auth_token,
                target_org=config.org.id)
            self.sources_by_id = {
                self.source.id: self.source
            } if self.source else {
                source.id: source
                for source in catalog_client.list()
            }

        def get_things(self):
            def get_timestamp(x: SourceConfig) -> datetime:
                assert x.last_updated_timestamp
                return x.last_updated_timestamp

            return sorted(self.client.list(
                source_id=self.source.id if self.source else None),
                          key=get_timestamp,
                          reverse=True)

        def things_header(self):
            return [("ID", 36), ("Source", 40)]

        def format_thing(self, config: SourceConfig) -> str:
            source = self.sources_by_id.get(config.source_id)
            return f"{config.id.rjust(36)} {source.display_name.rjust(40) if source else 'UNKNOWN'.rjust(40)}"

        @builder.empty_cmd()
        def _do_list(self):
            '''list source configs'''
            return self._list()

        @builder.cmd(
            permissions=['admin:source:config:read', 'source:config:read'])
        def do_info(self, idx):
            '''retrieve details for config [idx]'''

            selected: SourceConfig = self.get_thing_by_index(
                self.arg_as_idx(idx))

            pprint(selected.as_dict())

        @builder.cmd(
            permissions=['admin:source:audit:read', 'source:audit:read'])
        def do_audit(self, idx_n):
            '''retrieve last N execution audit logs for config [idx]'''

            idx, _, n = idx_n.partition(' ')

            n = int(n) if n else 10

            selected: SourceConfig = self.get_thing_by_index(
                self.arg_as_idx(idx))

            client = SourceAuditClient(
                api_host=self.config.credentials.api_host,
                auth_token=self.config.credentials.auth_token,
                target_org=self.config.org.id)

            for audit in client.list("execution",
                                     source_config_id=selected.id,
                                     per_page=n):
                pprint(audit.as_dict(), width=240)

        @builder.cmd(
            permissions=['admin:source:config:write', 'source:config:write'])
        def do_oauth(self, idx):
            '''initiate OAuth2 workflow for config [idx]'''

            selected: SourceConfig = self.get_thing_by_index(
                self.arg_as_idx(idx))
            assert selected.id

            if selected.auth and selected.auth.schema != 'oauth2':
                raise Exception("Not an OAuth2 config")

            client = SourceOAuthClient(
                api_host=self.config.credentials.api_host,
                auth_token=self.config.credentials.auth_token,
                target_org=self.config.org.id)

            url = client.initiate(source_config_id=selected.id)
            print(
                "Browse to the following URL to initiate the OAuth workflow:")
            print(url)

        @builder.cmd(
            permissions=['admin:source:config:delete', 'source:config:delete'])
        def do_del(self, idx):
            '''delete config [idx]'''
            selected: SourceConfig = self.get_thing_by_index(
                self.arg_as_idx(idx))
            assert selected.id

            if Builder.get_yes_no(f"Really delete {selected.id}?",
                                  default_yes=False):
                self.client.delete(selected.id)
                print(f"Deleted {selected.id}")

        @builder.cmd(permissions=['admin:source:schedule:execute'])
        def do_exec(self, idx):
            '''execute config [idx]'''
            selected: SourceConfig = self.get_thing_by_index(
                self.arg_as_idx(idx))
            assert selected.id

            print(self.scheduler_client.execute(source_config_id=selected.id))