Skip to content

Commit

Permalink
CFS-7074 - Terraform Provider AWS - QuickSight Integration (#24)
Browse files Browse the repository at this point in the history
* Rectify inconsistent vendoring

Addresses error:
go: inconsistent vendoring in /code:
    github.com/aws/aws-sdk-go@v1.35.8: is explicitly required in
    go.mod, but vendor/modules.txt indicates
    github.com/aws/aws-sdk-go@v1.29.24

        To ignore the vendor directory, use -mod=readonly or -mod=mod.
        To sync the vendor directory, run:
            go mod vendor

* Add AWS QuickSight Group Membership

Cribbed from prior art implementation here:
hashicorp/terraform-provider-aws#11160

* Add AWS QuickSight IAM Policy Assignment

Cribbed from prior art implementation here:
hashicorp/terraform-provider-aws#12279

* Add AWS QuickSight Namespaces
  • Loading branch information
a11e99 authored Jul 20, 2021
1 parent eef85e4 commit ac4e03f
Show file tree
Hide file tree
Showing 1,645 changed files with 430,423 additions and 230,463 deletions.
3 changes: 3 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func Provider() terraform.ResourceProvider {
"aws_iam_role_policy": resourceAwsIamRolePolicy(),
"aws_iam_role_policy_attachment": resourceAwsIamRolePolicyAttachment(),
"aws_quicksight_data_source": resourceAwsQuickSightDataSource(),
"aws_quicksight_group_membership": resourceAwsQuickSightGroupMembership(),
"aws_quicksight_iam_policy_assignment": resourceAwsQuickSightIAMPolicyAssignment(),
"aws_quicksight_namespace": resourceAwsQuickSightNamespace(),
"aws_internet_gateway_detach": resourceAwsInternetGatewayDetach(),
"aws_internet_gateway_delete": resourceAwsInternetGatewayDelete(),
},
Expand Down
177 changes: 177 additions & 0 deletions aws/resource_aws_quicksight_group_membership.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package aws

import (
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/quicksight"
)

func resourceAwsQuickSightGroupMembership() *schema.Resource {
return &schema.Resource{
Create: resourceAwsQuickSightGroupMembershipCreate,
Read: resourceAwsQuickSightGroupMembershipRead,
Delete: resourceAwsQuickSightGroupMembershipDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},

"aws_account_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"member_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"group_name": {
Type: schema.TypeString,
Required: true,
//Optional: true,
ForceNew: true,
},

"namespace": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "default",
ValidateFunc: validation.StringInSlice([]string{
"default",
}, false),
},
},
}
}

func resourceAwsQuickSightGroupMembershipCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).quicksightconn

awsAccountID := meta.(*AWSClient).accountid
namespace := d.Get("namespace").(string)
groupName := d.Get("group_name").(string)

if v, ok := d.GetOk("aws_account_id"); ok {
awsAccountID = v.(string)
}

createOpts := &quicksight.CreateGroupMembershipInput{
AwsAccountId: aws.String(awsAccountID),
GroupName: aws.String(groupName),
MemberName: aws.String(d.Get("member_name").(string)),
Namespace: aws.String(namespace),
}

resp, err := conn.CreateGroupMembership(createOpts)
if err != nil {
return fmt.Errorf("Error adding QuickSight user to group: %s", err)
}

d.SetId(fmt.Sprintf("%s/%s/%s/%s", awsAccountID, namespace, groupName, aws.StringValue(resp.GroupMember.MemberName)))

return resourceAwsQuickSightGroupMembershipRead(d, meta)
}

func resourceAwsQuickSightGroupMembershipRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).quicksightconn

awsAccountID, namespace, groupName, userName, err := resourceAwsQuickSightGroupMembershipParseID(d.Id())
if err != nil {
return err
}

listOpts := &quicksight.ListUserGroupsInput{
AwsAccountId: aws.String(awsAccountID),
Namespace: aws.String(namespace),
UserName: aws.String(userName),
}

found := false

for {
resp, err := conn.ListUserGroups(listOpts)
if isAWSErr(err, quicksight.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] QuickSight User %s is not found", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error listing QuickSight User groups (%s): %s", d.Id(), err)
}

for _, group := range resp.GroupList {
if *group.GroupName == groupName {
found = true
break
}
}

if found || resp.NextToken == nil {
break
}

listOpts.NextToken = resp.NextToken
}

if found {
d.Set("aws_account_id", awsAccountID)
d.Set("namespace", namespace)
d.Set("member_name", userName)
d.Set("group_name", groupName)
} else {
log.Printf("[WARN] QuickSight User-group membership %s is not found", d.Id())
d.SetId("")
}

return nil
}

func resourceAwsQuickSightGroupMembershipDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).quicksightconn

awsAccountID, namespace, groupName, userName, err := resourceAwsQuickSightGroupMembershipParseID(d.Id())
if err != nil {
return err
}

deleteOpts := &quicksight.DeleteGroupMembershipInput{
AwsAccountId: aws.String(awsAccountID),
Namespace: aws.String(namespace),
MemberName: aws.String(userName),
GroupName: aws.String(groupName),
}

if _, err := conn.DeleteGroupMembership(deleteOpts); err != nil {
if isAWSErr(err, quicksight.ErrCodeResourceNotFoundException, "") {
return nil
}
return fmt.Errorf("Error deleting QuickSight User-group membership %s: %s", d.Id(), err)
}

return nil
}

func resourceAwsQuickSightGroupMembershipParseID(id string) (string, string, string, string, error) {
parts := strings.SplitN(id, "/", 4)
if len(parts) < 4 || parts[0] == "" || parts[1] == "" || parts[2] == "" || parts[3] == "" {
return "", "", "", "", fmt.Errorf("unexpected format of ID (%s), expected AWS_ACCOUNT_ID/NAMESPACE/GROUP_NAME/USER_NAME", id)
}
return parts[0], parts[1], parts[2], parts[3], nil
}
Loading

0 comments on commit ac4e03f

Please sign in to comment.