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

azurerm_app_configuration - support for replica #22452

Merged
merged 3 commits into from
Sep 20, 2023
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
37 changes: 37 additions & 0 deletions internal/services/appconfiguration/app_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
package appconfiguration

import (
"bytes"
"context"
"fmt"

"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2023-03-01/configurationstores"
"github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2023-03-01/replicas"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/tombuildsstuff/kermit/sdk/appconfiguration/1.0/appconfiguration"
)
Expand All @@ -25,6 +29,39 @@ func flattenAppConfigurationEncryption(input *configurationstores.EncryptionProp
},
}
}

func flattenAppConfigurationReplicas(input []replicas.Replica) ([]interface{}, error) {
results := make([]interface{}, 0)
for _, v := range input {
if v.Properties == nil {
return results, fmt.Errorf("retrieving Replica %s Properties is nil", *v.Id)
}

replicaId, err := replicas.ParseReplicaIDInsensitively(pointer.From(v.Id))
if err != nil {
return results, err
}

result := map[string]interface{}{
"name": pointer.From(v.Name),
"location": pointer.From(v.Location),
"endpoint": pointer.From(v.Properties.Endpoint),
"id": replicaId.ID(),
}
results = append(results, result)
}
return results, nil
}

func resourceConfigurationStoreReplicaHash(input interface{}) int {
var buf bytes.Buffer
if rawData, ok := input.(map[string]interface{}); ok {
buf.WriteString(rawData["name"].(string))
buf.WriteString(rawData["location"].(string))
}
return pluginsdk.HashString(buf.String())
}

func appConfigurationGetKeyRefreshFunc(ctx context.Context, client *appconfiguration.BaseClient, key, label string) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.GetKeyValue(ctx, key, label, "", "", "", []appconfiguration.KeyValueFields{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2023-03-01/configurationstores"
"github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2023-03-01/replicas"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -93,6 +94,28 @@ func dataSourceAppConfiguration() *pluginsdk.Resource {
Computed: true,
},

"replica": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"location": commonschema.LocationComputed(),
"endpoint": {
Type: pluginsdk.TypeString,
Computed: true,
},
"id": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"primary_read_key": {
Type: pluginsdk.TypeList,
Computed: true,
Expand Down Expand Up @@ -257,6 +280,19 @@ func dataSourceAppConfigurationRead(d *pluginsdk.ResourceData, meta interface{})
return fmt.Errorf("setting `identity`: %+v", err)
}

replicasClient := meta.(*clients.Client).AppConfiguration.ReplicasClient
resp, err := replicasClient.ListByConfigurationStoreComplete(ctx, replicas.NewConfigurationStoreID(id.SubscriptionId, id.ResourceGroupName, id.ConfigurationStoreName))
if err != nil {
return fmt.Errorf("retrieving replicas for %s: %+v", id, err)
}

replica, err := flattenAppConfigurationReplicas(resp.Items)
if err != nil {
return fmt.Errorf("flattening replicas for %s: %+v", id, err)
}

d.Set("replica", replica)

return tags.FlattenAndSet(d, model.Tags)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func TestAccAppConfigurationDataSource_basic(t *testing.T) {
check.That(data.ResourceName).Key("secondary_write_key.0.secret").Exists(),
check.That(data.ResourceName).Key("sku").Exists(),
check.That(data.ResourceName).Key("soft_delete_retention_days").Exists(),
check.That(data.ResourceName).Key("replica.#").HasValue("2"),
check.That(data.ResourceName).Key("replica.0.name").Exists(),
check.That(data.ResourceName).Key("replica.1.id").Exists(),
check.That(data.ResourceName).Key("replica.1.endpoint").Exists(),
),
},
})
Expand Down
Loading
Loading