Exemplo n.º 1
0
def _read_from_polyaxon_hub(hub: str):
    from polyaxon.client import PolyaxonClient
    from polyaxon.constants.globals import DEFAULT_HUB, NO_AUTH
    from polyaxon.env_vars.getters import get_component_info
    from polyaxon.schemas.cli.client_config import ClientConfig

    owner, component, version = get_component_info(hub)

    try:
        if owner == DEFAULT_HUB:
            config = ClientConfig()
            client = PolyaxonClient(
                config=config,
                token=NO_AUTH,
            )
        else:
            client = PolyaxonClient()
        response = client.component_hub_v1.get_component_version(
            owner, component, version
        )
        return _read_from_stream(response.content)
    except (ApiException, HTTPError) as e:
        raise PolyaxonClientException(
            "Component `{}` could not be fetched, "
            "an error was encountered".format(hub, e)
        )
Exemplo n.º 2
0
def _read_from_polyaxon_hub(hub: str):
    from polyaxon.client import PolyaxonClient, ProjectClient
    from polyaxon.env_vars.getters.versioned_entity import get_component_info
    from polyaxon.schemas.cli.client_config import ClientConfig

    from polyaxon_schemas.constants import DEFAULT_HUB, NO_AUTH
    from polyaxon_schemas.lifecycle import V1ProjectVersionKind

    owner, component, version = get_component_info(hub)

    try:
        if owner == DEFAULT_HUB:
            config = ClientConfig()
            client = PolyaxonClient(
                config=config,
                token=NO_AUTH,
            )
        else:
            client = PolyaxonClient()
        client = ProjectClient(owner=owner, project=component, client=client)
        response = client.get_version(
            kind=V1ProjectVersionKind.COMPONENT, version=version
        )
        return _read_from_stream(response.content)
    except (ApiException, HTTPError) as e:
        raise PolyaxonClientException(
            "Component `{}` could not be fetched, "
            "an error was encountered".format(hub, e)
        )
Exemplo n.º 3
0
def get_compatibility(key: str, service: str, version: str, is_cli=True):
    if not key:
        key = uuid.uuid4().hex
    try:
        version = version.lstrip("v").replace(".", "-")[:5]
    except Exception as e:
        CliConfigManager.reset(last_check=now())
        if is_cli:
            handle_cli_error(
                e,
                message="Could parse the version {}.".format(version),
            )
    polyaxon_client = PolyaxonClient(config=ClientConfig(), token=NO_AUTH)
    try:
        return polyaxon_client.versions_v1.get_compatibility(uuid=key,
                                                             service=service,
                                                             version=version)
    except ApiException as e:
        if e.status == 403:
            session_expired()
        CliConfigManager.reset(last_check=now())
        if is_cli:
            handle_cli_error(
                e,
                message="Could not reach the compatibility API.",
            )
    except HTTPError:
        CliConfigManager.reset(last_check=now())
        if is_cli:
            Printer.print_error(
                "Could not connect to remote server to fetch compatibility versions.",
            )
Exemplo n.º 4
0
    def test_get_headers(self):
        assert self.transport._get_headers() == {}
        assert self.transport._get_headers({"foo": "bar"}) == {"foo": "bar"}

        self.transport.config = ClientConfig(token="token", host="host")

        assert self.transport._get_headers() == {
            "Authorization": "{} {}".format(AuthenticationTypes.TOKEN, "token")
        }
        assert self.transport._get_headers({"foo": "bar"}) == {
            "foo": "bar",
            "Authorization": "{} {}".format(AuthenticationTypes.TOKEN,
                                            "token"),
        }

        self.transport.config.authentication_type = AuthenticationTypes.INTERNAL_TOKEN
        assert self.transport._get_headers() == {
            "Authorization":
            "{} {}".format(AuthenticationTypes.INTERNAL_TOKEN, "token")
        }
        assert self.transport._get_headers({"foo": "bar"}) == {
            "foo":
            "bar",
            "Authorization":
            "{} {}".format(AuthenticationTypes.INTERNAL_TOKEN, "token"),
        }
Exemplo n.º 5
0
def patch_settings(
    set_auth: bool = True,
    set_client: bool = True,
    set_cli: bool = True,
    set_agent: bool = True,
    set_proxies: bool = True,
):
    settings.AUTH_CONFIG = None
    if set_auth:
        settings.AUTH_CONFIG = AccessTokenConfig()

    settings.CLIENT_CONFIG = None
    if set_client:
        settings.CLIENT_CONFIG = ClientConfig(host="1.2.3.4")

    settings.CLI_CONFIG = None
    if set_cli:
        settings.CLI_CONFIG = CliConfig()

    settings.AGENT_CONFIG = None
    if set_agent:
        settings.AGENT_CONFIG = AgentConfig()

    settings.PROXIES_CONFIG = None
    if set_proxies:
        settings.PROXIES_CONFIG = ProxiesConfig()

    settings.CLIENT_CONFIG.tracking_timeout = 0
