Skip to content

Commit a1d0a42

Browse files
committed
Merge pull request Azure#117 from dud5/userdefinedroutes
Add Get-AzureEffectiveRouteTable cmdlet
2 parents 1138ff5 + e845c2a commit a1d0a42

File tree

7 files changed

+433
-4
lines changed

7 files changed

+433
-4
lines changed

src/ServiceManagement/Network/Commands.Network.Test/Commands.Network.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
<Compile Include="IPForwarding\GetIPForwardingTests.cs" />
148148
<Compile Include="IPForwarding\SetIPForwardingTests.cs" />
149149
<Compile Include="Properties\AssemblyInfo.cs" />
150+
<Compile Include="Routes\GetEffectiveRouteTests.cs" />
150151
<Compile Include="ScenarioTests\IPForwarding\IPForwardingScenarioTests.cs" />
151152
<Compile Include="ScenarioTests\NetworkSecurityGroup\NSGScenarioTests.cs" />
152153
<Compile Include="ScenarioTests\NetworkTests.cs" />
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Collections.Generic;
16+
using Microsoft.Azure.Commands.Network.Routes;
17+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
18+
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
19+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
20+
using Microsoft.WindowsAzure.Management;
21+
using Microsoft.WindowsAzure.Management.Compute;
22+
using Microsoft.WindowsAzure.Management.Compute.Models;
23+
using Microsoft.WindowsAzure.Management.Network;
24+
using Microsoft.WindowsAzure.Management.Network.Models;
25+
using Moq;
26+
using System.Threading;
27+
using System.Threading.Tasks;
28+
using Xunit;
29+
30+
namespace Microsoft.Azure.Commands.Network.Test.Routes
31+
{
32+
public class GetEffectiveRouteTests
33+
{
34+
private const string ServiceName = "serviceName";
35+
private const string DeploymentName = "deploymentName";
36+
private const string RoleInstanceName = "roleInstanceName";
37+
private const string NetworkInterfaceName = "networkInterfaceName";
38+
39+
private PersistentVMRoleContext VM = new PersistentVMRoleContext()
40+
{
41+
// these are the only 2 properties being used in the cmdlet
42+
InstanceName = RoleInstanceName,
43+
DeploymentName = DeploymentName
44+
};
45+
46+
private MockCommandRuntime mockCommandRuntime;
47+
48+
private GetAzureEffectiveRouteTable cmdlet;
49+
50+
private NetworkClient client;
51+
private Mock<INetworkManagementClient> networkingClientMock;
52+
private Mock<IComputeManagementClient> computeClientMock;
53+
private Mock<IManagementClient> managementClientMock;
54+
55+
public GetEffectiveRouteTests()
56+
{
57+
this.networkingClientMock = new Mock<INetworkManagementClient>();
58+
this.computeClientMock = new Mock<IComputeManagementClient>();
59+
this.managementClientMock = new Mock<IManagementClient>();
60+
this.mockCommandRuntime = new MockCommandRuntime();
61+
this.client = new NetworkClient(
62+
networkingClientMock.Object,
63+
computeClientMock.Object,
64+
managementClientMock.Object,
65+
mockCommandRuntime);
66+
67+
this.computeClientMock
68+
.Setup(c => c.Deployments.GetBySlotAsync(ServiceName, DeploymentSlot.Production, It.IsAny<CancellationToken>()))
69+
.Returns(Task.Factory.StartNew(() => new DeploymentGetResponse()
70+
{
71+
Name = DeploymentName
72+
}));
73+
74+
this.networkingClientMock
75+
.Setup(c => c.Routes.GetEffectiveRouteTableForRoleInstanceAsync(
76+
ServiceName,
77+
DeploymentName,
78+
RoleInstanceName,
79+
It.IsAny<CancellationToken>()))
80+
.Returns(Task.Factory.StartNew(() => new GetEffectiveRouteTableResponse()
81+
{
82+
EffectiveRouteTable = new EffectiveRouteTable()
83+
{
84+
EffectiveRoutes = new List<EffectiveRoute>()
85+
}
86+
}));
87+
88+
this.networkingClientMock
89+
.Setup(c => c.Routes.GetEffectiveRouteTableForNetworkInterfaceAsync(
90+
ServiceName,
91+
DeploymentName,
92+
RoleInstanceName,
93+
NetworkInterfaceName,
94+
It.IsAny<CancellationToken>()))
95+
.Returns(Task.Factory.StartNew(() => new GetEffectiveRouteTableResponse()
96+
{
97+
EffectiveRouteTable = new EffectiveRouteTable()
98+
{
99+
EffectiveRoutes = new List<EffectiveRoute>()
100+
}
101+
}));
102+
}
103+
104+
[Fact]
105+
public void GetEffectiveRouteTableOnRoleSucceeds()
106+
{
107+
GetEffectiveRouteTableForRoleInstance();
108+
}
109+
110+
[Fact]
111+
public void GetEffectiveRouteTableOnVMSucceeds()
112+
{
113+
GetEffectiveRouteTableForVM();
114+
}
115+
116+
[Fact]
117+
public void GetEffectiveRouteTableOnVMNicSucceeds()
118+
{
119+
GetEffectiveRouteTableForVMNic();
120+
}
121+
122+
#region helpers
123+
124+
private void GetEffectiveRouteTableForRoleInstance()
125+
{
126+
// Setup
127+
cmdlet = new GetAzureEffectiveRouteTable
128+
{
129+
ServiceName = ServiceName,
130+
RoleInstanceName = RoleInstanceName,
131+
CommandRuntime = mockCommandRuntime,
132+
Client = this.client
133+
};
134+
cmdlet.SetParameterSet(GetAzureEffectiveRouteTable.SlotGetEffectiveRouteTableParamSet);
135+
136+
// Action
137+
cmdlet.ExecuteCmdlet();
138+
139+
// Assert
140+
computeClientMock.Verify(
141+
c => c.Deployments.GetBySlotAsync(
142+
ServiceName,
143+
DeploymentSlot.Production,
144+
It.IsAny<CancellationToken>()),
145+
Times.Once());
146+
147+
networkingClientMock.Verify(
148+
c => c.Routes.GetEffectiveRouteTableForRoleInstanceAsync(
149+
cmdlet.ServiceName,
150+
DeploymentName,
151+
cmdlet.RoleInstanceName,
152+
It.IsAny<CancellationToken>()),
153+
Times.Once());
154+
155+
Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
156+
Assert.IsType<EffectiveRouteTable>(mockCommandRuntime.OutputPipeline[0]);
157+
}
158+
159+
private void GetEffectiveRouteTableForVM()
160+
{
161+
// Setup
162+
163+
cmdlet = new GetAzureEffectiveRouteTable
164+
{
165+
ServiceName = ServiceName,
166+
VM = VM,
167+
CommandRuntime = mockCommandRuntime,
168+
Client = this.client,
169+
};
170+
cmdlet.SetParameterSet(GetAzureEffectiveRouteTable.IaaSGetEffectiveRouteTableParamSet);
171+
172+
// Action
173+
cmdlet.ExecuteCmdlet();
174+
175+
// Assert
176+
computeClientMock.Verify(
177+
c => c.Deployments.GetBySlotAsync(
178+
ServiceName,
179+
DeploymentSlot.Production,
180+
It.IsAny<CancellationToken>()),
181+
Times.Never());
182+
183+
networkingClientMock.Verify(
184+
c => c.Routes.GetEffectiveRouteTableForRoleInstanceAsync(
185+
cmdlet.ServiceName,
186+
DeploymentName,
187+
VM.InstanceName,
188+
It.IsAny<CancellationToken>()),
189+
Times.Once());
190+
191+
Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
192+
Assert.IsType<EffectiveRouteTable>(mockCommandRuntime.OutputPipeline[0]);
193+
}
194+
195+
private void GetEffectiveRouteTableForVMNic()
196+
{
197+
// Setup
198+
cmdlet = new GetAzureEffectiveRouteTable
199+
{
200+
ServiceName = ServiceName,
201+
VM = VM,
202+
NetworkInterfaceName = NetworkInterfaceName,
203+
CommandRuntime = mockCommandRuntime,
204+
Client = this.client,
205+
};
206+
cmdlet.SetParameterSet(GetAzureEffectiveRouteTable.IaaSGetEffectiveRouteTableParamSet);
207+
208+
// Action
209+
cmdlet.ExecuteCmdlet();
210+
211+
// Assert
212+
computeClientMock.Verify(
213+
c => c.Deployments.GetBySlotAsync(
214+
ServiceName,
215+
DeploymentSlot.Production,
216+
It.IsAny<CancellationToken>()),
217+
Times.Never());
218+
219+
networkingClientMock.Verify(
220+
c => c.Routes.GetEffectiveRouteTableForNetworkInterfaceAsync(
221+
cmdlet.ServiceName,
222+
DeploymentName,
223+
VM.InstanceName,
224+
cmdlet.NetworkInterfaceName,
225+
It.IsAny<CancellationToken>()),
226+
Times.Once());
227+
228+
Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
229+
Assert.IsType<EffectiveRouteTable>(mockCommandRuntime.OutputPipeline[0]);
230+
}
231+
232+
#endregion
233+
}
234+
}

