Skip to content

Commit 1597f72

Browse files
committed
add helper methods to set code env settings
1 parent ea837b3 commit 1597f72

1 file changed

Lines changed: 192 additions & 0 deletions

File tree

dataikuapi/dss/admin.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,39 @@ def get_version_for_project(self, project_key):
727727
return self.client._perform_json(
728728
"GET", "/admin/code-envs/%s/%s/%s/version" % (self.env_lang, self.env_name, project_key))
729729

730+
731+
def get_settings(self):
732+
"""
733+
Returns the settings of this code env as a :class:`DSSCodeEnvSettings`, or one of its subclasses.
734+
735+
Know subclasses of :class:`DSSCodeEnvSettings` include :class:`DSSDesignCodeEnvSettings`
736+
and :class:`DSSAutomationCodeEnvSettings`
737+
738+
You must use :meth:`~DSSCodeEnvSettings.save()` on the returned object to make your changes effective
739+
on the code env.
740+
741+
.. code-block:: python
742+
743+
# Example: setting the required packagd
744+
codeenv = client.get_code_env("PYTHON", "code_env_name")
745+
settings = codeenv.get_settings()
746+
settings.set_required_packages("dash==2.0.0", "bokeh<2.0")
747+
settings.save()
748+
# then proceed to update_packages()
749+
750+
:rtype: :class:`DSSCodeEnvSettings`
751+
"""
752+
data = self.client._perform_json(
753+
"GET", "/admin/code-envs/%s/%s" % (self.env_lang, self.env_name))
754+
755+
# you can't just use deploymentMode to check if it's an automation code
756+
# env, because some modes are common to both types of nodes. So we rely
757+
# on a non-null field that only the automation code envs have
758+
if data.get("versions", None) is not None:
759+
return DSSAutomationCodeEnvSettings(self, data)
760+
else:
761+
return DSSDesignCodeEnvSettings(self, data)
762+
730763

731764
########################################################
732765
# Code env actions
@@ -811,6 +844,165 @@ def get_log(self, log_name):
811844
"GET", "/admin/code-envs/%s/%s/logs/%s" % (self.env_lang, self.env_name, log_name))
812845

813846

