def _validate(self): # basic validate, future will add restrict validate if not TemplateReimplemented._check_key_exist("Resources", self._template): raise InvalidTemplateException("'Resources' section is required") if len(self._template["Resources"]) < 1: raise InvalidTemplateException("Function not found in the template") self._resources = self._template["Resources"] for logic_id in self._resources: if "Type" not in self._resources[logic_id]: raise InvalidTemplateException("Resource '{}' define error: Lost 'Type' properties".format(logic_id)) if self._resources[logic_id]["Type"] != TENCENT_FUNCTION_TYPE: raise InvalidTemplateException("Resource '{}' define error: Must define the resource 'Type' like as " "'{}'".format(logic_id, TENCENT_FUNCTION_TYPE)) if not TemplateReimplemented._check_key_exist("Properties", self._resources[logic_id]): raise InvalidTemplateException("Resource '{}' define error: Must define the resource 'Properties'". format(logic_id)) if TemplateReimplemented._check_key_exist("Events", self._resources[logic_id]["Properties"]): self._check_events_valid(logic_id, self._resources[logic_id]["Properties"]["Events"]) if TemplateReimplemented._check_key_exist("Globals", self._template): self._glob = self._template["Globals"] if TemplateReimplemented._check_key_exist("Function", self._glob): self._glob_settings = self._glob["Function"] TemplateReimplemented._check_properties_valid(self._glob_settings, "Globals")
def _check_events_valid(func, dict_map): for event_id, config in dict_map.items(): if "Type" not in config: raise InvalidTemplateException( "Events 'Type' not found in Resource '{}'".format(func)) if config["Type"] != 'Api': raise InvalidTemplateException( "Events 'Type' not equal 'Api' in Resource '{}'".format( func)) if "Properties" not in config: raise InvalidTemplateException( "Events 'Properties' not found in Resource '{}'".format( func)) if "Path" not in config["Properties"]: raise InvalidTemplateException( "Api Path not found in Event from Resource '{}'".format( func)) if "Method" not in config["Properties"]: raise InvalidTemplateException( "Api Method not found in Event from Resource '{}'".format( func)) if "StageName" in config["Properties"]: # TODO: if check? pass
def _check_vpc_valid(dict_map, blob): if not TemplateReimplemented._check_key_exist("VpcId", dict_map): raise InvalidTemplateException( "VpcId not found in '{}'".format(blob)) if not TemplateReimplemented._check_key_exist("SubnetId", dict_map): raise InvalidTemplateException( "SubnetTd not found in '{}'".format(blob))
def _check_env_valid(dict_map, blob): if not TemplateReimplemented._check_key_exist("Variables", dict_map): raise InvalidTemplateException( "Has Environment but not define Variables in '{}'".format( blob)) if len(dict_map["Variables"]) < 1: raise InvalidTemplateException( "Environment values in '{}' not defined".format(blob))
def __init__(self, model): if not isinstance(model, dict): raise InvalidTemplateException("%s invalid" % self.__class__.__name__) resource = model.get("Resources", {}) glo = model.get("Globals", {}) if not isinstance(resource, dict) or not isinstance(glo, dict): raise InvalidTemplateException("%s invalid" % self.__class__.__name__) super(Resources, self).__init__(resource, glo)
def __init__(self, model): if not isinstance(model, dict) or VARIABLE not in model \ or not isinstance(model[VARIABLE], dict): raise InvalidTemplateException("invalid") for m in model[VARIABLE]: if not isinstance(model[VARIABLE][m], str): raise InvalidTemplateException("%s invalid" % m) try: setattr(self, m, model[VARIABLE][m]) except InvalidTemplateException as e: raise InvalidTemplateException("{proper} ".format(proper=m) + str(e))
def __init__(self, config): if not isinstance(config, dict): raise InvalidTemplateException("%s invalid" % self.__class__.__name__) resource = config.get(RESOURCE, {}) if not resource: raise InvalidTemplateException("Resources not found") glo = config.get(GLOBAL, {}) if not isinstance(resource, dict) or not isinstance(glo, dict): raise InvalidTemplateException("%s invalid" % self.__class__.__name__) super(Resources, self).__init__(resource, glo)
def __init__(self, model, dft): if not isinstance(dft, dict): raise InvalidTemplateException("Globals invalid") if dft.has_key(FUNCTION): if not isinstance(dft[FUNCTION], dict): raise InvalidTemplateException("Globals invalid") for proper in dft[FUNCTION]: try: setattr(self, proper, self.PROPER[proper][TYPE](dft[FUNCTION][proper])) except InvalidTemplateException as e: raise InvalidTemplateException("{proper} ".format(proper=proper) + str(e)) super(Function, self).__init__(model)
def _check_properties_valid(dict_map, blob): if "Environment" in dict_map: TemplateReimplemented._check_env_valid(dict_map["Environment"], blob) if "VpcConfig" in dict_map: TemplateReimplemented._check_vpc_valid(dict_map["VpcConfig"], blob) if "FunctionName" in dict_map: raise InvalidTemplateException("Can not define 'FunctionName' in Globals block") if "CodeUri" in dict_map: raise InvalidTemplateException("Can not define 'CodeUri' in Globals block") if "Events" in dict_map: raise InvalidTemplateException("Can not define 'Events' in Globals block")
def __init__(self, model, dft={}): if not isinstance(model, dict): raise InvalidTemplateException("invalid") for m in model: mod = model[m] if not isinstance(mod, dict) or TYPE not in mod: raise InvalidTemplateException("%s invalid" % m) type = mod[TYPE] del mod[TYPE] if type not in self.AVA_TYPE: raise InvalidTemplateException("%s invalid" % m) try: setattr(self, m, self.AVA_TYPE[type](mod, dft)) except InvalidTemplateException as e: raise InvalidTemplateException("{proper} ".format(proper=m) + str(e))
def __init__(self, proper): self.codeuri = proper.get(CODE_URI) self.env = proper.get(ENVIRONMENT, {VARIABLE: {}}).get(VARIABLE, {}) self.handler = proper.get(HANDLER, "") self.mem_size = proper.get(MEMORY_SIZE, 128) self.runtime = proper.get(RUNTIME, "").lower() self.timeout = proper.get(TIMEOUT, 3) if self.runtime not in self.RUNTIME.keys(): raise InvalidTemplateException("Invalid runtime. supports [{}]". format(",".join(self.RUNTIME.keys())))
def __init__(self, model): if not isinstance(model, dict) or PROPERTY not in model: raise InvalidTemplateException("%s invalid" % PROPERTY) if not isinstance(model[PROPERTY], dict): if model[PROPERTY] is None: model[PROPERTY] = {} else: raise InvalidTemplateException("%s invalid" % PROPERTY) for m in model[PROPERTY]: if m not in self.PROPER: raise InvalidTemplateException("%s invalid" % m) try: setattr(self, m, self.PROPER[m][TYPE](model[PROPERTY][m])) except InvalidTemplateException as e: raise InvalidTemplateException("{proper} ".format(proper=m) + str(e)) proper = [x for x in self.PROPER if self.PROPER[x][MUST]] for p in proper: v = getattr(self, p, None) if v is None: raise InvalidTemplateException("%s invalid" % p) elif isinstance(self.PROPER[p][VALUE], tuple) and v not in self.PROPER[p][VALUE]: raise InvalidTemplateException("%s invalid" % p)
def __init__(self, proper): self.codeuri = proper.get(tsmacro.CodeUri) self.env = proper.get(tsmacro.Envi, { tsmacro.Vari: {} }).get(tsmacro.Vari, {}) self.handler = proper.get(tsmacro.Handler) self.mem_size = proper.get(tsmacro.MemSize) self.runtime = proper.get(tsmacro.Runtime, "").lower() self.timeout = proper.get(tsmacro.Timeout, 3) if self.runtime not in self.RUNTIME.keys(): raise InvalidTemplateException( "Invalid runtime. supports [{}]".format(",".join( self.RUNTIME.keys())))