Skip to content

Commit

Permalink
Check if client grant already exists before creating
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Mar 12, 2023
1 parent 76cd907 commit 152b512
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/auth0/client/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ func NewGrantResource() *schema.Resource {
func createClientGrant(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

grantList, err := api.ClientGrant.List(
management.Parameter("audience", d.Get("audience").(string)),
management.Parameter("client_id", d.Get("client_id").(string)),
)
if err != nil {
return diag.FromErr(err)
}

if len(grantList.ClientGrants) != 0 {
d.SetId(grantList.ClientGrants[0].GetID())
return readClientGrant(ctx, d, m)
}

clientGrant := expandClientGrant(d)
if err := api.ClientGrant.Create(clientGrant); err != nil {
return diag.FromErr(err)
Expand Down
28 changes: 28 additions & 0 deletions internal/auth0/client/resource_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ resource "auth0_client_grant" "my_client_grant" {
}
`

const testAccAlreadyExistingGrantWillNotConflict = testAccClientGrantAuxConfig + `
resource "auth0_client" "my_client_alt" {
name = "Acceptance Test - Client Grant Alt - {{.testName}}"
custom_login_page_on = true
is_first_party = true
}
resource "auth0_client_grant" "my_client_grant" {
client_id = "${auth0_client.my_client_alt.id}"
audience = "${auth0_resource_server.my_resource_server.identifier}"
scope = [ ]
}
resource "auth0_client_grant" "no_conflict_client_grant" {
depends_on = [ auth0_client_grant.my_client_grant ]
client_id = "${auth0_client.my_client_alt.id}"
audience = "${auth0_resource_server.my_resource_server.identifier}"
scope = [ ]
}
`

func TestAccClientGrant(t *testing.T) {
acctest.Test(t, resource.TestCase{
Steps: []resource.TestStep{
Expand Down Expand Up @@ -98,6 +120,12 @@ func TestAccClientGrant(t *testing.T) {
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scope.#", "0"),
),
},
{
Config: template.ParseTestName(testAccAlreadyExistingGrantWillNotConflict, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client_grant.no_conflict_client_grant", "scope.#", "0"),
),
},
},
})
}

0 comments on commit 152b512

Please sign in to comment.