Skip to content

Commit d7e0b5a

Browse files
authored
mgmt, bug fix for cdn (#46262)
* test case pom * fix customDomain * support deleteByName in DnsRecords * fix record test * recording, changelog * revert Dns new feature * update samples recording * do not call list when no to delete * changelog
1 parent 4da271b commit d7e0b5a

File tree

7 files changed

+146
-23
lines changed

7 files changed

+146
-23
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fixed a bug in `CdnEndpoint.withoutCustomDomain(hostname)` implementation. Now it should work as expected.
12+
1113
### Other Changes
1214

1315
## 2.53.0 (2025-07-25)

sdk/resourcemanager/azure-resourcemanager-cdn/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "java",
44
"TagPrefix": "java/resourcemanager/azure-resourcemanager-cdn",
5-
"Tag": "java/resourcemanager/azure-resourcemanager-cdn_684f7f8637"
5+
"Tag": "java/resourcemanager/azure-resourcemanager-cdn_6809e0b2b9"
66
}

sdk/resourcemanager/azure-resourcemanager-cdn/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
--add-opens com.azure.resourcemanager.cdn/com.azure.resourcemanager.cdn=ALL-UNNAMED
4545

4646
--add-opens com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED
47+
48+
--add-exports com.azure.resourcemanager.resources/com.azure.resourcemanager.resources.fluentcore.arm.implementation=ALL-UNNAMED
49+
--add-exports com.azure.resourcemanager.resources/com.azure.resourcemanager.resources.fluentcore.arm.collection.implementation=ALL-UNNAMED
50+
--add-exports com.azure.resourcemanager.resources/com.azure.resourcemanager.resources.fluentcore.arm.models.implementation=ALL-UNNAMED
51+
--add-exports com.azure.resourcemanager.resources/com.azure.resourcemanager.resources.fluentcore.model.implementation=ALL-UNNAMED
52+
--add-opens com.azure.resourcemanager.resources/com.azure.resourcemanager.resources=ALL-UNNAMED
53+
--add-opens com.azure.resourcemanager.resources/com.azure.resourcemanager.resources.fluentcore.arm=ALL-UNNAMED
4754
</javaModulesSurefireArgLine>
4855
</properties>
4956

@@ -60,6 +67,18 @@
6067
<artifactId>azure-resourcemanager-resources</artifactId>
6168
<version>2.54.0-beta.1</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;current} -->
6269
</dependency>
70+
<dependency>
71+
<groupId>com.azure.resourcemanager</groupId>
72+
<artifactId>azure-resourcemanager-appservice</artifactId>
73+
<version>2.54.0-beta.1</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-appservice;current} -->
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.azure.resourcemanager</groupId>
78+
<artifactId>azure-resourcemanager-dns</artifactId>
79+
<version>2.54.0-beta.1</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-dns;current} -->
80+
<scope>test</scope>
81+
</dependency>
6382
<dependency>
6483
<groupId>org.junit.jupiter</groupId>
6584
<artifactId>junit-jupiter-engine</artifactId>

sdk/resourcemanager/azure-resourcemanager-cdn/src/main/java/com/azure/resourcemanager/cdn/implementation/CdnEndpointImpl.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Comparator;
3434
import java.util.HashMap;
3535
import java.util.HashSet;
36+
import java.util.LinkedHashSet;
3637
import java.util.List;
3738
import java.util.Map;
3839
import java.util.Set;
@@ -57,15 +58,15 @@ class CdnEndpointImpl extends ExternalChildResourceImpl<CdnEndpoint, EndpointInn
5758