847+
class DSSCodeEnvSettings(object):
848+
"""
849+
Base settings class for a DSS code env.
850+
Do not instantiate this class directly, use :meth:`DSSCodeEnv.get_settings`
851+
852+
Use :meth:`save` to save your changes
853+
"""
854+
855+
def __init__(self, codeenv, settings):
856+
self.codeenv = codeenv
857+
self.settings = settings
858+
859+
def get_raw(self):
860+
"""Get the raw code env settings as a dict"""
861+
return self.settings
862+
863+
@property
864+
def env_lang(self):
865+
return self.codeenv.env_lang
866+
867+
@property
868+
def env_name(self):
869+
return self.codeenv.env_name
870+
871+
def save(self):
872+
self.codeenv.client._perform_json(
873+
"PUT", "/admin/code-envs/%s/%s" % (self.env_lang, self.env_name), body=self.settings)
874+
875+
class DSSCodeEnvPackageListBearer(object):
876+
def get_required_packages(self):
877+
"""
878+
Return the list of required packages, as a single string
879+
"""
880+
return self.settings.get("specPackageList", "")
881+
def set_required_packages(self, *packages):
882+
"""
883+
Set the list of required packages
884+
"""
885+
self.settings["specPackageList"] = '\n'.join(packages)
886+
887+
def get_required_conda_spec(self):
888+
"""
889+
Return the list of required conda packages, as a single string
890+
"""
891+
return self.settings.get("specCondaEnvironment", "")
892+
def set_required_conda_spec(self, *spec):
893+
"""
894+
Set the list of required conda packages
895+
"""
896+
self.settings["specCondaEnvironment"] = '\n'.join(packages)
897+
898+
class DSSCodeEnvContainerConfsBearer(object):
899+
def built_for_all_container_confs(self):
900+
"""
901+
Returns whether the code env creates an image for each container config
902+
"""
903+
return self.settings.get("allContainerConfs", False)
904+
def get_built_container_confs(self):
905+
"""
906+
Returns the list of container configs for which the code env builds an image (if not all)
907+
"""
908+
return self.settings.get("containerConfs", [])
909+
def set_built_container_confs(self, *configs, **kwargs):
910+
"""
911+
Sets the list of container configs for which the code env builds an image
912+
913+
:param boolean all: if True, an image is built for each config
914+
:param list configs: list of configuration names to build images for
915+
"""
916+
all = kwargs.get("all", False)
917+
self.settings['allContainerConfs'] = all
918+
if not all:
919+
self.settings['containerConfs'] = configs
920+
def built_for_all_spark_kubernetes_confs(self):
921+
"""
922+
Returns whether the code env creates an image for each managed Spark over Kubernetes config
923+
"""
924+
return self.settings.get("allSparkKubernetesConfs", False)
925+
def get_built_spark_kubernetes_confs(self):
926+
"""
927+
Returns the list of managed Spark over Kubernetes configs for which the code env builds an image (if not all)
928+
"""
929+
return self.settings.get("sparkKubernetesConfs", [])
930+
def set_built_spark_kubernetes_confs(self, *configs, **kwargs):
931+
"""
932+
Sets the list of managed Spark over Kubernetes configs for which the code env builds an image
933+
934+
:param boolean all: if True, an image is built for each config
935+
:param list configs: list of configuration names to build images for
936+
"""
937+
all = kwargs.get("all", False)
938+
self.settings['allSparkKubernetesConfs'] = all
939+
if not all:
940+
self.settings['sparkKubernetesConfs'] = configs
941+
942+
943+
class DSSDesignCodeEnvSettings(DSSCodeEnvSettings, DSSCodeEnvPackageListBearer, DSSCodeEnvContainerConfsBearer):
944+
"""
945+
Base settings class for a DSS code env on a design node.
946+
Do not instantiate this class directly, use :meth:`DSSCodeEnv.get_settings`
947+
948+
Use :meth:`save` to save your changes
949+
"""
950+
951+
def __init__(self, codeenv, settings):
952+
super(DSSDesignCodeEnvSettings, self).__init__(codeenv, settings)
953+
954+
955+
class DSSAutomationCodeEnvSettings(DSSCodeEnvSettings, DSSCodeEnvContainerConfsBearer):
956+
"""
957+
Base settings class for a DSS code env on an automation node.
958+
Do not instantiate this class directly, use :meth:`DSSCodeEnv.get_settings`
959+
960+
Use :meth:`save` to save your changes
961+
"""
962+
963+
def __init__(self, codeenv, settings):
964+
super(DSSAutomationCodeEnvSettings, self).__init__(codeenv, settings)
965+
966+
967+
def get_version(self, version_id=None):
968+
"""
969+
Get a specific code env version (for versioned envs) or the single
970+
version
971+
972+
:param string version_id: for versioned code env, identifier of the desired version
973+
974+
:rtype: :class:`DSSAutomationCodeEnvVersionSettings`
975+
"""
976+
deployment_mode = self.settings.get("deploymentMode", None)
977+
if deployment_mode in ['AUTOMATION_SINGLE']:
978+
return DSSAutomationCodeEnvVersionSettings(self.codeenv, self.settings.get('currentVersion', {}))
979+
elif deployment_mode in ['AUTOMATION_VERSIONED']:
980+
versions = self.settings.get("versions", [])
981+
version_ids = [v.get('versionId') for v in versions]
982+
if version_id is None:
983+
raise Exception("A version id is required in a versioned code env. Existing ids: %s" % ', '.join(version_ids))
984+
for version in versions:
985+
if version_id == version.get("versionId"):
986+
return DSSAutomationCodeEnvVersionSettings(self, version)
987+
raise Exception("Version %s not found in : %s" % (version_id, ', '.join(version_ids)))
988+
elif deployment_mode in ['PLUGIN_NON_MANAGED', 'PLUGIN_MANAGED', 'AUTOMATION_NON_MANAGED_PATH', 'EXTERNAL_CONDA_NAMED']:
989+
return DSSAutomationCodeEnvVersionSettings(self.codeenv, self.settings.get('noVersion', {}))
990+
else:
991+
raise Exception("Unexpected deloyment mode %s for an automation node code env. Alter the settings directly with get_raw()", deployment_mode)
992+
993+
class DSSAutomationCodeEnvVersionSettings(DSSCodeEnvPackageListBearer):
994+
"""
995+
Base settings class for a DSS code env version on an automation node.
996+
Do not instantiate this class directly, use :meth:`DSSAutomationCodeEnvSettings.get_version`
997+
998+
Use :meth:`save` on the :class:`DSSAutomationCodeEnvSettings` to save your changes
999+
"""
1000+
1001+
def __init__(self, codeenv_settings, version_settings):
1002+
self.codeenv_settings = codeenv_settings
1003+
self.settings = version_settings
1004+
1005+
8141006
class DSSGlobalApiKey(object):
8151007
"""
8161008
A global API key on the DSS instance

0 commit comments

Comments
 (0)