def test_Sub(self): a = '!Sub\n- www.${Domain}\n- {Domain: github.com}\n' b = dump(load(a)) assert(a==b) a = "- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/sfjs/packagelambda/*\n" print(a) print(dump(load(a))) assert(dump(load(a)) == a)
def test_Select(self): a = "!Select\n- '1'\n- [apples, grapes, oranges, mangoes]\n" b = dump(load(a)) print('====================') print(a, b) assert(a==b) a = "!Select\n- 0\n- {'Fn::GetAZs': ''}\n" b = dump(load(a)) print('====================') print(a, b) assert(a==b)
def test_And(self): a = ''' !And - !Not [!Equals [!Ref EnvironmentType, prod]] - !Equals ["sg-mysggroup", !Ref "ASecurityGroup"] ''' print(load(a)) print(dump(load(a)))
def work(templatepath, cachedir, outtemplatepath): # templatepath = os.path.abspath(DEFAULT_TEMPLATE if templatepath is None else templatepath) basedir = os.path.abspath(os.path.join(templatepath, '..')) # cachedir = os.path.abspath(os.path.join(basedir, DEFAULT_CACHE) if cachedir is None else cachedir) # outtemplatepath = os.path.abspath(os.path.join(basedir, DEFAULT_OUT_TEMPLATE) if outtemplatepath is None else outtemplatepath) try: with open(templatepath) as f: template = cfnyaml.load(f) except Exception as e: print('Error when load template file: {}\n{}'.format(templatepath, e)) exit(1) try: os.makedirs(cachedir, exist_ok=True) except Exception as e: print('Error when create cache dir: {}\n{}'.format(cachedir, e)) exit(1) for key, resource in list(template['Resources'].items()): if resource.get('Type') == 'AWS::Serverless::Function': runtime = resource['Properties']['Runtime'] downloadercls = downloaders.cls.get(runtime) if downloadercls is None: continue downloader = downloadercls(resource, basedir) if not downloader.isdepfilesexists(): print( 'Lambda [{}] with Runtime {} does not have dependency files' .format(key, runtime)) continue print( 'Download dependencies for Lambda [{}] with Runtime {}'.format( key, runtime)) layername = '{}{}H{}'.format(LAYER_PREFIX, downloader.prefix, downloader.gethash()[:7]) if layername not in template: if not createlayer(template, layername, resource, downloader, cachedir): continue if 'Layers' not in resource['Properties']: resource['Properties']['Layers'] = [] for ref in resource['Properties']['Layers']: if ref.logicalName.find(LAYER_PREFIX) == 0: ref.logicalName = layername break else: resource['Properties']['Layers'].append({'Ref': layername}) try: with open(outtemplatepath, 'w') as f: if '.json' == outtemplatepath[-5:]: json.dump(template, f) else: f.write(cfnyaml.dump(template)) except Exception as e: print('Error when create out template file: {}\n{}'.format( outtemplatepath, e)) exit(1)
def test_Base64(self): a= "- !Base64 YWJjMTIzIS49JeeugOS9k+e5gemrlOOBq+OBu+OCk+OBk+OCmQ==\n" b = [Base64('abc123!.=%简体繁體にほんご', True)] assert(dump(load(a)) == a) assert(dump(b) == a)
def test_Cidr(self): a = '!Select [ 0, !Cidr [ !Select [ 0, !Ref VpcCidrBlock], 1, 8 ]]' print(load(a)) print(dump(load(a)))
def test_Ref(self): a = "!Ref 'aaa'\n" b = dump(load(a))