Beispiel #1
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            // Verify if the subnet exists in the VirtualNetwork
            var peering = this.Circuit.Peerings.SingleOrDefault(resource => string.Equals(resource.Name, this.Name, System.StringComparison.CurrentCultureIgnoreCase));

            if (peering != null)
            {
                throw new ArgumentException("Peering with the specified name already exists");
            }

            peering = new PSPeering();

            peering.Name        = this.Name;
            peering.PeeringType = this.PeeringType;
            peering.PrimaryPeerAddressPrefix   = this.PrimaryPeerAddressPrefix;
            peering.SecondaryPeerAddressPrefix = this.SecondaryPeerAddressPrefix;
            peering.AzureASN = this.AzureASN;
            peering.PeerASN  = this.PeerASN;
            peering.VlanId   = this.VlanId;

            if (this.MircosoftConfigAdvertisedPublicPrefixes != null &&
                this.MircosoftConfigAdvertisedPublicPrefixes.Any())
            {
                peering.MicrosoftPeeringConfig = new PSPeeringConfig();
                peering.MicrosoftPeeringConfig.AdvertisedPublicPrefixes = this.MircosoftConfigAdvertisedPublicPrefixes;
                peering.MicrosoftPeeringConfig.CustomerASN         = this.MircosoftConfigCustomerAsn;
                peering.MicrosoftPeeringConfig.RoutingRegistryName = this.MircosoftConfigRoutingRegistryName;
            }

            this.Circuit.Peerings.Add(peering);

            WriteObject(this.Circuit);
        }
Beispiel #2
0
        /// <summary>
        /// The update exchange peering.
        /// </summary>
        /// <param name="psExchange">
        /// The ps exchange.
        /// </param>
        /// <param name="resourceGroupName">
        /// The resource group name.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="PSExchangePeeringModelView"/>.
        /// </returns>
        /// <exception cref="PSArgumentNullException"> Not valid operation
        /// </exception>
        private PSExchangePeeringModelView UpdateExchangePeering(
            PSPeering exchangePeering,
            string resourceGroupName,
            string name)
        {
            exchangePeering.Exchange.PeerAsn = new PSSubResource(exchangePeering.Exchange.PeerAsn.Id);
            var count = exchangePeering.Exchange.Connections.Count;

            if (this.ExchangeConnection != null)
            {
                for (int i = 0; i < count; i++)
                {
                    foreach (var connection in this.ExchangeConnection)
                    {
                        var connectionDifferencesCount =
                            exchangePeering.Exchange.Connections[i].DetailedCompare(connection).Count;
                        if (connectionDifferencesCount == 1)
                        {
                            exchangePeering.Exchange.Connections[i] = connection;
                            continue;
                        }
                        exchangePeering.Exchange.Connections.Add(connection);
                    }
                }
            }

            return(new PSExchangePeeringModelView(
                       this.ToPeeringPs(
                           this.PeeringClient.CreateOrUpdate(resourceGroupName, name, this.ToPeering(exchangePeering)))));

            throw new PSArgumentNullException("Exchange Connections is Null or Invalid.");
        }
Beispiel #3
0
        /// <summary>
        /// The convert classic to Exchange peering.
        /// </summary>
        /// <param name="classicPeering">
        /// The classic peering.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        private PSPeering ConvertClassicToExchangePeering(PSPeering classicPeering)
        {
            if (classicPeering == null)
            {
                throw new PSArgumentNullException(string.Format(Resources.Error_UnableToConvertLegacy, "LegacyPeering"));
            }
            if (classicPeering.Exchange.Connections == null)
            {
                throw new PSArgumentNullException(string.Format(Resources.Error_UnableToConvertLegacy, "Connection"));
            }
            var connections = classicPeering.Exchange.Connections.ToList();

            var newPeering = new PSPeering
            {
                Location        = this.GetAzureRegion(classicPeering.PeeringLocation, Constants.Exchange),
                PeeringLocation = classicPeering.PeeringLocation,
                Kind            = classicPeering.Kind ?? Constants.Exchange,
                Sku             =
                    classicPeering.Sku ?? new PSPeeringSku {
                    Name = Constants.BasicExchangeFree
                },
                Exchange = new PSPeeringPropertiesExchange
                {
                    Connections = connections,
                    PeerAsn     = new PSSubResource(this.PeerAsnResourceId)
                }
            };

            foreach (var connection in newPeering.Exchange.Connections)
            {
                connection.ConnectionIdentifier = Guid.NewGuid().ToString();
            }

            return(newPeering);
        }
