From aa16d31f5a382a040c54e4da836d7f13af63ab6b Mon Sep 17 00:00:00 2001 From: id Date: Wed, 29 May 2024 15:21:32 +0000 Subject: [PATCH] Add feedback --- .../applications/application_resource.go | 16 ++++++++-- .../services/applications/applications.go | 31 +++++++++---------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/internal/services/applications/application_resource.go b/internal/services/applications/application_resource.go index 23950f77d..c544e4e03 100644 --- a/internal/services/applications/application_resource.go +++ b/internal/services/applications/application_resource.go @@ -1146,8 +1146,13 @@ func applicationResourceCreate(ctx context.Context, d *pluginsdk.ResourceData, m // set the pw credentials to state if app.PasswordCredentials != nil { - if credentials := flattenApplicationPasswordCredentials(app.PasswordCredentials, d); credentials != nil { - d.Set("password", credentials) + var cred map[string]interface{} + for _, password := range d.Get("password").(*pluginsdk.Set).List() { + cred = password.(map[string]interface{}) + } + + if credentials := flattenApplicationPasswordCredentials(app.PasswordCredentials, cred); credentials != nil { + tf.Set(d, "password", credentials) } } @@ -1406,7 +1411,12 @@ func applicationResourceRead(ctx context.Context, d *pluginsdk.ResourceData, met } if app.PasswordCredentials != nil { - if credentials := flattenApplicationPasswordCredentials(app.PasswordCredentials, d); credentials != nil { + var cred map[string]interface{} + for _, password := range d.Get("password").(*pluginsdk.Set).List() { + cred = password.(map[string]interface{}) + } + + if credentials := flattenApplicationPasswordCredentials(app.PasswordCredentials, cred); credentials != nil { tf.Set(d, "password", credentials) } } diff --git a/internal/services/applications/applications.go b/internal/services/applications/applications.go index a57032771..11758b83f 100644 --- a/internal/services/applications/applications.go +++ b/internal/services/applications/applications.go @@ -14,7 +14,6 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-sdk/sdk/odata" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-azuread/internal/helpers" "github.com/hashicorp/terraform-provider-azuread/internal/tf" "github.com/hashicorp/terraform-provider-azuread/internal/tf/pluginsdk" @@ -854,26 +853,24 @@ func flattenApplicationSpa(in *msgraph.ApplicationSpa) []map[string]interface{} }} } -func flattenApplicationPasswordCredentials(passwordCredentials *[]msgraph.PasswordCredential, d *schema.ResourceData) []map[string]interface{} { - if v, ok := d.GetOk("password"); ok { - for _, cred := range v.(*pluginsdk.Set).List() { - keyId := cred.(map[string]interface{})["key_id"] - - // update, read case - if keyId != nil { - if credRead := helpers.GetPasswordCredential(passwordCredentials, keyId.(string)); credRead != nil { - return helpers.FlattenCredential(credRead, cred.(map[string]interface{})) - } - } +func flattenApplicationPasswordCredentials(passwordCredentials *[]msgraph.PasswordCredential, d map[string]interface{}) []map[string]interface{} { + if d == nil { + return []map[string]interface{}{} + } - // create case - for _, appCred := range *passwordCredentials { - return helpers.FlattenCredential(&appCred, cred.(map[string]interface{})) - } + // update, read case + if d["key_id"] != nil { + if credRead := helpers.GetPasswordCredential(passwordCredentials, d["key_id"].(string)); credRead != nil { + return helpers.FlattenCredential(credRead, d) } } - return nil + // create case + for _, appCred := range *passwordCredentials { + return helpers.FlattenCredential(&appCred, d) + } + + return []map[string]interface{}{} } func flattenApplicationWeb(in *msgraph.ApplicationWeb) []map[string]interface{} {