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

DXCDT-80 Stop ignoring errors on remaining resources #114

Merged
merged 7 commits into from
Mar 31, 2022
53 changes: 29 additions & 24 deletions auth0/resource_auth0_client_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import (

"github.com/auth0/go-auth0"
"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func newClientGrant() *schema.Resource {
return &schema.Resource{

Create: createClientGrant,
Read: readClientGrant,
Update: updateClientGrant,
Delete: deleteClientGrant,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"client_id": {
Type: schema.TypeString,
Expand All @@ -40,18 +39,20 @@ func newClientGrant() *schema.Resource {
}

func createClientGrant(d *schema.ResourceData, m interface{}) error {
g := buildClientGrant(d)
clientGrant := buildClientGrant(d)
api := m.(*management.Management)
if err := api.ClientGrant.Create(g); err != nil {
if err := api.ClientGrant.Create(clientGrant); err != nil {
return err
}
d.SetId(auth0.StringValue(g.ID))

d.SetId(auth0.StringValue(clientGrant.ID))

return readClientGrant(d, m)
}

func readClientGrant(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
g, err := api.ClientGrant.Read(d.Id())
clientGrant, err := api.ClientGrant.Read(d.Id())
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
Expand All @@ -61,29 +62,31 @@ func readClientGrant(d *schema.ResourceData, m interface{}) error {
}
return err
}
d.SetId(auth0.StringValue(g.ID))
d.Set("client_id", g.ClientID)
d.Set("audience", g.Audience)
d.Set("scope", g.Scope)
return nil

result := multierror.Append(
d.Set("client_id", clientGrant.ClientID),
d.Set("audience", clientGrant.Audience),
d.Set("scope", clientGrant.Scope),
)

return result.ErrorOrNil()
}

func updateClientGrant(d *schema.ResourceData, m interface{}) error {
g := buildClientGrant(d)
g.Audience = nil
g.ClientID = nil
clientGrant := buildClientGrant(d)
clientGrant.Audience = nil
clientGrant.ClientID = nil
api := m.(*management.Management)
err := api.ClientGrant.Update(d.Id(), g)
if err != nil {
if err := api.ClientGrant.Update(d.Id(), clientGrant); err != nil {
return err
}

return readClientGrant(d, m)
}

func deleteClientGrant(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
err := api.ClientGrant.Delete(d.Id())
if err != nil {
if err := api.ClientGrant.Delete(d.Id()); err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
Expand All @@ -92,18 +95,20 @@ func deleteClientGrant(d *schema.ResourceData, m interface{}) error {
}
return err
}
return err

return nil
}

func buildClientGrant(d *schema.ResourceData) *management.ClientGrant {
g := &management.ClientGrant{
clientGrant := &management.ClientGrant{
ClientID: String(d, "client_id"),
Audience: String(d, "audience"),
}

clientGrant.Scope = []interface{}{}
if scope, ok := d.GetOk("scope"); ok {
g.Scope = scope.([]interface{})
} else {
g.Scope = []interface{}{}
clientGrant.Scope = scope.([]interface{})
}
return g

return clientGrant
}
8 changes: 1 addition & 7 deletions auth0/resource_auth0_client_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func TestAccClientGrant(t *testing.T) {

rand := random.String(6)

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -49,7 +48,6 @@ func TestAccClientGrant(t *testing.T) {
}

const testAccClientGrantAuxConfig = `

resource "auth0_client" "my_client" {
name = "Acceptance Test - Client Grant - {{.random}}"
custom_login_page_on = true
Expand All @@ -71,7 +69,6 @@ resource "auth0_resource_server" "my_resource_server" {
`

const testAccClientGrantConfigCreate = testAccClientGrantAuxConfig + `

resource "auth0_client_grant" "my_client_grant" {
client_id = "${auth0_client.my_client.id}"
audience = "${auth0_resource_server.my_resource_server.identifier}"
Expand All @@ -80,16 +77,14 @@ resource "auth0_client_grant" "my_client_grant" {
`

const testAccClientGrantConfigUpdate = testAccClientGrantAuxConfig + `

resource "auth0_client_grant" "my_client_grant" {
client_id = "${auth0_client.my_client.id}"
audience = "${auth0_resource_server.my_resource_server.identifier}"
scope = [ "create:foo" ]
scope = [ "create:foo" ]
}
`

const testAccClientGrantConfigUpdateAgain = testAccClientGrantAuxConfig + `

resource "auth0_client_grant" "my_client_grant" {
client_id = "${auth0_client.my_client.id}"
audience = "${auth0_resource_server.my_resource_server.identifier}"
Expand All @@ -98,7 +93,6 @@ resource "auth0_client_grant" "my_client_grant" {
`

const testAccClientGrantConfigUpdateChangeClient = testAccClientGrantAuxConfig + `

resource "auth0_client" "my_client_alt" {
name = "Acceptance Test - Client Grant Alt - {{.random}}"
custom_login_page_on = true
Expand Down
40 changes: 21 additions & 19 deletions auth0/resource_auth0_custom_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import (

"github.com/auth0/go-auth0"
"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func newCustomDomain() *schema.Resource {
return &schema.Resource{

Create: createCustomDomain,
Read: readCustomDomain,
Delete: deleteCustomDomain,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Expand Down Expand Up @@ -68,18 +67,20 @@ func newCustomDomain() *schema.Resource {
}

func createCustomDomain(d *schema.ResourceData, m interface{}) error {
c := buildCustomDomain(d)
customDomain := buildCustomDomain(d)
api := m.(*management.Management)
if err := api.CustomDomain.Create(c); err != nil {
if err := api.CustomDomain.Create(customDomain); err != nil {
return err
}
d.SetId(auth0.StringValue(c.ID))

d.SetId(auth0.StringValue(customDomain.ID))

return readCustomDomain(d, m)
}

func readCustomDomain(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
c, err := api.CustomDomain.Read(d.Id())
customDomain, err := api.CustomDomain.Read(d.Id())
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
Expand All @@ -90,33 +91,34 @@ func readCustomDomain(d *schema.ResourceData, m interface{}) error {
return err
}

d.SetId(auth0.StringValue(c.ID))
d.Set("domain", c.Domain)
d.Set("type", c.Type)
d.Set("primary", c.Primary)
d.Set("status", c.Status)
result := multierror.Append(
d.Set("domain", customDomain.Domain),
d.Set("type", customDomain.Type),
d.Set("primary", customDomain.Primary),
d.Set("status", customDomain.Status),
)

if c.Verification != nil {
d.Set("verification", []map[string]interface{}{
{"methods": c.Verification.Methods},
})
if customDomain.Verification != nil {
result = multierror.Append(result, d.Set("verification", []map[string]interface{}{
{"methods": customDomain.Verification.Methods},
}))
}

return nil
return result.ErrorOrNil()
}

func deleteCustomDomain(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
err := api.CustomDomain.Delete(d.Id())
if err != nil {
if err := api.CustomDomain.Delete(d.Id()); err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
}
}
return err

return nil
}

func buildCustomDomain(d *schema.ResourceData) *management.CustomDomain {
Expand Down
8 changes: 3 additions & 5 deletions auth0/resource_auth0_custom_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func init() {
}
for _, domain := range domains {
log.Printf("[DEBUG] ➝ %s", domain.GetDomain())
if strings.Contains(domain.GetDomain(), "auth.uat.alexkappa.com") {
if strings.Contains(domain.GetDomain(), "auth.uat.terraform-provider-auth0.com") {
if e := api.CustomDomain.Delete(domain.GetID()); e != nil {
multierror.Append(err, e)
}
Expand All @@ -39,7 +39,6 @@ func init() {
}

func TestAccCustomDomain(t *testing.T) {

rand := random.String(6)

resource.Test(t, resource.TestCase{
Expand All @@ -50,7 +49,7 @@ func TestAccCustomDomain(t *testing.T) {
{
Config: random.Template(testAccCustomDomain, rand),
Check: resource.ComposeTestCheckFunc(
random.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "domain", "{{.random}}.auth.uat.alexkappa.com", rand),
random.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "domain", "{{.random}}.auth.uat.terraform-provider-auth0.com", rand),
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "type", "auth0_managed_certs"),
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "status", "pending_verification"),
),
Expand All @@ -60,9 +59,8 @@ func TestAccCustomDomain(t *testing.T) {
}

const testAccCustomDomain = `

resource "auth0_custom_domain" "my_custom_domain" {
domain = "{{.random}}.auth.uat.alexkappa.com"
domain = "{{.random}}.auth.uat.terraform-provider-auth0.com"
type = "auth0_managed_certs"
}
`
26 changes: 15 additions & 11 deletions auth0/resource_auth0_custom_domain_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ import (

func newCustomDomainVerification() *schema.Resource {
return &schema.Resource{

Create: createCustomDomainVerification,
Read: readCustomDomainVerification,
Delete: deleteCustomDomainVerification,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"custom_domain_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
},
Expand All @@ -38,22 +35,28 @@ func newCustomDomainVerification() *schema.Resource {
func createCustomDomainVerification(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
c, err := api.CustomDomain.Verify(d.Get("custom_domain_id").(string))
customDomainVerification, err := api.CustomDomain.Verify(d.Get("custom_domain_id").(string))
if err != nil {
return resource.NonRetryableError(err)
}
if c.GetStatus() != "ready" {
return resource.RetryableError(fmt.Errorf("Custom domain has status %q", c.GetStatus()))

if customDomainVerification.GetStatus() != "ready" {
return resource.RetryableError(
fmt.Errorf("custom domain has status %q", customDomainVerification.GetStatus()),
)
}
log.Printf("[INFO] Custom domain %s verified", c.GetDomain())
d.SetId(c.GetID())

log.Printf("[INFO] Custom domain %s verified", customDomainVerification.GetDomain())

d.SetId(customDomainVerification.GetID())

return resource.NonRetryableError(readCustomDomainVerification(d, m))
})
}

func readCustomDomainVerification(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
c, err := api.CustomDomain.Read(d.Id())
customDomain, err := api.CustomDomain.Read(d.Id())
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
Expand All @@ -63,10 +66,11 @@ func readCustomDomainVerification(d *schema.ResourceData, m interface{}) error {
}
return err
}
d.Set("custom_domain_id", c.GetID())
return nil

return d.Set("custom_domain_id", customDomain.GetID())
}

func deleteCustomDomainVerification(d *schema.ResourceData, m interface{}) error {
d.SetId("")
return nil
}
Loading