Beispiel #4
0
        /// <summary>
        /// The convert classic to direct peering.
        /// </summary>
        /// <param name="classicPeering">
        /// The classic peering.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        private PSPeering ConvertClassicToDirectPeering(PSPeering classicPeering)
        {
            if (classicPeering == null)
            {
                throw new PSArgumentNullException(string.Format(Resources.Error_UnableToConvertLegacy, "LegacyPeering"));
            }
            if (classicPeering.Direct.Connections == null)
            {
                throw new PSArgumentNullException(string.Format(Resources.Error_UnableToConvertLegacy, "Connection"));
            }
            var connections = classicPeering.Direct.Connections.ToList();

            var newPeering = new PSPeering
            {
                Location        = this.GetAzureRegion(classicPeering.PeeringLocation, Constants.Direct),
                PeeringLocation = classicPeering.PeeringLocation,
                Kind            = classicPeering.Kind ?? Constants.Direct,
                Sku             = classicPeering.Sku ?? new PSPeeringSku(Constants.BasicDirectFree),
                Direct          = new PSPeeringPropertiesDirect
                {
                    Connections = connections,
                    PeerAsn     = new PSSubResource(this.PeerAsnResourceId)
                }
            };

            foreach (var connection in newPeering.Direct.Connections)
            {
                connection.ConnectionIdentifier = Guid.NewGuid().ToString();
                connection.BandwidthInMbps      = connection.ProvisionedBandwidthInMbps ?? 10000;
            }

            return(newPeering);
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            var peering = new PSPeering();

            peering.Name        = this.Name;
            peering.PeeringType = this.PeeringType;
            peering.PrimaryPeerAddressPrefix   = this.PrimaryPeerAddressPrefix;
            peering.SecondaryPeerAddressPrefix = this.SecondaryPeerAddressPrefix;
            peering.AzureASN = this.AzureASN;
            peering.PeerASN  = this.PeerASN;
            peering.VlanId   = this.VlanId;

            if (this.MircosoftConfigAdvertisedPublicPrefixes != null &&
                this.MircosoftConfigAdvertisedPublicPrefixes.Any())
            {
                peering.MicrosoftPeeringConfig = new PSPeeringConfig();
                peering.MicrosoftPeeringConfig.AdvertisedPublicPrefixes = this.MircosoftConfigAdvertisedPublicPrefixes;
                peering.MicrosoftPeeringConfig.CustomerASN         = this.MircosoftConfigCustomerAsn;
                peering.MicrosoftPeeringConfig.RoutingRegistryName = this.MircosoftConfigRoutingRegistryName;
            }

            WriteObject(peering);
        }
Beispiel #6
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            var peering = new PSPeering();

            peering.Name        = this.Name;
            peering.PeeringType = this.PeeringType;
            peering.PrimaryPeerAddressPrefix   = this.PrimaryPeerAddressPrefix;
            peering.SecondaryPeerAddressPrefix = this.SecondaryPeerAddressPrefix;
            peering.PeerASN = this.PeerASN;
            peering.VlanId  = this.VlanId;

            if (!string.IsNullOrEmpty(this.SharedKey))
            {
                peering.SharedKey = this.SharedKey;
            }

            if (this.MicrosoftConfigAdvertisedPublicPrefixes != null &&
                this.MicrosoftConfigAdvertisedPublicPrefixes.Any())
            {
                peering.MicrosoftPeeringConfig = new PSPeeringConfig();
                peering.MicrosoftPeeringConfig.AdvertisedPublicPrefixes = this.MicrosoftConfigAdvertisedPublicPrefixes;
                peering.MicrosoftPeeringConfig.CustomerASN         = this.MicrosoftConfigCustomerAsn;
                peering.MicrosoftPeeringConfig.RoutingRegistryName = this.MicrosoftConfigRoutingRegistryName;
            }

            WriteObject(peering);
        }
Beispiel #7
0
        /// <summary>
        /// The put new InputObject.
        /// </summary>
        /// <param name="newPeering">
        /// The new InputObject.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        private object PutNewPeering(PSPeering newPeering)
        {
            var peering = PeeringResourceManagerProfile.Mapper.Map <PeeringModel>(newPeering);

            this.PeeringClient.CreateOrUpdate(this.ResourceGroupName, this.Name, peering);
            return(PeeringResourceManagerProfile.Mapper.Map <PSPeering>(
                       this.PeeringClient.Get(this.ResourceGroupName, this.Name)));
        }
        /// <summary>
        /// The create exchange peering.
        /// </summary>
        /// <returns>
        /// The <see cref="PSPeering"/>.
        /// </returns>
        /// <exception cref="PSArgumentNullException">
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// </exception>
        /// <exception cref="Exception">
        /// </exception>
        /// <exception cref="ArmErrorException">
        /// </exception>
        /// <exception cref="HttpRequestException">
        /// </exception>
        private PSPeering CreateExchangePeering()
        {
            var peeringRequest =
                new PSPeering(
                    name: this.Name,
                    location: this.GetAzureRegion(this.PeeringLocation, Constants.Exchange),
                    sku: new PSPeeringSku {
                Name = Constants.BasicExchangeFree
            },
                    kind: Constants.Exchange)
            {
                PeeringLocation = this.PeeringLocation,
                Exchange        = new PSPeeringPropertiesExchange
                {
                    Connections = new List <PSExchangeConnection>(),
                    PeerAsn     = new PSSubResource(this.PeerAsnResourceId)
                },
                Tags = TagsConversionHelper.CreateTagDictionary(this.Tag, true),
            };

            if (this.ExchangeConnection == null)
            {
                throw new PSArgumentNullException(
                          string.Format(Resources.Create_NewConnectionObject, Constants.Exchange));
            }

            foreach (PSExchangeConnection psExchangeConnection in this.ExchangeConnection)
            {
                if (this.IsValidConnection(psExchangeConnection))
                {
                    peeringRequest.Exchange.Connections.Add(psExchangeConnection);
                }
            }

            if (this.ParameterSetName.Equals(Constants.Exchange))
            {
                if (peeringRequest.Exchange?.Connections.Count <= 0)
                {
                    throw new PSArgumentException(string.Format(Resources.Error_InvalidConnection, "Exchange Connection is null."));
                }
                if (peeringRequest.Direct != null)
                {
                    peeringRequest.Direct = null;
                }

                try
                {
                    return((PSPeering)this.PutNewPeering(peeringRequest));
                }
                catch (ErrorResponseException ex)
                {
                    var error = this.GetErrorCodeAndMessageFromArmOrErm(ex);
                    throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
                }
            }
            throw new PSInvalidOperationException(string.Format(Resources.Error_GenericSyntax));
        }
