public override void ExecuteCmdlet() { AzureBgpPeering route = null; route = ExpressRouteClient.NewAzureBGPPeering(ServiceKey, PeerAddressType, AdvertisedPublicPrefixes, CustomerAsn, PeerAsn, PrimaryPeerSubnet, RoutingRegistryName, SecondaryPeerSubnet, VlanId, AccessType, SharedKey, Convert.ToUInt32(LegacyMode)); WriteObject(route); }
private AzureDedicatedCircuitStats compute(AzureDedicatedCircuitStats stats, Guid key, BgpPeeringAccessType type) { try { AzureBgpPeering peering = ExpressRouteClient.GetAzureBGPPeering(ServiceKey, type); if (peering != null) { var tempstats = ExpressRouteClient.GetAzureDedicatedCircuitStatsInfo(key, type); stats.PrimaryBytesIn += tempstats.PrimaryBytesIn; stats.PrimaryBytesOut += tempstats.PrimaryBytesOut; stats.SecondaryBytesIn += tempstats.SecondaryBytesIn; stats.SecondaryBytesOut += tempstats.SecondaryBytesOut; } return(stats); } catch { // The cirucit might not have corresponding peering return(stats); } }
public override void ExecuteCmdlet() { if (string.IsNullOrEmpty(AccessType)) { AzureDedicatedCircuitStats stats = new AzureDedicatedCircuitStats { PrimaryBytesIn = (ulong)0, PrimaryBytesOut = (ulong)0, SecondaryBytesIn = (ulong)0, SecondaryBytesOut = (ulong)0 }; AzureBgpPeering peering = ExpressRouteClient.GetAzureBGPPeering(ServiceKey, BgpPeeringAccessType.Private); if (peering != null) { stats = compute(stats, ServiceKey, BgpPeeringAccessType.Private); } peering = ExpressRouteClient.GetAzureBGPPeering(ServiceKey, BgpPeeringAccessType.Public); if (peering != null) { stats = compute(stats, ServiceKey, BgpPeeringAccessType.Public); } peering = ExpressRouteClient.GetAzureBGPPeering(ServiceKey, BgpPeeringAccessType.Microsoft); if (peering != null) { stats = compute(stats, ServiceKey, BgpPeeringAccessType.Microsoft); } WriteObject(stats); } BgpPeeringAccessType type; if (BgpPeeringAccessType.TryParse(AccessType, out type)) { var stats = ExpressRouteClient.GetAzureDedicatedCircuitStatsInfo(ServiceKey, type); WriteObject(stats); } }
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); }
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 <IBgpPeeringOperations>(); BgpPeeringGetResponse expected = new BgpPeeringGetResponse() { BgpPeering = new AzureBgpPeering() { AzureAsn = azureAsn, PeerAsn = peerAsn, PrimaryAzurePort = primaryAzurePort, PrimaryPeerSubnet = primaryPeerSubnet, SecondaryAzurePort = secondaryAzurePort, SecondaryPeerSubnet = secondayPeerSubnet, State = state, VlanId = vlanId }, RequestId = "", StatusCode = new HttpStatusCode() }; var t = new Task <BgpPeeringGetResponse>(() => expected); t.Start(); BgpPeeringGetResponse expected2 = new BgpPeeringGetResponse() { BgpPeering = new AzureBgpPeering() { AzureAsn = azureAsn, PeerAsn = peerAsn, PrimaryAzurePort = primaryAzurePort, PrimaryPeerSubnet = newPrimaryPeerSubnet, SecondaryAzurePort = secondaryAzurePort, SecondaryPeerSubnet = secondayPeerSubnet, State = state, VlanId = vlanId }, RequestId = "", StatusCode = new HttpStatusCode() }; var t2 = new Task <BgpPeeringGetResponse>(() => 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 <BgpPeeringUpdateParameters>(z => z.PrimaryPeerSubnet == newPrimaryPeerSubnet), It.IsAny <CancellationToken>())) .Returns((string sKey, BgpPeeringAccessType atype, BgpPeeringUpdateParameters param, CancellationToken cancellation) => t2); client.SetupGet(f => f.BgpPeering).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); }
/// <summary> /// The Get Bgp Peering operation retrieves the bgp peering for the /// dedicated circuit with the specified service key. /// </summary> /// <param name='serviceKey'> /// The servicee key representing the dedicated circuit. /// </param> /// <param name='accessType'> /// Whether the peering is private or public. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The Get Bgp Peering Operation Response. /// </returns> public async Task <BgpPeeringGetResponse> 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 url = new Uri(this.Client.BaseUri, "/").ToString() + this.Client.Credentials.SubscriptionId + "/services/networking/dedicatedcircuits/" + serviceKey + "/bgppeerings/" + accessType + "?api-version=1.0"; // 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), CloudExceptionType.Xml); if (shouldTrace) { Tracing.Error(invocationId, ex); } throw ex; } // Create Result BgpPeeringGetResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new BgpPeeringGetResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement bgpPeeringElement = responseDoc.Element(XName.Get("BgpPeering", "http://schemas.microsoft.com/windowsazure")); if (bgpPeeringElement != null) { AzureBgpPeering bgpPeeringInstance = new AzureBgpPeering(); result.BgpPeering = bgpPeeringInstance; XElement azureAsnElement = bgpPeeringElement.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 = bgpPeeringElement.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 = bgpPeeringElement.Element(XName.Get("PrimaryAzurePort", "http://schemas.microsoft.com/windowsazure")); if (primaryAzurePortElement != null) { string primaryAzurePortInstance = primaryAzurePortElement.Value; bgpPeeringInstance.PrimaryAzurePort = primaryAzurePortInstance; } XElement primaryPeerSubnetElement = bgpPeeringElement.Element(XName.Get("PrimaryPeerSubnet", "http://schemas.microsoft.com/windowsazure")); if (primaryPeerSubnetElement != null) { string primaryPeerSubnetInstance = primaryPeerSubnetElement.Value; bgpPeeringInstance.PrimaryPeerSubnet = primaryPeerSubnetInstance; } XElement secondaryAzurePortElement = bgpPeeringElement.Element(XName.Get("SecondaryAzurePort", "http://schemas.microsoft.com/windowsazure")); if (secondaryAzurePortElement != null) { string secondaryAzurePortInstance = secondaryAzurePortElement.Value; bgpPeeringInstance.SecondaryAzurePort = secondaryAzurePortInstance; } XElement secondaryPeerSubnetElement = bgpPeeringElement.Element(XName.Get("SecondaryPeerSubnet", "http://schemas.microsoft.com/windowsazure")); if (secondaryPeerSubnetElement != null) { string secondaryPeerSubnetInstance = secondaryPeerSubnetElement.Value; bgpPeeringInstance.SecondaryPeerSubnet = secondaryPeerSubnetInstance; } XElement stateElement = bgpPeeringElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure")); if (stateElement != null) { BGPPeeringState stateInstance = (BGPPeeringState)Enum.Parse(typeof(BGPPeeringState), stateElement.Value, false); bgpPeeringInstance.State = stateInstance; } XElement vlanIdElement = bgpPeeringElement.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(); } } }
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); }