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

enhance application and SP replication wait #93

Merged
merged 9 commits into from
Jun 4, 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
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