Skip to content

Commit 0346da9

Browse files
authored
Support clusterConstraints.gcp.nodes.requiredOauthScopes in schema (#423)
* Support clusterConstraints.gcp.nodes.requiredOauthScopes in schema * Address review suggestsions: fix bug and set default value of []
1 parent 1c66945 commit 0346da9

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

marketplace/deployer_util/config_helper.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
WIDGET_TYPES = ['help']
4444

45+
_OAUTH_SCOPE_PREFIX = 'https://www.googleapis.com/auth/'
46+
4547

4648
class InvalidName(Exception):
4749
pass
@@ -283,15 +285,17 @@ def __init__(self, dictionary):
283285
self._k8s_version = dictionary.get('k8sVersion', None)
284286
self._resources = None
285287
self._istio = None
288+
self._gcp = None
286289

287290
if 'resources' in dictionary:
288291
resources = dictionary['resources']
289292
if not isinstance(resources, list):
290293
raise InvalidSchema('clusterConstraints.resources must be a list')
291294
self._resources = [SchemaResourceConstraints(r) for r in resources]
292295

293-
if 'istio' in dictionary:
294-
self._istio = SchemaIstio(dictionary['istio'])
296+
self._istio = _maybe_get_and_apply(dictionary, 'istio',
297+
lambda v: SchemaIstio(v))
298+
self._gcp = _maybe_get_and_apply(dictionary, 'gcp', lambda v: SchemaGcp(v))
295299

296300
@property
297301
def k8s_version(self):
@@ -305,6 +309,10 @@ def resources(self):
305309
def istio(self):
306310
return self._istio
307311

312+
@property
313+
def gcp(self):
314+
return self._gcp
315+
308316

309317
class SchemaResourceConstraints:
310318
"""Accesses a single resource's constraints."""
@@ -400,6 +408,36 @@ def type(self):
400408
return self._type
401409

402410

411+
class SchemaGcp:
412+
"""Accesses top level GCP constraints."""
413+
414+
def __init__(self, dictionary):
415+
self._nodes = _maybe_get_and_apply(dictionary, 'nodes',
416+
lambda v: SchemaNodes(v))
417+
418+
@property
419+
def nodes(self):
420+
return self._nodes
421+
422+
423+
class SchemaNodes:
424+
"""Accesses GKE cluster node constraints."""
425+
426+
def __init__(self, dictionary):
427+
self._required_oauth_scopes = dictionary.get('requiredOauthScopes', [])
428+
if not isinstance(self._required_oauth_scopes, list):
429+
raise InvalidSchema('nodes.requiredOauthScopes must be a list')
430+
for scope in self._required_oauth_scopes:
431+
if not scope.startswith(_OAUTH_SCOPE_PREFIX):
432+
raise InvalidSchema(
433+
'OAuth scope references must be fully-qualified (start with {})'
434+
.format(_OAUTH_SCOPE_PREFIX))
435+
436+
@property
437+
def required_oauth_scopes(self):
438+
return self._required_oauth_scopes
439+
440+
403441
class SchemaImage:
404442
"""Accesses an image definition."""
405443

@@ -840,7 +878,7 @@ def _must_contain(value, valid_list, error_msg):
840878
"""Validates that value in valid_list, or raises InvalidSchema."""
841879
if value not in valid_list:
842880
raise InvalidSchema("{}. Must be one of {}".format(error_msg,
843-
', '.join(_ISTIO_TYPES)))
881+
', '.join(valid_list)))
844882

845883

846884
def _property_must_have_type(prop, expected_type):

marketplace/deployer_util/config_helper_test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,42 @@ def test_istio_invalid_type(self):
827827
type: INVALID_TYPE
828828
""")
829829

830+
def test_required_oauth_scopes_valid(self):
831+
schema = config_helper.Schema.load_yaml("""
832+
applicationApiVersion: v1beta1
833+
properties:
834+
simple:
835+
type: string
836+
x-google-marketplace:
837+
clusterConstraints:
838+
gcp:
839+
nodes:
840+
requiredOauthScopes:
841+
- https://www.googleapis.com/auth/cloud-platform
842+
""")
843+
schema.validate()
844+
self.assertEqual(
845+
schema.x_google_marketplace.cluster_constraints.gcp.nodes
846+
.required_oauth_scopes,
847+
["https://www.googleapis.com/auth/cloud-platform"])
848+
849+
def test_required_oauth_scopes_invalid_scope(self):
850+
with self.assertRaisesRegexp(
851+
config_helper.InvalidSchema,
852+
"OAuth scope references must be fully-qualified"):
853+
config_helper.Schema.load_yaml("""
854+
applicationApiVersion: v1beta1
855+
properties:
856+
simple:
857+
type: string
858+
x-google-marketplace:
859+
clusterConstraints:
860+
gcp:
861+
nodes:
862+
requiredOauthScopes:
863+
- cloud-platform
864+
""")
865+
830866
def test_deployer_service_account(self):
831867
schema = config_helper.Schema.load_yaml("""
832868
x-google-marketplace:

0 commit comments

Comments
 (0)