Skip to content

Commit 6a9d9bc

Browse files
mgmt, support multiple source/destination ASG in NSG (Azure#21980)
* mgmt, support multiple source/destination ASG in NSG * checkstyle * changelog
1 parent 301a92d commit 6a9d9bc

File tree

5 files changed

+1081
-1
lines changed

5 files changed

+1081
-1
lines changed

sdk/resourcemanager/azure-resourcemanager-network/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.6.0-beta.1 (Unreleased)
44

55
- Updated `api-version` to `2021-02-01`
6+
- Supported multiple `ApplicationSecurityGroup` in rules of `NetworkSecurityGroup`.
67

78
## 2.5.0 (2021-05-28)
89
- Updated `api-version` to `2020-11-01`

sdk/resourcemanager/azure-resourcemanager-network/src/main/java/com/azure/resourcemanager/network/implementation/NetworkSecurityRuleImpl.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.List;
2020
import java.util.Map;
2121
import java.util.Set;
22+
import java.util.function.Function;
23+
import java.util.stream.Collectors;
2224

2325
/** Implementation for {@link NetworkSecurityRule} and its create and update interfaces. */
2426
class NetworkSecurityRuleImpl
@@ -286,6 +288,21 @@ public NetworkSecurityRuleImpl withSourceApplicationSecurityGroup(String id) {
286288
return this;
287289
}
288290

291+
@Override
292+
public NetworkSecurityRuleImpl withoutSourceApplicationSecurityGroup(String id) {
293+
sourceAsgs.remove(id);
294+
return this;
295+
}
296+
297+
@Override
298+
public NetworkSecurityRuleImpl withSourceApplicationSecurityGroup(String... ids) {
299+
sourceAsgs = Arrays.stream(ids)
300+
.collect(Collectors.toMap(Function.identity(), id -> new ApplicationSecurityGroupInner().withId(id)));
301+
innerModel().withSourceAddressPrefix(null);
302+
innerModel().withSourceAddressPrefixes(null);
303+
return this;
304+
}
305+
289306
@Override
290307
public NetworkSecurityRuleImpl withDestinationApplicationSecurityGroup(String id) {
291308
destinationAsgs.put(id, new ApplicationSecurityGroupInner().withId(id));
@@ -294,6 +311,21 @@ public NetworkSecurityRuleImpl withDestinationApplicationSecurityGroup(String id
294311
return this;
295312
}
296313

314+
@Override
315+
public NetworkSecurityRuleImpl withoutDestinationApplicationSecurityGroup(String id) {
316+
destinationAsgs.remove(id);
317+
return this;
318+
}
319+
320+
@Override
321+
public NetworkSecurityRuleImpl withDestinationApplicationSecurityGroup(String... ids) {
322+
destinationAsgs = Arrays.stream(ids)
323+
.collect(Collectors.toMap(Function.identity(), id -> new ApplicationSecurityGroupInner().withId(id)));
324+
innerModel().withDestinationAddressPrefix(null);
325+
innerModel().withDestinationAddressPrefixes(null);
326+
return this;
327+
}
328+
297329
// Helpers
298330

299331
private NetworkSecurityRuleImpl withDirection(SecurityRuleDirection direction) {
@@ -310,9 +342,14 @@ private NetworkSecurityRuleImpl withAccess(SecurityRuleAccess permission) {
310342

311343
@Override
312344
public NetworkSecurityGroupImpl attach() {
345+
return this.parent().withRule(this);
346+
}
347+
348+
@Override
349+
public NetworkSecurityGroupImpl parent() {
313350
innerModel().withSourceApplicationSecurityGroups(new ArrayList<>(sourceAsgs.values()));
314351
innerModel().withDestinationApplicationSecurityGroups(new ArrayList<>(destinationAsgs.values()));
315-
return this.parent().withRule(this);
352+
return super.parent();
316353
}
317354

318355
@Override

sdk/resourcemanager/azure-resourcemanager-network/src/main/java/com/azure/resourcemanager/network/models/NetworkSecurityRule.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.azure.resourcemanager.resources.fluentcore.model.Attachable;
99
import com.azure.resourcemanager.resources.fluentcore.model.HasInnerModel;
1010
import com.azure.resourcemanager.resources.fluentcore.model.Settable;
11+
1112
import java.util.List;
1213
import java.util.Set;
1314

@@ -200,6 +201,14 @@ interface WithDestinationAddressOrSecurityGroup<ParentT> {
200201
* @return the next stage of the definition
201202
*/
202203
WithDestinationPort<ParentT> withDestinationApplicationSecurityGroup(String id);
204+
205+
/**
206+
* Sets the application security group specified as destination.
207+
*
208+
* @param ids the collection of application security group ID
209+
* @return the next stage of the definition
210+
*/
211+
WithDestinationPort<ParentT> withDestinationApplicationSecurityGroup(String... ids);
203212
}
204213

205214
/**
@@ -279,6 +288,14 @@ interface WithSourceAddressOrSecurityGroup<ParentT> {
279288
* @return the next stage of the definition
280289
*/
281290
WithSourcePort<ParentT> withSourceApplicationSecurityGroup(String id);
291+
292+
/**
293+
* Sets the application security group specified as source.
294+
*
295+
* @param ids the collection of application security group ID
296+
* @return the next stage of the definition
297+
*/
298+
WithSourcePort<ParentT> withSourceApplicationSecurityGroup(String... ids);
282299
}
283300

284301
/**
@@ -460,6 +477,14 @@ interface WithSourceAddressOrSecurityGroup<ParentT> {
460477
* @return the next stage of the update
461478
*/
462479
WithSourcePort<ParentT> withSourceApplicationSecurityGroup(String id);
480+
481+
/**
482+
* Sets the application security group specified as source.
483+
*
484+
* @param ids the collection of application security group ID
485+
* @return the next stage of the definition
486+
*/
487+
WithSourcePort<ParentT> withSourceApplicationSecurityGroup(String... ids);
463488
}
464489

465490
/**
@@ -539,6 +564,14 @@ interface WithDestinationAddressOrSecurityGroup<ParentT> {
539564
* @return the next stage of the definition
540565
*/
541566
WithDestinationPort<ParentT> withDestinationApplicationSecurityGroup(String id);
567+
568+
/**
569+
* Sets the application security group specified as destination.
570+
*
571+
* @param ids the collection of application security group ID
572+
* @return the next stage of the definition
573+
*/
574+
WithDestinationPort<ParentT> withDestinationApplicationSecurityGroup(String... ids);
542575
}
543576

544577
/**
@@ -730,6 +763,14 @@ interface WithSourceAddressOrSecurityGroup {
730763
* @return the next stage of the update
731764
*/
732765
Update withSourceApplicationSecurityGroup(String id);
766+
767+
/**
768+
* Removes the application security group specified as source.
769+
*
770+
* @param id application security group id
771+
* @return the next stage of the update
772+
*/
773+
Update withoutSourceApplicationSecurityGroup(String id);
733774
}
734775

