Skip to content

Commit

Permalink
Merge pull request #114 from auth0/patch/DXCDT-80-remaining
Browse files Browse the repository at this point in the history
DXCDT-80 Stop ignoring errors on remaining resources
  • Loading branch information
sergiught authored Mar 31, 2022
2 parents e4453db + 2345eff commit 8f84b75
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 252 deletions.
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

0 comments on commit 8f84b75

Please sign in to comment.