Skip to content

Commit

Permalink
Live ACC and VCR ACC tests for data source otka_group_rule. Minor touch
Browse files Browse the repository at this point in the history
up on data source okta_group_rule.

Thank you @steveAG

Closes #1606
  • Loading branch information
monde committed Jun 29, 2023
1 parent f74a905 commit 14bf799
Show file tree
Hide file tree
Showing 5 changed files with 1,338 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/okta_group_rule/datasource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ resource "okta_group_rule" "test" {
expression_value = "String.startsWith(user.firstName,\"andy\")"
}

# This example is for syntax purposes only. If it was actually run
# data.okta_group_rule.test_by_id would fail because okta_group_rule.test
# wouldn't be in the search index yet. The data source implementation relies on
# a group rule search function in the Okta API

data "okta_group_rule" "test_by_id" {
id = okta_group_rule.test.id
}
Expand Down
17 changes: 12 additions & 5 deletions okta/data_source_okta_group_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,21 @@ func dataSourceGroupRuleRead(ctx context.Context, d *schema.ResourceData, m inte
return diag.Errorf("failed to get group rule by name: %v", err)
case len(rules) < 1:
return diag.Errorf("group rule with name '%s' does not exist", name)
case rules[0].Name != name:
logger(m).Warn("group rule with exact name match was not found: using partial match which contains name as a substring", "name", rules[0].Name)
}
rule = rules[0]
} else {
return diag.Errorf("config must provide 'name' or 'id' to retrieve a group rule")
// exact name search should only return one result, but loop through to be safe
for _, ruleCandidate := range rules {
if ruleName == ruleCandidate.Name {
rule = ruleCandidate
break
}
}
}
}

if rule == nil {
return diag.Errorf("config must provide 'name' or 'id' to retrieve a group rule")
}

d.SetId(rule.Id)
_ = d.Set("name", rule.Name)
_ = d.Set("status", rule.Status)
Expand Down
73 changes: 73 additions & 0 deletions okta/data_source_okta_group_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package okta

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccOktaDataSourceGroupRule_read(t *testing.T) {
mgr := newFixtureManager(groupRule, t.Name())
step1config := groupAndRule
step2config := fmt.Sprintf("%s%s", groupAndRule, groupRuleDataSources)

oktaResourceTest(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ErrorCheck: testAccErrorChecks(t),
ProviderFactories: testAccProvidersFactories,
Steps: []resource.TestStep{
{
Config: mgr.ConfigReplace(step1config),
},
{
Config: mgr.ConfigReplace(step2config),
Check: resource.ComposeTestCheckFunc(
// hack for eventual consistency on the group rule creation on Okta API side
sleepInSecondsForTest(2),

resource.TestCheckResourceAttrSet("data.okta_group_rule.test_by_id", "id"),
resource.TestCheckResourceAttr("data.okta_group_rule.test_by_id", "name", fmt.Sprintf("testAccTwo_%d", mgr.Seed)),
resource.TestCheckResourceAttr("data.okta_group_rule.test_by_id", "status", "ACTIVE"),

resource.TestCheckResourceAttrSet("data.okta_group_rule.test_by_name", "id"),
resource.TestCheckResourceAttr("data.okta_group_rule.test_by_name", "name", fmt.Sprintf("testAccTwo_%d", mgr.Seed)),
resource.TestCheckResourceAttr("data.okta_group_rule.test_by_name", "status", "ACTIVE"),
),
},
},
})
}

const groupAndRule = `
resource "okta_group" "test" {
name = "testAcc_replace_with_uuid"
}
resource "okta_group_rule" "test1" {
name = "testAccOne_replace_with_uuid"
status = "ACTIVE"
group_assignments = [okta_group.test.id]
expression_type = "urn:okta:expression:1.0"
expression_value = "String.startsWith(user.firstName,\"andy\")"
}
resource "okta_group_rule" "test2" {
name = "testAccTwo_replace_with_uuid"
status = "ACTIVE"
group_assignments = [okta_group.test.id]
expression_type = "urn:okta:expression:1.0"
expression_value = "String.startsWith(user.firstName,\"andy\")"
depends_on = [okta_group_rule.test1]
}
`

const groupRuleDataSources = `
data "okta_group_rule" "test_by_id" {
id = okta_group_rule.test2.id
}
data "okta_group_rule" "test_by_name" {
name = "testAccTwo_replace_with_uuid"
}
`
10 changes: 10 additions & 0 deletions okta/utils_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand Down Expand Up @@ -204,3 +205,12 @@ func testAttributeJSON(name, attribute, expectedJSON string) resource.TestCheckF
return nil
}
}

// sleepInSecondsForTest Add sleep in a test to allow for eventual consistency
// thanks github.com/hashicorp/terraform-provider-google/google/provider_test.go
func sleepInSecondsForTest(t int) resource.TestCheckFunc {
return func(s *terraform.State) error {
time.Sleep(time.Duration(t) * time.Second)
return nil
}
}
Loading

0 comments on commit 14bf799

Please sign in to comment.