src/ServiceManagement/Network/Commands.Network/Commands.Network.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
<Compile Include="IPForwarding\SetAzureIPForwarding.cs" />
175175
<Compile Include="NetworkClient.cs" />
176176
<Compile Include="NetworkCmdletBase.cs" />
177+
<Compile Include="Routes\GetAzureEffectiveRouteTable.cs" />
177178
<Compile Include="NetworkSecurityGroup\GetAzureNetworkSecurityGroupConfig.cs" />
178179
<Compile Include="Routes\GetAzureRouteTable.cs" />
179180
<Compile Include="Routes\GetAzureSubnetRouteTable.cs" />

src/ServiceManagement/Network/Commands.Network/Microsoft.Azure.Commands.Network.format.ps1xml

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,87 @@
192192
</TableRowEntry>
193193
</TableRowEntries>
194194
</TableControl>
195-
</View>
196-
195+
</View>
196+
197+
198+
<View>
199+
<Name>Microsoft.WindowsAzure.Management.Network.Models.EffectiveRouteTable</Name>
200+
<ViewSelectedBy>
201+
<TypeName>Microsoft.WindowsAzure.Management.Network.Models.EffectiveRouteTable</TypeName>
202+
</ViewSelectedBy>
203+
<ListControl>
204+
<ListEntries>
205+
<ListEntry>
206+
<ListItems>
207+
<ListItem>
208+
<Label> </Label>
209+
<ScriptBlock>$_.EffectiveRoutes | Format-Table | Out-String </ScriptBlock>
210+
</ListItem>
211+
</ListItems>
212+
</ListEntry>
213+
</ListEntries>
214+
</ListControl>
215+
</View>
216+
217+
<View>
218+
<Name>Microsoft.WindowsAzure.Management.Network.Models.EffectiveRoute</Name>
219+
<ViewSelectedBy>
220+
<TypeName>Microsoft.WindowsAzure.Management.Network.Models.EffectiveRoute</TypeName>
221+
</ViewSelectedBy>
222+
<TableControl>
223+
<TableHeaders>
224+
<TableColumnHeader>
225+
<Label>Name</Label>
226+
<Width>15</Width>
227+
</TableColumnHeader>
228+
<TableColumnHeader>
229+
<Label>Address Prefix</Label>
230+
<Width>17</Width>
231+
</TableColumnHeader>
232+
<TableColumnHeader>
233+
<Label>Next hop type</Label>
234+
<Width>16</Width>
235+
</TableColumnHeader>
236+
<TableColumnHeader>
237+
<Label>Next hop IP address</Label>
238+
<Width>19</Width>
239+
</TableColumnHeader>
240+
<TableColumnHeader>
241+
<Label>Status</Label>
242+
<Width>8</Width>
243+
</TableColumnHeader>
244+
<TableColumnHeader>
245+
<Label>Source</Label>
246+
<Width>11</Width>
247+
</TableColumnHeader>
248+
</TableHeaders>
249+
<TableRowEntries>
250+
<TableRowEntry>
251+
<Wrap/>
252+
<TableColumnItems>
253+
<TableColumnItem>
254+
<PropertyName>Name</PropertyName>
255+
</TableColumnItem>
256+
<TableColumnItem>
257+
<PropertyName>AddressPrefixes</PropertyName>
258+
</TableColumnItem>
259+
<TableColumnItem>
260+
<ScriptBlock>$_.EffectiveNextHop.Type</ScriptBlock>
261+
</TableColumnItem>
262+
<TableColumnItem>
263+
<PropertyName>$_.EffectiveNextHop.IpAddresses</PropertyName>
264+
</TableColumnItem>
265+
<TableColumnItem>
266+
<PropertyName>Status</PropertyName>
267+
</TableColumnItem>
268+
<TableColumnItem>
269+
<PropertyName>Source</PropertyName>
270+
</TableColumnItem>
271+
</TableColumnItems>
272+
</TableRowEntry>
273+
</TableRowEntries>
274+
</TableControl>
275+
</View>
276+
197277
</ViewDefinitions>
198278
</Configuration>

