Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Commit

Permalink
Fixing Globals > ... > Environment that could not use the Fn::Ref (
Browse files Browse the repository at this point in the history
…#223)

* - Adding support for global environment variables

* Add test for global ref

* update changelog
  • Loading branch information
acolombier authored and martysweet committed Feb 27, 2019
1 parent 8d2c3d8 commit 1cd0073
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic
Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Merge PR #223, allowing Ref usage inside SAM Global

## [1.9.5] - 2019-02-05
### Fixed
Expand Down
7 changes: 7 additions & 0 deletions src/test/validatorTest.ts
Expand Up @@ -1924,5 +1924,12 @@ describe('validator', () => {
expect(result).to.have.deep.property('templateValid', false);
expect(result['errors']['crit']).to.have.lengthOf(2);
});

it('a valid template with Ref in Globals (sam_20161031_environment_ref.yaml) should validate successfully', () => {
const input = 'testData/valid/yaml/sam_20161031_environment_ref.yaml';
let result = validator.validateFile(input);
expect(result).to.have.deep.property('templateValid', true);
expect(result['errors']['crit']).to.have.lengthOf(0);
});
});
});
15 changes: 8 additions & 7 deletions src/validator.ts
Expand Up @@ -852,7 +852,7 @@ function inferParameterValue(parameterName: string, parameter: any, okToGuess: b
normalizedType = 'string';
}

const parameterDefault = parameterDefaultsByType[parameterTypesSpec[parameterType]!]!
const parameterDefault = parameterDefaultsByType[parameterTypesSpec[parameterType]!]!
if (isList) {
return [parameterDefault];
} else {
Expand Down Expand Up @@ -1067,11 +1067,12 @@ function recursiveDecent(ref: any){
// Check if an Intrinsic function is allowed here
let inResourceProperty = (placeInTemplate[0] == "Resources" || placeInTemplate[2] == "Properties");
let inResourceMetadata = (placeInTemplate[0] == "Resources" || placeInTemplate[2] == "Metadata");
let inGlobalEnvironment = (placeInTemplate[0] == "Globals" && placeInTemplate[2] == "Environment");
let inOutputs = (placeInTemplate[0] == "Outputs");
let inConditions = (placeInTemplate[0] == "Conditions");
// TODO Check for usage inside update policy

if(!(inResourceProperty || inResourceMetadata || inOutputs || inConditions)){
if(!(inGlobalEnvironment || inResourceProperty || inResourceMetadata || inOutputs || inConditions)){
addError("crit", `Intrinsic function ${key} is not supported here`, placeInTemplate, key);
}else {
// Resolve the function
Expand Down Expand Up @@ -1382,7 +1383,7 @@ function doIntrinsicSelect(ref: any, key: string){
}
} else if (list.indexOf(null) > -1) {
addError('crit', "Fn::Select requires that the list be free of null values", placeInTemplate, "Fn::Select");

}
if (index >= 0 && index < list.length) {
return list[index];
Expand Down Expand Up @@ -1909,7 +1910,7 @@ export interface PrimitiveType {
resourceType: string,
primitiveType: string
}

export type ObjectType = ResourceType | NamedProperty | PropertyType | PrimitiveType;

/**
Expand All @@ -1929,7 +1930,7 @@ function getTypeName(objectType: ResourceType | NamedProperty | PropertyType ):
}

/**
*
*
*/
function getItemType(objectType: NamedProperty): PrimitiveType | PropertyType {
const maybePrimitiveType = resourcesSpec.getPrimitiveItemType(objectType.parentType, objectType.propertyName);
Expand Down Expand Up @@ -2064,7 +2065,7 @@ function check(objectType: ObjectType, objectToCheck: any) {
verify(isList, objectToCheck);
checkList(objectType as NamedProperty, objectToCheck);
break;
case KnownTypes.Arn:
case KnownTypes.Arn:
verify(isArn, objectToCheck);
break;
case KnownTypes.String:
Expand Down Expand Up @@ -2338,7 +2339,7 @@ function checkComplexObject(objectType: ResourceType | NamedProperty | PropertyT
parentType: objectTypeName,
propertyName: subPropertyName
} as NamedProperty;

check(subPropertyObjectType, propertyValue)

} finally {
Expand Down
17 changes: 17 additions & 0 deletions testData/valid/yaml/sam_20161031_environment_ref.yaml
@@ -0,0 +1,17 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Tests if Ref is allowed in Globals

Parameters:
LocalVar:
Type: String

Globals:
Function:
Environment:
Variables:
GLOB_ENV_VAR: !Ref LocalVar

Resources:
Bucket:
Type: AWS::S3::Bucket

0 comments on commit 1cd0073

Please sign in to comment.