From 5454f794eb57f913463742966925dc39f315b090 Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 21 Jan 2019 16:58:14 -0800 Subject: [PATCH 1/2] azuread_application: correctly read reply URL's from SDK --- azuread/data_application.go | 6 ++-- azuread/helpers/tf/marshall.go | 19 +++++++++++ azuread/resource_application.go | 48 ++++++---------------------- azuread/resource_application_test.go | 2 +- 4 files changed, 34 insertions(+), 41 deletions(-) create mode 100644 azuread/helpers/tf/marshall.go diff --git a/azuread/data_application.go b/azuread/data_application.go index 62198ce47f..0b1b84a342 100644 --- a/azuread/data_application.go +++ b/azuread/data_application.go @@ -3,6 +3,8 @@ package azuread import ( "fmt" + "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/tf" + "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/ar" "github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" @@ -125,11 +127,11 @@ func dataApplicationRead(d *schema.ResourceData, meta interface{}) error { d.Set("available_to_other_tenants", application.AvailableToOtherTenants) d.Set("oauth2_allow_implicit_flow", application.Oauth2AllowImplicitFlow) - if err := d.Set("identifier_uris", application.IdentifierUris); err != nil { + if err := d.Set("identifier_uris", tf.FlattenStringArrayPtr(application.IdentifierUris)); err != nil { return fmt.Errorf("Error setting `identifier_uris`: %+v", err) } - if err := d.Set("reply_urls", application.ReplyUrls); err != nil { + if err := d.Set("reply_urls", tf.FlattenStringArrayPtr(application.ReplyUrls)); err != nil { return fmt.Errorf("Error setting `reply_urls`: %+v", err) } diff --git a/azuread/helpers/tf/marshall.go b/azuread/helpers/tf/marshall.go new file mode 100644 index 0000000000..24be506f7f --- /dev/null +++ b/azuread/helpers/tf/marshall.go @@ -0,0 +1,19 @@ +package tf + +func ExpandStringArrayPtr(input []interface{}) *[]string { + result := make([]string, 0) + for _, item := range input { + result = append(result, item.(string)) + } + return &result +} + +func FlattenStringArrayPtr(input *[]string) []interface{} { + result := make([]interface{}, 0) + if input != nil { + for _, item := range *input { + result = append(result, item) + } + } + return result +} diff --git a/azuread/resource_application.go b/azuread/resource_application.go index dd1b7609b5..5ac41a55cc 100644 --- a/azuread/resource_application.go +++ b/azuread/resource_application.go @@ -4,6 +4,8 @@ import ( "fmt" "log" + "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/tf" + "github.com/hashicorp/terraform/helper/validation" "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate" @@ -86,8 +88,8 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { properties := graphrbac.ApplicationCreateParameters{ DisplayName: &name, Homepage: expandADApplicationHomepage(d, name), - IdentifierUris: expandADApplicationIdentifierUris(d), - ReplyUrls: expandADApplicationReplyUrls(d), + IdentifierUris: tf.ExpandStringArrayPtr(d.Get("identifier_uris").([]interface{})), + ReplyUrls: tf.ExpandStringArrayPtr(d.Get("reply_urls").([]interface{})), AvailableToOtherTenants: p.Bool(d.Get("available_to_other_tenants").(bool)), } @@ -122,11 +124,11 @@ func resourceApplicationUpdate(d *schema.ResourceData, meta interface{}) error { } if d.HasChange("identifier_uris") { - properties.IdentifierUris = expandADApplicationIdentifierUris(d) + properties.IdentifierUris = tf.ExpandStringArrayPtr(d.Get("identifier_uris").([]interface{})) } if d.HasChange("reply_urls") { - properties.ReplyUrls = expandADApplicationReplyUrls(d) + properties.ReplyUrls = tf.ExpandStringArrayPtr(d.Get("reply_urls").([]interface{})) } if d.HasChange("available_to_other_tenants") { @@ -167,19 +169,11 @@ func resourceApplicationRead(d *schema.ResourceData, meta interface{}) error { d.Set("available_to_other_tenants", resp.AvailableToOtherTenants) d.Set("oauth2_allow_implicit_flow", resp.Oauth2AllowImplicitFlow) - identifierUris := make([]string, 0) - if s := resp.IdentifierUris; s != nil { - identifierUris = *s - } - if err := d.Set("identifier_uris", identifierUris); err != nil { + if err := d.Set("identifier_uris", tf.FlattenStringArrayPtr(resp.IdentifierUris)); err != nil { return fmt.Errorf("Error setting `identifier_uris`: %+v", err) } - replyUrls := make([]string, 0) - if s := resp.IdentifierUris; s != nil { - replyUrls = *s - } - if err := d.Set("reply_urls", replyUrls); err != nil { + if err := d.Set("reply_urls", tf.FlattenStringArrayPtr(resp.ReplyUrls)); err != nil { return fmt.Errorf("Error setting `reply_urls`: %+v", err) } @@ -197,8 +191,8 @@ func resourceApplicationDelete(d *schema.ResourceData, meta interface{}) error { properties := graphrbac.ApplicationUpdateParameters{ AvailableToOtherTenants: p.Bool(false), } - _, err := client.Patch(ctx, d.Id(), properties) - if err != nil { + + if _, err := client.Patch(ctx, d.Id(), properties); err != nil { return fmt.Errorf("Error patching Azure AD Application with ID %q: %+v", d.Id(), err) } } @@ -220,25 +214,3 @@ func expandADApplicationHomepage(d *schema.ResourceData, name string) *string { return p.String(fmt.Sprintf("https://%s", name)) } - -func expandADApplicationIdentifierUris(d *schema.ResourceData) *[]string { - identifierUris := d.Get("identifier_uris").([]interface{}) - identifiers := make([]string, 0) - - for _, id := range identifierUris { - identifiers = append(identifiers, id.(string)) - } - - return &identifiers -} - -func expandADApplicationReplyUrls(d *schema.ResourceData) *[]string { - replyUrls := d.Get("reply_urls").([]interface{}) - urls := make([]string, 0) - - for _, url := range replyUrls { - urls = append(urls, url.(string)) - } - - return &urls -} diff --git a/azuread/resource_application_test.go b/azuread/resource_application_test.go index 1cb9ae5b27..55b4dd74b9 100644 --- a/azuread/resource_application_test.go +++ b/azuread/resource_application_test.go @@ -195,7 +195,7 @@ func testAccADApplication_complete(id string) string { resource "azuread_application" "test" { name = "acctest%s" homepage = "https://homepage-%s" - identifier_uris = ["http://%s.hashicorptest.com"] + identifier_uris = ["http://%s.hashicorptest.com/00000000-0000-0000-0000-00000000"] reply_urls = ["http://%s.hashicorptest.com"] oauth2_allow_implicit_flow = true } From a91ba72c191c5dbcf46a0ab9ec66de5f16e20b7d Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 21 Jan 2019 17:15:01 -0800 Subject: [PATCH 2/2] update tests --- azuread/resource_application_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/azuread/resource_application_test.go b/azuread/resource_application_test.go index 55b4dd74b9..f041930346 100644 --- a/azuread/resource_application_test.go +++ b/azuread/resource_application_test.go @@ -52,6 +52,8 @@ func TestAccAzureADApplication_availableToOtherTenants(t *testing.T) { Check: resource.ComposeTestCheckFunc( testCheckADApplicationExists(resourceName), resource.TestCheckResourceAttr(resourceName, "available_to_other_tenants", "true"), + resource.TestCheckResourceAttr(resourceName, "identifier_uris.#", "1"), + resource.TestCheckResourceAttr(resourceName, "identifier_uris.0", fmt.Sprintf("https://%s.hashicorptest.com", id)), ), }, { @@ -79,6 +81,7 @@ func TestAccAzureADApplication_complete(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("acctest%s", id)), resource.TestCheckResourceAttr(resourceName, "homepage", fmt.Sprintf("https://homepage-%s", id)), resource.TestCheckResourceAttr(resourceName, "identifier_uris.#", "1"), + resource.TestCheckResourceAttr(resourceName, "identifier_uris.0", fmt.Sprintf("http://%s.hashicorptest.com/00000000-0000-0000-0000-00000000", id)), resource.TestCheckResourceAttr(resourceName, "reply_urls.#", "1"), resource.TestCheckResourceAttrSet(resourceName, "application_id"), ), @@ -119,7 +122,9 @@ func TestAccAzureADApplication_update(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("acctest%s", updatedId)), resource.TestCheckResourceAttr(resourceName, "homepage", fmt.Sprintf("https://homepage-%s", updatedId)), resource.TestCheckResourceAttr(resourceName, "identifier_uris.#", "1"), + resource.TestCheckResourceAttr(resourceName, "identifier_uris.0", fmt.Sprintf("http://%s.hashicorptest.com/00000000-0000-0000-0000-00000000", updatedId)), resource.TestCheckResourceAttr(resourceName, "reply_urls.#", "1"), + resource.TestCheckResourceAttr(resourceName, "reply_urls.0", fmt.Sprintf("http://%s.hashicorptest.com", updatedId)), ), }, },