-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcloudaccounts.py
340 lines (277 loc) · 12.5 KB
/
cloudaccounts.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
from .future import FMFuture
class FMCloudAccountCreator(object):
def __init__(self, client, label):
"""
A builder class to create cloud accounts
:param str label: The label of the cloud account
"""
self.client = client
self.data = {"label": label}
def with_description(self, description):
"""
Set the description of this cloud account
:param str description: the description of the cloud account
:rtype: :class:`dataikuapi.fm.cloudaccounts.FMCloudAccountCreator`
"""
self.data["description"] = description
return self
class FMAWSCloudAccountCreator(FMCloudAccountCreator):
def same_as_fm(self):
"""
Use the same authentication as Fleet Manager
"""
self.data["awsAuthenticationMode"] = "DEFAULT_INSTANCE_CREDENTIALS"
return self
def with_iam_role(self, role_arn):
"""
Use an IAM Role
:param str role_arn: ARN of the IAM Role
"""
self.data["awsAuthenticationMode"] = "IAM_ROLE"
self.data["awsIAMRoleARN"] = role_arn
return self
def with_keypair(self, access_key_id, secret_access_key):
"""
Use an AWS Access Key
:param str access_key_id: AWS Access Key ID
:param str secret_access_key: AWS Secret Access Key
"""
self.data["awsAuthenticationMode"] = "KEYPAIR"
self.data["awsAccessKeyId"] = access_key_id
self.data["awsSecretAccessKey"] = secret_access_key
return self
def create(self):
"""
Create a new cloud account
:return: a newly created cloud account
:rtype: :class:`dataikuapi.fm.cloudaccounts.FMAWSCloudAccount`
"""
account = self.client._perform_tenant_json(
"POST", "/cloud-accounts", body=self.data
)
return FMAWSCloudAccount(self.client, account)
class FMAzureCloudAccountCreator(FMCloudAccountCreator):
def same_as_fm(self, subscription, tenant_id, image_resource_group):
"""
Use the same authentication as Fleet Manager
:param str subscription: Azure Subscription
:param str tenant_id: Azure Tenant Id
:param str image_resource_group: Azure image cached resource group
"""
self.data["azureAuthenticationMode"] = "DEFAULT_INSTANCE_CREDENTIALS"
self.data["azureSubscription"] = subscription
self.data["azureTenantId"] = tenant_id
self.data["azureImageResourceGroup"] = image_resource_group
return self
def with_secret(self, subscription, tenant_id, environment, image_resource_group, client_id, secret):
"""
Use a Secret based authentication
:param str subscription: Azure Subscription
:param str tenant_id: Azure Tenant Id
:param str environment: Azure Environment
:param str client_id: Azure Client Id
:param str image_resource_group: Azure image cached resource group
:param str secret: Azure Secret
"""
self.data["azureAuthenticationMode"] = "OAUTH2_AUTHENTICATION_METHOD_CLIENT_SECRET"
self.data["azureSubscription"] = subscription
self.data["azureTenantId"] = tenant_id
self.data["azureEnvironment"] = environment
self.data["azureClientId"] = client_id
self.data["azureSecret"] = secret
self.data["azureImageResourceGroup"] = image_resource_group
return self
def with_certificate(self, subscription, tenant_id, environment, client_id, image_resource_group, certificate_path, certificate_password):
"""
Use a Secret based authentication
:param str subscription: Azure Subscription
:param str tenant_id: Azure Tenant Id
:param str environment: Azure Environment
:param str client_id: Azure Client Id
:param str image_resource_group: Azure image cached resource group
:param str certificate_path: Azure certificate path
:param str certificate_password: Azure certificate password
"""
self.data["azureAuthenticationMode"] = "OAUTH2_AUTHENTICATION_METHOD_CERTIFICATE"
self.data["azureSubscription"] = subscription
self.data["azureTenantId"] = tenant_id
self.data["azureEnvironment"] = environment
self.data["azureClientId"] = client_id
self.data["azureImageResourceGroup"] = image_resource_group
self.data["azureCertificatePath"] = certificate_path
self.data["azureCertificatePassword"] = certificate_password
return self
def with_managed_identity(self, tenant_id, environment, image_resource_group, managed_identity):
"""
Use a Managed Identity based authentication
:param str tenant_id: Azure Tenant identifier
:param str environment: Azure Environment
:param str image_resource_group: Azure image cached resource group
:param str managed_identity: Azure Managed Identity
"""
self.data["azureAuthenticationMode"] = "MANAGED_IDENTITY"
self.data["azureTenantId"] = tenant_id
self.data["azureEnvironment"] = environment
self.data["azureManagedIdentityId"] = managed_identity
self.data["azureImageResourceGroup"] = image_resource_group
return self
def create(self):
"""
Create a new cloud account
:return: a newly created cloud account
:rtype: :class:`dataikuapi.fm.cloudaccounts.FMAzureCloudAccount`
"""
account = self.client._perform_tenant_json(
"POST", "/cloud-accounts", body=self.data
)
return FMAzureCloudAccount(self.client, account)
class FMGCPCloudAccountCreator(FMCloudAccountCreator):
def same_as_fm(self):
"""
Use the same authentication as Fleet Manager
"""
self.data["gcpAuthenticationMode"] = "DEFAULT_INSTANCE_CREDENTIALS"
return self
def with_service_account_key(self, project_id, service_account_key):
"""
Use a Service Account JSON key based authentication
:param str project_id: GCP project
:param str service_account_key: Optional, service account key (JSON)
"""
self.data["gcpAuthenticationMode"] = "JSON_KEY"
self.data["gcpProjectId"] = project_id
self.data["gcpServiceAccountKey"] = service_account_key
return self
def create(self):
"""
Create a new cloud account
:return: a newly created cloud account
:rtype: :class:`dataikuapi.fm.cloudaccounts.FMGCPCloudAccount`
"""
account = self.client._perform_tenant_json(
"POST", "/cloud-accounts", body=self.data
)
return FMGCPCloudAccount(self.client, account)
class FMCloudAccount(object):
def __init__(self, client, account_data):
self.client = client
self.account_data = account_data
self.id = self.account_data["id"]
def delete(self):
"""
Delete this cloud account.
:return: the `Future` object representing the deletion process
:rtype: :class:`dataikuapi.fm.future.FMFuture`
"""
if (self.account_data["nbNetworks"] > 0):
raise Exception("This account is in use by some networks, you cannot delete it")
future = self.client._perform_tenant_json(
"DELETE", "/cloud-accounts/%s" % self.id
)
return FMFuture.from_resp(self.client, future)
def save(self):
"""
Save this cloud account.
"""
self.client._perform_tenant_empty(
"PUT", "/cloud-accounts/%s" % self.id, body=self.account_data
)
class FMAWSCloudAccount(FMCloudAccount):
def set_same_as_fm(self):
"""
Use the same authentication as Fleet Manager
"""
self.account_data["awsAuthenticationMode"] = "DEFAULT_INSTANCE_CREDENTIALS"
def set_iam_role(self, role_arn):
"""
Use an IAM Role
:param str role_arn: ARN of the IAM Role
"""
self.account_data["awsAuthenticationMode"] = "IAM_ROLE"
self.account_data["awsIAMRoleARN"] = role_arn
def set_keypair(self, access_key_id, secret_access_key):
"""
Use an AWS Access Key
:param str access_key_id: AWS Access Key ID
:param str secret_access_key: AWS Secret Access Key
"""
self.account_data["awsAuthenticationMode"] = "KEYPAIR"
self.account_data["awsAccessKeyId"] = access_key_id
self.account_data["awsSecretAccessKey"] = secret_access_key
class FMAzureCloudAccount(FMCloudAccount):
def set_same_as_fm(self, tenant_id, image_resource_group):
"""
Use the same authentication as Fleet Manager
:param str tenant_id: Azure Tenant Id
:param str image_resource_group: Azure image cached resource group
"""
self.account_data["azureAuthenticationMode"] = "DEFAULT_INSTANCE_CREDENTIALS"
self.account_data["azureTenantId"] = tenant_id
self.account_data["azureImageResourceGroup"] = image_resource_group
def set_secret(self, subscription, tenant_id, environment, client_id, image_resource_group, secret):
"""
Use a Secret based authentication
:param str subscription: Azure Subscription
:param str tenant_id: Azure Tenant Id
:param str environment: Azure Environment
:param str client_id: Azure Client Id
:param str image_resource_group: Azure image cached resource group
:param str secret: Azure Secret
"""
self.account_data["azureAuthenticationMode"] = "OAUTH2_AUTHENTICATION_METHOD_CLIENT_SECRET"
self.account_data["azureSubscription"] = subscription
self.account_data["azureTenantId"] = tenant_id
self.account_data["azureEnvironment"] = environment
self.account_data["azureClientId"] = client_id
self.account_data["azureSecret"] = secret
self.account_data["azureImageResourceGroup"] = image_resource_group
return self
def set_certificate(self, subscription, tenant_id, environment, client_id, image_resource_group, certificate_path, certificate_password):
"""
Use a Secret based authentication
:param str subscription: Azure Subscription
:param str tenant_id: Azure Tenant Id
:param str environment: Azure Environment
:param str client_id: Azure Client Id
:param str image_resource_group: Azure image cached resource group
:param str certificate_path: Azure certificate path
:param str certificate_password: Azure certificate password
"""
self.account_data["azureAuthenticationMode"] = "OAUTH2_AUTHENTICATION_METHOD_CLIENT_SECRET"
self.account_data["azureSubscription"] = subscription
self.account_data["azureTenantId"] = tenant_id
self.account_data["azureEnvironment"] = environment
self.account_data["azureClientId"] = client_id
self.account_data["azureCertificatePath"] = certificate_path
self.account_data["azureCertificatePassword"] = certificate_password
self.account_data["azureImageResourceGroup"] = image_resource_group
return self
def set_managed_identity(self, tenant_id, environment, managed_identity, image_resource_group):
"""
Use a Managed Identity based authentication
:param str tenant_id: Azure Tenant identifier
:param str environment: Azure Environment
:param str image_resource_group: Azure image cached resource group
:param str managed_identity: Azure Managed Identity
"""
self.account_data["azureAuthenticationMode"] = "MANAGED_IDENTITY"
self.account_data["azureTenantId"] = tenant_id
self.account_data["azureEnvironment"] = environment
self.account_data["azureManagedIdentityId"] = managed_identity
self.account_data["azureImageResourceGroup"] = image_resource_group
return self
class FMGCPCloudAccount(FMCloudAccount):
def set_same_as_fm(self):
"""
Use the same authentication as Fleet Manager
"""
self.account_data["gcpAuthenticationMode"] = "DEFAULT_INSTANCE_CREDENTIALS"
def set_service_account_key(self, project_id, service_account_key):
"""
Use a Service Account JSON key based authentication
:param str project_id: GCP project
:param str service_account_key: Optional, service account key (JSON)
"""
self.account_data["gcpAuthenticationMode"] = "JSON_KEY"
self.account_data["gcpProjectId"] = project_id
self.account_data["gcpServiceAccountKey"] = service_account_key