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

azuread_application: correctly read reply URL's from SDK #21

Merged
merged 2 commits into from
Jan 22, 2019
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
6 changes: 4 additions & 2 deletions azuread/data_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
19 changes: 19 additions & 0 deletions azuread/helpers/tf/marshall.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 10 additions & 38 deletions azuread/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)),
}

Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}
}
Expand All @@ -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
}
7 changes: 6 additions & 1 deletion azuread/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
},
{
Expand Down Expand Up @@ -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"),
),
Expand Down Expand Up @@ -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)),
),
},
},
Expand Down Expand Up @@ -195,7 +200,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
}
Expand Down