-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathplugin.py
1089 lines (817 loc) · 35.6 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from ..utils import DataikuException
from .future import DSSFuture
class DSSPluginSettingsBase(object):
"""
Base class for plugin settings.
.. important::
Do not instantiate directly, use either :meth:`DSSPlugin.get_settings` or :meth:`DSSPlugin.get_project_settings`.
"""
def __init__(self, client, plugin_id, settings, project_key=None):
self.client = client
self.plugin_id = plugin_id
self.settings = settings
self.project_key = project_key
def get_raw(self):
"""
Get the raw settings object.
.. note::
This method returns a reference to the settings, not a copy. Changing values in the reference
then calling :meth:`save()` results in these changes being saved.
:return: the settings as a dict. The instance-level settings consist of the plugin code env's name,
the presets and the permissions to use the plugin components. The project-level settings consist of the
presets and the parameter set descriptions.
:rtype: dict
"""
return self.settings
def list_parameter_set_names(self):
"""
List the names of the parameter sets defined in this plugin.
:rtype: list[string]
"""
return [d["id"] for d in self.settings.get("accessibleParameterSetDescs", [])]
def _list_parameter_sets(self):
ret = []
for parameter_set_name in self.list_parameter_set_names():
parameter_set = self.get_parameter_set(parameter_set_name)
if parameter_set is not None:
ret.append(parameter_set)
return ret
def _get_parameter_set(self, parameter_set_name):
desc = None
data = None
for ps in self.settings.get("accessibleParameterSetDescs", []):
if ps["id"] == parameter_set_name:
desc = ps
for ps in self.settings.get("parameterSets", []):
if ps["name"] == parameter_set_name:
data = ps
if desc is None:
return None
if data is None:
# make a fake one, for example for project-level settings
data = {'type':desc["elementType"]}
presets_of_parameter_set = [p for p in self.settings["presets"] if p["type"] == data["type"]]
return self._make_parameter_set(desc, data, presets_of_parameter_set)
def _make_parameter_set(self, desc, data, presets_of_parameter_set):
return DSSPluginParameterSet(self, desc, data, presets_of_parameter_set)
def save(self):
"""
Save the settings to DSS.
"""
self.client._perform_empty("POST", "/plugins/%s/settings" % (self.plugin_id),
params={"projectKey": self.project_key},
body=self.settings)
class DSSPluginSettings(DSSPluginSettingsBase):
"""
The settings of a plugin.
.. important::
Do not instantiate directly, use :meth:`DSSPlugin.get_settings`.
"""
def __init__(self, client, plugin_id, settings):
super().__init__(client, plugin_id, settings)
def set_code_env(self, code_env_name):
"""
Set the name of the code env to use for this plugin.
:param string code_env_name: name of a code env
"""
self.settings["codeEnvName"] = code_env_name
def list_parameter_sets(self):
"""
List the parameter sets defined in this plugin.
:rtype: list[:class:`DSSPluginParameterSet`]
"""
return self._list_parameter_sets()
def get_parameter_set(self, parameter_set_name):
"""
Get a parameter set in this plugin.
:param string parameter_set_name: name of the parameter set
:return: a handle on the parameter set
:rtype: :class:`DSSPluginParameterSet`
"""
return self._get_parameter_set(parameter_set_name)
def _make_parameter_set(self, desc, data, presets_of_parameter_set):
return DSSPluginParameterSet(self, desc, data, presets_of_parameter_set)
class DSSPluginProjectSettings(DSSPluginSettingsBase):
"""
The project-level settings of a plugin.
.. important::
Do not instantiate directly, use :meth:`DSSPlugin.get_project_settings`.
"""
def __init__(self, client, plugin_id, settings, project_key):
super().__init__(client, plugin_id, settings, project_key)
def start_save(self):
"""
Save the settings to DSS.
Returns with a future representing the post actions done asynchronously (e.g. rebuild cde image for visual recipes)
:return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the save post process
:rtype: :class:`~dataikuapi.dss.future.DSSFuture`
"""
resp = self.client._perform_json("POST", "/plugins/%s/settings/future" % (self.plugin_id), body=self.settings)
return DSSFuture.from_resp(self.client, resp)
def list_parameter_sets(self):
"""
List the parameter sets defined in this plugin.
:rtype: list[:class:`DSSPluginProjectParameterSet`]
"""
return self._list_parameter_sets()
def get_parameter_set(self, parameter_set_name):
"""
Get a parameter set in this plugin.
:param string parameter_set_name: name of the parameter set
:return: a handle on the parameter set
:rtype: :class:`DSSPluginProjectParameterSet`
"""
return self._get_parameter_set(parameter_set_name)
def _make_parameter_set(self, desc, data, presets_of_parameter_set):
return DSSPluginProjectParameterSet(self, desc, data, presets_of_parameter_set)
class DSSPluginParameterSetBase(object):
"""
A parameter set in a plugin.
.. important::
Do not instantiate directly, use :meth:`DSSPluginSettings.get_parameter_set()` or :meth:`DSSPluginSettings.list_parameter_sets().
For project-level settings, use :meth:`DSSPluginProjectSettings.get_parameter_set()` or :meth:`DSSPluginProjectSettings.list_parameter_sets()`
The values in this class can be modified directly, and changes will be taken into account
when calling :meth:`DSSPluginSettings.save()` (or :meth:`DSSPluginProjectSettings.save()`
"""
def __init__(self, plugin_settings, desc, settings, presets):
self._plugin_settings = plugin_settings
self._desc = desc
self._settings = settings
self._presets = presets
@property
def desc(self):
"""
Get the raw definition of the parameter set.
:return: a parameter set definition, as a dict. The parameter set's contents is a **desc** sub-dict.
See `the doc <https://doc.dataiku.com/dss/latest/plugins/reference/params.html#preset-parameters>`_
:rtype: dict
"""
return self._desc
@property
def settings(self):
"""
Get the settings of the parameter set.
These settings control the behavior of the parameter set, and comprise notably the permissions,
but not the presets of this parameter set.
:return: the settings of the parameter set, as a dict. The parameter set's settings consist of the permissions
controlling whether the presets of the parameter set can be created inline or at the project level.
:rtype: dict
"""
return self._settings
def list_preset_names(self):
"""
List the names of the presets of this parameter set.
:rtype: list[string]
"""
return [p["name"] for p in self._presets]
def list_presets(self):
"""
List the presets of this parameter set.
:rtype: list[:class:`DSSPluginPreset`]
"""
return [DSSPluginPreset(self._plugin_settings, p, self._desc) for p in self._presets]
def get_preset(self, preset_name):
"""
Get a preset of this parameter set.
:param string preset_name: name of a preset
:return: a handle on the preset definition, or None if the preset doesn't exist
:rtype: :class:`DSSPluginPreset`
"""
for p in self._presets:
if p["name"] == preset_name:
return DSSPluginPreset(self._plugin_settings, p, self._desc)
return None
def delete_preset(self, preset_name):
"""
Remove a preset from this plugin's settings
:param string preset_name: name for the preset to remove
"""
preset = self.get_preset(preset_name)
if preset is not None:
# note: preset out of get_preset is a DSSPluginPreset, so it's
# not the object present in the lists
self._plugin_settings.settings["presets"].remove(preset._settings)
self._presets.remove(preset._settings)
else:
raise Exception("Preset '%s' not found" % preset_name)
def create_preset(self, preset_name, with_defaults=False):
"""
Create a new preset of this parameter set in the plugin settings.
:param string preset_name: name for the preset to create
:param bool with_defaults: if True, fill the new preset with the default value for each parameter
:return: a preset definition, as a :class:`DSSPluginPreset` (see :meth:`get_preset()`)
:rtype: dict
"""
for p in self._presets:
if p["name"] == preset_name:
raise Exception("A preset of the same name already exists")
new_preset = {"name":preset_name, "type":self._settings["type"], "config":{}, "pluginConfig":{}, "defaultPermission":{}, "permissions":[]}
if with_defaults:
for p in self._desc["desc"]["params"]:
v = p.get("defaultValue")
if v is not None:
new_preset["config"][p["name"]] = v
for p in self._desc["desc"]["pluginParams"]:
v = p.get("defaultValue")
if v is not None:
new_preset["pluginConfig"][p["name"]] = v
self._plugin_settings.settings["presets"].append(new_preset)
self._presets.append(new_preset)
return self.get_preset(preset_name)
def save(self):
"""
Save the settings to DSS.
"""
self._plugin_settings.save()
class DSSPluginParameterSet(DSSPluginParameterSetBase):
"""
A parameter set in a plugin.
.. important::
Do not instantiate directly, use :meth:`DSSPluginSettings.get_parameter_set()` or :meth:`DSSPluginSettings.list_parameter_sets()`.
The values in this class can be modified directly, and changes will be taken into account
when calling :meth:`DSSPluginSettings.save()`
"""
def __init__(self, plugin_settings, desc, settings, presets):
self._plugin_settings = plugin_settings
self._desc = desc
self._settings = settings
self._presets = presets
@property
def definable_inline(self):
"""
Whether presets for this parameter set can be defined directly in the form of the datasets, recipes, ...
:rtype: bool
"""
return self._settings["defaultPermission"].get("definableInline", False)
@definable_inline.setter
def definable_inline(self, definable):
self._settings["defaultPermission"]['definableInline'] = definable
@property
def definable_at_project_level(self):
"""
Whether presets for this parameter set can be defined at the project level
:rtype: bool
"""
return self._settings["defaultPermission"].get("definableAtProjectLevel", False)
@definable_at_project_level.setter
def definable_at_project_level(self, definable):
self._settings["defaultPermission"]['definableAtProjectLevel'] = definable
class DSSPluginProjectParameterSet(DSSPluginParameterSetBase):
"""
A parameter set in a plugin.
.. important::
Do not instantiate directly, use :meth:`DSSPluginProjectSettings.get_parameter_set()` or :meth:`DSSPluginProjectSettings.list_parameter_sets()`
The values in this class can be modified directly, and changes will be taken into account
when calling or :meth:`DSSPluginProjectSettings.save()`
"""
def __init__(self, plugin_settings, desc, settings, presets):
self._plugin_settings = plugin_settings
self._desc = desc
self._settings = settings
self._presets = presets
class DSSPluginPreset(dict):
"""
A preset of a parameter set in a plugin.
.. important::
Do not instantiate directly, use :meth:`DSSPluginParameterSet.get_preset()` , :meth:`DSSPluginParameterSet.list_presets()`
or :meth:`DSSPluginParameterSet.create_preset()`. For project-level presets, use :meth:`DSSPluginProjectParameterSet.get_preset()` ,
:meth:`DSSPluginProjectParameterSet.list_presets()` or :meth:`DSSPluginProjectParameterSet.create_preset()`
The values in this class can be modified directly, and changes will be taken into account
when calling :meth:`DSSPluginSettings.save()`.
"""
def __init__(self, plugin_settings, settings, parameter_set_desc):
super(DSSPluginPreset, self).__init__(settings)
self._plugin_settings = plugin_settings
self._settings = settings
self._parameter_set_desc = parameter_set_desc
def __repr__(self):
return "DSSPluginPreset(name={}, plugin={}, parameter_set={})".format(
self._settings["name"],
self._plugin_settings.plugin_id,
self._parameter_set_desc["id"]
)
def get_raw(self):
"""
Get the raw settings of the preset object.
.. note::
This method returns a reference to the preset, not a copy. Changing values in the reference
then calling :meth:`save()` results in these changes being saved.
:return: the preset's complete settings
:rtype: dict
"""
return self._settings
@property
def name(self):
"""
Get the name of the preset.
:return: the name of the preset
:rtype: string
"""
return self._settings["name"]
@property
def config(self):
"""
Get the raw config of the preset object.
.. note::
This method returns a reference to the preset, not a copy. Changing values in the reference
then calling :meth:`save()` results in these changes being saved.
:return: the preset's config as a dict. Each parameter of the parameter set is a field in the dict.
:rtype: dict
"""
return self._settings["config"]
@property
def plugin_config(self):
"""
Get the raw admin-level config of the preset object. Admin-level config parameters are not shown in
the UI to non-admin users.
.. note::
This method returns a reference to the preset, not a copy. Changing values in the reference
then calling :meth:`save()` results in these changes being saved.
:return: the preset's admin config as a dict. Each parameter of the parameter set is a field in the dict.
:rtype: dict
"""
return self._settings["pluginConfig"]
@property
def owner(self):
"""
The DSS user that owns this preset
:rtype: string
"""
return self._settings["owner"]
@owner.setter
def owner(self, login):
self._settings["owner"] = login
@property
def usable_by_all(self):
"""
Whether the preset is usable by any DSS user
:rtype: bool
"""
return self._settings["defaultPermission"].get("use", False)
@usable_by_all.setter
def usable_by_all(self, use):
self._settings["defaultPermission"]['use'] = use
def get_permission_item(self, group):
"""
Get permissions on the preset for a given group
:param string group: the name of the DSS group you want to check permissions for.
:return: the permissions as a dict
:rtype: dict
"""
if group is None:
return self._settings["defaultPermission"]
else:
for p in self._settings["permissions"]:
if p["group"] == group:
return p
return None
def is_usable_by(self, group):
"""
Get whether the preset is usable by DSS users in a group
:param string group: the name of the DSS group you want to check permissions for.
:return: True if the preset can be used by DSS users belonging to *group*. If *group* is None
then returns True if the preset can be used by any DSS user (like :meth:`usable_by_all`)
:rtype: bool
"""
permission_item = self.get_permission_item(group)
if permission_item is None:
return self._settings["defaultPermission"]["use"]
else:
return permission_item["use"]
def set_usable_by(self, group, use):
"""
Set whether the preset is usable by DSS users in a group
:param string group: the name of the DSS group you want to change permissions for.
:param bool use: whether the group should be allowed to use the preset or not
"""
permission_item = self.get_permission_item(group)
if permission_item is None:
# group can't be None here
self._settings["permissions"].append({"group":group, "use":use})
else:
permission_item["use"] = use
def save(self):
"""
Save the settings to DSS.
"""
self._plugin_settings.save()
class DSSPlugin(object):
"""
A plugin on the DSS instance.
.. important::
Do not instantiate directly, use :meth:`dataikuapi.DSSClient.get_plugin()`
"""
def __init__(self, client, plugin_id):
self.client = client
self.plugin_id = plugin_id
########################################################
# Settings
########################################################
def get_settings(self):
"""
Get the plugin-level settings.
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
:return: a handle on the settings
:rtype: :class:`DSSPluginSettings`
"""
settings = self.client._perform_json("GET", "/plugins/%s/settings" % (self.plugin_id))
return DSSPluginSettings(self.client, self.plugin_id, settings)
def get_project_settings(self, project_key):
"""
Get the project-level settings.
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
:return: a handle on the project-level settings
:rtype: :class:`DSSPluginProjectSettings`
"""
settings = self.client._perform_json("GET", "/plugins/%s/settings" % (self.plugin_id),
params={"projectKey": project_key})
return DSSPluginProjectSettings(self.client, self.plugin_id, settings, project_key)
########################################################
# Code env
########################################################
def create_code_env(self, python_interpreter=None, conda=False):
"""
Start the creation of the code env of the plugin.
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
If not passing any value to **python_interpreter**, the default defined by the plugin
will be used.
Usage example:
.. code-block:: python
# create a default code env for the plugin
plugin = client.get_plugin('the-plugin-id')
future = plugin.create_code_env()
creation = future.wait_for_result()
# take the name of the new code env
env_name = creation["envName"]
# set it as the current plugin code env
settings = plugin.get_settings()
settings.set_code_env(env_name)
settings.save()
:param string python_interpreter: which version of python to use. Possible values: PYTHON27, PYTHON34, PYTHON35, PYTHON36, PYTHON37, PYTHON38, PYTHON39, PYTHON310, PYTHON311, PYTHON312
:param boolean conda: if True use conda to create the code env, if False use virtualenv and pip.
:return: a handle on the operation
:rtype: :class:`dataikuapi.dss.future.DSSFuture`
"""
ret = self.client._perform_json("POST", "/plugins/%s/code-env/actions/create" % (self.plugin_id), body={
"deploymentMode" : "PLUGIN_MANAGED",
"conda": conda,
"pythonInterpreter": python_interpreter
})
return DSSFuture.from_resp(self.client, ret)
def update_code_env(self):
"""
Start an update of the code env of the plugin.
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
Usage example:
.. code-block:: python
# update the plugin code env after updating the plugin
plugin = client.get_plugin('the-plugin-id')
future = plugin.update_code_env()
future.wait_for_result()
:return: a handle on the operation
:rtype: :class:`dataikuapi.dss.future.DSSFuture`
"""
ret = self.client._perform_json("POST", "/plugins/%s/code-env/actions/update" % (self.plugin_id))
return DSSFuture.from_resp(self.client, ret)
########################################################
# Plugin update
########################################################
def update_from_zip(self, fp):
"""
Update the plugin from a plugin archive (as a file object).
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
:param file-like fp: A file-like object pointing to a plugin archive zip
"""
files = {'file': fp }
self.client._perform_json("POST", "/plugins/%s/actions/updateFromZip" % (self.plugin_id), files=files)
def start_update_from_zip(self, fp):
"""
Update the plugin from a plugin archive (as a file object).
Returns immediately with a future representing the process done asynchronously
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
:param file-like fp: A file-like object pointing to a plugin archive zip
:return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the update process
:rtype: :class:`~dataikuapi.dss.future.DSSFuture`
"""
files = {'file': fp }
f = self.client._perform_json("POST", "/plugins/%s/actions/future/updateFromZip" % (self.plugin_id), files=files)
return DSSFuture.from_resp(self.client, f)
def update_from_store(self):
"""
Update the plugin from the Dataiku plugin store.
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
Usage example:
.. code-block:: python
# update a plugin that was installed from the store
plugin = client.get_plugin("my-plugin-id")
future = plugin.update_from_store()
future.wait_for_result()
:return: a handle on the operation
:rtype: :class:`dataikuapi.dss.future.DSSFuture`
"""
ret = self.client._perform_json("POST", "/plugins/%s/actions/updateFromStore" % (self.plugin_id))
return DSSFuture.from_resp(self.client, ret)
def update_from_git(self, repository_url, checkout = "master", subpath=None):
"""
Updates the plugin from a Git repository.
.. note::
DSS must be setup to allow access to the repository.
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
Usage example:
.. code-block:: python
# update a plugin that was installed from git
plugin = client.get_plugin("my-plugin-id")
future = plugin.update_from_git("[email protected]:myorg/myrepo")
future.wait_for_result()
:param string repository_url: URL of a Git remote
:param string checkout: branch/tag/SHA1 to commit. For example "master"
:param string subpath: Optional, path within the repository to use as plugin. Should contain a 'plugin.json' file
:return: a handle on the operation
:rtype: :class:`dataikuapi.dss.future.DSSFuture`
"""
ret = self.client._perform_json("POST", "/plugins/%s/actions/updateFromGit" % (self.plugin_id), body={
"gitRepositoryUrl": repository_url,
"gitCheckout" : checkout,
"gitSubpath": subpath
})
return DSSFuture.from_resp(self.client, ret)
########################################################
# Plugin uninstall/delete
########################################################
def list_usages(self, project_key=None):
"""
Get the list of usages of the plugin.
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
:param string project_key: optional key of project where to look for usages. Default is None and looking in all projects.
:rtype: :class:`DSSPluginUsages`
"""
return DSSPluginUsages(
self.client._perform_json("GET", "/plugins/{pluginId}/actions/listUsages".format(pluginId=self.plugin_id),
params={"projectKey": project_key})
)
def delete(self, force=False):
"""
Delete a plugin.
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
:param boolean force: if True, plugin will be deleted even if usages are found or errors occurred during usages
analysis. Default: False.
:rtype: :class:`dataikuapi.dss.future.DSSFuture`
"""
params = {
"force": force
}
ret = self.client._perform_json("POST", "/plugins/{pluginId}/actions/delete".format(pluginId=self.plugin_id),
body=params)
return DSSFuture.from_resp(self.client, ret)
########################################################
# Managing the dev plugin's contents
########################################################
def list_files(self):
"""
Get the hierarchy of files in the plugin.
.. note::
Dev plugins only
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
:return: list of files or directories, each one a dict. Directories have a **children** field for recursion. Each dict has fields **name** and **path** (the path from the root of the plugin files)
:rtype: dict
"""
return self.client._perform_json("GET", "/plugins/%s/contents" % (self.plugin_id))
def get_file(self, path):
"""
Get a file from the plugin folder.
.. note::
Dev plugins only
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
Usage example:
.. code-block:: python
# read the code env desc of a plugin
plugin = client.get_plugin("my-plugin-name")
with plugin.get_file('code-env/python/desc.json') as fp:
desc = json.load(fp)
:param string path: the path of the file, relative to the root of the plugin
:return: the file's content
:rtype: file-like
"""
return self.client._perform_raw("GET", "/plugins/%s/contents/%s" % (self.plugin_id, path)).raw
def put_file(self, path, f):
"""
Update a file in the plugin folder.
.. note::
Dev plugins only
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
:param file-like f: the file contents, as a file-like object
:param string path: the path of the file, relative ot the root of the plugin
"""
file_name = path.split('/')[-1]
data = f.read() # eat it all, because making it work with a path variable and a MultifilePart in swing looks complicated
self.client._perform_empty("POST", "/plugins/%s/contents/%s" % (self.plugin_id, path), raw_body=data)
def rename_file(self, path, new_name):
"""
Rename a file/folder in the plugin.
.. note::
Dev plugins only
:param string path: the path of the file/folder, relative ot the root of the plugin
:param string new_name: the new name of the file/folder
"""
self.client._perform_empty("POST", "/plugins/%s/contents-actions/rename" % self.plugin_id, body={"oldPath": path, "newName": new_name})
def move_file(self, path, new_path):
"""
Move a file/folder in the plugin.
.. note::
Dev plugins only
.. note::
This call requires an API key with either:
* DSS admin permissions
* permission to develop plugins
* tied to a user with admin privileges on the plugin
:param string path: the path of the file/folder, relative ot the root of the plugin
:param string new_path: the new path relative at the root of the plugin
"""
self.client._perform_empty("POST", "/plugins/%s/contents-actions/move" % self.plugin_id, body={"oldPath": path, "newPath": new_path})
class DSSPluginUsage(object):
"""
Information on a usage of an element of a plugin.
.. important::
Do no instantiate directly, use :meth:`dataikuapi.dss.plugin.DSSPlugin.list_usages()`
"""
def __init__(self, data):
self._data = data
@property
def element_kind(self):
"""
Get the type of the plugin component.
:return: a kind of plugin component, like 'python-clusters', 'python-connectors', 'python-fs-providers', 'webapps', ...
:rtype: string
"""
return self._data["elementKind"]
@property
def element_type(self):
"""
Get the identifier of the plugin component.
:rtype: string
"""
return self._data["elementType"]
@property
def object_id(self):
"""
Get the identifier of the object using the plugin component.
:rtype: string
"""
return self._data["objectId"]
@property
def object_type(self):
"""
Get the type of the object using the plugin component.
:return: a type of DSS object, like 'CLUSTER', 'DATASET', 'RECIPE', ...
:rtype: string
"""
return self._data["objectType"]
@property
def project_key(self):
"""
Get the project key of the object using the plugin component.
:rtype: string
"""
return self._data["projectKey"]
class DSSMissingType(object):
"""
Information on a type not found while analyzing usages of a plugin.
Missing types can occur when plugins stop defining a given component, for example during development,
and DSS object still use the now-removed component.
.. important::
Do no instantiate directly, use :meth:`dataikuapi.dss.plugin.DSSPlugin.list_usages()`
"""
def __init__(self, data):
self._data = data
@property
def missing_type(self):
"""
Get the type of the plugin component.
:rtype: string
"""
return self._data["missingType"]
@property
def object_id(self):
"""