src/ServiceManagement/Network/Commands.Network/NetworkClient.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,12 @@ public AzureOperationResponse DeleteRouteTable(string routeTableName)
661661
return client.Routes.DeleteRouteTable(routeTableName);
662662
}
663663

664-
public AzureOperationResponse SetRoute(string routeTableName, string routeName, string addressPrefix, string nextHopType)
664+
public AzureOperationResponse SetRoute(string routeTableName, string routeName, string addressPrefix, string nextHopType, string ipAddress)
665665
{
666666
NextHop nextHop = new NextHop()
667667
{
668668
Type = nextHopType,
669+
IpAddress = ipAddress
669670
};
670671
SetRouteParameters parameters = new SetRouteParameters()
671672
{
@@ -879,6 +880,30 @@ public string GetDeploymentName(IPersistentVM vm, string slot, string serviceNam
879880
return deploymentName;
880881
}
881882

883+
public GetEffectiveRouteTableResponse GetEffectiveRouteTableForRoleInstance(
884+
string serviceName,
885+
string deploymentName,
886+
string roleInstanceName)
887+
{
888+
return this.client.Routes.GetEffectiveRouteTableForRoleInstance(
889+
serviceName,
890+
deploymentName,
891+
roleInstanceName);
892+
}
893+
894+
public GetEffectiveRouteTableResponse GetEffectiveRouteTableForNetworkInterface(
895+
string serviceName,
896+
string deploymentName,
897+
string roleInstanceName,
898+
string networkInterfaceName)
899+
{
900+
return this.client.Routes.GetEffectiveRouteTableForNetworkInterface(
901+
serviceName,
902+
deploymentName,
903+
roleInstanceName,
904+
networkInterfaceName);
905+
}
906+
882907
public void SetIPForwardingForRole(string serviceName, string deploymentName, string roleName, bool ipForwarding)
883908
{
884909
var parameters = new IPForwardingSetParameters()

0 commit comments

Comments
 (0)