Exemplo n.º 1
0
        private void CreateAfdRule()
        {
            try
            {
                Rule afdRule = new Rule();
                afdRule.Order      = this.Order;
                afdRule.Actions    = new List <DeliveryRuleAction>();
                afdRule.Conditions = new List <DeliveryRuleCondition>();

                if (MyInvocation.BoundParameters.ContainsKey("MatchProcessingBehavior"))
                {
                    afdRule.MatchProcessingBehavior = this.MatchProcessingBehavior;
                }

                if (this.MyInvocation.BoundParameters.ContainsKey("CacheExpirationAction"))
                {
                    DeliveryRuleCacheExpirationAction cacheExpirationAction = new DeliveryRuleCacheExpirationAction
                    {
                        Parameters = new CacheExpirationActionParameters()
                    };

                    cacheExpirationAction.Parameters.CacheBehavior = this.CacheExpirationAction.CacheBehavior;
                    cacheExpirationAction.Parameters.CacheDuration = this.CacheExpirationAction.CacheDuration;

                    afdRule.Actions.Add(cacheExpirationAction);
                }

                PSAfdRule psAfdRule = this.CdnManagementClient.Rules.Create(this.ResourceGroupName, this.ProfileName, this.RuleSetName, this.RuleName, afdRule).ToPSAfdRule();

                WriteObject(psAfdRule);
            }
            catch (AfdErrorResponseException errorResponse)
            {
                throw new PSArgumentException(errorResponse.Response.Content);
            }
        }
        private List <DeliveryRuleAction> CreateAfdRuleActions()
        {
            List <DeliveryRuleAction> afdRuleActions = new List <DeliveryRuleAction>();

            foreach (PSAfdRuleAction afdAction in this.Action)
            {
                if (afdAction is PSAfdRuleCacheExpirationAction)
                {
                    PSAfdRuleCacheExpirationAction psCacheExpirationAction = (PSAfdRuleCacheExpirationAction)afdAction;

                    DeliveryRuleCacheExpirationAction cacheExpirationAction = new DeliveryRuleCacheExpirationAction
                    {
                        Parameters = new CacheExpirationActionParameters
                        {
                            CacheBehavior = psCacheExpirationAction.CacheBehavior,
                            CacheDuration = psCacheExpirationAction.CacheDuration
                        }
                    };

                    afdRuleActions.Add(cacheExpirationAction);
                }

                if (afdAction is PSAfdRuleHeaderAction)
                {
                    PSAfdRuleHeaderAction psHeaderAction = (PSAfdRuleHeaderAction)afdAction;

                    if (psHeaderAction.HeaderType == "ModifyRequestHeader")
                    {
                        DeliveryRuleRequestHeaderAction requestHeaderAction = new DeliveryRuleRequestHeaderAction
                        {
                            Parameters = new HeaderActionParameters
                            {
                                HeaderAction = psHeaderAction.HeaderAction,
                                HeaderName   = psHeaderAction.HeaderName,
                                Value        = psHeaderAction.HeaderValue
                            }
                        };

                        afdRuleActions.Add(requestHeaderAction);
                    }
                    else if (psHeaderAction.HeaderType == "ModifyResponeHeader")
                    {
                        DeliveryRuleResponseHeaderAction responseHeaderAction = new DeliveryRuleResponseHeaderAction
                        {
                            Parameters = new HeaderActionParameters
                            {
                                HeaderAction = psHeaderAction.HeaderAction,
                                HeaderName   = psHeaderAction.HeaderName,
                                Value        = psHeaderAction.HeaderValue
                            }
                        };

                        afdRuleActions.Add(responseHeaderAction);
                    }
                }

                if (afdAction is PSAfdRuleCacheKeyQueryStringAction)
                {
                    PSAfdRuleCacheKeyQueryStringAction psCacheKeyQueryString = (PSAfdRuleCacheKeyQueryStringAction)afdAction;

                    DeliveryRuleCacheKeyQueryStringAction cacheKeyQueryStringAction = new DeliveryRuleCacheKeyQueryStringAction
                    {
                        Parameters = new CacheKeyQueryStringActionParameters
                        {
                            QueryParameters     = psCacheKeyQueryString.QueryParameters,
                            QueryStringBehavior = psCacheKeyQueryString.QueryStringBehavior
                        }
                    };

                    afdRuleActions.Add(cacheKeyQueryStringAction);
                }

                if (afdAction is PSAfdRuleUrlRedirectAction)
                {
                    PSAfdRuleUrlRedirectAction psUrlRedirectAction = (PSAfdRuleUrlRedirectAction)afdAction;

                    UrlRedirectAction urlRedirectAction = new UrlRedirectAction
                    {
                        Parameters = new UrlRedirectActionParameters
                        {
                            CustomFragment      = psUrlRedirectAction.CustomFragment,
                            CustomHostname      = psUrlRedirectAction.CustomHostname,
                            CustomPath          = psUrlRedirectAction.CustomPath,
                            CustomQueryString   = psUrlRedirectAction.CustomQueryString,
                            DestinationProtocol = psUrlRedirectAction.DestinationProtocol,
                            RedirectType        = psUrlRedirectAction.RedirectType
                        }
                    };

                    afdRuleActions.Add(urlRedirectAction);
                }

                if (afdAction is PSAfdRuleUrlRewriteAction)
                {
                    PSAfdRuleUrlRewriteAction psUrlRewriteAction = (PSAfdRuleUrlRewriteAction)afdAction;

                    UrlRewriteAction urlRewriteAction = new UrlRewriteAction
                    {
                        Parameters = new UrlRewriteActionParameters
                        {
                            SourcePattern         = psUrlRewriteAction.SourcePattern,
                            Destination           = psUrlRewriteAction.Destination,
                            PreserveUnmatchedPath = psUrlRewriteAction.PreservePath
                        }
                    };

                    afdRuleActions.Add(urlRewriteAction);
                }

                if (afdAction is PSAfdRuleOriginGroupOverrideAction)
                {
                    PSAfdRuleOriginGroupOverrideAction psOriginGroupOverrideAction = (PSAfdRuleOriginGroupOverrideAction)afdAction;

                    OriginGroupOverrideAction originGroupOverrideAction = new OriginGroupOverrideAction
                    {
                        Parameters = new OriginGroupOverrideActionParameters
                        {
                            OriginGroup = new ResourceReference(psOriginGroupOverrideAction.OriginGroup)
                        }
                    };

                    afdRuleActions.Add(originGroupOverrideAction);
                }
            }

            return(afdRuleActions);
        }