-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new data source - keycloak_openid_client_scope (#743)
- Loading branch information
Showing
8 changed files
with
173 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
page_title: "keycloak_openid_client_scope Data Source" | ||
--- | ||
|
||
# keycloak_openid_client_scope Data Source | ||
|
||
This data source can be used to fetch properties of a Keycloak OpenID client scope for usage with other resources. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "keycloak_openid_client_scope" "offline_access" { | ||
realm_id = "my-realm" | ||
name = "offline_access" | ||
} | ||
# use the data source | ||
resource "keycloak_openid_audience_protocol_mapper" "audience_mapper" { | ||
realm_id = data.keycloak_openid_client_scope.offline_access.realm_id | ||
client_scope_id = data.keycloak_openid_client_scope.offline_access.id | ||
name = "audience-mapper" | ||
included_custom_audience = "foo" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
- `realm_id` - (Required) The realm id. | ||
- `name` - (Required) The name of the client scope. | ||
|
||
## Attributes Reference | ||
|
||
See the docs for the `keycloak_openid_client_scope` resource for details on the exported attributes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/mrparkers/terraform-provider-keycloak/keycloak" | ||
) | ||
|
||
func dataSourceKeycloakOpenidClientScope() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceKeycloakOpenidClientScopeRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"realm_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"consent_screen_text": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"include_in_token_scope": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"gui_order": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceKeycloakOpenidClientScopeRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
keycloakClient := meta.(*keycloak.KeycloakClient) | ||
|
||
realmId := data.Get("realm_id").(string) | ||
name := data.Get("name").(string) | ||
|
||
scopes, err := keycloakClient.ListOpenidClientScopesWithFilter(ctx, realmId, keycloak.IncludeOpenidClientScopesMatchingNames([]string{name})) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if len(scopes) != 1 { | ||
return diag.Errorf("expected provided client scope name to match 1 scope, but matched %d scopes", len(scopes)) | ||
} | ||
|
||
setOpenidClientScopeData(data, scopes[0]) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccKeycloakDataSourceOpenidClientScope_basic(t *testing.T) { | ||
t.Parallel() | ||
clientScopeName := acctest.RandomWithPrefix("tf-acc-test") | ||
dataSourceName := "data.keycloak_openid_client_scope.test" | ||
resourceName := "keycloak_openid_client_scope.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccKeycloakOpenidClientScopeConfig(clientScopeName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrPair(dataSourceName, "realm_id", resourceName, "realm_id"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "consent_screen_text", resourceName, "consent_screen_text"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "include_in_token_scope", resourceName, "include_in_token_scope"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccKeycloakOpenidClientScopeConfig(name string) string { | ||
return fmt.Sprintf(` | ||
data "keycloak_realm" "realm" { | ||
realm = "%s" | ||
} | ||
resource "keycloak_openid_client_scope" "test" { | ||
name = "%s" | ||
realm_id = data.keycloak_realm.realm.id | ||
description = "%s" | ||
consent_screen_text = "%s" | ||
include_in_token_scope = %t | ||
} | ||
data "keycloak_openid_client_scope" "test" { | ||
name = keycloak_openid_client_scope.test.name | ||
realm_id = data.keycloak_realm.realm.id | ||
} | ||
resource "keycloak_openid_audience_protocol_mapper" "audience_mapper" { | ||
realm_id = data.keycloak_realm.realm.id | ||
client_scope_id = data.keycloak_openid_client_scope.test.id | ||
name = "audience-mapper" | ||
included_custom_audience = "foo" | ||
} | ||
`, testAccRealm.Realm, name, acctest.RandString(10), acctest.RandString(10), randomBool()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters