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

r/aws_connect_user_hierarchy_structure #22836

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1d312d6
feat(connect): userHierarchyStruc schema
GlennChia Jan 30, 2022
b903be7
feat(connect): userHierarchyStruc read
GlennChia Jan 30, 2022
63e424c
feat(connect): userHierarchyStruc create
GlennChia Jan 30, 2022
32b1dda
feat(connect): userHierarchyStruc update
GlennChia Jan 30, 2022
0a2ca36
feat(connect): userHierarchyStruc delete
GlennChia Jan 30, 2022
7851a96
feat(connect): connect_user_hierarchy_structure
GlennChia Jan 30, 2022
de511cb
test(connect): userHierarchyStruc level configs
GlennChia Jan 30, 2022
c37de46
test(connect): check userHierarchyStruc exists
GlennChia Jan 30, 2022
a7483a4
test(connect): check userHierarchyStruc destroy
GlennChia Jan 30, 2022
b9c5d78
test(connect): userHierarchyStruc create, update
GlennChia Jan 30, 2022
4b1eff4
test(connect): userHierarchyStruc disappear
GlennChia Jan 30, 2022
f7772f3
feat(connect): userHierarchyStruc validate name
GlennChia Jan 30, 2022
e616d0f
docs(connect): connect_user_hierarchy_structure
GlennChia Jan 30, 2022
3f4c447
refactor(connect): remove time from id
GlennChia Jan 30, 2022
939d40f
ci(connect): changelog for user_hierarchy_struct
GlennChia Jan 30, 2022
f693400
feat(connect): add MaxItems 1 to hierarchy levels
GlennChia Feb 2, 2022
562b8ec
refactor(connect): generalize level schema to func
GlennChia Feb 2, 2022
5ef8ef4
Merge branch 'main' into f-aws_connect_user_hierarchy_structure
GlennChia Feb 2, 2022
2a4ad3f
fix(connect): lint errors, change tfawserr package
GlennChia Feb 2, 2022
6e163de
Merge branch 'main' into HEAD
ewbankkit Mar 3, 2022
250b596
Fix semgrep 'use-error-code-equals-if-not-checking-message'.
ewbankkit Mar 3, 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
3 changes: 3 additions & 0 deletions .changelog/22836.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_connect_user_hierarchy_structure
```
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,9 @@ func Provider() *schema.Provider {
"aws_connect_lambda_function_association": connect.ResourceLambdaFunctionAssociation(),
"aws_connect_queue": connect.ResourceQueue(),
"aws_connect_quick_connect": connect.ResourceQuickConnect(),
"aws_connect_security_profile": connect.ResourceSecurityProfile(),
"aws_connect_routing_profile": connect.ResourceRoutingProfile(),
"aws_connect_security_profile": connect.ResourceSecurityProfile(),
"aws_connect_user_hierarchy_structure": connect.ResourceUserHierarchyStructure(),

"aws_cur_report_definition": cur.ResourceReportDefinition(),

Expand Down
264 changes: 264 additions & 0 deletions internal/service/connect/user_hierarchy_structure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
package connect

import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/connect"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"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"
)

func ResourceUserHierarchyStructure() *schema.Resource {
return &schema.Resource{
CreateContext: resourceUserHierarchyStructureCreate,
ReadContext: resourceUserHierarchyStructureRead,
UpdateContext: resourceUserHierarchyStructureUpdate,
DeleteContext: resourceUserHierarchyStructureDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"hierarchy_structure": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"level_one": func() *schema.Schema {
schema := connectUserHierarchyLevelSchema()
return schema
}(),
"level_two": func() *schema.Schema {
schema := connectUserHierarchyLevelSchema()
return schema
}(),
"level_three": func() *schema.Schema {
schema := connectUserHierarchyLevelSchema()
return schema
}(),
"level_four": func() *schema.Schema {
schema := connectUserHierarchyLevelSchema()
return schema
}(),
"level_five": func() *schema.Schema {
schema := connectUserHierarchyLevelSchema()
return schema
}(),
},
},
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
},
}
}

// Each level shares the same schema
func connectUserHierarchyLevelSchema() *schema.Schema {
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 50),
},
},
},
}
}

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

instanceID := d.Get("instance_id").(string)

input := &connect.UpdateUserHierarchyStructureInput{
HierarchyStructure: expandUserHierarchyStructure(d.Get("hierarchy_structure").([]interface{})),
InstanceId: aws.String(instanceID),
}

log.Printf("[DEBUG] Creating Connect User Hierarchy Structure %s", input)
_, err := conn.UpdateUserHierarchyStructureWithContext(ctx, input)

if err != nil {
return diag.FromErr(fmt.Errorf("error creating Connect User Hierarchy Structure for Connect Instance (%s): %w", instanceID, err))
}

d.SetId(instanceID)

return resourceUserHierarchyStructureRead(ctx, d, meta)
}

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

instanceID := d.Id()

resp, err := conn.DescribeUserHierarchyStructureWithContext(ctx, &connect.DescribeUserHierarchyStructureInput{
InstanceId: aws.String(instanceID),
})

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, connect.ErrCodeResourceNotFoundException) {
log.Printf("[WARN] Connect User Hierarchy Structure (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return diag.FromErr(fmt.Errorf("error getting Connect User Hierarchy Structure (%s): %w", d.Id(), err))
}

if resp == nil || resp.HierarchyStructure == nil {
return diag.FromErr(fmt.Errorf("error getting Connect User Hierarchy Structure (%s): empty response", d.Id()))
}

if err := d.Set("hierarchy_structure", flattenUserHierarchyStructure(resp.HierarchyStructure)); err != nil {
return diag.FromErr(fmt.Errorf("error setting Connect User Hierarchy Structure hierarchy_structure for Connect instance: (%s)", d.Id()))
}

d.Set("instance_id", instanceID)

return nil
}

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

instanceID := d.Id()

if d.HasChange("hierarchy_structure") {
_, err := conn.UpdateUserHierarchyStructureWithContext(ctx, &connect.UpdateUserHierarchyStructureInput{
HierarchyStructure: expandUserHierarchyStructure(d.Get("hierarchy_structure").([]interface{})),
InstanceId: aws.String(instanceID),
})

if err != nil {
return diag.FromErr(fmt.Errorf("error updating UserHierarchyStructure Name (%s): %w", d.Id(), err))
}
}

return resourceUserHierarchyStructureRead(ctx, d, meta)
}

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

instanceID := d.Id()

_, err := conn.UpdateUserHierarchyStructureWithContext(ctx, &connect.UpdateUserHierarchyStructureInput{
HierarchyStructure: &connect.HierarchyStructureUpdate{},
InstanceId: aws.String(instanceID),
})

if err != nil {
return diag.FromErr(fmt.Errorf("error deleting UserHierarchyStructure (%s): %w", d.Id(), err))
}

return nil
}

func expandUserHierarchyStructure(userHierarchyStructure []interface{}) *connect.HierarchyStructureUpdate {
if len(userHierarchyStructure) == 0 {
return &connect.HierarchyStructureUpdate{}
}

tfMap, ok := userHierarchyStructure[0].(map[string]interface{})

if !ok {
return nil
}

result := &connect.HierarchyStructureUpdate{
LevelOne: expandUserHierarchyStructureLevel(tfMap["level_one"].([]interface{})),
LevelTwo: expandUserHierarchyStructureLevel(tfMap["level_two"].([]interface{})),
LevelThree: expandUserHierarchyStructureLevel(tfMap["level_three"].([]interface{})),
LevelFour: expandUserHierarchyStructureLevel(tfMap["level_four"].([]interface{})),
LevelFive: expandUserHierarchyStructureLevel(tfMap["level_five"].([]interface{})),
}

return result
}

func expandUserHierarchyStructureLevel(userHierarchyStructureLevel []interface{}) *connect.HierarchyLevelUpdate {
if len(userHierarchyStructureLevel) == 0 {
return nil
}

tfMap, ok := userHierarchyStructureLevel[0].(map[string]interface{})

if !ok {
return nil
}

result := &connect.HierarchyLevelUpdate{
Name: aws.String(tfMap["name"].(string)),
}

return result
}

func flattenUserHierarchyStructure(userHierarchyStructure *connect.HierarchyStructure) []interface{} {
if userHierarchyStructure == nil {
return []interface{}{}
}

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

if userHierarchyStructure.LevelOne != nil {
values["level_one"] = flattenUserHierarchyStructureLevel(userHierarchyStructure.LevelOne)
}

if userHierarchyStructure.LevelTwo != nil {
values["level_two"] = flattenUserHierarchyStructureLevel(userHierarchyStructure.LevelTwo)
}

if userHierarchyStructure.LevelThree != nil {
values["level_three"] = flattenUserHierarchyStructureLevel(userHierarchyStructure.LevelThree)
}

if userHierarchyStructure.LevelFour != nil {
values["level_four"] = flattenUserHierarchyStructureLevel(userHierarchyStructure.LevelFour)
}

if userHierarchyStructure.LevelFive != nil {
values["level_five"] = flattenUserHierarchyStructureLevel(userHierarchyStructure.LevelFive)
}

return []interface{}{values}
}

func flattenUserHierarchyStructureLevel(userHierarchyStructureLevel *connect.HierarchyLevel) []interface{} {
if userHierarchyStructureLevel == nil {
return []interface{}{}
}

level := map[string]interface{}{
"arn": aws.StringValue(userHierarchyStructureLevel.Arn),
"id": aws.StringValue(userHierarchyStructureLevel.Id),
"name": aws.StringValue(userHierarchyStructureLevel.Name),
}

return []interface{}{level}
}
Loading