Beispiel #9
0
 public static bool IsIpv4PrivatePeeringNull(PSPeering privatePeering)
 {
     if (privatePeering == null ||
         (privatePeering.PrimaryPeerAddressPrefix == null &&
          privatePeering.SecondaryPeerAddressPrefix == null))
     {
         return(true);
     }
     return(false);
 }
 public void SetIpv4PeeringParameters(PSPeering peering)
 {
     peering.PrimaryPeerAddressPrefix   = this.PrimaryPeerAddressPrefix;
     peering.SecondaryPeerAddressPrefix = this.SecondaryPeerAddressPrefix;
     if (!string.IsNullOrEmpty(this.RouteFilterId))
     {
         peering.RouteFilter    = new PSRouteFilter();
         peering.RouteFilter.Id = this.RouteFilterId;
     }
 }
        public override void Execute()
        {
            base.Execute();
            // Verify if the subnet exists in the VirtualNetwork
            var peering = this.ExpressRouteCircuit.Peerings.SingleOrDefault(resource => string.Equals(resource.Name, this.Name, System.StringComparison.CurrentCultureIgnoreCase));

            if (peering != null)
            {
                throw new ArgumentException("Peering with the specified name already exists");
            }


            if (string.Equals(ParameterSetName, Microsoft.Azure.Commands.Network.Properties.Resources.SetByResource))
            {
                if (this.RouteFilter != null)
                {
                    this.RouteFilterId = this.RouteFilter.Id;
                }
            }

            peering = new PSPeering();

            peering.Name        = this.Name;
            peering.PeeringType = this.PeeringType;
            peering.PrimaryPeerAddressPrefix   = this.PrimaryPeerAddressPrefix;
            peering.SecondaryPeerAddressPrefix = this.SecondaryPeerAddressPrefix;
            peering.PeerASN = this.PeerASN;
            peering.VlanId  = this.VlanId;


            if (!string.IsNullOrEmpty(this.SharedKey))
            {
                peering.SharedKey = this.SharedKey;
            }

            if (this.MicrosoftConfigAdvertisedPublicPrefixes != null &&
                this.MicrosoftConfigAdvertisedPublicPrefixes.Any())
            {
                peering.MicrosoftPeeringConfig = new PSPeeringConfig();
                peering.MicrosoftPeeringConfig.AdvertisedPublicPrefixes = this.MicrosoftConfigAdvertisedPublicPrefixes;
                peering.MicrosoftPeeringConfig.CustomerASN         = this.MicrosoftConfigCustomerAsn;
                peering.MicrosoftPeeringConfig.RoutingRegistryName = this.MicrosoftConfigRoutingRegistryName;
                peering.MicrosoftPeeringConfig.LegacyMode          = Convert.ToInt32(this.LegacyMode);
            }

            if (!string.IsNullOrEmpty(this.RouteFilterId))
            {
                peering.RouteFilter    = new PSRouteFilter();
                peering.RouteFilter.Id = this.RouteFilterId;
            }

            this.ExpressRouteCircuit.Peerings.Add(peering);

            WriteObject(this.ExpressRouteCircuit);
        }
        public override void Execute()
        {
            base.Execute();

            if (string.Equals(ParameterSetName, Microsoft.Azure.Commands.Network.Properties.Resources.SetByResource))
            {
                if (this.RouteFilter != null)
                {
                    this.RouteFilterId = this.RouteFilter.Id;
                }
            }

            var peering = new PSPeering();

            peering.Name        = this.Name;
            peering.PeeringType = this.PeeringType;
            peering.PeerASN     = this.PeerASN;
            peering.VlanId      = this.VlanId;

            if (!string.IsNullOrEmpty(this.SharedKey))
            {
                peering.SharedKey = this.SharedKey;
            }

            if (this.PeerAddressType == IPv6)
            {
                peering.Ipv6PeeringConfig = new PSIpv6PeeringConfig();
                peering.Ipv6PeeringConfig.PrimaryPeerAddressPrefix   = this.PrimaryPeerAddressPrefix;
                peering.Ipv6PeeringConfig.SecondaryPeerAddressPrefix = this.SecondaryPeerAddressPrefix;
                if (!string.IsNullOrEmpty(this.RouteFilterId))
                {
                    peering.Ipv6PeeringConfig.RouteFilter    = new PSRouteFilter();
                    peering.Ipv6PeeringConfig.RouteFilter.Id = this.RouteFilterId;
                }
            }
            else
            {
                // Set IPv4 config even if no PeerAddresType has been specified for backward compatibility
                peering.PrimaryPeerAddressPrefix   = this.PrimaryPeerAddressPrefix;
                peering.SecondaryPeerAddressPrefix = this.SecondaryPeerAddressPrefix;
                if (!string.IsNullOrEmpty(this.RouteFilterId))
                {
                    peering.RouteFilter    = new PSRouteFilter();
                    peering.RouteFilter.Id = this.RouteFilterId;
                }
            }

            this.ConstructMicrosoftConfig(peering);

            WriteObject(peering);
        }
