Skip to content

Commit 4e01112

Browse files
authored
Merge pull request #2 from cloudera-labs/devel
Fix static credential and custom endpoint submission
2 parents e6e7993 + b560372 commit 4e01112

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/cdpy/common.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ def __init__(self, value, field='error_code', default=None, warning=None):
8585
class StaticCredentials(Credentials):
8686
"""A credential class that simply takes a set of static credentials."""
8787

88-
def __init__(self, access_key_id, private_key):
89-
super(StaticCredentials, self).__init__(access_key_id, private_key, 'static')
88+
def __init__(self, access_key_id, private_key, access_token='', method='static'):
89+
super(StaticCredentials, self).__init__(
90+
access_key_id=access_key_id, private_key=private_key,
91+
access_token=access_token, method=method
92+
)
9093

9194

9295
class CdpcliWrapper(object):
@@ -221,14 +224,15 @@ def _setup_logger(self, log_level, log_format):
221224

222225
self.logger.addHandler(handler)
223226

224-
def _build_client(self, service, credentials=None):
227+
def _build_client(self, service):
225228
if not self.cdp_credentials:
226-
credentials = self._client_creator.context.get_credentials()
229+
self.cdp_credentials = self._client_creator.context.get_credentials()
227230
return self._client_creator.create_client(
228231
service,
229232
self.client_endpoint,
230233
self.tls_verify,
231-
credentials)
234+
self.cdp_credentials
235+
)
232236

233237
@staticmethod
234238
def _default_throw_error(error: 'CdpError'):
@@ -262,6 +266,12 @@ def _default_throw_warning(warning: 'CdpWarning'):
262266
def regex_search(pattern, obj):
263267
return re.search(pattern, obj)
264268

269+
def validate_crn(self, obj: str):
270+
if obj is not None and obj.startswith('crn:'):
271+
pass
272+
else:
273+
self.throw_error(CdpError("Supplied env_crn %s is not a valid CDP crn" % str(obj)))
274+
265275
@staticmethod
266276
def sleep(seconds):
267277
sleep(seconds)

src/cdpy/dw.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ def list_clusters(self, env_crn=None):
6262
return resp
6363

6464
def gather_clusters(self, env_crn=None):
65-
if env_crn is not None and not env_crn.startswith('crn:cdp:environments'):
66-
self.sdk.throw_error(CdpError("Supplied env_crn %s is not a valid CDP Environment crn" % str(env_crn)))
65+
self.sdk.validate_crn(env_crn)
6766
clusters = self.list_clusters(env_crn=env_crn)
6867
out = []
6968
if clusters:
@@ -77,8 +76,7 @@ def gather_clusters(self, env_crn=None):
7776

7877
def create_cluster(self, env_crn: str, overlay: bool, aws_public_subnets: list = None,
7978
aws_private_subnets: list = None, az_subnet: str = None, az_enable_az: bool = None):
80-
if env_crn is not None and not env_crn.startswith('crn:cdp:environments'):
81-
self.sdk.throw_error(CdpError("Supplied env_crn %s is not a valid CDP Environment crn" % str(env_crn)))
79+
self.sdk.validate_crn(env_crn)
8280
if all(x is not None for x in [aws_private_subnets, aws_private_subnets]):
8381
aws_options = dict(publicSubnetIds=aws_public_subnets, privateSubnetIds=aws_private_subnets)
8482
else:

src/cdpy/environments.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def describe_environment(self, name):
6161
# Describe will fail in certain fault scenarios early in Environment creation
6262
# We helpfully provide the summary listing as a backup set of information
6363
# If the environment truly does not exist, then this will give the same response
64-
self.sdk.throw_warning(CdpWarning(resp.violations['message']))
64+
self.sdk.throw_warning(CdpWarning(resp.violations))
6565
return self.summarize_environment(name)
6666
self.sdk.throw_error(resp)
6767
return resp
@@ -107,7 +107,7 @@ def create_aws_environment(self, **kwargs):
107107
if resp.error_code == 'INVALID_ARGUMENT':
108108
if 'constraintViolations' not in resp.violations:
109109
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(resp.violations))
110-
self.sdk.throw_warning(CdpWarning(resp.violations['message']))
110+
self.sdk.throw_warning(CdpWarning(resp.violations))
111111
self.sdk.throw_error(resp)
112112
return resp
113113

@@ -121,7 +121,7 @@ def create_azure_environment(self, **kwargs):
121121
if resp.error_code == 'INVALID_ARGUMENT':
122122
if 'constraintViolations' not in resp.violations:
123123
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(resp.violations))
124-
self.sdk.throw_warning(CdpWarning(resp.violations['message']))
124+
self.sdk.throw_warning(CdpWarning(resp.violations))
125125
self.sdk.throw_error(resp)
126126
return resp
127127

@@ -135,7 +135,7 @@ def create_gcp_environment(self, **kwargs):
135135
if resp.error_code == 'INVALID_ARGUMENT':
136136
if 'constraintViolations' not in resp.violations:
137137
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(resp.violations))
138-
self.sdk.throw_warning(CdpWarning(resp.violations['message']))
138+
self.sdk.throw_warning(CdpWarning(resp.violations))
139139
self.sdk.throw_error(resp)
140140
return resp
141141

@@ -205,7 +205,7 @@ def sync_users(self, environments=None):
205205
)
206206
if isinstance(resp, CdpError):
207207
if resp.error_code == 'CONFLICT':
208-
operation_match = self.sdk.regex_search(self.sdk.OPERATION_REGEX, resp.violations['message'])
208+
operation_match = self.sdk.regex_search(self.sdk.OPERATION_REGEX, resp.violations)
209209
if operation_match is not None:
210210
existing_op_id = operation_match.group(1)
211211
if not self.sdk.strict_errors:
@@ -264,11 +264,11 @@ def create_aws_credential(self, name, role, description, retries=3, delay=2):
264264
consistency_violations = [
265265
'Unable to verify credential', 'sts:AssumeRole', 'You are not authorized'
266266
]
267-
if any(x in resp.violations['message'] for x in consistency_violations):
267+
if any(x in resp.violations for x in consistency_violations):
268268
retries = retries - 1
269269
self.sdk.throw_warning(
270270
CdpWarning('Got likely AWS IAM eventual consistency error [%s], %d retries left...'
271-
% (resp.violations['message'], retries))
271+
% (resp.violations, retries))
272272
)
273273
self.sdk.sleep(delay)
274274
return self.create_aws_credential(name, role, description, retries, delay)
@@ -313,7 +313,7 @@ def set_telemetry(self, name, workload_analytics=None, logs_collection=None):
313313
def resolve_environment_crn(self, env: Union[str, None]):
314314
"""Ensures a given env string is either the environment crn or None"""
315315
if isinstance(env, str):
316-
if env.startswith('crn:cdp:environments'):
316+
if env.startswith('crn:'):
317317
return env
318318
else:
319319
env_desc = self.describe_environment(env)

0 commit comments

Comments
 (0)