Skip to content

Commit f472801

Browse files
author
Thibaud Baas
committed
FM: Update Cloud Tags
1 parent 52f58fb commit f472801

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

dataikuapi/fm/tenant.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,64 @@ def save(self):
6565
)
6666

6767

68+
class FMCloudTags(object):
69+
"""
70+
A Tenant Cloud Tags in the FM instance
71+
"""
72+
73+
def __init__(self, client, tenant_id, cloud_tags):
74+
self.client = client
75+
self.tenant_id = tenant_id
76+
self.cloud_tags = json.loads(cloud_tags["msg"])
77+
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]
117+
118+
def save(self):
119+
"""Saves the tags on FM"""
120+
121+
self.client._perform_empty(
122+
"PUT", "/tenants/%s/cloud-tags" % (self.tenant_id), body=self.cloud_tags
123+
)
124+
125+
68126
class FMCloudAuthentication(dict):
69127
def __init__(self, data):
70128
"""

dataikuapi/fmclient.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from enum import Enum
88
from .utils import DataikuException
99

10-
from .fm.tenant import FMCloudCredentials
10+
from .fm.tenant import FMCloudCredentials, FMCloudTags
1111
from .fm.virtualnetworks import (
1212
FMVirtualNetwork,
1313
FMAWSVirtualNetworkCreator,
@@ -71,6 +71,18 @@ def get_cloud_credentials(self):
7171
creds = self._perform_tenant_json("GET", "/cloud-credentials")
7272
return FMCloudCredentials(self, creds)
7373

74+
def get_cloud_tags(self, tenant_id):
75+
"""
76+
Get Tenant's Cloud Tags
77+
78+
:param string tenant_id
79+
80+
:return: tenant's cloud tags
81+
:rtype: :class:`dataikuapi.fm.tenant.FMCloudTags`
82+
"""
83+
tags = self._perform_json("GET", "/tenants/%s/cloud-tags" % tenant_id)
84+
return FMCloudTags(self, tenant_id, tags)
85+
7486
########################################################
7587
# VirtualNetwork
7688
########################################################

0 commit comments

Comments
 (0)