Beispiel #13
0
        /// <summary>
        /// The input update exchange.
        /// </summary>
        /// <returns>
        /// The <see cref="PSExchangePeeringModelView"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException"> Only one value can be changed.
        /// </exception>
        /// <exception cref="ErrorResponseException"> Http request failed.
        /// </exception>
        private PSExchangePeeringModelView InputUpdateExchange()
        {
            try
            {
                if (this.InputObject is PSExchangePeeringModelView exchangePeeringModelView)
                {
                    var resourceId        = new ResourceIdentifier(exchangePeeringModelView.Id);
                    var resourceGroupName = resourceId.ResourceGroupName;
                    var peeringName       = resourceId.ResourceName;
                    var peering           = new PSPeering
                    {
                        Kind            = exchangePeeringModelView.Kind,
                        PeeringLocation = exchangePeeringModelView.PeeringLocation,
                        Location        = exchangePeeringModelView.Location,
                        Sku             = exchangePeeringModelView.Sku,
                        Tags            = exchangePeeringModelView.Tags,
                        Exchange        = new PSPeeringPropertiesExchange
                        {
                            Connections = exchangePeeringModelView.Connections,
                            PeerAsn     = new PSSubResource(
                                exchangePeeringModelView.PeerAsn.Id),
                        }
                    };

                    if (this.ExchangeConnection != null)
                    {
                        this.ExchangeConnection.ToList().ForEach(x => peering.Exchange.Connections.Add(x));
                    }

                    this.WriteVerbose($"Updating:{peeringName} for ResourceGroup:{resourceGroupName}");
                    this.PeeringClient.CreateOrUpdate(
                        resourceGroupName.ToString(),
                        peeringName.ToString(),
                        this.ToPeering(peering));
                    return(new PSExchangePeeringModelView(
                               this.ToPeeringPs(this.PeeringClient.Get(resourceGroupName.ToString(), peeringName.ToString()))));
                }
            }
            catch (InvalidOperationException mapException)
            {
                throw new InvalidOperationException(String.Format(Resources.Error_Mapping, mapException));
            }
            catch (ErrorResponseException ex)
            {
                var error = GetErrorCodeAndMessageFromArmOrErm(ex);
                throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
            }

            throw new InvalidOperationException("Check the input parameters.");
        }
Beispiel #14
0
        /// <summary>
        /// The get and update direct peering.
        /// </summary>
        /// <param name="directPeering">
        /// The direct peering.
        /// </param>
        /// <returns>
        /// The <see cref="PSDirectPeeringModelView"/>.
        /// </returns>
        private PSDirectPeeringModelView UpdateDirectPeering(
            PSPeering directPeering,
            string resourceGroupName,
            string name)
        {
            if (this.UseForPeeringService != null)
            {
                directPeering.Direct.UseForPeeringService = this.UseForPeeringService;
            }

            directPeering.Direct.PeerAsn = new PSSubResource(directPeering.Direct.PeerAsn.Id);
            if (this.DirectConnection != null)
            {
                for (int i = 0; i < directPeering.Direct.Connections.Count; i++)
                {
                    foreach (var connection in this.DirectConnection)
                    {
                        var connectionDifferencesCount =
                            directPeering.Direct.Connections[i].DetailedCompare(connection).Count;
                        if (connectionDifferencesCount == 0)
                        {
                            continue;
                        }

                        if (connectionDifferencesCount == 1)
                        {
                            directPeering.Direct.Connections[i] = connection;
                            continue;
                        }

                        if (connectionDifferencesCount <= 1)
                        {
                            continue;
                        }
                        if (!directPeering.Direct.Connections.Contains(connection))
                        {
                            directPeering.Direct.Connections.Add(connection);
                        }
                    }
                }
            }

            return(new PSDirectPeeringModelView(
                       this.ToPeeringPs(
                           this.PeeringClient.CreateOrUpdate(
                               resourceGroupName,
                               name,
                               this.ToPeering(directPeering)))));
        }
Beispiel #15
0
        public override void Execute()
        {
            base.Execute();
            // Verify if the subnet exists in the VirtualNetwork
            var peering = this.ExpressRouteCircuit.Peerings.SingleOrDefault(resource => string.Equals(resource.Name, this.Name, System.StringComparison.CurrentCultureIgnoreCase));

            if (peering != null)
            {
                throw new ArgumentException("Peering with the specified name already exists");
            }


            if (string.Equals(ParameterSetName, ParamSetByRouteFilter))
            {
                if (this.RouteFilter != null)
                {
                    this.RouteFilterId = this.RouteFilter.Id;
                }
            }

            peering = new PSPeering();

            peering.Name        = this.Name;
            peering.PeeringType = this.PeeringType;
            peering.PeerASN     = this.PeerASN;
            peering.VlanId      = this.VlanId;


            if (!string.IsNullOrEmpty(this.SharedKey))
            {
                peering.SharedKey = this.SharedKey;
            }

            if (this.PeerAddressType == IPv6)
            {
                this.SetIpv6PeeringParameters(peering);
            }
            else
            {
                // Set IPv4 config even if no PeerAddresType has been specified for backward compatibility
                this.SetIpv4PeeringParameters(peering);
            }

            this.ConstructMicrosoftConfig(peering);

            this.ExpressRouteCircuit.Peerings.Add(peering);

            WriteObject(this.ExpressRouteCircuit);
        }
Beispiel #16
0
        /// <summary>
        /// The update exchange peering.
        /// </summary>
        /// <param name="psExchange">
        /// The ps exchange.
        /// </param>
        /// <param name="resourceGroupName">
        /// The resource group name.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="PSExchangePeeringModelView"/>.
        /// </returns>
        /// <exception cref="PSArgumentNullException"> Not valid operation
        /// </exception>
        private PSExchangePeeringModelView UpdateExchangePeering(
            PSPeering exchangePeering,
            string resourceGroupName,
            string name)
        {
            exchangePeering.Exchange.PeerAsn = new PSSubResource(exchangePeering.Exchange.PeerAsn.Id);
            if (this.ExchangeConnection != null)
            {
                this.ExchangeConnection.ToList().ForEach(x => exchangePeering.Exchange.Connections.Add(x));
            }

            return(new PSExchangePeeringModelView(
                       this.ToPeeringPs(
                           this.PeeringClient.CreateOrUpdate(resourceGroupName, name, this.ToPeering(exchangePeering)))));

            throw new PSArgumentNullException("Exchange Connections is Null or Invalid.");
        }
        /// <summary>
        /// The get and update direct peering.
        /// </summary>
        /// <param name="directPeering">
        /// The direct peering.
        /// </param>
        /// <returns>
        /// The <see cref="PSDirectPeeringModelView"/>.
        /// </returns>
        private PSDirectPeeringModelView UpdateDirectPeering(
            PSPeering directPeering,
            string resourceGroupName,
            string name)
        {
            directPeering.Direct.PeerAsn = new PSSubResource(directPeering.Direct.PeerAsn.Id);
            if (this.DirectConnection != null)
            {
                this.DirectConnection.ToList().ForEach(x => directPeering.Direct.Connections.Add(x));
            }

            return(new PSDirectPeeringModelView(
                       this.ToPeeringPs(
                           this.PeeringClient.CreateOrUpdate(
                               resourceGroupName,
                               name,
                               this.ToPeering(directPeering)))));
        }
