From 4f7b4e3ea42dc924987981f27aaa40dd998bac3c Mon Sep 17 00:00:00 2001 From: TK Kiatkungwanglai Date: Wed, 27 Jun 2018 15:12:54 -0700 Subject: [PATCH] Improve handling of properties. (#152) * Improve handling of properties. - Error on malformed property types. - Recognize (and parse) REPORTING_SECRET properties. * Addressing PR feedback. --- marketplace/deployer_util/config_helper.py | 25 ++++++++- .../deployer_util/config_helper_test.py | 56 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/marketplace/deployer_util/config_helper.py b/marketplace/deployer_util/config_helper.py index b319e2a5..9ffac60b 100644 --- a/marketplace/deployer_util/config_helper.py +++ b/marketplace/deployer_util/config_helper.py @@ -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): @@ -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)) @@ -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), @@ -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 @@ -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'}: @@ -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 diff --git a/marketplace/deployer_util/config_helper_test.py b/marketplace/deployer_util/config_helper_test.py index 3e63aede..b827defe 100644 --- a/marketplace/deployer_util/config_helper_test.py +++ b/marketplace/deployer_util/config_helper_test.py @@ -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( """ @@ -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()