Esempio n. 1
0
        public bool TryParseResourceProperties(
            string awsType,
            string logicalId,
            object properties,
            out object resourceArnFn,
            out object resourceParamFn,
            out Humidifier.Resource resourceTemplate
            )
        {
            var type = GetHumidifierType(awsType);

            if (type == null)
            {
                resourceArnFn    = null;
                resourceParamFn  = null;
                resourceTemplate = null;
                return(false);
            }
            if (properties == null)
            {
                resourceTemplate = (Humidifier.Resource)Activator.CreateInstance(type);
            }
            else
            {
                if (properties is IDictionary <string, object> dictionary)
                {
                    // NOTE (2018-09-05, bjorg): Humidifier appends a '_' to property names
                    //  that conflict with the typename. This mimics the behavior by doing the
                    // thing before we attempt to deserialize into the target type.
                    var typeName = type.Name;
                    if (dictionary.TryGetValue(typeName, out object value))
                    {
                        dictionary.Remove(typeName);
                        dictionary[typeName + "_"] = value;
                    }
                }
                resourceTemplate = (Humidifier.Resource)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(properties), type);
            }

            // determine how we can get the ARN for the resource, which is used when we grant IAM permissions
            switch (awsType)
            {
            case "AWS::ApplicationAutoScaling::ScalingPolicy":
            case "AWS::AutoScaling::ScalingPolicy":
            case "AWS::Batch::ComputeEnvironment":
            case "AWS::Batch::JobDefinition":
            case "AWS::Batch::JobQueue":
            case "AWS::CertificateManager::Certificate":
            case "AWS::CloudFormation::Stack":
            case "AWS::CloudFormation::WaitCondition":
            case "AWS::ECS::Service":
            case "AWS::ECS::TaskDefinition":
            case "AWS::ElasticLoadBalancingV2::Listener":
            case "AWS::ElasticLoadBalancingV2::ListenerRule":
            case "AWS::ElasticLoadBalancingV2::LoadBalancer":
            case "AWS::ElasticLoadBalancingV2::TargetGroup":
            case "AWS::IAM::ManagedPolicy":
            case "AWS::Lambda::Alias":
            case "AWS::Lambda::Version":
            case "AWS::OpsWorks::UserProfile":
            case "AWS::SNS::Topic":
            case "AWS::StepFunctions::Activity":
            case "AWS::StepFunctions::StateMachine":

                // these AWS resources return their ARN using `!Ref`
                resourceArnFn   = Fn.Ref(logicalId);
                resourceParamFn = Fn.Ref(logicalId);
                break;

            case "AWS::S3::Bucket":

                // S3 Bucket resources must be granted permissions on the bucket AND the keys
                resourceArnFn = new object[] {
                    Fn.GetAtt(logicalId, "Arn"),
                    Fn.Join("", Fn.GetAtt(logicalId, "Arn"), "/*")
                };
                resourceParamFn = Fn.Ref(logicalId);
                break;

            case "AWS::DynamoDB::Table":

                // DynamoDB resources must be granted permissions on the table AND the stream
                resourceArnFn = new object[] {
                    Fn.GetAtt(logicalId, "Arn"),
                    Fn.Join("/", Fn.GetAtt(logicalId, "Arn"), "stream", "*"),
                    Fn.Join("/", Fn.GetAtt(logicalId, "Arn"), "index", "*")
                };
                resourceParamFn = Fn.Ref(logicalId);
                break;

            default:

                // most AWS resources expose an `Arn` attribute that we need to use
                resourceArnFn   = Fn.GetAtt(logicalId, "Arn");
                resourceParamFn = Fn.Ref(logicalId);
                break;
            }
            return(true);
        }
Esempio n. 2
0
        public bool TryParseResourceProperties(
            string awsType,
            string logicalId,
            object properties,
            out object resourceArnFn,
            out object resourceParamFn,
            out Humidifier.Resource resourceTemplate
            )
        {
            var type = GetHumidifierType(awsType);

            if (type == null)
            {
                resourceArnFn    = null;
                resourceParamFn  = null;
                resourceTemplate = null;
                return(false);
            }
            if (properties == null)
            {
                resourceTemplate = (Humidifier.Resource)Activator.CreateInstance(type);
            }
            else
            {
                resourceTemplate = (Humidifier.Resource)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(properties), type);
            }

            // determine how we can get the ARN for the resource, which is used when we grant IAM permissions
            switch (awsType)
            {
            case "AWS::ApplicationAutoScaling::ScalingPolicy":
            case "AWS::AutoScaling::ScalingPolicy":
            case "AWS::Batch::ComputeEnvironment":
            case "AWS::Batch::JobDefinition":
            case "AWS::Batch::JobQueue":
            case "AWS::CertificateManager::Certificate":
            case "AWS::CloudFormation::Stack":
            case "AWS::CloudFormation::WaitCondition":
            case "AWS::ECS::Service":
            case "AWS::ECS::TaskDefinition":
            case "AWS::ElasticLoadBalancingV2::Listener":
            case "AWS::ElasticLoadBalancingV2::ListenerRule":
            case "AWS::ElasticLoadBalancingV2::LoadBalancer":
            case "AWS::ElasticLoadBalancingV2::TargetGroup":
            case "AWS::IAM::ManagedPolicy":
            case "AWS::Lambda::Alias":
            case "AWS::Lambda::Version":
            case "AWS::OpsWorks::UserProfile":
            case "AWS::SNS::Topic":
            case "AWS::StepFunctions::Activity":
            case "AWS::StepFunctions::StateMachine":

                // these AWS resources return their ARN using `Fn::Ref()`
                resourceArnFn   = Fn.Ref(logicalId);
                resourceParamFn = Fn.Ref(logicalId);
                break;

            case "AWS::S3::Bucket":

                // most AWS resources expose an `Arn` attribute that we need to use
                resourceArnFn = new object[] {
                    Fn.GetAtt(logicalId, "Arn"),
                    Fn.Join("", Fn.GetAtt(logicalId, "Arn"), "/*")
                };
                resourceParamFn = Fn.Ref(logicalId);
                break;

            default:

                // most AWS resources expose an `Arn` attribute that we need to use
                resourceArnFn   = Fn.GetAtt(logicalId, "Arn");
                resourceParamFn = Fn.Ref(logicalId);
                break;
            }
            return(true);
        }