Beispiel #18
0
        public override void Execute()
        {
            base.Execute();

            if (string.Equals(ParameterSetName, Microsoft.Azure.Commands.Network.Properties.Resources.SetByResource))
            {
                if (this.RouteFilter != null)
                {
                    this.RouteFilterId = this.RouteFilter.Id;
                }
            }

            var peering = new PSPeering();

            peering.Name        = this.Name;
            peering.PeeringType = this.PeeringType;
            peering.PrimaryPeerAddressPrefix   = this.PrimaryPeerAddressPrefix;
            peering.SecondaryPeerAddressPrefix = this.SecondaryPeerAddressPrefix;
            peering.PeerASN = this.PeerASN;
            peering.VlanId  = this.VlanId;

            if (!string.IsNullOrEmpty(this.SharedKey))
            {
                peering.SharedKey = this.SharedKey;
            }

            if (this.MicrosoftConfigAdvertisedPublicPrefixes != null &&
                this.MicrosoftConfigAdvertisedPublicPrefixes.Any())
            {
                peering.MicrosoftPeeringConfig = new PSPeeringConfig();
                peering.MicrosoftPeeringConfig.AdvertisedPublicPrefixes = this.MicrosoftConfigAdvertisedPublicPrefixes;
                peering.MicrosoftPeeringConfig.CustomerASN         = this.MicrosoftConfigCustomerAsn;
                peering.MicrosoftPeeringConfig.RoutingRegistryName = this.MicrosoftConfigRoutingRegistryName;
                peering.MicrosoftPeeringConfig.LegacyMode          = Convert.ToInt32(this.LegacyMode);
            }

            if (!string.IsNullOrEmpty(this.RouteFilterId))
            {
                peering.RouteFilter    = new PSRouteFilter();
                peering.RouteFilter.Id = this.RouteFilterId;
            }

            WriteObject(peering);
        }
        /// <summary>
        /// The convert classic to Exchange peering.
        /// </summary>
        /// The classic peering.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        private PSPeering ConvertClassicToExchangePeering(PSExchangePeeringModelView peering)
        {
            if (peering == null)
            {
                throw new PSArgumentNullException(string.Format(Resources.Error_UnableToConvertLegacy, "LegacyPeering"));
            }
            if (peering.Connections == null)
            {
                throw new PSArgumentNullException(string.Format(Resources.Error_UnableToConvertLegacy, "Connection"));
            }

            var connections = peering.Connections.ToList();

            if (this.ExchangeConnection != null)
            {
                this.ExchangeConnection.ToList().ForEach(x => connections.Add(x));
            }

            var newPeering = new PSPeering
            {
                Location        = this.GetAzureRegion(peering.PeeringLocation, Constants.Exchange),
                PeeringLocation = peering.PeeringLocation,
                Kind            = peering.Kind ?? Constants.Exchange,
                Sku             = peering.Sku ?? new PSPeeringSku {
                    Name = Constants.BasicExchangeFree
                },
                Exchange = new PSPeeringPropertiesExchange
                {
                    Connections = connections,
                    PeerAsn     = new PSSubResource(this.PeerAsnResourceId)
                },
                Tags = TagsConversionHelper.CreateTagDictionary(this.Tag, true),
            };

            try
            {
                return((PSPeering)this.PutNewPeering(newPeering));
            }
            catch (ErrorResponseException ex)
            {
                var error = this.GetErrorCodeAndMessageFromArmOrErm(ex);
                throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
            }
        }
