From 87f7832e0e82a3f37e9abb20723734a02ee8a6b0 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 31 May 2019 17:51:07 -0700 Subject: [PATCH 1/9] change to statefunc --- azuread/resource_application.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/azuread/resource_application.go b/azuread/resource_application.go index 53fde6760f..5125149ed7 100644 --- a/azuread/resource_application.go +++ b/azuread/resource_application.go @@ -229,16 +229,27 @@ 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) + //todo graph.WaitForReplication + i, err := resource.StateChangeConf{ + Pending: []string{"Error"}, + Target: []string{"Found"}, + Timeout: 3 * time.Minute, + MinTimeout: 1 * time.Second, + ContinuousTargetOccurence: 10, + Refresh: func() (interface{}, string, error) { + + resp, err2 := client.Get(ctx, *app.ObjectID) + if err2 != nil { + return resp, "Error", fmt.Errorf("Error getting application: %+v", 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 From 34bbf550fb633799cf266cebd80e483f6b6ef3cb Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 3 Jun 2019 20:13:20 -0700 Subject: [PATCH 2/9] add to SP --- azuread/resource_application.go | 14 +++++++------- azuread/resource_service_principal.go | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/azuread/resource_application.go b/azuread/resource_application.go index 5125149ed7..a4458c6d05 100644 --- a/azuread/resource_application.go +++ b/azuread/resource_application.go @@ -229,12 +229,12 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { } d.SetId(*app.ObjectID) - //todo graph.WaitForReplication - i, err := resource.StateChangeConf{ - Pending: []string{"Error"}, - Target: []string{"Found"}, - Timeout: 3 * time.Minute, - MinTimeout: 1 * time.Second, + //todo graph.WaitForReplication? + i, err := (&resource.StateChangeConf{ + Pending: []string{"Error"}, + Target: []string{"Found"}, + Timeout: 3 * time.Minute, + MinTimeout: 1 * time.Second, ContinuousTargetOccurence: 10, Refresh: func() (interface{}, string, error) { @@ -245,7 +245,7 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { return resp, "Found", nil }, - }.WaitForState() + }).WaitForState() if err != nil { return fmt.Errorf("Error waiting for application: %+v", err) } diff --git a/azuread/resource_service_principal.go b/azuread/resource_service_principal.go index db189a0555..dea7f3d540 100644 --- a/azuread/resource_service_principal.go +++ b/azuread/resource_service_principal.go @@ -81,15 +81,26 @@ 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{"Error"}, + Target: []string{"Found"}, + Timeout: 3 * time.Minute, + MinTimeout: 1 * time.Second, + ContinuousTargetOccurence: 10, + Refresh: func() (interface{}, string, error) { + + resp, err2 := client.Get(ctx, *sp.ObjectID) + if err2 != nil { + return resp, "Error", fmt.Errorf("Error getting application: %+v", 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) } From f5acc76637717777ed0aee7a43186f661a7d0fbf Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 3 Jun 2019 20:22:42 -0700 Subject: [PATCH 3/9] fix wait for state --- azuread/resource_application.go | 5 ++++- azuread/resource_service_principal.go | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/azuread/resource_application.go b/azuread/resource_application.go index a4458c6d05..ca7e236d6c 100644 --- a/azuread/resource_application.go +++ b/azuread/resource_application.go @@ -240,7 +240,10 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { resp, err2 := client.Get(ctx, *app.ObjectID) if err2 != nil { - return resp, "Error", fmt.Errorf("Error getting application: %+v", err2) + if ar.ResponseWasNotFound(app.Response) { + return resp, "404", nil + } + return resp, "Error", fmt.Errorf("Error retrieving Application ID %q: %+v", *app.ObjectID, err2) } return resp, "Found", nil diff --git a/azuread/resource_service_principal.go b/azuread/resource_service_principal.go index dea7f3d540..aca65b2ba8 100644 --- a/azuread/resource_service_principal.go +++ b/azuread/resource_service_principal.go @@ -82,7 +82,7 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er // mimicking the behaviour of az tool retry until a successful get i, err := (&resource.StateChangeConf{ - Pending: []string{"Error"}, + Pending: []string{"404"}, Target: []string{"Found"}, Timeout: 3 * time.Minute, MinTimeout: 1 * time.Second, @@ -91,7 +91,10 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er resp, err2 := client.Get(ctx, *sp.ObjectID) if err2 != nil { - return resp, "Error", fmt.Errorf("Error getting application: %+v", err2) + if ar.ResponseWasNotFound(sp.Response) { + return resp, "404", nil + } + return resp, "Error", fmt.Errorf("Error retrieving Service Principal ID %q: %+v", *sp.ObjectID, err2) } return resp, "Found", nil From 0d1e849a74e4026a7e17cee11842c80a6f46bdcd Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 3 Jun 2019 20:33:54 -0700 Subject: [PATCH 4/9] fix typo --- azuread/resource_application.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azuread/resource_application.go b/azuread/resource_application.go index ca7e236d6c..b196551ae7 100644 --- a/azuread/resource_application.go +++ b/azuread/resource_application.go @@ -231,7 +231,7 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { //todo graph.WaitForReplication? i, err := (&resource.StateChangeConf{ - Pending: []string{"Error"}, + Pending: []string{"404"}, Target: []string{"Found"}, Timeout: 3 * time.Minute, MinTimeout: 1 * time.Second, From 2da8b2e23b57a25df4b538a9039d7b45f611c28b Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 3 Jun 2019 20:35:56 -0700 Subject: [PATCH 5/9] adjust check --- azuread/resource_application.go | 6 +++--- azuread/resource_service_principal.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/azuread/resource_application.go b/azuread/resource_application.go index b196551ae7..61c680e8eb 100644 --- a/azuread/resource_application.go +++ b/azuread/resource_application.go @@ -240,10 +240,10 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { resp, err2 := client.Get(ctx, *app.ObjectID) if err2 != nil { - if ar.ResponseWasNotFound(app.Response) { + //if ar.ResponseWasNotFound(app.Response) { return resp, "404", nil - } - return resp, "Error", fmt.Errorf("Error retrieving Application ID %q: %+v", *app.ObjectID, err2) + //} + //return resp, "Error", fmt.Errorf("Error retrieving Application ID %q: %+v", *app.ObjectID, err2) } return resp, "Found", nil diff --git a/azuread/resource_service_principal.go b/azuread/resource_service_principal.go index aca65b2ba8..aa1db32231 100644 --- a/azuread/resource_service_principal.go +++ b/azuread/resource_service_principal.go @@ -91,10 +91,10 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er resp, err2 := client.Get(ctx, *sp.ObjectID) if err2 != nil { - if ar.ResponseWasNotFound(sp.Response) { + //if ar.ResponseWasNotFound(sp.Response) { return resp, "404", nil - } - return resp, "Error", fmt.Errorf("Error retrieving Service Principal ID %q: %+v", *sp.ObjectID, err2) + //} + //return resp, "Error", fmt.Errorf("Error retrieving Service Principal ID %q: %+v", *sp.ObjectID, err2) } return resp, "Found", nil From 60de197b730918b5e16b179c448093797becbf73 Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 3 Jun 2019 20:49:43 -0700 Subject: [PATCH 6/9] final touchups --- azuread/provider.go | 4 ++++ azuread/resource_application.go | 10 +++++----- azuread/resource_service_principal.go | 10 +++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/azuread/provider.go b/azuread/provider.go index 1daa673133..526fd1a8ba 100644 --- a/azuread/provider.go +++ b/azuread/provider.go @@ -2,6 +2,7 @@ package azuread import ( "fmt" + `time` "github.com/hashicorp/go-azure-helpers/authentication" "github.com/hashicorp/terraform/helper/mutexkv" @@ -12,6 +13,9 @@ import ( // armMutexKV is the instance of MutexKV for ARM resources var armMutexKV = mutexkv.NewMutexKV() +const azureAdReplicationTimeout = 1 * time.Minute +const azureAdReplicationTargetOccurence = 10 + // Provider returns a terraform.ResourceProvider. func Provider() terraform.ResourceProvider { p := &schema.Provider{ diff --git a/azuread/resource_application.go b/azuread/resource_application.go index 61c680e8eb..e13d19553c 100644 --- a/azuread/resource_application.go +++ b/azuread/resource_application.go @@ -233,17 +233,17 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { i, err := (&resource.StateChangeConf{ Pending: []string{"404"}, Target: []string{"Found"}, - Timeout: 3 * time.Minute, + Timeout: azureAdReplicationTimeout, MinTimeout: 1 * time.Second, - ContinuousTargetOccurence: 10, + ContinuousTargetOccurence: azureAdReplicationTargetOccurence, Refresh: func() (interface{}, string, error) { resp, err2 := client.Get(ctx, *app.ObjectID) if err2 != nil { - //if ar.ResponseWasNotFound(app.Response) { + 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, "Error", fmt.Errorf("Error retrieving Application ID %q: %+v", *app.ObjectID, err2) } return resp, "Found", nil diff --git a/azuread/resource_service_principal.go b/azuread/resource_service_principal.go index aa1db32231..43c24338bb 100644 --- a/azuread/resource_service_principal.go +++ b/azuread/resource_service_principal.go @@ -84,17 +84,17 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er i, err := (&resource.StateChangeConf{ Pending: []string{"404"}, Target: []string{"Found"}, - Timeout: 3 * time.Minute, + Timeout: azureAdReplicationTimeout, MinTimeout: 1 * time.Second, - ContinuousTargetOccurence: 10, + ContinuousTargetOccurence: azureAdReplicationTargetOccurence, Refresh: func() (interface{}, string, error) { resp, err2 := client.Get(ctx, *sp.ObjectID) if err2 != nil { - //if ar.ResponseWasNotFound(sp.Response) { + 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 resp, "Error", fmt.Errorf("Error retrieving Service Principal ID %q: %+v", *sp.ObjectID, err2) } return resp, "Found", nil From 6edbbd5a4053a36c9775720a76c9ee10c1a3358b Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 3 Jun 2019 20:51:48 -0700 Subject: [PATCH 7/9] adjust timeout --- azuread/provider.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azuread/provider.go b/azuread/provider.go index 526fd1a8ba..7f4acdd817 100644 --- a/azuread/provider.go +++ b/azuread/provider.go @@ -13,7 +13,7 @@ import ( // armMutexKV is the instance of MutexKV for ARM resources var armMutexKV = mutexkv.NewMutexKV() -const azureAdReplicationTimeout = 1 * time.Minute +const azureAdReplicationTimeout = 5 * time.Minute const azureAdReplicationTargetOccurence = 10 // Provider returns a terraform.ResourceProvider. From a9b737a20782fe748b17b18ce694c2b3eb60adab Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 3 Jun 2019 20:53:55 -0700 Subject: [PATCH 8/9] make fmt --- azuread/provider.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azuread/provider.go b/azuread/provider.go index 7f4acdd817..4144e8118d 100644 --- a/azuread/provider.go +++ b/azuread/provider.go @@ -2,7 +2,7 @@ package azuread import ( "fmt" - `time` + "time" "github.com/hashicorp/go-azure-helpers/authentication" "github.com/hashicorp/terraform/helper/mutexkv" From 750e3a2251cf7c5cf58feda6798576c74857fdc4 Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 3 Jun 2019 21:00:58 -0700 Subject: [PATCH 9/9] misc touchups --- azuread/resource_application.go | 2 -- azuread/resource_service_principal.go | 2 -- 2 files changed, 4 deletions(-) diff --git a/azuread/resource_application.go b/azuread/resource_application.go index e13d19553c..c6734886af 100644 --- a/azuread/resource_application.go +++ b/azuread/resource_application.go @@ -229,7 +229,6 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { } d.SetId(*app.ObjectID) - //todo graph.WaitForReplication? i, err := (&resource.StateChangeConf{ Pending: []string{"404"}, Target: []string{"Found"}, @@ -237,7 +236,6 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error { 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) { diff --git a/azuread/resource_service_principal.go b/azuread/resource_service_principal.go index 43c24338bb..c405cd905a 100644 --- a/azuread/resource_service_principal.go +++ b/azuread/resource_service_principal.go @@ -80,7 +80,6 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er } d.SetId(*sp.ObjectID) - // mimicking the behaviour of az tool retry until a successful get i, err := (&resource.StateChangeConf{ Pending: []string{"404"}, Target: []string{"Found"}, @@ -88,7 +87,6 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er 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) {