public void NewAzureBgpPeeringSuccessful() { // Setup string serviceKey = "aa28cd19-b10a-41ff-981b-53c6bbf15ead"; UInt32 peerAsn = 64496; string primaryPeerSubnet = "aaa"; string secondayPeerSubnet = "bbb"; UInt32 azureAsn = 64494; string primaryAzurePort = "8081"; string secondaryAzurePort = "8082"; var state = BgpPeeringState.Enabled; uint vlanId = 2; var 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(); bgpMock.Setup( f => f.NewAsync(It.Is <string>(x => x == serviceKey), It.Is <BgpPeeringAccessType>( y => y == accessType), It.Is <BorderGatewayProtocolPeeringNewParameters>( z => z.PeerAutonomousSystemNumber == peerAsn && z.PrimaryPeerSubnet == primaryPeerSubnet && z.SecondaryPeerSubnet == secondayPeerSubnet && z.VirtualLanId == vlanId), It.IsAny <CancellationToken>())) .Returns((string sKey, BgpPeeringAccessType atype, BorderGatewayProtocolPeeringNewParameters param, CancellationToken cancellation) => t); client.SetupGet(f => f.BorderGatewayProtocolPeerings).Returns(bgpMock.Object); NewAzureBGPPeeringCommand cmdlet = new NewAzureBGPPeeringCommand() { ServiceKey = serviceKey, AccessType = accessType, PeerAsn = peerAsn, PrimaryPeerSubnet = primaryPeerSubnet, SecondaryPeerSubnet = secondayPeerSubnet, SharedKey = null, VlanId = vlanId, CommandRuntime = mockCommandRuntime, ExpressRouteClient = new ExpressRouteClient(client.Object) }; cmdlet.ExecuteCmdlet(); // Assert AzureBgpPeering actual = mockCommandRuntime.OutputPipeline[0] as AzureBgpPeering; Assert.AreEqual(expected.BgpPeering.State, actual.State); Assert.AreEqual(expected.BgpPeering.PrimaryAzurePort, actual.PrimaryAzurePort); }
/// <summary> /// The New Bgp Peering operation creates a new bgp peering associated /// with the dedicated circuit specified by the service key provided. /// </summary> /// <param name='serviceKey'> /// Required. The service key representing the relationship between /// Azure and the customer. /// </param> /// <param name='accessType'> /// Required. Whether the peering is private or public. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the New Bgp Peering operation. /// </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> NewAsync(string serviceKey, BgpPeeringAccessType accessType, BorderGatewayProtocolPeeringNewParameters parameters, CancellationToken cancellationToken) { ExpressRouteManagementClient client = this.Client; 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); tracingParameters.Add("parameters", parameters); Tracing.Enter(invocationId, this, "NewAsync", tracingParameters); } try { if (shouldTrace) { client = this.Client.WithHandler(new ClientRequestTrackingHandler(invocationId)); } cancellationToken.ThrowIfCancellationRequested(); ExpressRouteOperationResponse originalResponse = await client.BorderGatewayProtocolPeerings.BeginNewAsync(serviceKey, accessType, parameters, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); ExpressRouteOperationStatusResponse result = await client.GetOperationStatusAsync(originalResponse.OperationId, cancellationToken).ConfigureAwait(false); int delayInSeconds = 30; while (result.Status == ExpressRouteOperationStatus.InProgress) { cancellationToken.ThrowIfCancellationRequested(); await TaskEx.Delay(delayInSeconds * 1000, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); result = await client.GetOperationStatusAsync(originalResponse.OperationId, cancellationToken).ConfigureAwait(false); delayInSeconds = 10; } if (result.Status == ExpressRouteOperationStatus.Failed) { string exStr = "The BGP peering could not be created due to an internal server error."; throw new ArgumentException(exStr); } cancellationToken.ThrowIfCancellationRequested(); BorderGatewayProtocolPeeringGetResponse getResult = await client.BorderGatewayProtocolPeerings.GetAsync(serviceKey, accessType, cancellationToken).ConfigureAwait(false); if (shouldTrace) { Tracing.Exit(invocationId, result); } return(getResult); } finally { if (client != null && shouldTrace) { client.Dispose(); } } }
public void GetAzureBgpPeeringSuccessful() { // Setup string serviceKey = "aa28cd19-b10a-41ff-981b-53c6bbf15ead"; UInt32 peerAsn = 64496; string primaryPeerSubnet = "aaa"; string secondayPeerSubnet = "bbb"; string primaryPeerSubnetIpv6 = "ccc"; string secondayPeerSubnetIpv6 = "ddd"; UInt32 azureAsn = 64494; string primaryAzurePort = "8081"; string secondaryAzurePort = "8082"; var state = BgpPeeringState.Enabled; uint vlanId = 2; var accessType = BgpPeeringAccessType.Microsoft; string advertisedPublicPrefixes = "111"; string advertisedPublicPrefixesIpv6 = "222"; uint customerAsn = 11; uint customerAsnIpv6 = 22; string advertisedCommunities = "aaa"; string advertisedCommunitiesIpv6 = "bbb"; uint legacyMode = 0; string routingRegistryName = "yy"; string routingRegistryNameIpv6 = "xx"; string advertisedPublicPrefixesState = "Configured"; string advertisedPublicPrefixesStateIpv6 = "Configured"; MockCommandRuntime mockCommandRuntime = new MockCommandRuntime(); Mock <ExpressRouteManagementClient> client = InitExpressRouteManagementClient(); var bgpMock = new Mock <IBorderGatewayProtocolPeeringOperations>(); BorderGatewayProtocolPeeringGetResponse expected = new BorderGatewayProtocolPeeringGetResponse() { BgpPeering = new AzureBgpPeering() { AdvertisedPublicPrefixes = advertisedPublicPrefixes, AdvertisedPublicPrefixesState = advertisedPublicPrefixesState, AzureAsn = azureAsn, CustomerAutonomousSystemNumber = customerAsn, PeerAsn = peerAsn, PrimaryAzurePort = primaryAzurePort, PrimaryPeerSubnet = primaryPeerSubnet, SecondaryAzurePort = secondaryAzurePort, SecondaryPeerSubnet = secondayPeerSubnet, State = state, VlanId = vlanId, AdvertisedCommunities = advertisedCommunities, AdvertisedPublicPrefixesIpv6 = advertisedPublicPrefixesIpv6, AdvertisedPublicPrefixesStateIpv6 = advertisedPublicPrefixesStateIpv6, PrimaryPeerSubnetIpv6 = primaryPeerSubnetIpv6, SecondaryPeerSubnetIpv6 = secondayPeerSubnetIpv6, CustomerAutonomousSystemNumberIpv6 = customerAsnIpv6, LegacyMode = legacyMode, AdvertisedCommunitiesIpv6 = advertisedCommunitiesIpv6, RoutingRegistryName = routingRegistryName, RoutingRegistryNameIpv6 = routingRegistryNameIpv6 }, RequestId = "", StatusCode = new HttpStatusCode() }; var t = new Task <BorderGatewayProtocolPeeringGetResponse>(() => expected); t.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); client.SetupGet(f => f.BorderGatewayProtocolPeerings).Returns(bgpMock.Object); GetAzureBGPPeeringCommand cmdlet = new GetAzureBGPPeeringCommand() { ServiceKey = Guid.Parse(serviceKey), AccessType = accessType, CommandRuntime = mockCommandRuntime, ExpressRouteClient = new ExpressRouteClient(client.Object) }; cmdlet.ExecuteCmdlet(); // Assert AzureBgpPeering actual = mockCommandRuntime.OutputPipeline[0] as AzureBgpPeering; Assert.Equal(expected.BgpPeering.State, actual.State); Assert.Equal(expected.BgpPeering.PrimaryAzurePort, actual.PrimaryAzurePort); }
/// <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(); } } }