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

Adding type to data okta group #155

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions examples/okta_group/datasource.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
resource okta_group test {
resource "okta_group" "test" {
name = "testAcc_replace_with_uuid"
description = "testing, testing"
users = ["${okta_user.test.id}"]
}

resource okta_user test {
resource "okta_user" "test" {
first_name = "TestAcc"
last_name = "Jones"
login = "john_replace_with_uuid@ledzeppelin.com"
email = "john_replace_with_uuid@ledzeppelin.com"
}

data okta_group test {
data "okta_group" "test" {
include_users = true
name = "${okta_group.test.name}"
}

data "okta_group" "test_type" {
include_users = true
name = "${okta_group.test.name}"
type = "nonExistentType"
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ github.com/hashicorp/terraform-json v0.4.0 h1:KNh29iNxozP5adfUFBJ4/fWd0Cu3taGgjH
github.com/hashicorp/terraform-json v0.4.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-plugin-sdk v1.9.0 h1:WBHHIX/RgF6/lbfMCzx0qKl96BbQy3bexWFvDqt1bhE=
github.com/hashicorp/terraform-plugin-sdk v1.9.0/go.mod h1:C/AXwmDHqbc3h6URiHpIsVKrwV4PS0Sh0+VTaeEkShw=
github.com/hashicorp/terraform-plugin-sdk v1.15.0 h1:bmYnTT7MqNXlUHDc7pT8E6uKT2g/upjlRLypJFK1OQU=
github.com/hashicorp/terraform-plugin-test v1.2.0 h1:AWFdqyfnOj04sxTdaAF57QqvW7XXrT8PseUHkbKsE8I=
github.com/hashicorp/terraform-plugin-test v1.2.0/go.mod h1:QIJHYz8j+xJtdtLrFTlzQVC0ocr3rf/OjIpgZLK56Hs=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596 h1:hjyO2JsNZUKT1ym+FAdlBEkGPevazYsmVgIMw7dVELg=
Expand Down
15 changes: 14 additions & 1 deletion okta/data_source_okta_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ func dataSourceGroup() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: false,
Description: "Type of the group. When specified in the terraform resource, will act as a filter when searching for the group",
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a validation function for group types?
https://developer.okta.com/docs/reference/api/groups/#group-type

"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand All @@ -44,7 +50,13 @@ func dataSourceGroupRead(d *schema.ResourceData, m interface{}) error {

func findGroup(name string, d *schema.ResourceData, m interface{}) error {
client := getOktaClientFromMetadata(m)
groups, _, err := client.Group.ListGroups(&query.Params{Q: name})
searchParams := &query.Params{Q: name}
if d.Get("type") != nil {
groupType := d.Get("type").(string)
searchParams = &query.Params{Q: name, Type: groupType}
}
Copy link
Contributor

@bogdanprodan-okta bogdanprodan-okta Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified

	if d.Get("type") != nil {
		searchParams.Type = d.Get("type").(string)
	}


groups, _, err := client.Group.ListGroups(searchParams)
if err != nil {
return fmt.Errorf("failed to query for groups: %v", err)
} else if len(groups) < 1 {
Expand All @@ -53,6 +65,7 @@ func findGroup(name string, d *schema.ResourceData, m interface{}) error {

d.SetId(groups[0].Id)
d.Set("description", groups[0].Profile.Description)
d.Set("type", groups[0].Type)

if d.Get("include_users").(bool) {
userIdList, err := listGroupUserIds(m, d.Id())
Expand Down
17 changes: 17 additions & 0 deletions okta/data_source_okta_group_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package okta

import (
"fmt"
"testing"

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

func TestAccOktaDataSourceGroup_read(t *testing.T) {
Expand All @@ -22,10 +24,25 @@ func TestAccOktaDataSourceGroup_read(t *testing.T) {
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.okta_group.test", "id"),
resource.TestCheckResourceAttrSet("data.okta_group.test", "type"),
resource.TestCheckResourceAttrSet("okta_group.test", "id"),
resource.TestCheckResourceAttr("okta_group.test", "users.#", "1"),
),
},
{
Config: config,
Check: resource.ComposeTestCheckFunc(testDoesNotExist("okta_group.test_type")),
},
},
})
}

func testDoesNotExist(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
if ok {
return fmt.Errorf("Resource should not exist: %s", name)
}
return nil
}
}
4 changes: 4 additions & 0 deletions website/docs/d/group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ data "okta_group" "example" {

* `name` - (Required) name of group to retrieve.

* `type` - (Optional) type of the group to retrieve.

* `include_users` - (Optional) whether or not to retrieve all member ids.

## Attributes Reference
Expand All @@ -30,6 +32,8 @@ data "okta_group" "example" {

* `name` - name of group.

* `type` - type of group.

* `description` - description of group.

* `users` - user ids that are members of this group, only included if `include_users` is set to `true`.