5859
CdnEndpoint.UpdateStandardEndpoint, CdnEndpoint.UpdatePremiumEndpoint {
5960

60-
private List<CustomDomainInner> customDomainList;
61-
private List<CustomDomainInner> deletedCustomDomainList;
61+
private Set<String> customDomainHostnames;
62+
private Set<String> deletedCustomDomainHostnames;
6263
// rule map for rules engine in Standard Microsoft SKU, indexed by rule name
6364
private final Map<String, DeliveryRule> standardRulesEngineRuleMap = new HashMap<>();
6465

6566
CdnEndpointImpl(String name, CdnProfileImpl parent, EndpointInner inner) {
6667
super(name, parent, inner);
67-
this.customDomainList = new ArrayList<>();
68-
this.deletedCustomDomainList = new ArrayList<>();
68+
this.customDomainHostnames = new LinkedHashSet<>();
69+
this.deletedCustomDomainHostnames = new HashSet<>();
6970
initializeRuleMapForStandardMicrosoftSku();
7071
}
7172

@@ -94,8 +95,8 @@ public Mono<CdnEndpoint> createResourceAsync() {
9495
.createAsync(this.parent().resourceGroupName(), this.parent().name(), this.name(), this.innerModel())
9596
.flatMap(inner -> {
9697
self.setInner(inner);
97-
return Flux.fromIterable(self.customDomainList)
98-
.flatMapDelayError(customDomainInner -> self.parent()
98+
return Flux.fromIterable(self.customDomainHostnames)
99+
.flatMapDelayError(customDomain -> self.parent()
99100
.manager()
100101
.serviceClient()
101102
.getCustomDomains()
@@ -105,7 +106,7 @@ public Mono<CdnEndpoint> createResourceAsync() {
105106
.resourceManager()
106107
.internalContext()
107108
.randomResourceName("CustomDomain", 50),
108-
new CustomDomainParameters().withHostname(customDomainInner.hostname())),
109+
new CustomDomainParameters().withHostname(customDomain)),
109110
32, 32)
110111
.then(self.parent()
111112
.manager()
@@ -114,7 +115,9 @@ public Mono<CdnEndpoint> createResourceAsync() {
114115
.listByEndpointAsync(self.parent().resourceGroupName(), self.parent().name(), self.name())
115116
.collectList()
116117
.map(customDomainInners -> {
117-
self.customDomainList.addAll(customDomainInners);
118+
self.customDomainHostnames.addAll(customDomainInners.stream()
119+
.map(CustomDomainInner::hostname)
120+
.collect(Collectors.toSet()));
118121
return self;
119122
}));
120123
});
@@ -166,31 +169,42 @@ public Mono<CdnEndpoint> updateResourceAsync() {
166169
.updateAsync(this.parent().resourceGroupName(), this.parent().name(), this.name(),
167170
endpointUpdateParameters);
168171

169-
Flux<CustomDomainInner> customDomainCreateTask = Flux.fromIterable(this.customDomainList)
172+
Flux<CustomDomainInner> customDomainCreateTask = Flux.fromIterable(this.customDomainHostnames)
170173
.flatMapDelayError(itemToCreate -> this.parent()
171174
.manager()
172175
.serviceClient()
173176
.getCustomDomains()
174177
.createAsync(this.parent().resourceGroupName(), this.parent().name(), this.name(),
175178
self.parent().manager().resourceManager().internalContext().randomResourceName("CustomDomain", 50),
176-
new CustomDomainParameters().withHostname(itemToCreate.hostname())),
179+
new CustomDomainParameters().withHostname(itemToCreate)),
177180
32, 32);
178181

179-
Flux<CustomDomainInner> customDomainDeleteTask = Flux.fromIterable(this.deletedCustomDomainList)
180-
.flatMapDelayError(itemToDelete -> this.parent()
182+
Flux<CustomDomainInner> customDomainDeleteTask;
183+
if (this.deletedCustomDomainHostnames.isEmpty()) {
184+
customDomainDeleteTask = Flux.empty();
185+
} else {
186+
customDomainDeleteTask = this.parent()
181187
.manager()
182188
.serviceClient()
183189
.getCustomDomains()
184-
.deleteAsync(this.parent().resourceGroupName(), this.parent().name(), this.name(), itemToDelete.name()),
185-
32, 32);
190+
.listByEndpointAsync(this.parent().resourceGroupName(), this.parent().name(), this.name())
191+
.filter(customDomain -> this.deletedCustomDomainHostnames.contains(customDomain.hostname()))
192+
.flatMapDelayError(itemToDelete -> this.parent()
193+
.manager()
194+
.serviceClient()
195+
.getCustomDomains()
196+
.deleteAsync(this.parent().resourceGroupName(), this.parent().name(), this.name(),
197+
itemToDelete.name()),
198+
32, 32);
199+
}
186200

187201
Mono<EndpointInner> customDomainTask
188202
= Flux.concat(customDomainCreateTask, customDomainDeleteTask).then(Mono.empty());
189203

190204
return Flux.mergeDelayError(32, customDomainTask, originUpdateTask, endpointUpdateTask).last().map(inner -> {
191205
self.setInner(inner);
192-
self.customDomainList.clear();
193-
self.deletedCustomDomainList.clear();
206+
self.customDomainHostnames.clear();
207+
self.deletedCustomDomainHostnames.clear();
194208
return self;
195209
});
196210
}
@@ -208,8 +222,8 @@ public Mono<Void> deleteResourceAsync() {
208222
public Mono<CdnEndpoint> refreshAsync() {
209223
final CdnEndpointImpl self = this;
210224
return super.refreshAsync().flatMap(cdnEndpoint -> {
211-
self.customDomainList.clear();
212-
self.deletedCustomDomainList.clear();
225+
self.customDomainHostnames.clear();
226+
self.deletedCustomDomainHostnames.clear();
213227
initializeRuleMapForStandardMicrosoftSku();
214228
return self.parent()
215229
.manager()
@@ -218,7 +232,8 @@ public Mono<CdnEndpoint> refreshAsync() {
218232
.listByEndpointAsync(self.parent().resourceGroupName(), self.parent().name(), self.name())
219233
.collectList()
220234
.map(customDomainInners -> {
221-
self.customDomainList.addAll(customDomainInners);
235+
self.customDomainHostnames.addAll(
236+
customDomainInners.stream().map(CustomDomainInner::hostname).collect(Collectors.toSet()));
222237
return self;
223238
});
224239
});
@@ -580,7 +595,7 @@ public CdnEndpointImpl withoutGeoFilter(String relativePath) {
580595

581596
@Override
582597
public CdnEndpointImpl withCustomDomain(String hostName) {
583-
this.customDomainList.add(new CustomDomainInner().withHostname(hostName));
598+
this.customDomainHostnames.add(hostName);
584599
return this;
585600
}
586601

@@ -607,7 +622,7 @@ public CdnEndpointImpl withoutStandardRulesEngineRule(String name) {
607622

608623
@Override
609624
public CdnEndpointImpl withoutCustomDomain(String hostName) {
610-
deletedCustomDomainList.add(new CustomDomainInner().withHostname(hostName));
625+
deletedCustomDomainHostnames.add(hostName);
611626
return this;
612627
}
613628

sdk/resourcemanager/azure-resourcemanager-cdn/src/test/java/com/azure/resourcemanager/cdn/CdnManagementTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
import com.azure.core.http.policy.HttpPipelinePolicy;
1111
import com.azure.core.http.policy.RetryPolicy;
1212
import com.azure.core.management.profile.AzureProfile;
13+
import com.azure.resourcemanager.appservice.AppServiceManager;
14+
import com.azure.resourcemanager.dns.DnsZoneManager;
1315
import com.azure.resourcemanager.resources.ResourceManager;
1416
import com.azure.resourcemanager.resources.fluentcore.utils.HttpPipelineProvider;
1517
import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils;
1618
import com.azure.resourcemanager.test.ResourceManagerTestProxyTestBase;
1719
import com.azure.resourcemanager.test.utils.TestDelayProvider;
20+
import com.azure.resourcemanager.test.utils.TestIdentifierProvider;
1821

1922
import java.time.temporal.ChronoUnit;
2023
import java.util.List;
@@ -23,6 +26,8 @@ public abstract class CdnManagementTest extends ResourceManagerTestProxyTestBase
2326

2427
protected ResourceManager resourceManager;
2528
protected CdnManager cdnManager;
29+
protected AppServiceManager appServiceManager;
30+
protected DnsZoneManager dnsZoneManager;
2631

2732
@Override
2833
protected HttpPipeline buildHttpPipeline(TokenCredential credential, AzureProfile profile,
@@ -34,7 +39,12 @@ protected HttpPipeline buildHttpPipeline(TokenCredential credential, AzureProfil
3439
@Override
3540
protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile) {
3641
ResourceManagerUtils.InternalRuntimeContext.setDelayProvider(new TestDelayProvider(!isPlaybackMode()));
42+
ResourceManagerUtils.InternalRuntimeContext internalContext = new ResourceManagerUtils.InternalRuntimeContext();
43+
internalContext.setIdentifierFunction(name -> new TestIdentifierProvider(testResourceNamer));
3744
cdnManager = buildManager(CdnManager.class, httpPipeline, profile);
3845
resourceManager = cdnManager.resourceManager();
46+
appServiceManager = buildManager(AppServiceManager.class, httpPipeline, profile);
47+
dnsZoneManager = buildManager(DnsZoneManager.class, httpPipeline, profile);
48+
setInternalContext(internalContext, cdnManager, resourceManager, appServiceManager, dnsZoneManager);
3949
}
4050
}

sdk/resourcemanager/azure-resourcemanager-cdn/src/test/java/com/azure/resourcemanager/cdn/CdnProfileOperationsTests.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.azure.core.http.rest.PagedIterable;
88
import com.azure.core.management.Region;
99
import com.azure.core.management.profile.AzureProfile;
10+
import com.azure.resourcemanager.appservice.models.AppServiceDomain;
1011
import com.azure.resourcemanager.cdn.models.CacheBehavior;
1112
import com.azure.resourcemanager.cdn.models.CacheExpirationActionParameters;
1213
import com.azure.resourcemanager.cdn.models.CacheType;
@@ -25,6 +26,9 @@
2526
import com.azure.resourcemanager.cdn.models.RequestSchemeMatchConditionParametersMatchValuesItem;
2627
import com.azure.resourcemanager.cdn.models.UrlRedirectAction;
2728
import com.azure.resourcemanager.cdn.models.UrlRedirectActionParameters;
29+
import com.azure.resourcemanager.dns.models.DnsZone;
30+
import com.azure.resourcemanager.resources.fluentcore.arm.CountryIsoCode;
31+
import com.azure.resourcemanager.resources.fluentcore.arm.CountryPhoneCode;
2832
import com.azure.resourcemanager.resources.models.ResourceGroup;
2933
import com.azure.resourcemanager.test.utils.TestUtilities;
3034
import org.junit.jupiter.api.Assertions;
@@ -300,4 +304,77 @@ public void canCrudStandardRulesEngineRules() {
300304

301305
Assertions.assertEquals(2, cdnProfile.endpoints().get(cdnEndpointName).standardRulesEngineRules().size());
302306
}
307+
308+
@Test
309+
public void canCreateWithCustomDomain() {
310+
String cdnProfileName = generateRandomResourceName("cdnp", 15);
311+
String cdnEndpointName = generateRandomResourceName("cdnendp", 15);
312+
String domainName = generateRandomResourceName("jsdkcdn", 15) + ".com";
313+
String cname1 = "c1";
314+
String cname2 = "c2";
315+
String customDomain1 = cname1 + "." + domainName;
316+
String customDomain2 = cname2 + "." + domainName;
317+
318+
// purchase domain
319+
AppServiceDomain domain = appServiceManager.domains()
320+
.define(domainName)
321+
.withExistingResourceGroup(resourceManager.resourceGroups().define(rgName).withRegion(region).create())
322+
.defineRegistrantContact()
323+
.withFirstName("Jon")
324+
.withLastName("Doe")
325+
.withEmail("[email protected]")
326+
.withAddressLine1("123 4th Ave")
327+
.withCity("Redmond")
328+
.withStateOrProvince("WA")
329+
.withCountry(CountryIsoCode.UNITED_STATES)
330+
.withPostalCode("98052")
331+
.withPhoneCountryCode(CountryPhoneCode.UNITED_STATES)
332+
.withPhoneNumber("4258828080")
333+
.attach()
334+
.withDomainPrivacyEnabled(true)
335+
.withAutoRenewEnabled(false)
336+
.create();
337+
// create cname record for the custom domains and cdn endpoint
338+
DnsZone dnsZone = dnsZoneManager.zones()
339+
.define(domainName)
340+
.withExistingResourceGroup(rgName)
341+
.withCNameRecordSet(cname1, cdnEndpointName + ".azureedge.net")
342+
.withCNameRecordSet(cname2, cdnEndpointName + ".azureedge.net")
343+
.create();
344+
345+
CdnProfile cdnProfile = cdnManager.profiles()
346+
.define(cdnProfileName)
347+
.withRegion(region)
348+
.withNewResourceGroup(rgName)
349+
.withStandardMicrosoftSku()
350+
.defineNewEndpoint(cdnEndpointName)
351+
.withOrigin("origin1", "www.someDomain.net")
352+
.withHttpAllowed(false)
353+
.withHttpsAllowed(true)
354+
.withCustomDomain(customDomain1)
355+
.withCustomDomain(customDomain2)
356+
.attach()
357+
.create();
358+
359+
Assertions.assertNotNull(cdnProfile);
360+
Assertions.assertEquals(cdnProfileName, cdnProfile.name());
361+
362+
Map<String, CdnEndpoint> cdnEndpointMap = cdnProfile.endpoints();
363+
Assertions.assertEquals(1, cdnProfile.endpoints().size());
364+
365+
CdnEndpoint cdnEndpoint = cdnEndpointMap.get(cdnEndpointName);
366+
Assertions.assertEquals(2, cdnEndpoint.customDomains().size());
367+
368+
// delete the cname of custom domain1
369+
// Starting from April 9th 2021, Azure CDN requires removal of the CNAME records to Azure CDN endpoints before the resources can be deleted.
370+
// Resources include Azure CDN custom domains, Azure CDN profiles/endpoints or Azure resource groups that has Azure CDN custom domain(s) enabled.
371+
// https://learn.microsoft.com/answers/questions/1189452/trying-to-delete-cdn-endpoint-custom-domain
372+
dnsZone.update().withoutCNameRecordSet(cname1).apply();
373+
374+
// remove custom domain
375+
cdnProfile.update().updateEndpoint(cdnEndpointName).withoutCustomDomain(customDomain1).parent().apply();
376+
377+
cdnEndpoint.refresh();
378+
Assertions.assertEquals(1, cdnEndpoint.customDomains().size());
379+
}
303380
}

sdk/resourcemanager/azure-resourcemanager-samples/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "java",
44
"TagPrefix": "java/resourcemanager/azure-resourcemanager-samples",
5-
"Tag": "java/resourcemanager/azure-resourcemanager-samples_a67e3d0d37"
5+
"Tag": "java/resourcemanager/azure-resourcemanager-samples_2180f5586f"
66
}

0 commit comments

Comments
 (0)