Skip to content
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

Filter empty length key diffs in Storage Account resource #816

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion config/storage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,51 @@
package storage

import (
"errors"

"github.com/crossplane/upjet/pkg/config"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/upbound/provider-azure/apis/rconfig"
"github.com/upbound/provider-azure/config/common"
)

// Configure configures storage group
func Configure(p *config.Provider) {
func Configure(p *config.Provider) { //nolint:gocyclo
p.AddResourceConfigurator("azurerm_storage_account", func(r *config.Resource) {
r.TerraformCustomDiff = func(diff *terraform.InstanceDiff, state *terraform.InstanceState, config *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
// skip diff customization on create
if state == nil || state.Empty() {
return diff, nil
}
if config == nil {
return nil, errors.New("resource config cannot be nil")
}
// skip no diff or destroy diffs
if diff == nil || diff.Empty() || diff.Destroy || diff.Attributes == nil {
return diff, nil
}

lengthDiffKeys := []string{
"blob_properties.#",
"share_properties.#",
"queue_properties.#",
}
for _, key := range lengthDiffKeys {
// Note(cem): We should consider filtering effectively-empty
// (Old = "" and New = "") length keys (`*.#` and `*.%`) in
// Upjet. Doing so _should be_ safe, but in case we are afraid
// to deploy at scale, we might tie the functionality to a
// resource configuration flag, as a precaution.
if diff.Attributes[key] != nil && diff.Attributes[key].Old == "" && diff.Attributes[key].New == "" {
delete(diff.Attributes, key)
}
}

return diff, nil
}
})

p.AddResourceConfigurator("azurerm_storage_blob", func(r *config.Resource) {
r.References["storage_account_name"] = config.Reference{
TerraformName: "azurerm_storage_account",
Expand Down
44 changes: 44 additions & 0 deletions examples/storage/v1beta1/account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
#
# SPDX-License-Identifier: CC0-1.0

# Note that this resource is not _properly_ Uptestable, because Uptest
# cannot generate a random string that conforms to Azure Storage
# Account naming criteria:
# https://learn.microsoft.com/en-us/azure/storage/common/storage-account-overview#storage-account-name
#
# Uptest should succeed, as long as the hardcoded `metadata.name`
# below is available.

apiVersion: storage.azure.upbound.io/v1beta1
kind: Account
metadata:
annotations:
meta.upbound.io/example-id: storage/v1beta1/account
labels:
testing.upbound.io/example-name: example-storage
name: examplesa539891
spec:
forProvider:
accountReplicationType: LRS
accountTier: Standard
location: "West Europe"
resourceGroupNameSelector:
matchLabels:
testing.upbound.io/example-name: example-storage

---

apiVersion: azure.upbound.io/v1beta1
kind: ResourceGroup
metadata:
annotations:
meta.upbound.io/example-id: storage/v1beta1/account
labels:
testing.upbound.io/example-name: example-storage
name: example-storage-${Rand.RFC1123Subdomain}
spec:
forProvider:
location: "West Europe"
tags:
provisioner: crossplane
Loading