Skip to content

Commit 9c576de

Browse files
mgmt, support parameters in policy (Azure#22103)
* mgmt, support parameters in policy * changelog * use immutable collection
1 parent c4cdfd1 commit 9c576de

File tree

7 files changed

+223
-5
lines changed

7 files changed

+223
-5
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## 2.6.0-beta.1 (Unreleased)
44

5-
- Added Support for Challenge Based Authentication in `AuthenticationPolicy`.
5+
- Added support for Challenge Based Authentication in `AuthenticationPolicy`.
6+
- Added support for `parameters` in `PolicyDefinition` and `PolicyAssignment`.
67

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

sdk/resourcemanager/azure-resourcemanager-resources/src/main/java/com/azure/resourcemanager/resources/implementation/PolicyAssignmentImpl.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
package com.azure.resourcemanager.resources.implementation;
55

6+
import com.azure.resourcemanager.resources.models.EnforcementMode;
67
import com.azure.resourcemanager.resources.models.GenericResource;
8+
import com.azure.resourcemanager.resources.models.ParameterValuesValue;
79
import com.azure.resourcemanager.resources.models.PolicyAssignment;
810
import com.azure.resourcemanager.resources.models.PolicyDefinition;
911
import com.azure.resourcemanager.resources.models.ResourceGroup;
@@ -12,6 +14,12 @@
1214
import com.azure.resourcemanager.resources.fluent.PolicyAssignmentsClient;
1315
import reactor.core.publisher.Mono;
1416

17+
import java.util.ArrayList;
18+
import java.util.Collections;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.TreeMap;
22+
1523
/**
1624
* Implementation for {@link PolicyAssignment}.
1725
*/
@@ -55,6 +63,25 @@ public String type() {
5563
return innerModel().type();
5664
}
5765

66+
@Override
67+
public List<String> excludedScopes() {
68+
return innerModel().notScopes() == null
69+
? Collections.emptyList()
70+
: Collections.unmodifiableList(innerModel().notScopes());
71+
}
72+
73+
@Override
74+
public EnforcementMode enforcementMode() {
75+
return innerModel().enforcementMode();
76+
}
77+
78+
@Override
79+
public Map<String, ParameterValuesValue> parameters() {
80+
return innerModel().parameters() == null
81+
? Collections.emptyMap()
82+
: Collections.unmodifiableMap(innerModel().parameters());
83+
}
84+
5885
@Override
5986
public PolicyAssignmentImpl withDisplayName(String displayName) {
6087
innerModel().withDisplayName(displayName);
@@ -105,4 +132,28 @@ public boolean isInCreateMode() {
105132
protected Mono<PolicyAssignmentInner> getInnerAsync() {
106133
return innerCollection.getAsync(innerModel().scope(), name());
107134
}
135+
136+
@Override
137+
public PolicyAssignmentImpl withExcludedScope(String scope) {
138+
if (innerModel().notScopes() == null) {
139+
innerModel().withNotScopes(new ArrayList<>());
140+
}
141+
innerModel().notScopes().add(scope);
142+
return this;
143+
}
144+
145+
@Override
146+
public PolicyAssignmentImpl withParameter(String name, Object value) {
147+
if (innerModel().parameters() == null) {
148+
innerModel().withParameters(new TreeMap<>());
149+
}
150+
innerModel().parameters().put(name, new ParameterValuesValue().withValue(value));
151+
return this;
152+
}
153+
154+
@Override
155+
public PolicyAssignmentImpl withEnforcementMode(EnforcementMode mode) {
156+
innerModel().withEnforcementMode(mode);
157+
return this;
158+
}
108159
}

sdk/resourcemanager/azure-resourcemanager-resources/src/main/java/com/azure/resourcemanager/resources/implementation/PolicyDefinitionImpl.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package com.azure.resourcemanager.resources.implementation;
55

66
import com.azure.core.util.logging.ClientLogger;
7+
import com.azure.resourcemanager.resources.models.ParameterDefinitionsValue;
8+
import com.azure.resourcemanager.resources.models.ParameterType;
79
import com.azure.resourcemanager.resources.models.PolicyDefinition;
810
import com.azure.resourcemanager.resources.models.PolicyType;
911
import com.azure.resourcemanager.resources.fluent.models.PolicyDefinitionInner;
@@ -13,6 +15,9 @@
1315
import reactor.core.publisher.Mono;
1416

1517
import java.io.IOException;
18+
import java.util.Collections;
19+
import java.util.Map;
20+
import java.util.TreeMap;
1621

1722
/**
1823
* Implementation for {@link PolicyDefinition}.
@@ -51,6 +56,13 @@ public Object policyRule() {
5156
return innerModel().policyRule();
5257
}
5358

59+
@Override
60+
public Map<String, ParameterDefinitionsValue> parameters() {
61+
return innerModel().parameters() == null
62+
? Collections.emptyMap()
63+
: Collections.unmodifiableMap(innerModel().parameters());
64+
}
65+
5466
@Override
5567
public String id() {
5668
return innerModel().id();
@@ -105,4 +117,25 @@ public Mono<PolicyDefinition> createResourceAsync() {
105117
public boolean isInCreateMode() {
106118
return id() == null;
107119
}
120+
121+
@Override
122+
public PolicyDefinitionImpl withParameter(String name, ParameterDefinitionsValue definition) {
123+
if (innerModel().parameters() == null) {
124+
innerModel().withParameters(new TreeMap<>());
125+
}
126+
innerModel().parameters().put(name, definition);
127+
return this;
128+
}
129+
130+
@Override
131+
public PolicyDefinitionImpl withParameter(String name, ParameterType parameterType, Object defaultValue) {
132+
if (innerModel().parameters() == null) {
133+
innerModel().withParameters(new TreeMap<>());
134+
}
135+
innerModel().parameters().put(name,
136+
new ParameterDefinitionsValue()
137+
.withType(parameterType)
138+
.withDefaultValue(defaultValue));
139+
return this;
140+
}
108141
}

sdk/resourcemanager/azure-resourcemanager-resources/src/main/java/com/azure/resourcemanager/resources/models/PolicyAssignment.java

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import com.azure.resourcemanager.resources.fluentcore.model.Refreshable;
1313
import com.azure.resourcemanager.resources.fluent.models.PolicyAssignmentInner;
1414

15+
import java.util.List;
16+
import java.util.Map;
17+
1518
/**
1619
* An immutable client-side representation of an Azure policy assignment.
1720
*/
@@ -43,6 +46,21 @@ public interface PolicyAssignment extends
4346
*/
4447
String type();
4548

49+
/**
50+
* @return the excluded scopes of the policy assignment
51+
*/
52+
List<String> excludedScopes();
53+
54+
/**
55+
* @return the enforcement mode of the policy assignment
56+
*/
57+
EnforcementMode enforcementMode();
58+
59+
/**
60+
* @return the parameters of the policy assignment
61+
*/
62+
Map<String, ParameterValuesValue> parameters();
63+
4664
/**
4765
* Container interface for all the definitions that need to be implemented.
4866
*/
@@ -125,14 +143,57 @@ interface WithDisplayName {
125143
WithCreate withDisplayName(String displayName);
126144
}
127145

146+
/**
147+
* A policy assignment allowing the excluded scopes to be set.
148+
*/
149+
interface WithExcludedScopes {
150+
/**
151+
* Specifies the excluded scope of the policy assignment.
152+
*
153+
* @param scope the scope to be excluded from the policy assignment
154+
* @return the next stage of policy assignment
155+
*/
156+
WithCreate withExcludedScope(String scope);
157+
}
158+
159+
/**
160+
* A policy assignment allowing the parameters to be set.
161+
*/
162+
interface WithParameters {
163+
/**
164+
* Specifies the parameter of the policy assignment.
165+
*
166+
* @param name the name of the parameter
167+
* @param value the value of the parameter
168+
* @return the next stage of policy assignment
169+
*/
170+
WithCreate withParameter(String name, Object value);
171+
}
172+
173+
/**
174+
* A policy assignment allowing the enforcement mode to be set.
175+
*/
176+
interface WithEnforcementMode {
177+
/**
178+
* Specifies the enforcement mode of the policy assignment.
179+
*
180+
* @param mode the enforcement mode of the policy assignment
181+
* @return the next stage of policy assignment
182+
*/
183+
WithCreate withEnforcementMode(EnforcementMode mode);
184+
}
185+
128186
/**
129187
* A policy assignment with sufficient inputs to create a new policy
130188
* assignment in the cloud, but exposing additional optional inputs to
131189
* specify.
132190
*/
133191
interface WithCreate extends
134192
Creatable<PolicyAssignment>,
135-
DefinitionStages.WithDisplayName {
193+
DefinitionStages.WithDisplayName,
194+
DefinitionStages.WithExcludedScopes,
195+
DefinitionStages.WithParameters,
196+
DefinitionStages.WithEnforcementMode {
136197
}
137198
}
138199
}

sdk/resourcemanager/azure-resourcemanager-resources/src/main/java/com/azure/resourcemanager/resources/models/PolicyDefinition.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.azure.resourcemanager.resources.fluentcore.model.Updatable;
1515
import com.azure.resourcemanager.resources.fluentcore.model.HasInnerModel;
1616

17+
import java.util.Map;
18+
1719
/**
1820
* An immutable client-side representation of an Azure policy.
1921
*/
@@ -46,6 +48,11 @@ public interface PolicyDefinition extends
4648
*/
4749
Object policyRule();
4850

51+
/**
52+
* @return the parameters of the policy definition
53+
*/
54+
Map<String, ParameterDefinitionsValue> parameters();
55+
4956
/**
5057
* Container interface for all the definitions that need to be implemented.
5158
*/
@@ -124,6 +131,30 @@ interface WithDescription {
124131
WithCreate withDescription(String description);
125132
}
126133

134+
/**
135+
* A policy definition allowing parameters to be set.
136+
*/
137+
interface WithParameters {
138+
/**
139+
* Specifies the parameters of the policy.
140+
*
141+
* @param name the name of the parameter
142+
* @param definition the definition of the parameter
143+
* @return the next stage of policy definition
144+
*/
145+
WithCreate withParameter(String name, ParameterDefinitionsValue definition);
146+
147+
/**
148+
* Specifies the parameters of the policy.
149+
*
150+
* @param name the name of the parameter
151+
* @param parameterType the type of the parameter
152+
* @param defaultValue the default value of the parameter
153+
* @return the next stage of policy definition
154+
*/
155+
WithCreate withParameter(String name, ParameterType parameterType, Object defaultValue);
156+
}
157+
127158
/**
128159
* A policy definition with sufficient inputs to create a new
129160
* policy in the cloud, but exposing additional optional inputs to
@@ -133,7 +164,8 @@ interface WithCreate extends
133164
Creatable<PolicyDefinition>,
134165
DefinitionStages.WithDescription,
135166
DefinitionStages.WithDisplayName,
136-
DefinitionStages.WithPolicyType {
167+
DefinitionStages.WithPolicyType,
168+
DefinitionStages.WithParameters {
137169
}
138170
}
139171

sdk/resourcemanager/azure-resourcemanager-resources/src/test/java/com/azure/resourcemanager/resources/PolicyTests.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import com.azure.core.http.rest.PagedIterable;
77
import com.azure.core.test.annotation.DoNotRecord;
8+
import com.azure.resourcemanager.resources.models.EnforcementMode;
9+
import com.azure.resourcemanager.resources.models.ParameterDefinitionsValue;
10+
import com.azure.resourcemanager.resources.models.ParameterType;
811
import com.azure.resourcemanager.test.utils.TestUtilities;
912
import com.azure.resourcemanager.resources.models.GenericResource;
1013
import com.azure.resourcemanager.resources.models.PolicyAssignment;
@@ -16,8 +19,11 @@
1619
import org.junit.jupiter.api.Assertions;
1720
import org.junit.jupiter.api.Test;
1821

22+
import java.util.Collections;
23+
1924
public class PolicyTests extends ResourceManagementTest {
2025
private String policyRule = "{\"if\":{\"not\":{\"field\":\"location\",\"in\":[\"southcentralus\",\"westeurope\"]}},\"then\":{\"effect\":\"deny\"}}";
26+
private String policyRule2 = "{\"if\":{\"not\":{\"field\":\"name\",\"like\":\"[concat(parameters('prefix'),'*',parameters('suffix'))]\"}},\"then\":{\"effect\":\"deny\"}}";
2127

2228
@Override
2329
protected void cleanUpResources() {
@@ -64,10 +70,12 @@ public void canCRUDPolicyDefinition() throws Exception {
6470
@DoNotRecord(skipInPlayback = true)
6571
public void canCRUDPolicyAssignment() throws Exception {
6672
String policyName = generateRandomResourceName("policy", 15);
73+
String policyName2 = generateRandomResourceName("policy2", 15);
6774
String displayName = generateRandomResourceName("mypolicy", 15);
6875
String rgName = generateRandomResourceName("javarg", 15);
6976
String assignmentName1 = generateRandomResourceName("assignment1", 15);
7077
String assignmentName2 = generateRandomResourceName("assignment2", 15);
78+
String assignmentName3 = generateRandomResourceName("assignment3", 15);
7179
String resourceName = generateRandomResourceName("webassignment", 15);
7280
try {
7381
// Create definition
@@ -90,13 +98,18 @@ public void canCRUDPolicyAssignment() throws Exception {
9098
Assertions.assertNotNull(assignment1);
9199
Assertions.assertEquals("My Assignment", assignment1.displayName());
92100

101+
Assertions.assertEquals(group.id(), assignment1.scope());
102+
Assertions.assertEquals(0, assignment1.excludedScopes().size());
103+
Assertions.assertEquals(EnforcementMode.DEFAULT, assignment1.enforcementMode());
104+
Assertions.assertEquals(0, assignment1.parameters().size());
105+
93106
GenericResource resource = resourceClient.genericResources().define(resourceName)
94107
.withRegion(Region.US_SOUTH_CENTRAL)
95108
.withExistingResourceGroup(group)
96109
.withResourceType("sites")
97110
.withProviderNamespace("Microsoft.Web")
98111
.withoutPlan()
99-
.withApiVersion("2015-08-01")
112+
.withApiVersion("2020-12-01")
100113
.withParentResourcePath("")
101114
.withProperties(new ObjectMapper().readTree("{\"SiteMode\":\"Limited\",\"ComputeMode\":\"Shared\"}"))
102115
.create();
@@ -125,10 +138,37 @@ public void canCRUDPolicyAssignment() throws Exception {
125138
Assertions.assertTrue(foundAssignment1);
126139
Assertions.assertTrue(foundAssignment2);
127140

141+
// definition and assignment with parameters
142+
PolicyDefinition definition2 = resourceClient.policyDefinitions().define(policyName)
143+
.withPolicyRuleJson(policyRule2)
144+
.withPolicyType(PolicyType.CUSTOM)
145+
.withParameter("prefix", ParameterType.STRING, "dept")
146+
.withParameter("suffix", new ParameterDefinitionsValue().withType(ParameterType.STRING).withDefaultValue("-US"))
147+
.withDisplayName(displayName)
148+
.withDescription("Test policy")
149+
.create();
150+
PolicyAssignment assignment3 = resourceClient.policyAssignments().define(assignmentName3)
151+
.forResourceGroup(group)
152+
.withPolicyDefinition(definition2)
153+
.withExcludedScope(resource.id())
154+
.withEnforcementMode(EnforcementMode.DO_NOT_ENFORCE)
155+
.withParameter("prefix", "DeptA")
156+
.withParameter("suffix", "-LC")
157+
.withDisplayName("Test Assignment")
158+
.create();
159+
160+
assignment3 = resourceClient.policyAssignments().getById(assignment3.id());
161+
Assertions.assertEquals(group.id(), assignment3.scope());
162+
Assertions.assertEquals(Collections.singletonList(resource.id()), assignment3.excludedScopes());
163+
Assertions.assertEquals(EnforcementMode.DO_NOT_ENFORCE, assignment3.enforcementMode());
164+
Assertions.assertEquals(2, assignment3.parameters().size());
165+
128166
// Delete
129167
resourceClient.policyAssignments().deleteById(assignment1.id());
130168
resourceClient.policyAssignments().deleteById(assignment2.id());
169+
resourceClient.policyAssignments().deleteById(assignment3.id());
131170
resourceClient.policyDefinitions().deleteByName(policyName);
171+
resourceClient.policyDefinitions().deleteByName(policyName2);
132172
} finally {
133173
resourceClient.resourceGroups().deleteByName(rgName);
134174
}

0 commit comments

Comments
 (0)