-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new data source: keycloak_authentication_flow (#486)
- Loading branch information
Showing
6 changed files
with
236 additions
and
0 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,30 @@ | ||
--- | ||
page_title: "keycloak_authentication_flow Data Source" | ||
--- | ||
|
||
# keycloak\_authentication\_flow Data Source | ||
|
||
This data source can be used to fetch the ID of an authentication flow within Keycloak. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "keycloak_realm" "realm" { | ||
realm = "my-realm" | ||
enabled = true | ||
} | ||
data "keycloak_authentication_flow" "browser_auth_cookie" { | ||
realm_id = keycloak_realm.realm.id | ||
alias = "browser" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
- `realm_id` - (Required) The realm the authentication flow exists in. | ||
- `alias` - (Required) The alias of the flow. | ||
|
||
## Attributes Reference | ||
|
||
- `id` - (Computed) The unique ID of the authentication flow, which can be used as an argument to other resources supported by this provider. |
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,38 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/mrparkers/terraform-provider-keycloak/keycloak" | ||
) | ||
|
||
func dataSourceKeycloakAuthenticationFlow() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceKeycloakAuthenticationFlowRead, | ||
Schema: map[string]*schema.Schema{ | ||
"realm_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"alias": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceKeycloakAuthenticationFlowRead(data *schema.ResourceData, meta interface{}) error { | ||
keycloakClient := meta.(*keycloak.KeycloakClient) | ||
|
||
realmID := data.Get("realm_id").(string) | ||
alias := data.Get("alias").(string) | ||
|
||
authenticationFlowInfo, err := keycloakClient.GetAuthenticationFlowFromAlias(realmID, alias) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
mapFromAuthenticationFlowInfoToData(data, authenticationFlowInfo) | ||
|
||
return nil | ||
} |
116 changes: 116 additions & 0 deletions
116
provider/data_source_keycloak_authentication_flow_test.go
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,116 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccKeycloakDataSourceAuthenticationFlow_basic(t *testing.T) { | ||
t.Parallel() | ||
alias := acctest.RandomWithPrefix("tf-acc") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProviderFactories: testAccProviderFactories, | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
CheckDestroy: testAccCheckKeycloakAuthenticationFlowDestroy(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testDataSourceKeycloakAuthenticationFlow_basic(alias), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckKeycloakAuthenticationFlowExists("keycloak_authentication_flow.flow"), | ||
resource.TestCheckResourceAttrPair("keycloak_authentication_flow.flow", "id", "data.keycloak_authentication_flow.flow", "id"), | ||
testAccCheckDataKeycloakAuthenticationFlow("data.keycloak_authentication_flow.flow"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckDataKeycloakAuthenticationFlow(resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("resource not found: %s", resourceName) | ||
} | ||
|
||
id := rs.Primary.ID | ||
realmID := rs.Primary.Attributes["realm_id"] | ||
|
||
authenticationFlow, err := keycloakClient.GetAuthenticationFlow(realmID, id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if authenticationFlow.Id != id { | ||
return fmt.Errorf("expected authenticationFlow with ID %s but got %s", id, authenticationFlow.Id) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func TestAccKeycloakDataSourceAuthenticationExecution_wrongAlias(t *testing.T) { | ||
t.Parallel() | ||
alias := acctest.RandomWithPrefix("tf-acc") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProviderFactories: testAccProviderFactories, | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
CheckDestroy: testAccCheckKeycloakAuthenticationFlowDestroy(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testDataSourceKeycloakAuthenticationFlow_wrongAlias(alias), | ||
ExpectError: regexp.MustCompile("no authentication flow found for alias .*"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testDataSourceKeycloakAuthenticationFlow_basic(alias string) string { | ||
return fmt.Sprintf(` | ||
data "keycloak_realm" "realm" { | ||
realm = "%s" | ||
} | ||
resource "keycloak_authentication_flow" "flow" { | ||
realm_id = data.keycloak_realm.realm.id | ||
alias = "%s" | ||
} | ||
data "keycloak_authentication_flow" "flow" { | ||
realm_id = data.keycloak_realm.realm.id | ||
alias = keycloak_authentication_flow.flow.alias | ||
depends_on = [ | ||
keycloak_authentication_flow.flow, | ||
] | ||
} | ||
`, testAccRealm.Realm, alias) | ||
} | ||
|
||
func testDataSourceKeycloakAuthenticationFlow_wrongAlias(alias string) string { | ||
return fmt.Sprintf(` | ||
data "keycloak_realm" "realm" { | ||
realm = "%s" | ||
} | ||
resource "keycloak_authentication_flow" "flow" { | ||
realm_id = data.keycloak_realm.realm.id | ||
alias = "%s" | ||
} | ||
data "keycloak_authentication_flow" "flow" { | ||
realm_id = data.keycloak_realm.realm.id | ||
alias = "foo" | ||
depends_on = [ | ||
keycloak_authentication_flow.flow, | ||
] | ||
} | ||
`, testAccRealm.Realm, alias) | ||
} |
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