|
4 | 4 | # Copyright (c) 2023 Oracle and/or its affiliates.
|
5 | 5 | # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
|
6 | 6 |
|
| 7 | +import logging |
| 8 | +import os |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
7 | 11 | from ads.common.oci_mixin import OCIModelMixin
|
8 | 12 | import oci.feature_store
|
9 |
| -import os |
| 13 | +import yaml |
| 14 | + |
| 15 | + |
| 16 | +try: |
| 17 | + from yaml import CDumper as dumper |
| 18 | + from yaml import CLoader as loader |
| 19 | +except: |
| 20 | + from yaml import Dumper as dumper |
| 21 | + from yaml import Loader as loader |
| 22 | + |
| 23 | +try: |
| 24 | + from odsc_cli.utils import user_fs_config_loc, FsTemplate |
| 25 | +except ImportError: |
| 26 | + pass |
10 | 27 |
|
11 | 28 |
|
12 | 29 | class OCIFeatureStoreMixin(OCIModelMixin):
|
| 30 | + __mod_time = 0 |
| 31 | + __template: "FsTemplate" = None |
| 32 | + FS_SERVICE_ENDPOINT = "fs_service_endpoint" |
| 33 | + SERVICE_ENDPOINT = "service_endpoint" |
| 34 | + |
13 | 35 | @classmethod
|
14 | 36 | def init_client(
|
15 | 37 | cls, **kwargs
|
16 | 38 | ) -> oci.feature_store.feature_store_client.FeatureStoreClient:
|
17 |
| - # TODO: Getting the endpoint from authorizer |
18 |
| - fs_service_endpoint = os.environ.get("OCI_FS_SERVICE_ENDPOINT") |
| 39 | + default_kwargs: dict = cls._get_auth().get("client_kwargs", {}) |
| 40 | + |
| 41 | + fs_service_endpoint = ( |
| 42 | + kwargs.get(cls.FS_SERVICE_ENDPOINT, None) |
| 43 | + or kwargs.get(cls.SERVICE_ENDPOINT, None) |
| 44 | + or default_kwargs.get(cls.FS_SERVICE_ENDPOINT, None) |
| 45 | + ) |
| 46 | + |
| 47 | + if not fs_service_endpoint: |
| 48 | + try: |
| 49 | + mod_time = os.stat(user_fs_config_loc()).st_mtime |
| 50 | + if mod_time > cls.__mod_time: |
| 51 | + with open(user_fs_config_loc()) as ccf: |
| 52 | + cls.__template = FsTemplate(yaml.load(ccf, Loader=loader)) |
| 53 | + cls.__mod_time = mod_time |
| 54 | + except NameError: |
| 55 | + logger.info( |
| 56 | + "%s", |
| 57 | + "Feature store configuration helpers are missing. " |
| 58 | + "Support for reading service endpoint from config file is disabled", |
| 59 | + ) |
| 60 | + except FileNotFoundError: |
| 61 | + logger.info( |
| 62 | + "%s", |
| 63 | + "ODSC cli config for feature store was not found", |
| 64 | + ) |
| 65 | + pass |
| 66 | + if cls.__template: |
| 67 | + fs_service_endpoint = cls.__template.service_endpoint |
| 68 | + |
19 | 69 | if fs_service_endpoint:
|
20 |
| - kwargs = {"service_endpoint": fs_service_endpoint} |
| 70 | + kwargs[cls.SERVICE_ENDPOINT] = fs_service_endpoint |
21 | 71 |
|
22 | 72 | client = cls._init_client(
|
23 | 73 | client=oci.feature_store.feature_store_client.FeatureStoreClient, **kwargs
|
|
0 commit comments