Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit 415c691

Browse files
authored
Merge pull request #5 from sworisbreathing/data-block-group
Add `redshift_group` data source
2 parents 066df54 + 8083005 commit 415c691

5 files changed

Lines changed: 143 additions & 2 deletions

File tree

docs/data-sources/group.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "redshift_group Data Source - terraform-provider-redshift"
4+
subcategory: ""
5+
description: |-
6+
Groups are collections of users who are all granted whatever privileges are associated with the group. You can use groups to assign privileges by role. For example, you can create different groups for sales, administration, and support and give the users in each group the appropriate access to the data they require for their work. You can grant or revoke privileges at the group level, and those changes will apply to all members of the group, except for superusers.
7+
---
8+
9+
# redshift_group (Data Source)
10+
11+
Groups are collections of users who are all granted whatever privileges are associated with the group. You can use groups to assign privileges by role. For example, you can create different groups for sales, administration, and support and give the users in each group the appropriate access to the data they require for their work. You can grant or revoke privileges at the group level, and those changes will apply to all members of the group, except for superusers.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "redshift_group" "staff" {
17+
name = "group_users"
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Required
25+
26+
- **name** (String) Name of the user group. Group names beginning with two underscores are reserved for Amazon Redshift internal use.
27+
28+
### Optional
29+
30+
- **id** (String) The ID of this resource.
31+
32+
### Read-Only
33+
34+
- **users** (Set of String) List of the user names who belong to the group
35+
36+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "redshift_group" "staff" {
2+
name = "group_users"
3+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package redshift
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
9+
"github.com/lib/pq"
10+
)
11+
12+
func dataSourceRedshiftGroup() *schema.Resource {
13+
return &schema.Resource{
14+
Description: `
15+
Groups are collections of users who are all granted whatever privileges are associated with the group. You can use groups to assign privileges by role. For example, you can create different groups for sales, administration, and support and give the users in each group the appropriate access to the data they require for their work. You can grant or revoke privileges at the group level, and those changes will apply to all members of the group, except for superusers.
16+
`,
17+
Read: RedshiftResourceFunc(dataSourceRedshiftGroupRead),
18+
Schema: map[string]*schema.Schema{
19+
groupNameAttr: {
20+
Type: schema.TypeString,
21+
Required: true,
22+
Description: "Name of the user group. Group names beginning with two underscores are reserved for Amazon Redshift internal use.",
23+
ValidateFunc: validation.StringDoesNotMatch(regexp.MustCompile("^__.*"), "Group names beginning with two underscores are reserved for Amazon Redshift internal use"),
24+
StateFunc: func(val interface{}) string {
25+
return strings.ToLower(val.(string))
26+
},
27+
},
28+
groupUsersAttr: {
29+
Type: schema.TypeSet,
30+
Computed: true,
31+
Elem: &schema.Schema{
32+
Type: schema.TypeString,
33+
},
34+
Description: "List of the user names who belong to the group",
35+
},
36+
},
37+
}
38+
}
39+
40+
func dataSourceRedshiftGroupRead(db *DBConnection, d *schema.ResourceData) error {
41+
var (
42+
groupId string
43+
groupUsers []string
44+
)
45+
46+
sql := `SELECT ARRAY(SELECT u.usename FROM pg_user_info u, pg_group g WHERE g.groname = $1 AND u.usesysid = ANY(g.grolist)) AS members, grosysid FROM pg_group WHERE groname = $1`
47+
if err := db.QueryRow(sql, d.Get(groupNameAttr).(string)).Scan(pq.Array(&groupUsers), &groupId); err != nil {
48+
return err
49+
}
50+
51+
d.SetId(groupId)
52+
d.Set(groupUsersAttr, groupUsers)
53+
return nil
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package redshift
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
)
11+
12+
func TestAccDataSourceRedshiftGroup_basic(t *testing.T) {
13+
groupName := strings.ReplaceAll(acctest.RandomWithPrefix("tf_acc_data_basic"), "-", "_")
14+
userName := strings.ReplaceAll(acctest.RandomWithPrefix("tf_acc_data_basic"), "-", "_")
15+
resource.ParallelTest(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
CheckDestroy: testAccCheckRedshiftGroupDestroy,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDataSourceRedshiftGroupConfig_basic(groupName, userName),
22+
Check: resource.ComposeAggregateTestCheckFunc(
23+
resource.TestCheckResourceAttr("data.redshift_group.group", groupNameAttr, groupName),
24+
resource.TestCheckResourceAttr("data.redshift_group.group", fmt.Sprintf("%s.#", groupUsersAttr), "1"),
25+
resource.TestCheckTypeSetElemAttr("data.redshift_group.group", fmt.Sprintf("%s.*", groupUsersAttr), userName),
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
func testAccDataSourceRedshiftGroupConfig_basic(groupName string, userName string) string {
33+
return fmt.Sprintf(`
34+
resource "redshift_user" "user" {
35+
%[1]s = %[2]q
36+
}
37+
resource "redshift_group" "group" {
38+
%[3]s = %[4]q
39+
%[5]s = [ redshift_user.user.%[1]s ]
40+
}
41+
42+
data "redshift_group" "group" {
43+
%[3]s = redshift_group.group.%[3]s
44+
}
45+
`, userNameAttr, userName, groupNameAttr, groupName, groupUsersAttr)
46+
}

redshift/provider.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ func Provider() *schema.Provider {
121121
"redshift_schema": redshiftSchema(),
122122
"redshift_privilege": redshiftPrivilege(),
123123
},
124-
DataSourcesMap: map[string]*schema.Resource{},
125-
ConfigureFunc: providerConfigure,
124+
DataSourcesMap: map[string]*schema.Resource{
125+
"redshift_group": dataSourceRedshiftGroup(),
126+
},
127+
ConfigureFunc: providerConfigure,
126128
}
127129
}
128130

0 commit comments

Comments
 (0)