Skip to content

Commit 1de0927

Browse files
author
Thibaud Baas
committed
FM InstanceSettingsTemplateCreator
1 parent 00f698c commit 1de0927

File tree

2 files changed

+156
-56
lines changed

2 files changed

+156
-56
lines changed

dataikuapi/fm/instancesettingstemplates.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,151 @@
22
import json
33
from dataikuapi.fm.future import FMFuture
44

5+
class FMInstanceSettingsTemplateCreator(object):
6+
def __init__(self, client, label):
7+
"""
8+
Create an Instance Template
9+
10+
:param str azureSshKey: Optional, Azure Only, the ssh public key to add to the instance. Needed to get SSH access to the DSS instance, using the centos user.
11+
:param str startupManagedIdentity: Optional, Azure Only, the managed identity assigned to the DSS instance at startup time
12+
:param str runtimeManagedIdentity: Optional, Azure Only, the managed identity assigned to the DSS instance at runtime
13+
14+
:return: requested instance settings template
15+
:rtype: :class:`dataikuapi.fm.instancesettingstemplates.FMInstanceSettingsTemplate`
16+
"""
17+
18+
self.data = {}
19+
self.data["label"] = label
20+
self.client = client
21+
22+
def create(self):
23+
template = self.client._perform_tenant_json("POST", "/instance-settings-templates", body=self.data)
24+
return FMInstanceSettingsTemplate(self, template)
25+
26+
def with_setup_actions(self, setup_actions):
27+
"""
28+
Add setup actions
29+
30+
:param list setup_actions: List of :class:`dataikuapi.fm.instancesettingstemplates.FMSetupAction` to be played on an instance
31+
:rtype: :class:`dataikuapi.fm.instancesettingstemplates.FMInstanceSettingsTemplateCreator`
32+
"""
33+
self.data["setupActions"] = setup_actions
34+
return self
35+
36+
def with_license(self, license):
37+
self.data["license"] = license
38+
return self
39+
40+
41+
class FMAWSInstanceSettingsTemplateCreator(FMInstanceSettingsTemplateCreator):
42+
def with_aws_keypair(self, aws_keypair_name):
43+
"""
44+
Add an AWS Keypair to the DSS instance.
45+
Needed to get SSH access to the DSS instance, using the centos user.
46+
47+
:param str aws_keypair_name: Name of an AWS key pair to add to the instance.
48+
"""
49+
self.data["awsKeyPairName"] = aws_keypair_name
50+
return self
51+
52+
def with_startup_instance_profile(self, startup_instance_profile_arn):
53+
"""
54+
Add a Instance Profile to be assign to the DSS instance on startup
55+
56+
:param str startup_instance_profile_arn: ARN of the Instance profile assigned to the DSS instance at startup time
57+
"""
58+
self.data["startupInstanceProfileArn"] = startup_instance_profile_arn
59+
return self
60+
61+
def with_runtime_instance_profile(self, runtime_instance_profile_arn):
62+
"""
63+
Add a Instance Profile to be assign to the DSS instance when running
64+
65+
:param str runtime_instance_profile_arn: ARN of the Instance profile assigned to the DSS instance during runtime
66+
"""
67+
self.data["runtimeInstanceProfileArn"] = runtime_instance_profile_arn
68+
return self
69+
70+
def with_restrict_aws_metadata_server_access(self, restrict_aws_metadata_server_access = True):
71+
"""
72+
Restrict AWS metadata server access on the DSS instance.
73+
74+
:param boolean restrict_aws_metadata_server_access: Optional, If true, restrict the access to the metadata server access. Defaults to true
75+
"""
76+
self.data["restrictAwsMetadataServerAccess"] = restrict_aws_metadata_server_access
77+
return self
78+
79+
def with_default_aws_api_access_mode(self):
80+
"""
81+
The DSS Instance will use the Runtime Instance Profile to access AWS API.
82+
"""
83+
self.data["dataikuAwsAPIAccessMode"] = "NONE"
84+
return self
85+
86+
def with_keypair_aws_api_access_mode(self, aws_access_key_id, aws_keypair_storage_mode="NONE", aws_secret_access_key=None, aws_secret_access_key_aws_secret_name=None, aws_secrets_manager_region=None):
87+
"""
88+
DSS Instance will use an Access Key to authenticate against the AWS API.
89+
90+
:param str aws_access_key_id: AWS Access Key ID.
91+
:param str aws_keypair_storage_mode: Optional, the storage mode of the AWS api key. Accepts "NONE", "INLINE_ENCRYPTED" or "AWS_SECRETS_MANAGER". Defaults to "NONE"
92+
:param str aws_secret_access_key: Optional, AWS Access Key Secret. Only needed if keypair_storage_mode is "INLINE_ENCRYPTED"
93+
:param str aws_secret_access_key_aws_secret_name: Optional, ASM secret name. Only needed if aws_keypair_storage_mode is "AWS_SECRET_MANAGER"
94+
:param str aws_secrets_manager_region: Optional, Secret Manager region to use. Only needed if aws_keypair_storage_mode is "AWS_SECRET_MANAGER"
95+
"""
96+
if aws_keypair_storage_mode not in ["NONE", "INLINE_ENCRYPTED", "AWS_SECRETS_MANAGER"]:
97+
raise ValueError("aws_keypair_storage_mode should be either \"NONE\", \"INLINE_ENCRYPTED\" or \"AWS_SECRET_MANAGER\"")
98+
99+
self.data["dataikuAwsAPIAccessMode"] = "KEYPAIR"
100+
self.data["dataikuAwsKeypairStorageMode"] = aws_keypair_storage_mode
101+
102+
if aws_keypair_storage_mode == "NONE":
103+
return self
104+
105+
self.data["dataikuAwsAccessKeyId"] = aws_access_key_id
106+
107+
if aws_keypair_storage_mode == "INLINE_ENCRYPTED":
108+
if aws_secret_access_key == None:
109+
raise ValueError("When aws_keypair_storage_mode is \"INLINE_ENCRYPTED\", aws_secret_access_key should be provided")
110+
self.data["dataikuAwsSecretAccessKey"] = aws_secret_access_key
111+
elif aws_keypair_storage_mode == "AWS_SECRETS_MANAGER":
112+
if aws_secret_access_key_aws_secret_name == None:
113+
raise ValueError("When aws_keypair_storage_mode is \"AWS_SECRETS_MANAGER\", aws_secret_access_key_aws_secret_name should be provided")
114+
self.data["dataikuAwsSecretAccessKeyAwsSecretName"] = aws_secret_access_key_aws_secret_name
115+
self.data["awsSecretsManagerRegion"] = aws_secrets_manager_region
116+
117+
return self
118+
119+
120+
class FMAzureInstanceSettingsTemplateCreator(FMInstanceSettingsTemplateCreator):
121+
def with_ssh_key(self, ssh_public_key):
122+
"""
123+
Add an SSH public key to the DSS Instance.
124+
Needed to access it through SSH, using the centos user.
125+
126+
:param str ssh_public_key: The content of the public key to add to the instance.
127+
"""
128+
self.data["azureSshKey"] = ssh_public_key
129+
return self
130+
131+
def with_startup_managed_identity(self, startup_managed_identity):
132+
"""
133+
Add a managed identity to be assign to the DSS instance on startup
134+
135+
:param str startup_managed_identity: Managed Identity ID
136+
"""
137+
self.data["startupManagedIdentity"] = startup_managed_identity
138+
return self
139+
140+
def with_runtime_managed_identity(self, runtime_managed_identity):
141+
"""
142+
Add a managed identity to be assign to the DSS instance when running
143+
144+
:param str runtime_managed_identity: Managed Identity ID
145+
"""
146+
self.data["runtimeManagedIdentity"] = runtime_managed_identity
147+
return self
148+
149+
5150
class FMInstanceSettingsTemplate(object):
6151
def __init__(self, client, ist_data):
7152
self.client = client

