From b1d365e220fb5c102a27a05b7e88c9582cf557b4 Mon Sep 17 00:00:00 2001 From: kt Date: Wed, 29 May 2019 09:15:34 -0700 Subject: [PATCH] Application & Service Principal Creation should now wait on replication (#86) Should fix #4 (or at least help) --- azuread/data_application.go | 8 +++----- azuread/resource_application.go | 17 ++++++++++++++--- azuread/resource_service_principal.go | 22 ++++++++++++---------- azuread/resource_user.go | 1 - 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/azuread/data_application.go b/azuread/data_application.go index 1ce102f633..3480a20d15 100644 --- a/azuread/data_application.go +++ b/azuread/data_application.go @@ -3,12 +3,12 @@ package azuread import ( "fmt" + "github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/ar" "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/tf" "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate" - - "github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" - "github.com/hashicorp/terraform/helper/schema" ) func dataApplication() *schema.Resource { @@ -168,7 +168,6 @@ func dataApplicationRead(d *schema.ResourceData, meta interface{}) error { var app graphrbac.Application if oId, ok := d.GetOk("object_id"); ok { - // use the object_id to find the Azure AD application objectId := oId.(string) resp, err := client.Get(ctx, objectId) @@ -182,7 +181,6 @@ func dataApplicationRead(d *schema.ResourceData, meta interface{}) error { app = resp } else { - // use the name to find the Azure AD application name := d.Get("name").(string) filter := fmt.Sprintf("displayName eq '%s'", name) diff --git a/azuread/resource_application.go b/azuread/resource_application.go index 1271b33057..53fde6760f 100644 --- a/azuread/resource_application.go +++ b/azuread/resource_application.go @@ -3,8 +3,10 @@ package azuread import ( "fmt" "log" + "time" "github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/ar" @@ -222,10 +224,21 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { if err != nil { return err } - if app.ObjectID == nil { return fmt.Errorf("Application objectId is nil") } + d.SetId(*app.ObjectID) + + // mimicking the behaviour of az tool retry until a successful get + if err := resource.Retry(3*time.Minute, func() *resource.RetryError { + if _, err := client.Get(ctx, *app.ObjectID); err != nil { + return resource.RetryableError(err) + } + + return nil + }); err != nil { + return fmt.Errorf("Error waiting for Application %q to become available: %+v", name, err) + } // follow suggested hack for azure-cli // AAD graph doesn't have the API to create a native app, aka public client, the recommended hack is @@ -244,8 +257,6 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { } } - d.SetId(*app.ObjectID) - return resourceApplicationRead(d, meta) } diff --git a/azuread/resource_service_principal.go b/azuread/resource_service_principal.go index 0345e51764..db189a0555 100644 --- a/azuread/resource_service_principal.go +++ b/azuread/resource_service_principal.go @@ -3,7 +3,9 @@ package azuread import ( "fmt" "log" + "time" + "github.com/hashicorp/terraform/helper/resource" "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/tf" "github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate" @@ -73,21 +75,21 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er if err != nil { return fmt.Errorf("Error creating Service Principal for application %q: %+v", applicationId, err) } - if sp.ObjectID == nil { - return fmt.Errorf("Create returned a nil object id for application %q", applicationId) + return fmt.Errorf("Service Principal objectID is nil") } - objectId := *sp.ObjectID + d.SetId(*sp.ObjectID) - resp, err := client.Get(ctx, objectId) - if err != nil { - return fmt.Errorf("Error retrieving Service Principal with ID %q: %+v", objectId, err) - } + // mimicking the behaviour of az tool retry until a successful get + if err := resource.Retry(3*time.Minute, func() *resource.RetryError { + if _, err := client.Get(ctx, *sp.ObjectID); err != nil { + return resource.RetryableError(err) + } - if resp.ObjectID == nil { - return fmt.Errorf("Get returned a nil object ID for %q", objectId) + return nil + }); err != nil { + return fmt.Errorf("Error waiting for Service Principal %q to become available: %+v", applicationId, err) } - d.SetId(*resp.ObjectID) return resourceServicePrincipalRead(d, meta) } diff --git a/azuread/resource_user.go b/azuread/resource_user.go index 21c4c20bf3..0d09abda85 100644 --- a/azuread/resource_user.go +++ b/azuread/resource_user.go @@ -108,7 +108,6 @@ func resourceUserCreate(d *schema.ResourceData, meta interface{}) error { if err != nil { return fmt.Errorf("Error retrieving User (%q) with ObjectID %q: %+v", userPrincipalName, *objectId, err) } - if resp.ObjectID == nil { return fmt.Errorf("User objectId is nil") }