Beispiel #20
0
        /// <summary>
        /// The input update exchange.
        /// </summary>
        /// <returns>
        /// The <see cref="PSExchangePeeringModelView"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException"> Only one value can be changed.
        /// </exception>
        /// <exception cref="ErrorResponseException"> Http request failed.
        /// </exception>
        private PSExchangePeeringModelView InputUpdateExchange()
        {
            try
            {
                if (this.InputObject is PSExchangePeeringModelView exchangePeeringModelView)
                {
                    var resourceId        = new ResourceIdentifier(exchangePeeringModelView.Id);
                    var resourceGroupName = resourceId.ResourceGroupName;
                    var peeringName       = resourceId.ResourceName;
                    var peering           = new PSPeering
                    {
                        Kind            = exchangePeeringModelView.Kind,
                        PeeringLocation = exchangePeeringModelView.PeeringLocation,
                        Location        = exchangePeeringModelView.Location,
                        Sku             = exchangePeeringModelView.Sku,
                        Tags            = exchangePeeringModelView.Tags,
                        Exchange        = new PSPeeringPropertiesExchange
                        {
                            Connections = exchangePeeringModelView.Connections,
                            PeerAsn     = new PSSubResource(
                                exchangePeeringModelView.PeerAsn.Id),
                        }
                    };
                    this.PeeringClient.CreateOrUpdate(
                        resourceGroupName.ToString(),
                        peeringName.ToString(),
                        this.ToPeering(peering));
                    return(new PSExchangePeeringModelView(
                               this.ToPeeringPs(this.PeeringClient.Get(resourceGroupName.ToString(), peeringName.ToString()))));
                }
            }
            catch (InvalidOperationException mapException)
            {
                throw new InvalidOperationException(String.Format(Resources.Error_Mapping, mapException));
            }
            catch (ErrorResponseException ex)
            {
                var error = ex.Response.Content.Contains("\"error\\\":") ? JsonConvert.DeserializeObject <Dictionary <string, ErrorResponse> >(JsonConvert.DeserializeObject(ex.Response.Content).ToString()).FirstOrDefault().Value : JsonConvert.DeserializeObject <ErrorResponse>(ex.Response.Content);
                throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
            }

            throw new InvalidOperationException("Check the input parameters.");
        }
        /// <summary>
        /// The convert classic to direct peering.
        /// </summary>
        /// <param name="this.LegacyPeering">
        /// The classic peering.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        private PSPeering ConvertClassicToDirectPeering(PSDirectPeeringModelView peering)
        {
            if (peering == null)
            {
                throw new PSArgumentNullException(string.Format(Resources.Error_UnableToConvertLegacy, "LegacyPeering"));
            }
            if (peering.Connections == null)
            {
                throw new PSArgumentNullException(string.Format(Resources.Error_UnableToConvertLegacy, "Connection"));
            }
            var connections = peering.Connections.ToList();
            var newPeering  = new PSPeering
            {
                Location        = this.GetAzureRegion(peering.PeeringLocation, Constants.Direct),
                PeeringLocation = peering.PeeringLocation,
                Kind            = peering.Kind ?? Constants.Direct,
                Sku             = peering.Sku ?? new PSPeeringSku(Constants.BasicDirectFree),
                Direct          = new PSPeeringPropertiesDirect
                {
                    Connections = connections,
                    PeerAsn     = new PSSubResource(this.PeerAsnResourceId)
                },
                Tags = TagsConversionHelper.CreateTagDictionary(this.Tag, true),
            };

            foreach (var connection in newPeering.Direct.Connections)
            {
                connection.ConnectionIdentifier = Guid.NewGuid().ToString();
                connection.BandwidthInMbps      = connection.ProvisionedBandwidthInMbps ?? 10000;
            }

            try
            {
                return((PSPeering)this.PutNewPeering(newPeering));
            }
            catch (ErrorResponseException ex)
            {
                var error = this.GetErrorCodeAndMessageFromArmOrErm(ex);
                throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
            }
        }
 public void ConstructMicrosoftConfig(PSPeering peering)
 {
     if (this.MicrosoftConfigAdvertisedPublicPrefixes != null && this.MicrosoftConfigAdvertisedPublicPrefixes.Any())
     {
         if (PeerAddressType == IPv6)
         {
             peering.Ipv6PeeringConfig.MicrosoftPeeringConfig = new PSPeeringConfig();
             peering.Ipv6PeeringConfig.MicrosoftPeeringConfig.AdvertisedPublicPrefixes = this.MicrosoftConfigAdvertisedPublicPrefixes;
             peering.Ipv6PeeringConfig.MicrosoftPeeringConfig.CustomerASN         = this.MicrosoftConfigCustomerAsn;
             peering.Ipv6PeeringConfig.MicrosoftPeeringConfig.RoutingRegistryName = this.MicrosoftConfigRoutingRegistryName;
         }
         else
         {
             // Set IPv4 config even if no PeerAddresType has been specified for backward compatibility
             peering.MicrosoftPeeringConfig = new PSPeeringConfig();
             peering.MicrosoftPeeringConfig.AdvertisedPublicPrefixes = this.MicrosoftConfigAdvertisedPublicPrefixes;
             peering.MicrosoftPeeringConfig.CustomerASN         = this.MicrosoftConfigCustomerAsn;
             peering.MicrosoftPeeringConfig.RoutingRegistryName = this.MicrosoftConfigRoutingRegistryName;
         }
     }
 }
        public override void Execute()
        {
            base.Execute();

            if (string.Equals(ParameterSetName, ParamSetByRouteFilter))
            {
                if (this.RouteFilter != null)
                {
                    this.RouteFilterId = this.RouteFilter.Id;
                }
            }

            var peering = new PSPeering();

            peering.Name        = this.Name;
            peering.PeeringType = this.PeeringType;
            peering.PeerASN     = this.PeerASN;
            peering.VlanId      = this.VlanId;

            if (!string.IsNullOrEmpty(this.SharedKey))
            {
                peering.SharedKey = this.SharedKey;
            }

            if (this.PeerAddressType == IPv6)
            {
                this.SetIpv6PeeringParameters(peering);
            }
            else
            {
                // Set IPv4 config even if no PeerAddresType has been specified for backward compatibility
                this.SetIpv4PeeringParameters(peering);
            }

            this.ConstructMicrosoftConfig(peering);

            WriteObject(peering);
        }
