Skip to content

Commit

Permalink
Application & Service Principal Creation should now wait on replicati…
Browse files Browse the repository at this point in the history
…on (#86)

Should fix #4 (or at least help)
  • Loading branch information
katbyte authored May 29, 2019
1 parent 4db1525 commit b1d365e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
8 changes: 3 additions & 5 deletions azuread/data_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions azuread/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -244,8 +257,6 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error {
}
}

d.SetId(*app.ObjectID)

return resourceApplicationRead(d, meta)
}

Expand Down
22 changes: 12 additions & 10 deletions azuread/resource_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 0 additions & 1 deletion azuread/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit b1d365e

Please sign in to comment.