Skip to content

Commit e3947dd

Browse files
Thibaud Baaslpenet
authored andcommitted
FM: Simplify CloudTags
1 parent a766032 commit e3947dd

File tree

2 files changed

+23
-64
lines changed

2 files changed

+23
-64
lines changed

dataikuapi/fm/tenant.py

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,27 @@ def __init__(self, client, cloud_credentials):
1212

1313
def set_cmk_key(self, cmk_key_id):
1414
self.cloud_credentials["awsCMKId"] = cmk_key_id
15-
self.save()
15+
return self
1616

17-
def set_static_license(self, license_file=None, license_string=None):
17+
def set_static_license(self, license_file_path=None, license_string=None):
1818
"""
1919
Set a default static license for the DSS instances
2020
21-
Requires either a license_file or a license_string
22-
23-
:param str license_file: Optional, load the license from a json file
21+
:param str license_file_path: Optional, load the license from a json file
2422
:param str license_string: Optional, load the license from a json string
2523
"""
26-
if license_file is not None:
27-
with open(license_file) as json_file:
24+
if license_file_path is not None:
25+
with open(license_file_path) as json_file:
2826
license = json.load(json_file)
2927
elif license_string is not None:
3028
license = json.loads(license_string)
3129
else:
3230
raise ValueError(
33-
"a valid license_file or license_string needs to be provided"
31+
"a valid license_file_path or license_string needs to be provided"
3432
)
3533
self.cloud_credentials["licenseMode"] = "STATIC"
3634
self.cloud_credentials["license"] = json.dumps(license, indent=2)
37-
self.save()
35+
return self
3836

3937
def set_automatically_updated_license(self, license_token):
4038
"""
@@ -46,7 +44,7 @@ def set_automatically_updated_license(self, license_token):
4644
raise ValueError("a valid license_token needs to be provided")
4745
self.cloud_credentials["licenseMode"] = "AUTO_UPDATE"
4846
self.cloud_credentials["licenseToken"] = license_token
49-
self.save()
47+
return self
5048

5149
def set_authentication(self, authentication):
5250
"""
@@ -55,7 +53,7 @@ def set_authentication(self, authentication):
5553
:param object: a :class:`dataikuapi.fm.tenant.FMCloudAuthentication`
5654
"""
5755
self.cloud_credentials.update(authentication)
58-
self.save()
56+
return self
5957

6058
def save(self):
6159
"""Saves back the settings to the project"""
@@ -70,57 +68,18 @@ class FMCloudTags(object):
7068
A Tenant Cloud Tags in the FM instance
7169
"""
7270

73-
def __init__(self, client, tenant_id, cloud_tags):
71+
def __init__(self, client, cloud_tags):
7472
self.client = client
75-
self.tenant_id = tenant_id
7673
self.cloud_tags = json.loads(cloud_tags["msg"])
7774

78-
def add_tag(self, key, value):
79-
"""
80-
Add a tag to the tenant
81-
82-
83-
:param str key: Tag key
84-
:param str value: Tag value
85-
"""
86-
if key in self.cloud_tags:
87-
raise Exception("Key already exists")
88-
self.cloud_tags[key] = value
89-
90-
def update_tag(self, key, new_key=None, new_value=None):
91-
"""
92-
Update a tag key or value
93-
94-
95-
:param str key: Key of the tag to update
96-
:param str new_key: Optional, new key for the tag
97-
:param str new_value: Optional, new value for the tag
98-
"""
99-
if key not in self.cloud_tags:
100-
raise Exception("Key does not exists")
101-
if new_value:
102-
self.cloud_tags[key] = new_value
103-
if new_key:
104-
self.cloud_tags[new_key] = self.cloud_tags[key]
105-
del self.cloud_tags[key]
106-
107-
def delete_tag(self, key):
108-
"""
109-
Delete a tag
110-
111-
112-
:param str key: Key of the tag to delete
113-
"""
114-
if key not in self.cloud_tags:
115-
raise Exception("Key does not exists")
116-
del self.cloud_tags[key]
75+
@property
76+
def tags(self):
77+
return self.cloud_tags
11778

11879
def save(self):
11980
"""Saves the tags on FM"""
12081

121-
self.client._perform_empty(
122-
"PUT", "/tenants/%s/cloud-tags" % (self.tenant_id), body=self.cloud_tags
123-
)
82+
self.client._perform_tenant_empty("PUT", "/cloud-tags", body=self.cloud_tags)
12483

12584

12685
class FMCloudAuthentication(dict):

dataikuapi/fmclient.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
FMAWSVirtualNetworkCreator,
1414
FMAzureVirtualNetworkCreator,
1515
FMAWSVirtualNetwork,
16-
FMAzureVirtualNetwork
16+
FMAzureVirtualNetwork,
1717
)
1818
from .fm.instances import (
1919
FMInstance,
2020
FMInstanceEncryptionMode,
2121
FMAWSInstanceCreator,
2222
FMAzureInstanceCreator,
2323
FMAWSInstance,
24-
FMAzureInstance
24+
FMAzureInstance,
2525
)
2626
from .fm.instancesettingstemplates import (
2727
FMInstanceSettingsTemplate,
@@ -75,7 +75,7 @@ def get_cloud_credentials(self):
7575
creds = self._perform_tenant_json("GET", "/cloud-credentials")
7676
return FMCloudCredentials(self, creds)
7777

78-
def get_cloud_tags(self, tenant_id):
78+
def get_cloud_tags(self):
7979
"""
8080
Get Tenant's Cloud Tags
8181
@@ -84,17 +84,17 @@ def get_cloud_tags(self, tenant_id):
8484
:return: tenant's cloud tags
8585
:rtype: :class:`dataikuapi.fm.tenant.FMCloudTags`
8686
"""
87-
tags = self._perform_json("GET", "/tenants/%s/cloud-tags" % tenant_id)
88-
return FMCloudTags(self, tenant_id, tags)
87+
tags = self._perform_tenant_json("GET", "/cloud-tags")
88+
return FMCloudTags(self, tags)
8989

9090
########################################################
9191
# VirtualNetwork
9292
########################################################
9393

9494
def _make_virtual_network(self, vn):
95-
if self.cloud == 'AWS':
95+
if self.cloud == "AWS":
9696
return FMAWSVirtualNetwork(self, vn)
97-
elif self.cloud == 'Azure':
97+
elif self.cloud == "Azure":
9898
return FMAzureVirtualNetwork(self, vn)
9999
else:
100100
raise Exception("Unknown cloud type %s" % self.cloud)
@@ -156,9 +156,9 @@ def get_instance_template(self, template_id):
156156
########################################################
157157

158158
def _make_instance(self, i):
159-
if self.cloud == 'AWS':
159+
if self.cloud == "AWS":
160160
return FMAWSInstance(self, i)
161-
elif self.cloud == 'Azure':
161+
elif self.cloud == "Azure":
162162
return FMAzureInstance(self, i)
163163
else:
164164
raise Exception("Unknown cloud type %s" % self.cloud)

0 commit comments

Comments
 (0)