Beispiel #24
0
        /// <summary>
        /// The input update direct.
        /// </summary>
        /// <returns>
        /// The <see cref="PSDirectPeeringModelView"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException"> Only one value can be changed.
        /// </exception>
        /// <exception cref="ErrorResponseException"> Http request failed.
        /// </exception>
        private PSDirectPeeringModelView InputUpdateDirect()
        {
            try
            {
                if (this.InputObject is PSDirectPeeringModelView directPeeringModelView)
                {
                    var resourceId        = new ResourceIdentifier(directPeeringModelView.Id);
                    var resourceGroupName = resourceId.ResourceGroupName;
                    var peeringName       = resourceId.ResourceName;
                    var peering           = new PSPeering
                    {
                        Kind            = directPeeringModelView.Kind,
                        PeeringLocation = directPeeringModelView.PeeringLocation,
                        Location        = directPeeringModelView.Location,
                        Sku             = directPeeringModelView.Sku,
                        Tags            = directPeeringModelView.Tags,
                        Direct          = new PSPeeringPropertiesDirect
                        {
                            Connections = directPeeringModelView.Connections,
                            PeerAsn     = new PSSubResource(
                                directPeeringModelView.PeerAsn.Id),
                            UseForPeeringService = directPeeringModelView
                                                   .UseForPeeringService
                        }
                    };
                    if (this.UseForPeeringService != null)
                    {
                        peering.Sku = this.UseForPeeringService == true
                                          ? new PSPeeringSku {
                            Name = Constants.PremiumDirectFree
                        }
                                          : new PSPeeringSku {
                            Name = Constants.BasicDirectFree
                        };
                        peering.Direct.UseForPeeringService = this.UseForPeeringService;
                    }

                    if (this.DirectConnection != null)
                    {
                        this.DirectConnection.ToList().ForEach(x => peering.Direct.Connections.Add(x));
                    }

                    this.WriteVerbose($"Updating:{peeringName} for ResourceGroup:{resourceGroupName}");
                    return(new PSDirectPeeringModelView(
                               this.ToPeeringPs(
                                   this.PeeringClient.CreateOrUpdate(
                                       resourceGroupName,
                                       peeringName,
                                       this.ToPeering(peering)))));
                }
            }
            catch (InvalidOperationException mapException)
            {
                throw new InvalidOperationException(String.Format(Resources.Error_Mapping, mapException));
            }
            catch (ErrorResponseException ex)
            {
                var error = GetErrorCodeAndMessageFromArmOrErm(ex);
                throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
            }

            throw new InvalidOperationException("Check the input parameters.");
        }
        /// <summary>
        /// The create exchange peering.
        /// </summary>
        /// <returns>
        /// The <see cref="PSPeering"/>.
        /// </returns>
        /// <exception cref="PSArgumentNullException">
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// </exception>
        /// <exception cref="Exception">
        /// </exception>
        /// <exception cref="ArmErrorException">
        /// </exception>
        /// <exception cref="HttpRequestException">
        /// </exception>
        private PSPeering CreateExchangePeering()
        {
            var peeringRequest =
                new PSPeering(
                    name: this.Name,
                    location: this.GetAzureRegion(this.PeeringLocation, Constants.Exchange),
                    sku: new PSPeeringSku {
                Name = Constants.BasicExchangeFree
            },
                    kind: Constants.Exchange)
            {
                PeeringLocation = this.PeeringLocation,
                Exchange        = new PSPeeringPropertiesExchange
                {
                    Connections = new List <PSExchangeConnection>(),
                    PeerAsn     = new PSSubResource(this.PeerAsnResourceId)
                },
                Tags = TagsConversionHelper.CreateTagDictionary(this.Tag, true),
            };

            if (this.ExchangeConnection == null)
            {
                throw new PSArgumentNullException(
                          string.Format(Resources.Create_NewConnectionObject, Constants.Exchange));
            }

            foreach (PSExchangeConnection psExchangeConnection in this.ExchangeConnection)
            {
                if (this.IsValidConnection(psExchangeConnection))
                {
                    peeringRequest.Exchange.Connections.Add(psExchangeConnection);
                }
            }

            if (this.ParameterSetName.Equals(Constants.Exchange))
            {
                if (peeringRequest.Exchange?.Connections.Count <= 0)
                {
                    throw new PSArgumentException(string.Format(Resources.Error_InvalidConnection, "Exchange Connection is null."));
                }
                if (peeringRequest.Direct != null)
                {
                    peeringRequest.Direct = null;
                }

                try
                {
                    return((PSPeering)this.PutNewPeering(peeringRequest));
                }
                catch (ErrorResponseException ex)
                {
                    var error = ex.Response.Content.Contains("\"error\\\":") ? JsonConvert.DeserializeObject <Dictionary <string, ErrorResponse> >(JsonConvert.DeserializeObject(ex.Response.Content).ToString()).FirstOrDefault().Value : JsonConvert.DeserializeObject <ErrorResponse>(ex.Response.Content);
                    throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
                }
            }
            else if (this.ParameterSetName.Equals(Constants.ParameterSetNameConvertLegacyPeering))
            {
                try
                {
                    return((PSPeering)this.PutNewPeering(this.ConvertClassicToExchangePeering(this.LegacyPeering)));
                }
                catch (ErrorResponseException ex)
                {
                    var error = ex.Response.Content.Contains("\"error\\\":") ? JsonConvert.DeserializeObject <Dictionary <string, ErrorResponse> >(JsonConvert.DeserializeObject(ex.Response.Content).ToString()).FirstOrDefault().Value : JsonConvert.DeserializeObject <ErrorResponse>(ex.Response.Content);
                    throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
                }
            }
            throw new PSInvalidOperationException(string.Format(Resources.Error_GenericSyntax));
        }
Beispiel #26
0
        /// <summary>
        /// The input update direct.
        /// </summary>
        /// <returns>
        /// The <see cref="PSDirectPeeringModelView"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException"> Only one value can be changed.
        /// </exception>
        /// <exception cref="ErrorResponseException"> Http request failed.
        /// </exception>
        private PSDirectPeeringModelView InputUpdateDirect()
        {
            try
            {
                if (this.InputObject is PSDirectPeeringModelView directPeeringModelView)
                {
                    var resourceId        = new ResourceIdentifier(directPeeringModelView.Id);
                    var resourceGroupName = resourceId.ResourceGroupName;
                    var peeringName       = resourceId.ResourceName;
                    var peering           = new PSPeering
                    {
                        Kind            = directPeeringModelView.Kind,
                        PeeringLocation = directPeeringModelView.PeeringLocation,
                        Location        = directPeeringModelView.Location,
                        Sku             = directPeeringModelView.Sku,
                        Tags            = directPeeringModelView.Tags,
                        Direct          = new PSPeeringPropertiesDirect
                        {
                            Connections = directPeeringModelView.Connections,
                            PeerAsn     = new PSSubResource(
                                directPeeringModelView.PeerAsn.Id),
                            UseForPeeringService = directPeeringModelView
                                                   .UseForPeeringService
                        }
                    };
                    if (this.UseForPeeringService != null)
                    {
                        peering.Sku = this.UseForPeeringService == true
                                          ? new PSPeeringSku {
                            Name = Constants.PremiumDirectFree
                        }
                                          : new PSPeeringSku {
                            Name = Constants.BasicDirectFree
                        };
                        peering.Direct.UseForPeeringService = this.UseForPeeringService;
                    }

                    if (this.DirectConnection != null)
                    {
                        for (int i = 0; i < peering.Direct.Connections.Count; i++)
                        {
                            foreach (var connection in this.DirectConnection)
                            {
                                var connectionDifferencesCount =
                                    peering.Direct.Connections[i].DetailedCompare(connection).Count;
                                if (connectionDifferencesCount == 0)
                                {
                                    continue;
                                }

                                if (connectionDifferencesCount == 1)
                                {
                                    peering.Direct.Connections[i] = connection;
                                    continue;
                                }

                                if (connectionDifferencesCount <= 1)
                                {
                                    continue;
                                }
                                if (!peering.Direct.Connections.Contains(connection))
                                {
                                    peering.Direct.Connections.Add(connection);
                                }
                            }
                        }
                    }

                    return(new PSDirectPeeringModelView(
                               this.ToPeeringPs(
                                   this.PeeringClient.CreateOrUpdate(
                                       resourceGroupName,
                                       peeringName,
                                       this.ToPeering(peering)))));
                }
            }
            catch (InvalidOperationException mapException)
            {
                throw new InvalidOperationException(String.Format(Resources.Error_Mapping, mapException));
            }
            catch (ErrorResponseException ex)
            {
                var error = ex.Response.Content.Contains("\"error\\\":") ? JsonConvert.DeserializeObject <Dictionary <string, ErrorResponse> >(JsonConvert.DeserializeObject(ex.Response.Content).ToString()).FirstOrDefault().Value : JsonConvert.DeserializeObject <ErrorResponse>(ex.Response.Content);
                throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
            }

            throw new InvalidOperationException("Check the input parameters.");
        }
Beispiel #27
0
        /// <summary>
        /// The create direct peering.
        /// </summary>
        /// <returns>
        /// The <see cref="PSPeering"/>.
        /// </returns>
        /// <exception cref="PSArgumentNullException">
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// </exception>
        /// <exception cref="Exception">
        /// </exception>
        /// <exception cref="ArmErrorException">
        /// </exception>
        /// <exception cref="HttpRequestException">
        /// </exception>
        private PSPeering CreateDirectPeering()
        {
            var peeringRequest =
                new PSPeering(
                    name: this.Name,
                    location: this.GetAzureRegion(this.PeeringLocation, Constants.Direct),
                    sku: this.UseForPeeringService ? new PSPeeringSku {
                Name = Constants.PremiumDirectFree
            } : new PSPeeringSku {
                Name = Constants.BasicDirectFree
            },
                    kind: Constants.Direct)
            {
                PeeringLocation = this.PeeringLocation,
                Direct          = new PSPeeringPropertiesDirect
                {
                    UseForPeeringService = this.UseForPeeringService,
                    Connections          = new List <PSDirectConnection>(),
                    PeerAsn = new PSSubResource(this.PeerAsnResourceId)
                },
                Tags = TagsConversionHelper.CreateTagDictionary(this.Tag, true),
            };

            if (this.DirectConnection == null)
            {
                throw new PSArgumentNullException(string.Format(Resources.Create_NewConnectionObject, Constants.Direct));
            }

            foreach (var psDirectConnection in this.DirectConnection)
            {
                if (this.IsValidConnection(psDirectConnection))
                {
                    peeringRequest.Direct.Connections.Add(psDirectConnection);
                }
            }

            if (this.ParameterSetName.Equals(Constants.Direct))
            {
                if (peeringRequest.Direct?.Connections.Count <= 0)
                {
                    throw new PSArgumentException(string.Format(Resources.Error_NullConnection, Constants.Direct));
                }
                if (peeringRequest.Exchange != null)
                {
                    peeringRequest.Exchange = null;
                }

                try
                {
                    return((PSPeering)this.PutNewPeering(peeringRequest));
                }
                catch (ErrorResponseException ex)
                {
                    var error = this.GetErrorCodeAndMessageFromArmOrErm(ex);
                    throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error?.Code, error?.Message));
                }
            }
            else if (this.ParameterSetName.Equals(Constants.ParameterSetNameConvertLegacyPeering))
            {
                try
                {
                    return((PSPeering)this.PutNewPeering(this.ConvertClassicToDirectPeering(this.LegacyPeering)));
                }
                catch (ErrorResponseException ex)
                {
                    var error = this.GetErrorCodeAndMessageFromArmOrErm(ex);
                    throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
                }
            }
            else
            {
                throw new PSInvalidOperationException(string.Format(Resources.Error_GenericSyntax));
            }
        }