Skip to content

Commit

Permalink
change "georeplications" type from list to set
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Apr 20, 2021
1 parent 599bfc2 commit e08fb47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
22 changes: 11 additions & 11 deletions azurerm/internal/services/containers/container_registry_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func resourceContainerRegistry() *schema.Resource {
},

"georeplications": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Computed: true, // TODO -- remove this when deprecation resolves
ConflictsWith: []string{"georeplication_locations"},
Expand Down Expand Up @@ -250,8 +250,8 @@ func resourceContainerRegistry() *schema.Resource {
CustomizeDiff: func(d *schema.ResourceDiff, v interface{}) error {
sku := d.Get("sku").(string)
geoReplicationLocations := d.Get("georeplication_locations").(*schema.Set)
geoReplications := d.Get("georeplications").([]interface{})
hasGeoReplicationsApplied := geoReplicationLocations.Len() > 0 || len(geoReplications) > 0
geoReplications := d.Get("georeplications").(*schema.Set)
hasGeoReplicationsApplied := geoReplicationLocations.Len() > 0 || geoReplications.Len() > 0
// if locations have been specified for geo-replication then, the SKU has to be Premium
if hasGeoReplicationsApplied && !strings.EqualFold(sku, string(containerregistry.Premium)) {
return fmt.Errorf("ACR geo-replication can only be applied when using the Premium Sku.")
Expand Down Expand Up @@ -317,7 +317,7 @@ func resourceContainerRegistryCreate(d *schema.ResourceData, meta interface{}) e
adminUserEnabled := d.Get("admin_enabled").(bool)
t := d.Get("tags").(map[string]interface{})
geoReplicationLocations := d.Get("georeplication_locations").(*schema.Set)
geoReplications := d.Get("georeplications").([]interface{})
geoReplications := d.Get("georeplications").(*schema.Set)

networkRuleSet := expandNetworkRuleSet(d.Get("network_rule_set").([]interface{}))
if networkRuleSet != nil && !strings.EqualFold(sku, string(containerregistry.Premium)) {
Expand Down Expand Up @@ -382,7 +382,7 @@ func resourceContainerRegistryCreate(d *schema.ResourceData, meta interface{}) e
if geoReplicationLocations != nil && geoReplicationLocations.Len() > 0 {
newGeoReplicationLocations = expandReplicationsFromLocations(geoReplicationLocations.List())
} else {
newGeoReplicationLocations = expandReplications(geoReplications)
newGeoReplicationLocations = expandReplications(geoReplications.List())
}
// geo replications have been specified
if len(newGeoReplicationLocations) > 0 {
Expand Down Expand Up @@ -431,8 +431,8 @@ func resourceContainerRegistryUpdate(d *schema.ResourceData, meta interface{}) e

oldReplicationsRaw, newReplicationsRaw := d.GetChange("georeplications")
hasGeoReplicationsChanges := d.HasChange("georeplications")
oldReplications := oldReplicationsRaw.([]interface{})
newReplications := newReplicationsRaw.([]interface{})
oldReplications := oldReplicationsRaw.(*schema.Set)
newReplications := newReplicationsRaw.(*schema.Set)

// handle upgrade to Premium SKU first
if skuChange && isPremiumSku {
Expand Down Expand Up @@ -470,13 +470,13 @@ func resourceContainerRegistryUpdate(d *schema.ResourceData, meta interface{}) e
}

// geo replication is only supported by Premium Sku
hasGeoReplicationsApplied := newGeoReplicationLocations.Len() > 0 || len(newReplications) > 0
hasGeoReplicationsApplied := newGeoReplicationLocations.Len() > 0 || newReplications.Len() > 0
if hasGeoReplicationsApplied && !strings.EqualFold(sku, string(containerregistry.Premium)) {
return fmt.Errorf("ACR geo-replication can only be applied when using the Premium Sku.")
}

if hasGeoReplicationsChanges {
err := applyGeoReplicationLocations(d, meta, resourceGroup, name, expandReplications(oldReplications), expandReplications(newReplications))
err := applyGeoReplicationLocations(d, meta, resourceGroup, name, expandReplications(oldReplications.List()), expandReplications(newReplications.List()))
if err != nil {
return fmt.Errorf("Error applying geo replications for Container Registry %q (Resource Group %q): %+v", name, resourceGroup, err)
}
Expand Down Expand Up @@ -549,7 +549,7 @@ func applyGeoReplicationLocations(d *schema.ResourceData, meta interface{}, reso

// delete previously deployed locations
for _, replication := range oldGeoReplications {
if replication == nil {
if replication == nil || len(*replication.Location) == 0 {
continue
}
oldLocation := azure.NormalizeLocation(*replication.Location)
Expand All @@ -565,7 +565,7 @@ func applyGeoReplicationLocations(d *schema.ResourceData, meta interface{}, reso

// create new geo-replication locations
for _, replication := range newGeoReplications {
if replication == nil {
if replication == nil || len(*replication.Location) == 0 {
continue
}
locationToCreate := azure.NormalizeLocation(*replication.Location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,7 @@ func TestAccContainerRegistry_geoReplication(t *testing.T) {
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku").HasValue(skuPremium),
check.That(data.ResourceName).Key("georeplications.#").HasValue("2"),
check.That(data.ResourceName).Key("georeplications.0.location").HasValue(secondaryLocation),
check.That(data.ResourceName).Key("georeplications.0.tags.%").HasValue("0"),
check.That(data.ResourceName).Key("georeplications.1.location").HasValue(ternaryLocation),
check.That(data.ResourceName).Key("georeplications.1.tags.%").HasValue("0"),
),
},
Expand Down Expand Up @@ -368,10 +366,6 @@ func TestAccContainerRegistry_geoReplicationSwitch(t *testing.T) {
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku").HasValue(skuPremium),
check.That(data.ResourceName).Key("georeplications.#").HasValue("2"),
check.That(data.ResourceName).Key("georeplications.0.location").HasValue(secondaryLocation),
check.That(data.ResourceName).Key("georeplications.0.tags.%").HasValue("0"),
check.That(data.ResourceName).Key("georeplications.1.location").HasValue(ternaryLocation),
check.That(data.ResourceName).Key("georeplications.1.tags.%").HasValue("0"),
),
},
data.ImportStep(),
Expand Down

0 comments on commit e08fb47

Please sign in to comment.