Skip to content

Commit

Permalink
work in progress with iam building
Browse files Browse the repository at this point in the history
  • Loading branch information
moofish32 committed May 27, 2019
1 parent db0a023 commit f823eb2
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 76 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-apigateway/test/test.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,4 @@ export = {

}
}
};
};
30 changes: 17 additions & 13 deletions packages/@aws-cdk/aws-iam/lib/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ import { IPrincipal } from './principals';
import { IUser } from './user';
import { AttachedPolicies, undefinedIfEmpty } from './util';

export interface IGroup extends IIdentity {
/**
* @attribute
*/
readonly groupName: string;

/**
* @attribute
*/
readonly groupArn: string;
}

export interface GroupProps {
/**
* A name for the IAM group. For valid values, see the GroupName parameter
Expand Down Expand Up @@ -51,7 +39,7 @@ export interface GroupProps {
readonly path?: string;
}

abstract class GroupBase extends Resource implements IGroup {
export abstract class GroupBase extends Resource implements IGroup {
public abstract readonly groupName: string;
public abstract readonly groupArn: string;

Expand Down Expand Up @@ -143,3 +131,19 @@ export class Group extends GroupBase {
this.managedPolicies.push(arn);
}
}

export interface IGroup extends IIdentity {
/**
* Returns the IAM Group Name
*
* @attribute
*/
readonly groupName: string;

/**
* Returns the IAM Group ARN
*
* @attribute
*/
readonly groupArn: string;
}
26 changes: 13 additions & 13 deletions packages/@aws-cdk/cdk/lib/cfn-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ export class CfnResource extends CfnRefElement {
* Derived classes should expose a strongly-typed version of this object as
* a public property called `propertyOverrides`.
*/
protected readonly untypedPropertyOverrides: any = { };
protected readonly untypedPropertyOverrides: any = {};

/**
* An object to be merged on top of the entire resource definition.
*/
private readonly rawOverrides: any = { };
private readonly rawOverrides: any = {};

/**
* Logical IDs of dependencies.
Expand All @@ -116,7 +116,7 @@ export class CfnResource extends CfnRefElement {
}

this.resourceType = props.type;
this.properties = props.properties || { };
this.properties = props.properties || {};

// if aws:cdk:enable-path-metadata is set, embed the current construct's
// path in the CloudFormation template, so it will be possible to trace
Expand Down Expand Up @@ -159,7 +159,7 @@ export class CfnResource extends CfnRefElement {
// object overwrite it with an object.
const isObject = curr[key] != null && typeof(curr[key]) === 'object' && !Array.isArray(curr[key]);
if (!isObject) {
curr[key] = { };
curr[key] = {};
}

curr = curr[key];
Expand Down Expand Up @@ -227,15 +227,15 @@ export class CfnResource extends CfnRefElement {
Type: this.resourceType,
Properties: ignoreEmpty(properties),
DependsOn: ignoreEmpty(renderDependsOn(this.dependsOn)),
CreationPolicy: capitalizePropertyNames(this, this.options.creationPolicy),
CreationPolicy: capitalizePropertyNames(this, this.options.creationPolicy),
UpdatePolicy: capitalizePropertyNames(this, this.options.updatePolicy),
UpdateReplacePolicy: capitalizePropertyNames(this, this.options.updateReplacePolicy),
DeletionPolicy: capitalizePropertyNames(this, this.options.deletionPolicy),
Metadata: ignoreEmpty(this.options.metadata),
Condition: this.options.condition && this.options.condition.logicalId
}, props => {
const r = deepMerge(props, this.rawOverrides);
r.Properties = this.renderProperties(r.Properties);
r.Properties = this.renderProperties();
return r;
})
}
Expand All @@ -262,8 +262,8 @@ export class CfnResource extends CfnRefElement {
}
}

protected renderProperties(properties: any): { [key: string]: any } {
return properties;
protected renderProperties(): { [key: string]: any } {
return {};
}

protected validateProperties(_properties: any) {
Expand All @@ -272,10 +272,10 @@ export class CfnResource extends CfnRefElement {
}

export enum TagType {
Standard = 'StandardTag',
AutoScalingGroup = 'AutoScalingGroupTag',
Map = 'StringToStringMap',
NotTaggable = 'NotTaggable',
Standard = "StandardTag",
AutoScalingGroup = "AutoScalingGroupTag",
Map = "StringToStringMap",
NotTaggable = "NotTaggable"
}

export interface IResourceOptions {
Expand Down Expand Up @@ -338,7 +338,7 @@ export function deepMerge(target: any, ...sources: any[]) {
// if the value at the target is not an object, override it with an
// object so we can continue the recursion
if (typeof(target[key]) !== 'object') {
target[key] = { };
target[key] = {};
}

deepMerge(target[key], value);
Expand Down
12 changes: 8 additions & 4 deletions packages/@aws-cdk/cdk/test/test.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,10 @@ function withoutHash(logId: string) {
}

class CustomizableResource extends CfnResource {
public prop1: any;
public prop2: any;
public prop3: any;

constructor(scope: Construct, id: string, props?: any) {
super(scope, id, { type: 'MyResourceType', properties: props });
}
Expand All @@ -676,11 +680,11 @@ class CustomizableResource extends CfnResource {
this.untypedPropertyOverrides[key] = value;
}

public renderProperties(properties: any) {
public renderProperties() {
return {
PROP1: properties.prop1,
PROP2: properties.prop2,
PROP3: properties.prop3
PROP1: this.prop1,
PROP2: this.prop2,
PROP3: this.prop3
};
}
}
14 changes: 3 additions & 11 deletions tools/awslint/lib/rules/cfn-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ export class CfnResourceReflection {

this.namespace = fullname.split('::').slice(0, 2).join('::');

// special-case
const basename = this.basename
.replace(/VPC/g, 'Vpc')
.replace(/DB/g, 'Db');

this.attributePrefix = basename[0].toLowerCase() + basename.slice(1);
this.attributePrefix = 'attr';

this.attributeNames = cls.ownProperties
.filter(p => (p.docs.docs.custom || {}).cloudformationAttribute)
Expand All @@ -89,9 +84,6 @@ export class CfnResourceReflection {
return 'securityGroupId';
}

const cfnName = name.startsWith(this.basename) ? name.slice(this.basename.length) : name;

// if the CFN attribute name already have the type name as a prefix (i.e. RoleId), we only take the "Id" as the "name".
return this.attributePrefix + camelcase(cfnName, { pascalCase: true });
return this.attributePrefix + camelcase(name, { pascalCase: true });
}
}
}
2 changes: 1 addition & 1 deletion tools/awslint/lib/rules/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,4 @@ constructLinter.add({
e.assert(property.docs.docs.default !== undefined, `${e.ctx.propsFqn}.${property.name}`);
}
}
});
});
14 changes: 10 additions & 4 deletions tools/awslint/lib/rules/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,16 @@ export class ResourceReflection {
private findAttributeProperties(): Attribute[] {
const result = new Array<Attribute>();

const attrPrefix = this.basename.charAt(0).toLowerCase() + this.basename.slice(1);
for (const p of this.construct.classType.allProperties) {
if (p.protected) {
continue; // skip any protected properties
}

// an attribute property is a property which starts with the type name
// (e.g. "bucketXxx") and/or has an @attribute doc tag.
// an attribute property is a property which starts with `attr`
// and/or has an @attribute doc tag.
const tag = getDocTag(p, 'attribute');
if (!p.name.startsWith(this.cfn.attributePrefix) && !tag) {
if (!p.name.startsWith(attrPrefix) && !tag) {
continue;
}

Expand Down Expand Up @@ -152,7 +153,12 @@ resourceLinter.add({
message: 'resources must represent all cloudformation attributes as attribute properties. missing property: ',
eval: e => {
for (const name of e.ctx.cfn.attributeNames) {
const found = e.ctx.attributes.find(a => a.names.includes(name));
const basename = e.ctx.basename.charAt(0).toLowerCase() + e.ctx.basename.slice(1);
const stripAttr = name.replace('attr', '');
const lookup = stripAttr.toLowerCase().startsWith(basename) ?
stripAttr.charAt(0).toLowerCase() + stripAttr.slice(1) :
basename + stripAttr;
const found = e.ctx.attributes.find(a => a.names.includes(lookup));
e.assert(found, `${e.ctx.fqn}.${name}`, name);
}
}
Expand Down
43 changes: 14 additions & 29 deletions tools/cfn2ts/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ export default class CodeGenerator {
resource: genspec.CodeName,
propertiesSpec: { [name: string]: schema.Property },
container: Container): Dictionary<string> {

const propertyMap: Dictionary<string> = {};

Object.keys(propertiesSpec).sort(propertyComparator).forEach(propName => {
Expand Down Expand Up @@ -250,8 +249,9 @@ export default class CodeGenerator {
}

// set class properties to match CloudFormation Properties spec
let propMap;
if (propsType) {
this.emitPropsTypeProperties(resourceName, spec.Properties!, Container.Class);
propMap = this.emitPropsTypeProperties(resourceName, spec.Properties!, Container.Class);
}

//
Expand Down Expand Up @@ -318,9 +318,10 @@ export default class CodeGenerator {
// propertyOverrides
//

if (propsType) {
/// come backe here to finish
if (propsType && propMap) {
this.code.line();
this.emitCloudFormationPropertiesOverride(propsType);
this.emitCloudFormationProperties(propsType, propMap);
}

this.closeClass(resourceName);
Expand All @@ -333,12 +334,13 @@ export default class CodeGenerator {
*
* Since resolve() deep-resolves, we only need to do this once.
*/
private emitCloudFormationPropertiesOverride(propsType: genspec.CodeName) {
this.code.openBlock(`public get propertyOverrides(): ${propsType.className}`);
this.code.line(`return this.untypedPropertyOverrides;`);
this.code.closeBlock();

this.code.openBlock('protected renderProperties(properties: any): { [key: string]: any } ');
private emitCloudFormationProperties(propsType: genspec.CodeName, propMap: Dictionary<string>) {
this.code.openBlock('protected renderProperties(): { [key: string]: any } ');
this.code.indent('const properties = {');
for (const prop of Object.values(propMap)) {
this.code.line(`${prop}: this.${prop},`);
}
this.code.unindent('};');
this.code.line(`return ${genspec.cfnMapperName(propsType).fqn}(properties);`);
this.code.closeBlock();
}
Expand Down Expand Up @@ -521,13 +523,13 @@ export default class CodeGenerator {
const line = `: ${this.findNativeType(props.context, props.spec, props.propName)};`;

if (props.container === Container.Interface) {
this.code.line(`readonly ${javascriptPropertyName}${question}${line}`);
this.code.line(`readonly ${javascriptPropertyName}${question}${line}`);
}
if (props.container === Container.Class) {
if (props.propName === 'Tags' && schema.isTagProperty(props.spec)) {
this.code.line(`public readonly tags: ${TAG_MANAGER};`);
} else {
this.code.line(`public ${javascriptPropertyName}${line}`);
this.code.line(`public ${javascriptPropertyName}${line}`);
}
}
return javascriptPropertyName;
Expand Down Expand Up @@ -731,20 +733,3 @@ interface EmitPropertyProps {
additionalDocs: string;
container: Container;
}

// remove export
// export class PropertyGenerator {
//
// private readonly resource: genspec.CodeName;
// private readonly spec: schema.ResourceType;
//
// constructor(resourceContext: genspec.CodeName, spec: schema.ResourceType) {
// this.resource = resourceContext;
// this.spec = spec;
// }
//
// // const conversionTable = this.emitPropsTypeProperties(resourceContext, spec.Properties);
// public hasProperties(): boolean {
// return this.spec.Properties ? Object.keys(this.spec.Properties).length > 0 : false;
// }
// }

0 comments on commit f823eb2

Please sign in to comment.