akiyoshi83
10/19/2016 - 2:48 AM

cloudfront.ts

class CloudFormation {
  createStack(version: AWSTemplateFormatVersion): CloudFormationStack {
    return new CloudFormationStack(version);
  }
}

class AWSTemplateFormatVersion {
  private version: string;
  private constructor(version: string) {
    this.version = version;
  }
  toString(): string {
    return this.version;
  }

  static V2010_09_09 = new AWSTemplateFormatVersion("2010-09-09");
  static DEFAULT = AWSTemplateFormatVersion.V2010_09_09;
}

class CloudFormationStack {
  constructor(version: AWSTemplateFormatVersion);
  constructor(option: any) {
    if (option instanceof AWSTemplateFormatVersion) {
      this.version = option;
    } else {
      this.version = option.version;
      this.description = option.description;
      this.parameters = option.parameters;
      this.resources = option.resources;
      this.mappings = option.mappings;
      this.outputs = option.outputs;
    }
  }
  version: AWSTemplateFormatVersion;
  description: string = "Empty Template"
  parameters: Parameter[] = [];
  resources: Resource[] = [];
  mappings: Mapping[] = [];
  outputs: Output[] = [];

  toJSON(): string {
    let ret: Object = {
      AWSTemplateFormatVersion: this.version.toString(),
      Description: this.description,
      Parameters: this.parameters.map(e => e.toJSON()),
      Resources: this.resources.map(e => e.toJSON()),
      Mappings: this.mappings.map(e => e.toJSON()),
      Outputs: this.outputs.map(e => e.toJSON()),
    };
    return JSON.stringify(ret);
  }
}

interface Parameter {
  toJSON(): string;
  toYAML(): string;
}
interface Resource {
  toJSON(): string;
  toYAML(): string;
}
interface Mapping {
  toJSON(): string;
  toYAML(): string;
}
interface Output {
  toJSON(): string;
  toYAML(): string;
}

class Ec2 implements Resource {
  amiId: string
  instanceType: string
  securityGroups: SecurityGroup[]
  toJSON(): string { return "" }
  toYAML(): string { return "" }
}

class SecurityGroup implements Resource {
  securityGroupId: string
  toJSON(): string { return "" }
  toYAML(): string { return "" }
}

let cf = new CloudFormation();
let stack = cf.createStack(AWSTemplateFormatVersion.V2010_09_09);
console.log(stack.toJSON());