-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mgmt, support multiple source/destination ASG in NSG #21980
Merged
weidongxu-microsoft
merged 3 commits into
Azure:master
from
weidongxu-microsoft:mgmt_nsg-multiple-asg
Jun 2, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...er-network/src/test/java/com/azure/resourcemanager/network/NetworkSecurityGroupTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.resourcemanager.network; | ||
|
||
import com.azure.core.management.Region; | ||
import com.azure.resourcemanager.network.models.ApplicationSecurityGroup; | ||
import com.azure.resourcemanager.network.models.NetworkSecurityGroup; | ||
import com.azure.resourcemanager.network.models.SecurityRuleProtocol; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
|
||
public class NetworkSecurityGroupTests extends NetworkManagementTest { | ||
|
||
@Test | ||
public void canCRUDNetworkSecurityGroup() { | ||
|
||
final String asgName = generateRandomResourceName("asg", 8); | ||
final String asgName2 = generateRandomResourceName("asg", 8); | ||
final String asgName3 = generateRandomResourceName("asg", 8); | ||
final String asgName4 = generateRandomResourceName("asg", 8); | ||
final String asgName5 = generateRandomResourceName("asg", 8); | ||
final String asgName6 = generateRandomResourceName("asg", 8); | ||
final String nsgName = generateRandomResourceName("nsg", 8); | ||
|
||
final Region region = Region.US_SOUTH_CENTRAL; | ||
|
||
ApplicationSecurityGroup asg = networkManager.applicationSecurityGroups().define(asgName) | ||
.withRegion(region) | ||
.withNewResourceGroup(rgName) | ||
.create(); | ||
|
||
ApplicationSecurityGroup asg2 = networkManager.applicationSecurityGroups().define(asgName2) | ||
.withRegion(region) | ||
.withExistingResourceGroup(rgName) | ||
.create(); | ||
|
||
ApplicationSecurityGroup asg3 = networkManager.applicationSecurityGroups().define(asgName3) | ||
.withRegion(region) | ||
.withExistingResourceGroup(rgName) | ||
.create(); | ||
|
||
ApplicationSecurityGroup asg4 = networkManager.applicationSecurityGroups().define(asgName4) | ||
.withRegion(region) | ||
.withExistingResourceGroup(rgName) | ||
.create(); | ||
|
||
NetworkSecurityGroup nsg = networkManager.networkSecurityGroups().define(nsgName) | ||
.withRegion(region) | ||
.withExistingResourceGroup(rgName) | ||
.defineRule("rule1") | ||
.allowOutbound() | ||
.fromAnyAddress() | ||
.fromAnyPort() | ||
.toAnyAddress() | ||
.toPort(80) | ||
.withProtocol(SecurityRuleProtocol.TCP) | ||
.attach() | ||
.defineRule("rule2") | ||
.allowInbound() | ||
.withSourceApplicationSecurityGroup(asg.id(), asg2.id()) | ||
.fromAnyPort() | ||
.toAnyAddress() | ||
.toPortRange(22, 25) | ||
.withAnyProtocol() | ||
.withPriority(200) | ||
.withDescription("foo!!") | ||
.attach() | ||
.defineRule("rule3") | ||
.denyInbound() | ||
.fromAnyAddress() | ||
.fromAnyPort() | ||
.withDestinationApplicationSecurityGroup(asg3.id(), asg4.id()) | ||
.toPort(22) | ||
.withAnyProtocol() | ||
.withPriority(300) | ||
.attach() | ||
.create(); | ||
|
||
Assertions.assertEquals(2, nsg.securityRules().get("rule2").sourceApplicationSecurityGroupIds().size()); | ||
Assertions.assertEquals(2, nsg.securityRules().get("rule3").destinationApplicationSecurityGroupIds().size()); | ||
Assertions.assertEquals(new HashSet<>(Arrays.asList(asg.id(), asg2.id())), nsg.securityRules().get("rule2").sourceApplicationSecurityGroupIds()); | ||
Assertions.assertEquals(new HashSet<>(Arrays.asList(asg3.id(), asg4.id())), nsg.securityRules().get("rule3").destinationApplicationSecurityGroupIds()); | ||
|
||
ApplicationSecurityGroup asg5 = networkManager.applicationSecurityGroups().define(asgName5) | ||
.withRegion(region) | ||
.withExistingResourceGroup(rgName) | ||
.create(); | ||
|
||
ApplicationSecurityGroup asg6 = networkManager.applicationSecurityGroups().define(asgName6) | ||
.withRegion(region) | ||
.withExistingResourceGroup(rgName) | ||
.create(); | ||
|
||
nsg.update() | ||
.updateRule("rule2") | ||
.withoutSourceApplicationSecurityGroup(asg2.id()) | ||
.withSourceApplicationSecurityGroup(asg5.id()) | ||
.parent() | ||
.updateRule("rule3") | ||
.withoutDestinationApplicationSecurityGroup(asg4.id()) | ||
.withDestinationApplicationSecurityGroup(asg6.id()) | ||
.parent() | ||
.apply(); | ||
|
||
Assertions.assertEquals(2, nsg.securityRules().get("rule2").sourceApplicationSecurityGroupIds().size()); | ||
Assertions.assertEquals(2, nsg.securityRules().get("rule3").destinationApplicationSecurityGroupIds().size()); | ||
Assertions.assertEquals(new HashSet<>(Arrays.asList(asg.id(), asg5.id())), nsg.securityRules().get("rule2").sourceApplicationSecurityGroupIds()); | ||
Assertions.assertEquals(new HashSet<>(Arrays.asList(asg3.id(), asg6.id())), nsg.securityRules().get("rule3").destinationApplicationSecurityGroupIds()); | ||
|
||
networkManager.networkSecurityGroups().deleteById(nsg.id()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You add a replace scenario. How about append scenario? Should we consider it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In update flow, one can call multiple
withDestinationApplicationSecurityGroup
andwithoutDestinationApplicationSecurityGroup
to update the list (there is no method taking multiple ids in update flow).In create flow, one just choose the method that take 1 id, or multiple ids. No replace or append.