Пример #1
0
        public void SetBgpPeeringSuccessful()
        {
            // Setup

            string               serviceKey           = "aa28cd19-b10a-41ff-981b-53c6bbf15ead";
            UInt32               peerAsn              = 64496;
            string               primaryPeerSubnet    = "aaa";
            string               newPrimaryPeerSubnet = "ccc";
            string               secondayPeerSubnet   = "bbb";
            UInt32               azureAsn             = 64494;
            string               primaryAzurePort     = "8081";
            string               secondaryAzurePort   = "8082";
            BgpPeeringState      state      = BgpPeeringState.Enabled;
            uint                 vlanId     = 2;
            BgpPeeringAccessType accessType = BgpPeeringAccessType.Private;

            MockCommandRuntime mockCommandRuntime      = new MockCommandRuntime();
            Mock <ExpressRouteManagementClient> client = InitExpressRouteManagementClient();
            var bgpMock = new Mock <IBorderGatewayProtocolPeeringOperations>();

            BorderGatewayProtocolPeeringGetResponse expected =
                new BorderGatewayProtocolPeeringGetResponse()
            {
                BgpPeering = new AzureBgpPeering()
                {
                    AzureAutonomousSystemNumber = azureAsn,
                    PeerAutonomousSystemNumber  = peerAsn,
                    PrimaryAzurePort            = primaryAzurePort,
                    PrimaryPeerSubnet           = primaryPeerSubnet,
                    SecondaryAzurePort          = secondaryAzurePort,
                    SecondaryPeerSubnet         = secondayPeerSubnet,
                    State        = state,
                    VirtualLanId = vlanId
                },
                RequestId  = "",
                StatusCode = new HttpStatusCode()
            };
            var t = new Task <BorderGatewayProtocolPeeringGetResponse>(() => expected);

            t.Start();

            BorderGatewayProtocolPeeringGetResponse expected2 =
                new BorderGatewayProtocolPeeringGetResponse()
            {
                BgpPeering = new AzureBgpPeering()
                {
                    AzureAutonomousSystemNumber = azureAsn,
                    PeerAutonomousSystemNumber  = peerAsn,
                    PrimaryAzurePort            = primaryAzurePort,
                    PrimaryPeerSubnet           = newPrimaryPeerSubnet,
                    SecondaryAzurePort          = secondaryAzurePort,
                    SecondaryPeerSubnet         = secondayPeerSubnet,
                    State        = state,
                    VirtualLanId = vlanId
                },
                RequestId  = "",
                StatusCode = new HttpStatusCode()
            };
            var t2 = new Task <BorderGatewayProtocolPeeringGetResponse>(() => expected2);

            t2.Start();

            bgpMock.Setup(
                f =>
                f.GetAsync(It.Is <string>(x => x == serviceKey),
                           It.Is <BgpPeeringAccessType>(
                               y => y == accessType),
                           It.IsAny <CancellationToken>()))
            .Returns((string sKey, BgpPeeringAccessType atype, CancellationToken cancellation) => t);

            bgpMock.Setup(
                f =>
                f.UpdateAsync(It.Is <string>(x => x == serviceKey),
                              It.Is <BgpPeeringAccessType>(
                                  y => y == accessType),
                              It.Is <BorderGatewayProtocolPeeringUpdateParameters>(z => z.PrimaryPeerSubnet == newPrimaryPeerSubnet),
                              It.IsAny <CancellationToken>()))
            .Returns((string sKey, BgpPeeringAccessType atype, BorderGatewayProtocolPeeringUpdateParameters param, CancellationToken cancellation) => t2);
            client.SetupGet(f => f.BorderGatewayProtocolPeerings).Returns(bgpMock.Object);

            SetAzureBGPPeeringCommand cmdlet = new SetAzureBGPPeeringCommand()
            {
                ServiceKey         = serviceKey,
                AccessType         = accessType,
                PrimaryPeerSubnet  = newPrimaryPeerSubnet,
                CommandRuntime     = mockCommandRuntime,
                ExpressRouteClient = new ExpressRouteClient(client.Object)
            };

            cmdlet.ExecuteCmdlet();

            // Assert
            AzureBgpPeering actual = mockCommandRuntime.OutputPipeline[0] as AzureBgpPeering;

            Assert.AreEqual <string>(expected2.BgpPeering.PrimaryPeerSubnet, actual.PrimaryPeerSubnet);
            Assert.AreEqual(expected.BgpPeering.PrimaryAzurePort, actual.PrimaryAzurePort);
        }
