Skip to content

Commit

Permalink
azuread_application_password: add replication wait (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
katbyte authored Jul 12, 2019
1 parent 6f5629c commit c29badf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
28 changes: 28 additions & 0 deletions azuread/helpers/graph/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/go-uuid"
"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"

"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/p"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate"
Expand Down Expand Up @@ -210,3 +212,29 @@ func PasswordCredentialResultRemoveByKeyId(existing graphrbac.PasswordCredential

return &newCreds
}

func WaitForPasswordCredentialReplication(keyId string, f func() (graphrbac.PasswordCredentialListResult, error)) (interface{}, error) {
return (&resource.StateChangeConf{
Pending: []string{"404", "BadCast", "NotFound"},
Target: []string{"Found"},
Timeout: 5 * time.Minute,
MinTimeout: 1 * time.Second,
ContinuousTargetOccurence: 10,
Refresh: func() (interface{}, string, error) {
creds, err := f()
if err != nil {
if ar.ResponseWasNotFound(creds.Response) {
return creds, "404", nil
}
return creds, "Error", fmt.Errorf("Error calling f, response was not 404 (%d): %v", creds.Response.StatusCode, err)
}

credential := PasswordCredentialResultFindByKeyId(creds, keyId)
if credential == nil {
return creds, "NotFound", nil
}

return creds, "Found", nil
},
}).WaitForState()
}
20 changes: 10 additions & 10 deletions azuread/helpers/graph/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ func WaitForReplication(f func() (interface{}, error)) (interface{}, error) {
ContinuousTargetOccurence: 10,
Refresh: func() (interface{}, string, error) {
i, err := f()
if err != nil {
r, ok := i.(autorest.Response)
if !ok {
return i, "BadCast", nil // sometimes the SDK bubbles up an entirely empty object
}
if ar.ResponseWasNotFound(r) {
return i, "404", nil
}
return i, "Error", fmt.Errorf("Error calling f, response was not 404 (%d): %v", r.StatusCode, err)
if err == nil {
return i, "Found", nil
}

return i, "Found", nil
r, ok := i.(autorest.Response)
if !ok {
return i, "BadCast", nil // sometimes the SDK bubbles up an entirely empty object
}
if ar.ResponseWasNotFound(r) {
return i, "404", nil
}
return i, "Error", fmt.Errorf("Error calling f, response was not 404 (%d): %v", r.StatusCode, err)
},
}).WaitForState()
}
10 changes: 8 additions & 2 deletions azuread/resource_application_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ func resourceApplicationPasswordCreate(d *schema.ResourceData, meta interface{})
}

if _, err = client.UpdatePasswordCredentials(ctx, id.ObjectId, graphrbac.PasswordCredentialsUpdateParameters{Value: newCreds}); err != nil {
return fmt.Errorf("Error creating Application Credentials %q for Object ID %q: %+v", *cred.KeyID, id.ObjectId, err)
return fmt.Errorf("Error creating Application Credentials %q for Object ID %q: %+v", id.KeyId, id.ObjectId, err)
}

_, err = graph.WaitForPasswordCredentialReplication(id.KeyId, func() (graphrbac.PasswordCredentialListResult, error) {
return client.ListPasswordCredentials(ctx, id.ObjectId)
})
if err != nil {
return fmt.Errorf("Error waiting for Application password (AppID %q, KeyID %q: %+v", id.ObjectId, id.KeyId, err)
}

d.SetId(id.String())
Expand All @@ -138,7 +145,6 @@ func resourceApplicationPasswordRead(d *schema.ResourceData, meta interface{}) e
if err != nil {
return fmt.Errorf("Error parsing Application Password ID: %v", err)
}

// ensure the Application Object exists
app, err := client.Get(ctx, id.ObjectId)
if err != nil {
Expand Down

0 comments on commit c29badf

Please sign in to comment.