//--- Constructors ---
        static ResourceMapping()
        {
            // read short-hand for IAM mappings from embedded resource
            var assembly = typeof(ResourceMapping).Assembly;

            using (var iamResource = assembly.GetManifestResourceStream("LambdaSharp.Tool.Resources.IAM-Mappings.yml"))
                using (var reader = new StreamReader(iamResource, Encoding.UTF8)) {
                    var deserializer = new DeserializerBuilder()
                                       .WithNamingConvention(new NullNamingConvention())
                                       .Build();
                    _iamMappings = deserializer.Deserialize <IDictionary <string, IDictionary <string, IList <string> > > >(reader);
                }

            // create list of natively supported CloudFormation types
            _cloudFormationParameterTypes = new HashSet <string> {
                "String",
                "Number",
                "List<Number>",
                "CommaDelimitedList",
                "AWS::SSM::Parameter::Name",
                "AWS::SSM::Parameter::Value<String>",
                "AWS::SSM::Parameter::Value<List<String>>",
                "AWS::SSM::Parameter::Value<CommaDelimitedList>"
            };
            var awsTypes = new[] {
                "AWS::EC2::AvailabilityZone::Name",
                "AWS::EC2::Image::Id",
                "AWS::EC2::Instance::Id",
                "AWS::EC2::KeyPair::KeyName",
                "AWS::EC2::SecurityGroup::GroupName",
                "AWS::EC2::SecurityGroup::Id",
                "AWS::EC2::Subnet::Id",
                "AWS::EC2::Volume::Id",
                "AWS::EC2::VPC::Id",
                "AWS::Route53::HostedZone::Id"
            };

            foreach (var awsType in awsTypes)
            {
                _cloudFormationParameterTypes.Add(awsType);
                _cloudFormationParameterTypes.Add($"List<{awsType}>");
                _cloudFormationParameterTypes.Add($"AWS::SSM::Parameter::Value<{awsType}>");
                _cloudFormationParameterTypes.Add($"AWS::SSM::Parameter::Value<List<{awsType}>>");
            }

            // read CloudFormation specification
            using (var specResource = assembly.GetManifestResourceStream("LambdaSharp.Tool.Resources.CloudFormationResourceSpecification.json.gz"))
                using (var specGzipStream = new GZipStream(specResource, CompressionMode.Decompress))
                    using (var specReader = new StreamReader(specGzipStream)) {
                        CloudformationSpec = JsonConvert.DeserializeObject <CloudFormationSpec>(specReader.ReadToEnd());
                    }
        }
示例#2
0
        //--- Constructors ---
        static ResourceMapping()
        {
            // read short-hand for IAM mappings from embedded resource
            var assembly = typeof(ResourceMapping).Assembly;

            using (var iamResource = assembly.GetManifestResourceStream("LambdaSharp.Tool.Resources.IAM-Mappings.yml"))
                using (var reader = new StreamReader(iamResource, Encoding.UTF8)) {
                    var deserializer = new DeserializerBuilder()
                                       .WithNamingConvention(new NullNamingConvention())
                                       .Build();
                    _iamMappings = deserializer.Deserialize <IDictionary <string, IDictionary <string, IList <string> > > >(reader);
                }

            // create list of natively supported CloudFormation types
            _cloudFormationParameterTypes = new HashSet <string> {
                "String",
                "Number",
                "List<Number>",
                "CommaDelimitedList",
                "AWS::SSM::Parameter::Name",
                "AWS::SSM::Parameter::Value<String>",
                "AWS::SSM::Parameter::Value<List<String>>",
                "AWS::SSM::Parameter::Value<CommaDelimitedList>"
            };
            foreach (var awsType in new[] {
                "AWS::EC2::AvailabilityZone::Name",
                "AWS::EC2::Image::Id",
                "AWS::EC2::Instance::Id",
                "AWS::EC2::KeyPair::KeyName",
                "AWS::EC2::SecurityGroup::GroupName",
                "AWS::EC2::SecurityGroup::Id",
                "AWS::EC2::Subnet::Id",
                "AWS::EC2::Volume::Id",
                "AWS::EC2::VPC::Id",
                "AWS::Route53::HostedZone::Id"
            })
            {
                _cloudFormationParameterTypes.Add(awsType);
                _cloudFormationParameterTypes.Add($"List<{awsType}>");
                _cloudFormationParameterTypes.Add($"AWS::SSM::Parameter::Value<{awsType}>");
                _cloudFormationParameterTypes.Add($"AWS::SSM::Parameter::Value<List<{awsType}>>");
            }

            // read CloudFormation specification
            var cloudFormationSpecFile = Settings.CloudFormationResourceSpecificationCacheFilePath;

            if (File.Exists(cloudFormationSpecFile))
            {
                // read date-time of embedded resource
                DateTime embeddedDateTime;
                using (var specResourceDate = assembly.GetManifestResourceStream("LambdaSharp.Tool.Resources.CloudFormationResourceSpecification.json.gz.timestamp"))
                    using (var specReaderDate = new StreamReader(specResourceDate)) {
                        embeddedDateTime = DateTime.Parse(specReaderDate.ReadToEnd()).ToUniversalTime();
                    }

                // check if embedded spec is newer
                if (File.GetLastWriteTimeUtc(cloudFormationSpecFile) > embeddedDateTime)
                {
                    // read spec from global folder
                    CloudformationSpec = JsonConvert.DeserializeObject <CloudFormationSpec>(File.ReadAllText(cloudFormationSpecFile));
                    return;
                }
            }

            // read spec from embedded resource
            using (var specResource = assembly.GetManifestResourceStream("LambdaSharp.Tool.Resources.CloudFormationResourceSpecification.json.gz"))
                using (var specGzipStream = new GZipStream(specResource, CompressionMode.Decompress))
                    using (var specReader = new StreamReader(specGzipStream)) {
                        CloudformationSpec = JsonConvert.DeserializeObject <CloudFormationSpec>(specReader.ReadToEnd());
                    }
        }