Пример #2
0
        /// <summary>
        /// The Get Bgp Peering operation retrieves the bgp peering for the
        /// dedicated circuit with the specified service key.
        /// </summary>
        /// <param name='serviceKey'>
        /// Required. The servicee key representing the dedicated circuit.
        /// </param>
        /// <param name='accessType'>
        /// Required. Whether the peering is private or public.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The Get Bgp Peering Operation Response.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.ExpressRoute.Models.BorderGatewayProtocolPeeringGetResponse> GetAsync(string serviceKey, BgpPeeringAccessType accessType, CancellationToken cancellationToken)
        {
            // Validate
            if (serviceKey == null)
            {
                throw new ArgumentNullException("serviceKey");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("serviceKey", serviceKey);
                tracingParameters.Add("accessType", accessType);
                Tracing.Enter(invocationId, this, "GetAsync", tracingParameters);
            }

            // Construct URL
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            string url     = "/" + (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/services/networking/dedicatedcircuits/" + serviceKey.Trim() + "/bgppeerings/" + accessType + "?api-version=1.0";

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2011-10-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    BorderGatewayProtocolPeeringGetResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new BorderGatewayProtocolPeeringGetResponse();
                    XDocument responseDoc = XDocument.Parse(responseContent);

                    XElement borderGatewayProtocolPeeringElement = responseDoc.Element(XName.Get("BorderGatewayProtocolPeering", "http://schemas.microsoft.com/windowsazure"));
                    if (borderGatewayProtocolPeeringElement != null)
                    {
                        AzureBgpPeering bgpPeeringInstance = new AzureBgpPeering();
                        result.BgpPeering = bgpPeeringInstance;

                        XElement azureAsnElement = borderGatewayProtocolPeeringElement.Element(XName.Get("AzureAsn", "http://schemas.microsoft.com/windowsazure"));
                        if (azureAsnElement != null)
                        {
                            uint azureAsnInstance = uint.Parse(azureAsnElement.Value, CultureInfo.InvariantCulture);
                            bgpPeeringInstance.AzureAsn = azureAsnInstance;
                        }

                        XElement peerAsnElement = borderGatewayProtocolPeeringElement.Element(XName.Get("PeerAsn", "http://schemas.microsoft.com/windowsazure"));
                        if (peerAsnElement != null)
                        {
                            uint peerAsnInstance = uint.Parse(peerAsnElement.Value, CultureInfo.InvariantCulture);
                            bgpPeeringInstance.PeerAsn = peerAsnInstance;
                        }

                        XElement primaryAzurePortElement = borderGatewayProtocolPeeringElement.Element(XName.Get("PrimaryAzurePort", "http://schemas.microsoft.com/windowsazure"));
                        if (primaryAzurePortElement != null)
                        {
                            string primaryAzurePortInstance = primaryAzurePortElement.Value;
                            bgpPeeringInstance.PrimaryAzurePort = primaryAzurePortInstance;
                        }

                        XElement primaryPeerSubnetElement = borderGatewayProtocolPeeringElement.Element(XName.Get("PrimaryPeerSubnet", "http://schemas.microsoft.com/windowsazure"));
                        if (primaryPeerSubnetElement != null)
                        {
                            string primaryPeerSubnetInstance = primaryPeerSubnetElement.Value;
                            bgpPeeringInstance.PrimaryPeerSubnet = primaryPeerSubnetInstance;
                        }

                        XElement secondaryAzurePortElement = borderGatewayProtocolPeeringElement.Element(XName.Get("SecondaryAzurePort", "http://schemas.microsoft.com/windowsazure"));
                        if (secondaryAzurePortElement != null)
                        {
                            string secondaryAzurePortInstance = secondaryAzurePortElement.Value;
                            bgpPeeringInstance.SecondaryAzurePort = secondaryAzurePortInstance;
                        }

                        XElement secondaryPeerSubnetElement = borderGatewayProtocolPeeringElement.Element(XName.Get("SecondaryPeerSubnet", "http://schemas.microsoft.com/windowsazure"));
                        if (secondaryPeerSubnetElement != null)
                        {
                            string secondaryPeerSubnetInstance = secondaryPeerSubnetElement.Value;
                            bgpPeeringInstance.SecondaryPeerSubnet = secondaryPeerSubnetInstance;
                        }

                        XElement stateElement = borderGatewayProtocolPeeringElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
                        if (stateElement != null)
                        {
                            BgpPeeringState stateInstance = ((BgpPeeringState)Enum.Parse(typeof(BgpPeeringState), stateElement.Value, true));
                            bgpPeeringInstance.State = stateInstance;
                        }

                        XElement vlanIdElement = borderGatewayProtocolPeeringElement.Element(XName.Get("VlanId", "http://schemas.microsoft.com/windowsazure"));
                        if (vlanIdElement != null)
                        {
                            uint vlanIdInstance = uint.Parse(vlanIdElement.Value, CultureInfo.InvariantCulture);
                            bgpPeeringInstance.VlanId = vlanIdInstance;
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }