Skip to content

Commit

Permalink
enhance application and SP replication wait (#93)
Browse files Browse the repository at this point in the history
refinement of #86
  • Loading branch information
katbyte authored Jun 4, 2019
1 parent b39d6fa commit a5aa644
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
4 changes: 4 additions & 0 deletions azuread/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azuread

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/authentication"
"github.com/hashicorp/terraform/helper/mutexkv"
Expand All @@ -12,6 +13,9 @@ import (
// armMutexKV is the instance of MutexKV for ARM resources
var armMutexKV = mutexkv.NewMutexKV()

const azureAdReplicationTimeout = 5 * time.Minute
const azureAdReplicationTargetOccurence = 10

// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
p := &schema.Provider{
Expand Down
30 changes: 21 additions & 9 deletions azuread/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,28 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error {
}
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)
i, err := (&resource.StateChangeConf{
Pending: []string{"404"},
Target: []string{"Found"},
Timeout: azureAdReplicationTimeout,
MinTimeout: 1 * time.Second,
ContinuousTargetOccurence: azureAdReplicationTargetOccurence,
Refresh: func() (interface{}, string, error) {
resp, err2 := client.Get(ctx, *app.ObjectID)
if err2 != nil {
if ar.ResponseWasNotFound(resp.Response) {
return resp, "404", nil
}
return resp, "Error", fmt.Errorf("Error retrieving Application ID %q: %+v", *app.ObjectID, err2)
}

return resp, "Found", nil
},
}).WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for application: %+v", err)
}
app = i.(graphrbac.Application)

// 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 Down
28 changes: 20 additions & 8 deletions azuread/resource_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,28 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er
}
d.SetId(*sp.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, *sp.ObjectID); err != nil {
return resource.RetryableError(err)
}
i, err := (&resource.StateChangeConf{
Pending: []string{"404"},
Target: []string{"Found"},
Timeout: azureAdReplicationTimeout,
MinTimeout: 1 * time.Second,
ContinuousTargetOccurence: azureAdReplicationTargetOccurence,
Refresh: func() (interface{}, string, error) {
resp, err2 := client.Get(ctx, *sp.ObjectID)
if err2 != nil {
if ar.ResponseWasNotFound(resp.Response) {
return resp, "404", nil
}
return resp, "Error", fmt.Errorf("Error retrieving Service Principal ID %q: %+v", *sp.ObjectID, err2)
}

return nil
}); err != nil {
return fmt.Errorf("Error waiting for Service Principal %q to become available: %+v", applicationId, err)
return resp, "Found", nil
},
}).WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for application: %+v", err)
}
sp = i.(graphrbac.ServicePrincipal)

return resourceServicePrincipalRead(d, meta)
}
Expand Down

0 comments on commit a5aa644

Please sign in to comment.