dataikuapi/fmclient.py

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
from .fm.tenant import FMCloudCredentials
1111
from .fm.virtualnetworks import FMVirtualNetwork
12-
from .fm.instances import FMInstance, FMInstanceEncryptionMode
13-
from .fm.instancesettingstemplates import FMInstanceSettingsTemplate
14-
from dataikuapi.fm.instances import FMAWSInstanceCreator, FMAzureInstanceCreator
12+
from .fm.instances import FMInstance, FMInstanceEncryptionMode, FMAWSInstanceCreator, FMAzureInstanceCreator
13+
from .fm.instancesettingstemplates import FMInstanceSettingsTemplate, FMAWSInstanceSettingsTemplateCreator, FMAzureInstanceSettingsTemplateCreator
1514

1615
class FMClient(object):
1716
"""Entry point for the FM API client"""
@@ -159,62 +158,17 @@ def get_instance_template(self, template_id):
159158
template = self._perform_tenant_json("GET", "/instance-settings-templates/%s" % template_id)
160159
return FMInstanceSettingsTemplate(self, template)
161160

162-
163-
def create_instance_template(self, label,
164-
setupActions=None, license=None,
165-
awsKeyPairName=None, startupInstanceProfileArn=None, runtimeInstanceProfileArn=None,
166-
restrictAwsMetadataServerAccess=True, dataikuAwsAPIAccessMode="NONE", dataikuAwsKeypairStorageMode=None,
167-
dataikuAwsAccessKeyId=None, dataikuAwsSecretAccessKey=None,
168-
dataikuAwsSecretAccessKeyAwsSecretName=None, awsSecretsManagerRegion=None,
169-
azureSshKey=None, startupManagedIdentity=None, runtimeManagedIdentity=None):
161+
def new_instance_template_creator(self, label):
170162
"""
171-
Create an Instance Template
172-
173-
:param str label: The label of the Instance Settings Template
174-
175-
:param list setupActions: Optional, a list of :class:`dataikuapi.fm.instancesettingstemplates.FMSetupAction` to be played on an instance
176-
:param str license: Optional, overrides the license set in Cloud Setup
177-
178-
:param str awsKeyPairName: Optional, AWS Only, the name of an AWS key pair to add to the instance. Needed to get SSH access to the DSS instance, using the centos user.
179-
:param str startupInstanceProfileArn: Optional, AWS Only, the ARN of the Instance profile assigned to the DSS instance at startup time
180-
:param str runtimeInstanceProfileArn: Optional, AWS Only, the ARN of the Instance profile assigned to the DSS instance at runtime
181-
:param boolean restrictAwsMetadataServerAccess: Optional, AWS Only, If true, restrict the access to the metadata server access. Defaults to true
182-
:param str dataikuAwsAPIAccessMode: Optional, AWS Only, the access mode DSS is using to connect to the AWS API. If "NONE" DSS will use the Instance Profile, If "KEYPAIR", an AWS access key id and secret will be securely given to the dataiku account.
183-
:param str dataikuAwsKeypairStorageMode: Optional, AWS Only, the storage mode of the AWS api key. Accepts "NONE", "INLINE_ENCRYPTED" or "AWS_SECRETS_MANAGER"
184-
:param str dataikuAwsAccessKeyId: Optional, AWS Only, AWS Access Key ID. Only needed if dataikuAwsAPIAccessMode is "KEYPAIR"
185-
:param str dataikuAwsSecretAccessKey: Optional, AWS Only, AWS Access Key Secret. Only needed if dataikuAwsAPIAccessMode is "KEYPAIR" and dataikuAwsKeypairStorageMode is "INLINE_ENCRYPTED"
186-
:param str dataikuAwsSecretAccessKeyAwsSecretName: Optional, AWS Only, ASM secret name. Only needed if dataikuAwsAPIAccessMode is "KEYPAIR" and dataikuAwsKeypairStorageMode is "AWS_SECRET_MANAGER"
187-
:param str awsSecretsManagerRegion: Optional, AWS Only
163+
Instantiate a new instance template creator
188164
189-
:param str azureSshKey: Optional, Azure Only, the ssh public key to add to the instance. Needed to get SSH access to the DSS instance, using the centos user.
190-
:param str startupManagedIdentity: Optional, Azure Only, the managed identity assigned to the DSS instance at startup time
191-
:param str runtimeManagedIdentity: Optional, Azure Only, the managed identity assigned to the DSS instance at runtime
192-
193-
:return: requested instance settings template
194-
:rtype: :class:`dataikuapi.fm.instancesettingstemplates.FMInstanceSettingsTemplate`
165+
:param str label: The label of the instance
166+
:rtype: :class:`dataikuapi.fm.instancesettingstemplates.FMInstanceSettingsTemplateCreator`
195167
"""
196-
197-
data = {
198-
"label": label,
199-
"setupActions": setupActions,
200-
"license": license,
201-
"awsKeyPairName": awsKeyPairName,
202-
"startupInstanceProfileArn": startupInstanceProfileArn,
203-
"runtimeInstanceProfileArn": runtimeInstanceProfileArn,
204-
"restrictAwsMetadataServerAccess": restrictAwsMetadataServerAccess,
205-
"dataikuAwsAPIAccessMode": "dataikuAwsAPIAccessMode",
206-
"dataikuAwsKeypairStorageMode": dataikuAwsKeypairStorageMode,
207-
"dataikuAwsAccessKeyId": dataikuAwsAccessKeyId,
208-
"dataikuAwsSecretAccessKey": dataikuAwsSecretAccessKey,
209-
"dataikuAwsSecretAccessKeyAwsSecretName": dataikuAwsSecretAccessKeyAwsSecretName,
210-
"awsSecretsManagerRegion": awsSecretsManagerRegion,
211-
"azureSshKey": azureSshKey,
212-
"startupManagedIdentity": startupManagedIdentity,
213-
"runtimeManagedIdentity": runtimeManagedIdentity
214-
}
215-
216-
template = self._perform_tenant_json("POST", "/instance-settings-templates", body=data)
217-
return FMInstanceSettingsTemplate(self, template)
168+
if self.cloud == "AWS":
169+
return FMAWSInstanceSettingsTemplateCreator(self, label)
170+
elif self.cloud == "Azure":
171+
return FMAzureInstanceSettingsTemplateCreator(self, label)
218172

219173

220174
########################################################
@@ -251,6 +205,7 @@ def new_instance_creator(self, label, instance_settings_template_id, virtual_net
251205
:param str instance_settings_template: The instance settings template id this instance should be based on
252206
:param str virtual_network: The virtual network where the instance should be spawned
253207
:param str image_id: The ID of the DSS runtime image (ex: dss-9.0.3-default)
208+
:rtype: :class:`dataikuapi.fm.instances.FMInstanceCreator`
254209
"""
255210
if self.cloud == "AWS":
256211
return FMAWSInstanceCreator(self, label, instance_settings_template_id, virtual_network_id, image_id)

0 commit comments

Comments
 (0)