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-487: Rename scope to scopes in client_grant resource #717

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/resources/client_grant.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Auth0 uses various grant types, or methods by which you grant limited access to
## Example Usage

```terraform
# The following example grants a client the "create:foo" permission (scope).
# The following example grants a client the "create:foo" and "create:bar" permissions (scopes).

resource "auth0_client" "my_client" {
name = "Example Application - Client Grant (Managed by Terraform)"
Expand All @@ -35,7 +35,7 @@ resource "auth0_resource_server" "my_resource_server" {
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"]
scopes = ["create:foo", "create:bar"]
}
```

Expand All @@ -46,7 +46,7 @@ resource "auth0_client_grant" "my_client_grant" {

- `audience` (String) Audience or API Identifier for this grant.
- `client_id` (String) ID of the client for this grant.
- `scope` (List of String) Permissions (scopes) included in this grant.
- `scopes` (List of String) Permissions (scopes) included in this grant.

### Read-Only

Expand Down
4 changes: 2 additions & 2 deletions examples/resources/auth0_client_grant/resource.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# The following example grants a client the "create:foo" permission (scope).
# The following example grants a client the "create:foo" and "create:bar" permissions (scopes).

resource "auth0_client" "my_client" {
name = "Example Application - Client Grant (Managed by Terraform)"
Expand All @@ -22,5 +22,5 @@ resource "auth0_resource_server" "my_resource_server" {
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"]
scopes = ["create:foo", "create:bar"]
}
23 changes: 12 additions & 11 deletions internal/auth0/client/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ func NewGrantResource() *schema.Resource {
ForceNew: true,
Description: "Audience or API Identifier for this grant.",
},
"scope": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
"scopes": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
Description: "Permissions (scopes) included in this grant.",
},
Expand Down Expand Up @@ -86,13 +88,14 @@ func readClientGrant(ctx context.Context, d *schema.ResourceData, m interface{})
d.SetId("")
return nil
}

return diag.FromErr(err)
}

result := multierror.Append(
d.Set("client_id", clientGrant.GetClientID()),
d.Set("audience", clientGrant.GetAudience()),
d.Set("scope", clientGrant.Scope),
d.Set("scopes", clientGrant.Scope),
)

return diag.FromErr(result.ErrorOrNil())
Expand All @@ -116,28 +119,26 @@ func deleteClientGrant(ctx context.Context, d *schema.ResourceData, m interface{

if err := api.ClientGrant.Delete(ctx, d.Id()); err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
return diag.FromErr(err)
}

d.SetId("")
return nil
}

func expandClientGrant(d *schema.ResourceData) *management.ClientGrant {
config := d.GetRawConfig()
cfg := d.GetRawConfig()

clientGrant := &management.ClientGrant{}

if d.IsNewResource() {
clientGrant.ClientID = value.String(config.GetAttr("client_id"))
clientGrant.Audience = value.String(config.GetAttr("audience"))
clientGrant.ClientID = value.String(cfg.GetAttr("client_id"))
clientGrant.Audience = value.String(cfg.GetAttr("audience"))
}

if d.IsNewResource() || d.HasChange("scope") {
scopeListFromConfig := d.Get("scope").([]interface{})
if d.HasChange("scopes") {
scopeListFromConfig := d.Get("scopes").([]interface{})
scopeList := make([]string, 0)
for _, scope := range scopeListFromConfig {
scopeList = append(scopeList, scope.(string))
Expand Down
24 changes: 12 additions & 12 deletions internal/auth0/client/resource_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ resource "auth0_client_grant" "my_client_grant" {

client_id = auth0_client.my_client.id
audience = auth0_resource_server.my_resource_server.identifier
scope = [ ]
scopes = [ ]
}
`

Expand All @@ -54,7 +54,7 @@ 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" ]
scopes = [ "create:foo" ]
}
`

Expand All @@ -64,7 +64,7 @@ resource "auth0_client_grant" "my_client_grant" {

client_id = auth0_client.my_client.id
audience = auth0_resource_server.my_resource_server.identifier
scope = [ ]
scopes = [ ]
}
`

Expand All @@ -82,7 +82,7 @@ resource "auth0_client_grant" "my_client_grant" {

client_id = auth0_client.my_client_alt.id
audience = auth0_resource_server.my_resource_server.identifier
scope = [ ]
scopes = [ ]
}
`

Expand All @@ -100,15 +100,15 @@ resource "auth0_client_grant" "my_client_grant" {

client_id = auth0_client.my_client_alt.id
audience = auth0_resource_server.my_resource_server.identifier
scope = [ ]
scopes = [ ]
}

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 = [ ]
scopes = [ ]
}
`

Expand All @@ -119,32 +119,32 @@ func TestAccClientGrant(t *testing.T) {
Config: acctest.ParseTestName(testAccClientGrantConfigCreate, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "audience", fmt.Sprintf("https://uat.tf.terraform-provider-auth0.com/client-grant/%s", t.Name())),
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scope.#", "0"),
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scopes.#", "0"),
),
},
{
Config: acctest.ParseTestName(testAccClientGrantConfigUpdate, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scope.#", "1"),
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scope.0", "create:foo"),
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scopes.#", "1"),
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scopes.0", "create:foo"),
),
},
{
Config: acctest.ParseTestName(testAccClientGrantConfigUpdateAgain, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scope.#", "0"),
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scopes.#", "0"),
),
},
{
Config: acctest.ParseTestName(testAccClientGrantConfigUpdateChangeClient, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scope.#", "0"),
resource.TestCheckResourceAttr("auth0_client_grant.my_client_grant", "scopes.#", "0"),
),
},
{
Config: acctest.ParseTestName(testAccAlreadyExistingGrantWillNotConflict, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client_grant.no_conflict_client_grant", "scope.#", "0"),
resource.TestCheckResourceAttr("auth0_client_grant.no_conflict_client_grant", "scopes.#", "0"),
),
},
},
Expand Down