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

New resource aws_identitystore_group #26674

Merged
merged 26 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a49ad08
Create group.go
bschaatsbergen Sep 6, 2022
2a6c126
Update provider.go
bschaatsbergen Sep 6, 2022
a41f99d
Make `identity_store_id` required
bschaatsbergen Sep 6, 2022
ef18114
alphabetical schema order + add force news to optional/required args
bschaatsbergen Sep 6, 2022
ad6a986
Merge branch 'main' into r/identitystore-group
bschaatsbergen Sep 14, 2022
4d07e27
Tidy up `group.go` to be in line with `user.go`
bschaatsbergen Sep 14, 2022
c5e677e
Remove ForceNew on non required params
bschaatsbergen Sep 14, 2022
67211b7
Create group_test.go
bschaatsbergen Sep 14, 2022
a16d176
Change test description
bschaatsbergen Sep 14, 2022
45651d4
Create identitystore_group.html.markdown
bschaatsbergen Sep 14, 2022
680eec7
Update text under import paragraph
bschaatsbergen Sep 14, 2022
bbedb07
fmt `testAccGroupConfig_basic`
bschaatsbergen Sep 14, 2022
031bffa
Set `d.Id()` to the group ID
bschaatsbergen Sep 14, 2022
dacaa52
Provide an actual group ID in the import example
bschaatsbergen Sep 14, 2022
3f74774
Fix import by changing the internal resource ID
bschaatsbergen Sep 22, 2022
c879f99
Remove unused code
bschaatsbergen Sep 22, 2022
be594da
update resourceGroupParseID error message
bschaatsbergen Sep 25, 2022
55905b9
Change subcategory to `SSO Identity Store`
bschaatsbergen Sep 25, 2022
60c94a5
Fix import example
bschaatsbergen Sep 25, 2022
028831d
Fix import order
bschaatsbergen Sep 25, 2022
f8f94d1
Add testAccPreCheck
bschaatsbergen Sep 25, 2022
906ffe7
ForceNew the `display_name` arg
bschaatsbergen Sep 25, 2022
a9ed197
Merge branch 'main' into HEAD
ewbankkit Sep 27, 2022
6118b61
Add CHANGELOG entry.
ewbankkit Sep 27, 2022
cdbf1b7
Remove duplicated code.
ewbankkit Sep 27, 2022
4a15904
Add 'findGroupByID'.
ewbankkit Sep 27, 2022
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
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,8 @@ func New(_ context.Context) (*schema.Provider, error) {
"aws_iam_user_ssh_key": iam.ResourceUserSSHKey(),
"aws_iam_virtual_mfa_device": iam.ResourceVirtualMFADevice(),

"aws_identitystore_group": identitystore.ResourceGroup(),

"aws_imagebuilder_component": imagebuilder.ResourceComponent(),
"aws_imagebuilder_container_recipe": imagebuilder.ResourceContainerRecipe(),
"aws_imagebuilder_distribution_configuration": imagebuilder.ResourceDistributionConfiguration(),
Expand Down
226 changes: 226 additions & 0 deletions internal/service/identitystore/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package identitystore

import (
"context"
"errors"
"fmt"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/identitystore"
"github.com/aws/aws-sdk-go-v2/service/identitystore/document"
types "github.com/aws/aws-sdk-go-v2/service/identitystore/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

func ResourceGroup() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceGroupCreate,
ReadWithoutTimeout: resourceGroupRead,
UpdateWithoutTimeout: resourceGroupUpdate,
DeleteWithoutTimeout: resourceGroupDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"display_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"external_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"issuer": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"group_id": {
Type: schema.TypeString,
Computed: true,
},
"identity_store_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

const (
ResNameGroup = "Group"
)

func resourceGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).IdentityStoreConn

identityStoreId := d.Get("identity_store_id").(string)

input := &identitystore.CreateGroupInput{
IdentityStoreId: aws.String(identityStoreId),
}

if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}

if v, ok := d.GetOk("display_name"); ok {
input.DisplayName = aws.String(v.(string))
}

out, err := conn.CreateGroup(ctx, input)

if err != nil {
return create.DiagError(names.IdentityStore, create.ErrActionCreating, ResNameGroup, d.Get("identity_store_id").(string), err)
}

if out == nil || out.GroupId == nil {
return create.DiagError(names.IdentityStore, create.ErrActionCreating, ResNameGroup, d.Get("identity_store_id").(string), errors.New("empty output"))
}

d.SetId(fmt.Sprintf("%s/%s", aws.ToString(out.IdentityStoreId), aws.ToString(out.GroupId)))

return resourceGroupRead(ctx, d, meta)
}

func resourceGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).IdentityStoreConn

input := &identitystore.DescribeGroupInput{
GroupId: aws.String(d.Id()),
IdentityStoreId: aws.String(d.Get("identity_store_id").(string)),
}

out, err := conn.DescribeGroup(ctx, input)

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] IdentityStore Group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return create.DiagError(names.IdentityStore, create.ErrActionReading, ResNameGroup, d.Id(), err)
}

d.Set("group_id", out.GroupId)
d.Set("identity_store_id", out.IdentityStoreId)
d.Set("description", out.Description)
d.Set("display_name", out.DisplayName)

if err := d.Set("external_ids", flattenExternalIds(out.ExternalIds)); err != nil {
return create.DiagError(names.IdentityStore, create.ErrActionSetting, ResNameGroup, d.Id(), err)
}

return nil
}

func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).IdentityStoreConn

in := &identitystore.UpdateGroupInput{
GroupId: aws.String(d.Get("group_id").(string)),
IdentityStoreId: aws.String(d.Get("identity_store_id").(string)),
Operations: nil,
}

if d.HasChange("display_name") {
in.Operations = append(in.Operations, types.AttributeOperation{
AttributePath: aws.String("displayName"),
AttributeValue: document.NewLazyDocument(d.Get("display_name").(string)),
})
}

if len(in.Operations) > 0 {
log.Printf("[DEBUG] Updating IdentityStore Group (%s): %#v", d.Id(), in)
_, err := conn.UpdateGroup(ctx, in)
if err != nil {
return create.DiagError(names.IdentityStore, create.ErrActionUpdating, ResNameGroup, d.Id(), err)
}
}

return resourceGroupRead(ctx, d, meta)
}

func resourceGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).IdentityStoreConn

log.Printf("[INFO] Deleting IdentityStore Group %s", d.Id())

input := &identitystore.DeleteGroupInput{
GroupId: aws.String(d.Id()),
IdentityStoreId: aws.String(d.Get("identity_store_id").(string)),
}

_, err := conn.DeleteGroup(ctx, input)

if err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
return nil
}

return create.DiagError(names.IdentityStore, create.ErrActionDeleting, ResNameGroup, d.Id(), err)
}

return nil
}

func flattenExternalIds(apiObjects []types.ExternalId) []interface{} {
if len(apiObjects) == 0 {
return nil
}

var l []interface{}

for _, apiObject := range apiObjects {
if apiObject == (types.ExternalId{}) {
continue
}

l = append(l, flattenExternalId(apiObject))
}

return l
}

func flattenExternalId(apiObject types.ExternalId) map[string]interface{} {
if apiObject == (types.ExternalId{}) {
return nil
}

m := map[string]interface{}{}

if v := apiObject.Id; v != nil {
m["id"] = aws.ToString(v)
}

if v := apiObject.Issuer; v != nil {
m["issuer"] = aws.ToString(v)
}

return m
}
Loading