Skip to content

Commit 495c1b6

Browse files
committed
Fix static credential and custom endpoint submission
Move crn validation to common method, make more generic to work with Public and Private cloud endpoints Fixes #1 Signed-off-by: Daniel Chaffelson <[email protected]>
1 parent e6e7993 commit 495c1b6

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)