Exemplo n.º 6
0
 def test_client_config(self):
     config_dict = {
         POLYAXON_KEYS_DEBUG: True,
         POLYAXON_KEYS_HOST: "http://localhost:8000",
         POLYAXON_KEYS_VERIFY_SSL: True,
     }
     config = ClientConfig.from_dict(config_dict)
     assert config.debug is True
     assert config.host == "http://localhost:8000"
     assert config.base_url == "http://localhost:8000/api/v1"
     assert config.verify_ssl is True
Exemplo n.º 7
0
    def get_config_from_env(cls, **kwargs) -> ClientConfig:
        tmp_path = os.path.join(CONTEXT_TMP_POLYAXON_PATH, cls.CONFIG_FILE_NAME)
        user_path = os.path.join(CONTEXT_USER_POLYAXON_PATH, cls.CONFIG_FILE_NAME)

        config = ConfigManager.read_configs(
            [
                ConfigSpec(tmp_path, config_type=".json", check_if_exists=False),
                ConfigSpec(user_path, config_type=".json", check_if_exists=False),
                os.environ,
            ]
        )
        return ClientConfig.from_dict(config.data)
Exemplo n.º 8
0
def get_compatibility(
    key: str,
    service: str,
    version: str,
    is_cli: bool = True,
    set_config: bool = True,
):
    if not key:
        installation = CliConfigManager.get_value("installation") or {}
        key = installation.get("key") or uuid.uuid4().hex
    try:
        version = clean_version_for_compatibility(version)
    except Exception as e:
        if set_config:
            CliConfigManager.reset(last_check=now())
        if is_cli:
            handle_cli_error(
                e,
                message="Could parse the version {}.".format(version),
            )
    polyaxon_client = PolyaxonClient(config=ClientConfig(), token=NO_AUTH)
    try:
        return polyaxon_client.versions_v1.get_compatibility(
            uuid=key,
            service=service,
            version=version,
            _request_timeout=2,
        )
    except ApiException as e:
        if e.status == 403 and is_cli:
            session_expired()
        if set_config:
            CliConfigManager.reset(last_check=now())
        if is_cli:
            handle_cli_error(
                e,
                message="Could not reach the compatibility API.",
            )
    except HTTPError:
        if set_config:
            CliConfigManager.reset(last_check=now())
        if is_cli:
            Printer.print_error(
                "Could not connect to remote server to fetch compatibility versions.",
            )
    except Exception as e:
        if set_config:
            CliConfigManager.reset(last_check=now())
        if is_cli:
            Printer.print_error(
                "Unexpected error %s, "
                "could not connect to remote server to fetch compatibility versions."
                % e, )
Exemplo n.º 9
0
    def setUp(self):
        super().setUp()
        settings.AUTH_CONFIG = None
        if self.SET_AUTH_SETTINGS:
            settings.AUTH_CONFIG = AccessTokenConfig()

        settings.CLIENT_CONFIG = None
        if self.SET_CLIENT_SETTINGS:
            settings.CLIENT_CONFIG = ClientConfig()

        settings.AGENT_CONFIG = None
        if self.SET_AGENT_SETTINGS:
            settings.AGENT_CONFIG = AgentConfig()

        settings.PROXIES_CONFIG = None
        if self.SET_PROXIES_SETTINGS:
            settings.PROXIES_CONFIG = ProxiesConfig()

        fpath = tempfile.mkdtemp()
        settings.CLIENT_CONFIG.agent_path = fpath
Exemplo n.º 10
0
 def test_is_managed(self):
     config = ClientConfig(host=None, is_managed=True)
     assert config.is_managed is True
     assert config.version == "v1"
     assert config.host == "https://cloud.polyaxon.com"
Exemplo n.º 11
0
 def setUp(self):
     super().setUp()
     self.host = "http://localhost:8000"
     self.config = ClientConfig(host=self.host, version="v1", token="token")
Exemplo n.º 12
0
def get_current_or_public_client():
    if settings.CLI_CONFIG.is_ce:
        return PolyaxonClient(config=ClientConfig(), token=NO_AUTH)

    return PolyaxonClient()
Exemplo n.º 13
0
 def test_from_config(self):
     settings.CLIENT_CONFIG.host = "localhost"
     client = PolyaxonClient(config=ClientConfig())
     assert client.config.is_managed is False
     assert client.config.host == "https://cloud.polyaxon.com"
     assert client.config.token is None