735776
/** The stage of the network rule description allowing the source port(s) to be specified. */
@@ -803,6 +844,14 @@ interface WithDestinationAddressOrSecurityGroup {
803844
* @return the next stage of the update
804845
*/
805846
Update withDestinationApplicationSecurityGroup(String id);
847+
848+
/**
849+
* Removes the application security group specified as destination.
850+
*
851+
* @param id application security group id
852+
* @return the next stage of the definition
853+
*/
854+
Update withoutDestinationApplicationSecurityGroup(String id);
806855
}
807856

808857
/** The stage of the network rule description allowing the destination port(s) to be specified. */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.resourcemanager.network;
5+
6+
import com.azure.core.management.Region;
7+
import com.azure.resourcemanager.network.models.ApplicationSecurityGroup;
8+
import com.azure.resourcemanager.network.models.NetworkSecurityGroup;
9+
import com.azure.resourcemanager.network.models.SecurityRuleProtocol;
10+
11+
import org.junit.jupiter.api.Assertions;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.util.Arrays;
15+
import java.util.HashSet;
16+
17+
public class NetworkSecurityGroupTests extends NetworkManagementTest {
18+
19+
@Test
20+
public void canCRUDNetworkSecurityGroup() {
21+
22+
final String asgName = generateRandomResourceName("asg", 8);
23+
final String asgName2 = generateRandomResourceName("asg", 8);
24+
final String asgName3 = generateRandomResourceName("asg", 8);
25+
final String asgName4 = generateRandomResourceName("asg", 8);
26+
final String asgName5 = generateRandomResourceName("asg", 8);
27+
final String asgName6 = generateRandomResourceName("asg", 8);
28+
final String nsgName = generateRandomResourceName("nsg", 8);
29+
30+
final Region region = Region.US_SOUTH_CENTRAL;
31+
32+
ApplicationSecurityGroup asg = networkManager.applicationSecurityGroups().define(asgName)
33+
.withRegion(region)
34+
.withNewResourceGroup(rgName)
35+
.create();
36+
37+
ApplicationSecurityGroup asg2 = networkManager.applicationSecurityGroups().define(asgName2)
38+
.withRegion(region)
39+
.withExistingResourceGroup(rgName)
40+
.create();
41+
42+
ApplicationSecurityGroup asg3 = networkManager.applicationSecurityGroups().define(asgName3)
43+
.withRegion(region)
44+
.withExistingResourceGroup(rgName)
45+
.create();
46+
47+
ApplicationSecurityGroup asg4 = networkManager.applicationSecurityGroups().define(asgName4)
48+
.withRegion(region)
49+
.withExistingResourceGroup(rgName)
50+
.create();
51+
52+
NetworkSecurityGroup nsg = networkManager.networkSecurityGroups().define(nsgName)
53+
.withRegion(region)
54+
.withExistingResourceGroup(rgName)
55+
.defineRule("rule1")
56+
.allowOutbound()
57+
.fromAnyAddress()
58+
.fromAnyPort()
59+
.toAnyAddress()
60+
.toPort(80)
61+
.withProtocol(SecurityRuleProtocol.TCP)
62+
.attach()
63+
.defineRule("rule2")
64+
.allowInbound()
65+
.withSourceApplicationSecurityGroup(asg.id(), asg2.id())
66+
.fromAnyPort()
67+
.toAnyAddress()
68+
.toPortRange(22, 25)
69+
.withAnyProtocol()
70+
.withPriority(200)
71+
.withDescription("foo!!")
72+
.attach()
73+
.defineRule("rule3")
74+
.denyInbound()
75+
.fromAnyAddress()
76+
.fromAnyPort()
77+
.withDestinationApplicationSecurityGroup(asg3.id(), asg4.id())
78+
.toPort(22)
79+
.withAnyProtocol()
80+
.withPriority(300)
81+
.attach()
82+
.create();
83+
84+
Assertions.assertEquals(2, nsg.securityRules().get("rule2").sourceApplicationSecurityGroupIds().size());
85+
Assertions.assertEquals(2, nsg.securityRules().get("rule3").destinationApplicationSecurityGroupIds().size());
86+
Assertions.assertEquals(new HashSet<>(Arrays.asList(asg.id(), asg2.id())), nsg.securityRules().get("rule2").sourceApplicationSecurityGroupIds());
87+
Assertions.assertEquals(new HashSet<>(Arrays.asList(asg3.id(), asg4.id())), nsg.securityRules().get("rule3").destinationApplicationSecurityGroupIds());
88+
89+
ApplicationSecurityGroup asg5 = networkManager.applicationSecurityGroups().define(asgName5)
90+
.withRegion(region)
91+
.withExistingResourceGroup(rgName)
92+
.create();
93+
94+
ApplicationSecurityGroup asg6 = networkManager.applicationSecurityGroups().define(asgName6)
95+
.withRegion(region)
96+
.withExistingResourceGroup(rgName)
97+
.create();
98+
99+
nsg.update()
100+
.updateRule("rule2")
101+
.withoutSourceApplicationSecurityGroup(asg2.id())
102+
.withSourceApplicationSecurityGroup(asg5.id())
103+
.parent()
104+
.updateRule("rule3")
105+
.withoutDestinationApplicationSecurityGroup(asg4.id())
106+
.withDestinationApplicationSecurityGroup(asg6.id())
107+
.parent()
108+
.apply();
109+
110+
Assertions.assertEquals(2, nsg.securityRules().get("rule2").sourceApplicationSecurityGroupIds().size());
111+
Assertions.assertEquals(2, nsg.securityRules().get("rule3").destinationApplicationSecurityGroupIds().size());
112+
Assertions.assertEquals(new HashSet<>(Arrays.asList(asg.id(), asg5.id())), nsg.securityRules().get("rule2").sourceApplicationSecurityGroupIds());
113+
Assertions.assertEquals(new HashSet<>(Arrays.asList(asg3.id(), asg6.id())), nsg.securityRules().get("rule3").destinationApplicationSecurityGroupIds());
114+
115+
networkManager.networkSecurityGroups().deleteById(nsg.id());
116+
}
117+
}

0 commit comments

Comments
 (0)