Skip to content

Commit

Permalink
mgmt, skip PATCH if no change to private dns zone (Azure#19343)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Feb 20, 2021
1 parent f971a33 commit cdc8c1b
Show file tree
Hide file tree
Showing 8 changed files with 1,444 additions and 1,435 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public JsonProperty.Access findPropertyAccess(Annotated annotated) {
@Override
public VirtualMachineImpl update() {
updateParameterSnapshotOnUpdate = this.deepCopyInnerToUpdateParameter();
return this;
return super.update();
};

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.2.0-beta.1 (Unreleased)

- Improved performance on `PrivateDnsZone` update

## 2.1.0 (2020-11-24)

Expand Down
6 changes: 6 additions & 0 deletions sdk/resourcemanager/azure-resourcemanager-privatedns/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<version>1.8.0</version> <!-- {x-version-update;com.azure:azure-core-http-netty;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version> <!-- {x-version-update;org.slf4j:slf4j-simple;external_dependency} -->
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import reactor.core.publisher.Mono;
import com.azure.resourcemanager.resources.fluentcore.utils.PagedConverter;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/** Implementation for {@link PrivateDnsZone}. */
class PrivateDnsZoneImpl
extends GroupableResourceImpl<PrivateDnsZone, PrivateZoneInner, PrivateDnsZoneImpl, PrivateDnsZoneManager>
Expand All @@ -42,6 +46,8 @@ class PrivateDnsZoneImpl
private VirtualNetworkLinksImpl virtualNetworkLinks;
private final ETagState etagState = new ETagState();

private Map<String, String> resourceTagsSnapshotOnUpdate = null;

PrivateDnsZoneImpl(String name, final PrivateZoneInner innerModel, final PrivateDnsZoneManager manager) {
super(name, innerModel, manager);
this.recordSets = new PrivateDnsRecordSetsImpl(this);
Expand Down Expand Up @@ -384,20 +390,48 @@ public Update withETagCheck(String etagValue) {

@Override
public Mono<PrivateDnsZone> createResourceAsync() {
return manager().serviceClient().getPrivateZones()
.createOrUpdateAsync(
resourceGroupName(),
name(),
innerModel(),
etagState.ifMatchValueOnUpdate(innerModel().etag()),
etagState.ifNonMatchValueOnCreate())
.map(innerToFluentMap(this))
Mono<PrivateDnsZone> mono;
if (isInCreateMode()) {
mono = manager().serviceClient().getPrivateZones()
.createOrUpdateAsync(
resourceGroupName(),
name(),
innerModel(),
etagState.ifMatchValueOnUpdate(innerModel().etag()),
etagState.ifNonMatchValueOnCreate())
.map(innerToFluentMap(this));
} else {
if (!Objects.equals(resourceTagsSnapshotOnUpdate, innerModel().tags())) {
mono = manager().serviceClient().getPrivateZones()
.updateAsync(
resourceGroupName(),
name(),
innerModel(),
etagState.ifMatchValueOnUpdate(innerModel().etag()))
.map(innerToFluentMap(this));
} else {
// skip update, as tags is the only property to update
mono = Mono.just(this);
}
}

return mono
.map(privateDnsZone -> {
etagState.clear();
return privateDnsZone;
});
}

@Override
public PrivateDnsZoneImpl update() {
if (innerModel() != null && innerModel().tags() != null) {
resourceTagsSnapshotOnUpdate = new HashMap<>(innerModel().tags());
} else {
resourceTagsSnapshotOnUpdate = null;
}
return super.update();
}

@Override
public Mono<Void> afterPostRunAsync(boolean isGroupFaulted) {
return Mono.just(true).map(ignored -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.junit.jupiter.api.Test;

import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class PrivateDnsZoneRecordSetETagTests extends ResourceManagerTestBase {
Expand Down Expand Up @@ -63,7 +66,40 @@ protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile

@Override
protected void cleanUpResources() {
resourceManager.resourceGroups().deleteByName(rgName);
resourceManager.resourceGroups().beginDeleteByName(rgName);
}

@Test
public void canUpdateZone() {
final Region region = Region.US_EAST;
final String topLevelDomain = "www.contoso" + generateRandomResourceName("z", 10) + ".com";

PrivateDnsZone privateDnsZone =
privateZoneManager.privateZones().define(topLevelDomain)
.withNewResourceGroup(rgName, region)
.withTag("key1", "value1")
.create();

privateDnsZone.update()
.withoutTag("key1")
.withTag("key2", "value2")
.withTag("key3", "value3")
.apply();

privateDnsZone.refresh();
Assertions.assertEquals(2, privateDnsZone.tags().size());
Assertions.assertEquals(new HashSet<>(Arrays.asList("key2", "key3")), privateDnsZone.tags().keySet());
Assertions.assertEquals(Arrays.asList("value2", "value3"), new ArrayList<>(privateDnsZone.tags().values()));

privateDnsZone.update()
.defineARecordSet("www")
.withIPv4Address("23.96.104.40")
.withIPv4Address("24.97.105.41")
.withTimeToLive(7200)
.withETagCheck()
.attach()
.apply();
Assertions.assertEquals(1, TestUtilities.getSize(privateDnsZone.aRecordSets().list()));
}

@Test
Expand Down Expand Up @@ -95,7 +131,10 @@ public void canUpdateZoneWithExplicitETag() {
privateZoneManager.privateZones().define(topLevelDomain).withNewResourceGroup(rgName, region).withETagCheck().create();
Assertions.assertNotNull(privateDnsZone.etag());

Runnable runnable = () -> privateDnsZone.update().withETagCheck(privateDnsZone.etag() + "-foo").apply();
Runnable runnable = () -> privateDnsZone.update()
.withTag("k1", "v1")
.withETagCheck(privateDnsZone.etag() + "-foo")
.apply();
ensureETagExceptionIsThrown(runnable);
privateDnsZone.update().withETagCheck(privateDnsZone.etag()).apply();
}
Expand Down
Loading

0 comments on commit cdc8c1b

Please sign in to comment.