Skip to content

Commit

Permalink
Improve handling of properties. (#152)
Browse files Browse the repository at this point in the history
* Improve handling of properties.

- Error on malformed property types.
- Recognize (and parse) REPORTING_SECRET properties.

* Addressing PR feedback.
  • Loading branch information
trironkk authored Jun 27, 2018
1 parent 98ed1da commit 4f7b4e3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
25 changes: 24 additions & 1 deletion marketplace/deployer_util/config_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
NAME_RE = re.compile(r'[a-zA-z0-9_\.]+$')

XGOOGLE = 'x-google-marketplace'
XTYPE_NAME = 'NAME'
XTYPE_NAMESPACE = 'NAMESPACE'
XTYPE_IMAGE = 'IMAGE'
XTYPE_PASSWORD = 'GENERATED_PASSWORD'
XTYPE_SERVICE_ACCOUNT = 'SERVICE_ACCOUNT'
XTYPE_STORAGE_CLASS = 'STORAGE_CLASS'
XTYPE_REPORTING_SECRET = 'REPORTING_SECRET'


class InvalidName(Exception):
Expand Down Expand Up @@ -127,6 +131,7 @@ def __init__(self, name, dictionary, required):
self._password = None
self._service_account = None
self._storage_class = None
self._reporting_secret = None

if not NAME_RE.match(name):
raise InvalidSchema('Invalid property name: {}'.format(name))
Expand All @@ -152,7 +157,9 @@ def __init__(self, name, dictionary, required):
raise InvalidSchema(
'Property {} has {} without a type'.format(name, XGOOGLE))
xt = self._x['type']
if xt == XTYPE_PASSWORD:
if xt in (XTYPE_IMAGE, XTYPE_NAME, XTYPE_NAMESPACE):
pass
elif xt == XTYPE_PASSWORD:
d = self._x.get('generatedPassword', {})
spec = {
'length': d.get('length', 10),
Expand All @@ -166,6 +173,11 @@ def __init__(self, name, dictionary, required):
elif xt == XTYPE_STORAGE_CLASS:
d = self._x.get('storageClass', {})
self._storage_class = SchemaXStorageClass(d)
elif xt == XTYPE_REPORTING_SECRET:
d = self._x.get('reportingSecret', {})
self._reporting_secret = SchemaXReportingSecret(d)
else:
raise InvalidSchema('Property {} has an unknown type: {}'.format(name, xt))


@property
Expand Down Expand Up @@ -203,6 +215,10 @@ def service_account(self):
def storage_class(self):
return self._storage_class

@property
def reporting_secret(self):
return self._reporting_secret

def str_to_type(self, str_val):
if self._type == bool:
if str_val in {'true', 'True', 'yes', 'Yes'}:
Expand Down Expand Up @@ -295,3 +311,10 @@ def __init__(self, dictionary):
@property
def ssd(self):
return self._type == 'SSD'


class SchemaXReportingSecret:
"""Wrapper class providing convenient access to REPORTING_SECRET property"""

def __init__(self, dictionary):
pass
56 changes: 56 additions & 0 deletions marketplace/deployer_util/config_helper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,39 @@ def test_schema_properties_matching(self):
'type': 'int',
}))

def test_name_type(self):
schema = config_helper.Schema.load_yaml(
"""
properties:
n:
type: string
x-google-marketplace:
type: NAME
""")
self.assertIsNotNone(schema.properties['n'])

def test_namespace_type(self):
schema = config_helper.Schema.load_yaml(
"""
properties:
ns:
type: string
x-google-marketplace:
type: NAMESPACE
""")
self.assertIsNotNone(schema.properties['ns'])

def test_image_type(self):
schema = config_helper.Schema.load_yaml(
"""
properties:
i:
type: string
x-google-marketplace:
type: IMAGE
""")
self.assertIsNotNone(schema.properties['i'])

def test_password(self):
schema = config_helper.Schema.load_yaml(
"""
Expand Down Expand Up @@ -374,6 +407,29 @@ def test_storage_class(self):
sc = schema.properties['sc'].storage_class
self.assertTrue(sc.ssd)

def test_reporting_secret(self):
schema = config_helper.Schema.load_yaml(
"""
properties:
rs:
type: string
x-google-marketplace:
type: REPORTING_SECRET
""")
self.assertIsNotNone(schema.properties['rs'].reporting_secret)

def test_unknown_type(self):
self.assertRaises(
config_helper.InvalidSchema,
lambda: config_helper.Schema.load_yaml(
"""
properties:
unk:
type: string
x-google-marketplace:
type: UNKNOWN
"""))


if __name__ == 'main':
unittest.main()

0 comments on commit 4f7b4e3

Please sign in to comment.