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

Add d/aws_identitystore_groups #36993

Merged
3 changes: 3 additions & 0 deletions .changelog/36993.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_identitystore_groups
```
102 changes: 102 additions & 0 deletions internal/service/identitystore/groups_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package identitystore

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/identitystore"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
)

// @FrameworkDataSource(name="Groups")
func newGroupsDataSource(context.Context) (datasource.DataSourceWithConfigure, error) {
return &groupsDataSource{}, nil
}

type groupsDataSource struct {
framework.DataSourceWithConfigure
}

func (*groupsDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
response.TypeName = "aws_identitystore_groups"
}

func (d *groupsDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"groups": schema.ListAttribute{
CustomType: fwtypes.NewListNestedObjectTypeOf[groupModel](ctx),
Computed: true,
ElementType: types.ObjectType{
AttrTypes: fwtypes.AttributeTypesMust[groupModel](ctx),
},
},
"identity_store_id": schema.StringAttribute{
Required: true,
},
},
}
}

func (d *groupsDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
var data groupsDataSourceModel
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := d.Meta().IdentityStoreClient(ctx)

input := &identitystore.ListGroupsInput{
IdentityStoreId: fwflex.StringFromFramework(ctx, data.IdentityStoreID),
}

var output *identitystore.ListGroupsOutput
pages := identitystore.NewListGroupsPaginator(conn, input)
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if err != nil {
response.Diagnostics.AddError("listing IdentityStore Groups", err.Error())

return
}

if output == nil {
output = page
} else {
output.Groups = append(output.Groups, page.Groups...)
}
}

response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
if response.Diagnostics.HasError() {
return
}

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

type groupsDataSourceModel struct {
IdentityStoreID types.String `tfsdk:"identity_store_id"`
Groups fwtypes.ListNestedObjectValueOf[groupModel] `tfsdk:"groups"`
}

type groupModel struct {
Description types.String `tfsdk:"description"`
DisplayName types.String `tfsdk:"display_name"`
ExternalIDs fwtypes.ListNestedObjectValueOf[externalIDModel] `tfsdk:"external_ids"`
GroupID types.String `tfsdk:"group_id"`
IdentityStoreID types.String `tfsdk:"identity_store_id"`
}

type externalIDModel struct {
ID types.String `tfsdk:"id"`
Issuer types.String `tfsdk:"issuer"`
}
55 changes: 55 additions & 0 deletions internal/service/identitystore/groups_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package identitystore_test

import (
"fmt"
"testing"

sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccIdentityStoreGroupsDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_identitystore_groups.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckSSOAdminInstances(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.IdentityStoreServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccConfigGroups_basic(rName),
Check: resource.ComposeTestCheckFunc(
acctest.CheckResourceAttrGreaterThanValue(dataSourceName, "groups.#", 0),
),
},
},
})
}

func testAccConfigGroups_basic(groupName string) string {
return fmt.Sprintf(`
data "aws_ssoadmin_instances" "test" {}

resource "aws_identitystore_group" "test" {
identity_store_id = data.aws_ssoadmin_instances.test.identity_store_ids[0]
display_name = %[1]q
description = "Acceptance Test"
}

data "aws_identitystore_groups" "test" {
depends_on = [aws_identitystore_group.test]

identity_store_id = data.aws_ssoadmin_instances.test.identity_store_ids[0]
}
`, groupName)
}
7 changes: 6 additions & 1 deletion internal/service/identitystore/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions website/docs/d/identitystore_groups.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subcategory: "SSO Identity Store"
layout: "aws"
page_title: "AWS: aws_identitystore_groups"
description: |-
Terraform data source for managing an AWS SSO Identity Store Groups.
---

# Data Source: aws_identitystore_groups

Terraform data source for managing an AWS SSO Identity Store Groups.

## Example Usage

### Basic Usage

```terraform
data "aws_ssoadmin_instances" "example" {}

data "aws_identitystore_groups" "example" {
identity_store_id = data.aws_ssoadmin_instances.example.identity_store_ids[0]
}
```

## Argument Reference

The following arguments are required:

* `identity_store_id` - (Required) Identity Store ID associated with the Single Sign-On (SSO) Instance.

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `groups` - List of Identity Store Groups
* `group_id` - Identifier of the group in the Identity Store.
* `description` - Description of the specified group.
* `display_name` - Group's display name.
* `external_ids` - List of identifiers issued to this resource by an external identity provider.
* `id` - Identifier issued to this resource by an external identity provider.
* `issuer` - Issuer for an external identifier.
Loading