コード例 #1
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()
コード例 #2
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))