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_sso_permission_set: New Resource #15808

Merged
merged 37 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
fbf5fb2
add aws_sso_instance data source
lawdhavmercy Oct 22, 2020
dfdcee6
add aws_sso_permission_set data source
lawdhavmercy Oct 22, 2020
a0a625b
use testAccCheckResourceDisappears
lawdhavmercy Oct 23, 2020
b098aed
fix misspelling
lawdhavmercy Nov 4, 2020
48ab1f0
fix aws partition lints
burck1 Nov 4, 2020
b105c99
fix aws_sso_instance id
burck1 Nov 4, 2020
e33652f
Update aws/data_source_aws_sso_instance.go
burck1 Jan 1, 2021
cca8081
Update aws/data_source_aws_sso_instance.go
burck1 Jan 1, 2021
85249f3
Update aws/data_source_aws_sso_instance.go
burck1 Jan 1, 2021
4712077
Update aws/data_source_aws_sso_instance.go
burck1 Jan 1, 2021
9bef8e5
remove unused function
burck1 Jan 1, 2021
a35a77b
Update aws/data_source_aws_sso_permission_set.go
burck1 Jan 1, 2021
c7b1c87
Update aws/data_source_aws_sso_permission_set.go
burck1 Jan 1, 2021
e2587d4
Update aws/data_source_aws_sso_permission_set.go
burck1 Jan 1, 2021
b0e14a2
Update aws/resource_aws_sso_permission_set.go
burck1 Jan 1, 2021
8b85070
Update aws/resource_aws_sso_permission_set.go
burck1 Jan 1, 2021
207b3fa
Update aws/resource_aws_sso_permission_set_test.go
burck1 Jan 1, 2021
46615f2
Update aws/resource_aws_sso_permission_set.go
burck1 Jan 1, 2021
014e53e
Update aws/resource_aws_sso_permission_set_test.go
burck1 Jan 1, 2021
6695ca1
Update aws/resource_aws_sso_permission_set.go
burck1 Jan 1, 2021
7a2bfcb
Update aws/resource_aws_sso_permission_set.go
burck1 Jan 1, 2021
a0fd88a
fix missing quote
burck1 Jan 1, 2021
0a367d3
Update aws/resource_aws_sso_permission_set.go
burck1 Jan 1, 2021
0b7692b
Update aws/resource_aws_sso_permission_set.go
burck1 Jan 1, 2021
69ebf83
Update website/docs/d/sso_instance.html.markdown
burck1 Jan 1, 2021
a539d12
remove redundant error checks
burck1 Jan 1, 2021
7949b0f
update tfawsresource to use resource package
burck1 Jan 2, 2021
f5495d0
Update aws/resource_aws_sso_permission_set_test.go
lawdhavmercy Jan 4, 2021
ab182a1
remove unused parameter
lawdhavmercy Jan 4, 2021
211f5dc
move global/no account arn test
lawdhavmercy Jan 5, 2021
8fcc98f
CR updates; align with design
anGie44 Jan 6, 2021
833ff5d
importlint, typo, docs, remove unused
anGie44 Jan 7, 2021
db0df8d
add inline policy resource for permission set
anGie44 Jan 7, 2021
05d56d5
inline policy resource updates; managed policy attachment resource
anGie44 Jan 7, 2021
4e86ee0
add missing attribute
anGie44 Jan 7, 2021
ca0a0f2
add mixed policy attachments test to permission set
anGie44 Jan 7, 2021
2b5a8ba
rebased to master version w/changes
anGie44 Jan 7, 2021
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
69 changes: 69 additions & 0 deletions aws/data_source_aws_ssoadmin_instances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssoadmin"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceAwsSsoAdminInstances() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsSsoAdminInstancesRead,

Schema: map[string]*schema.Schema{
"arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"identity_store_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

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

var instances []*ssoadmin.InstanceMetadata
var arns, ids []string

err := conn.ListInstancesPages(&ssoadmin.ListInstancesInput{}, func(page *ssoadmin.ListInstancesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

instances = append(instances, page.Instances...)

return !lastPage
})

if err != nil {
return fmt.Errorf("error reading SSO Instances: %w", err)
}

if len(instances) == 0 {
return fmt.Errorf("error reading SSO Instance: no instances found")
}

for _, instance := range instances {
arns = append(arns, aws.StringValue(instance.InstanceArn))
ids = append(ids, aws.StringValue(instance.IdentityStoreId))
}

d.SetId(meta.(*AWSClient).region)
if err := d.Set("arns", arns); err != nil {
return fmt.Errorf("error setting arns: %w", err)
}
if err := d.Set("identity_store_ids", ids); err != nil {
return fmt.Errorf("error setting identity_store_ids: %w", err)
}

return nil
}
58 changes: 58 additions & 0 deletions aws/data_source_aws_ssoadmin_instances_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package aws

import (
"regexp"
"testing"

"github.com/aws/aws-sdk-go/service/ssoadmin"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func testAccPreCheckAWSSSOAdminInstances(t *testing.T) {
conn := testAccProvider.Meta().(*AWSClient).ssoadminconn

var instances []*ssoadmin.InstanceMetadata
err := conn.ListInstancesPages(&ssoadmin.ListInstancesInput{}, func(page *ssoadmin.ListInstancesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

instances = append(instances, page.Instances...)

return !lastPage
})

if testAccPreCheckSkipError(err) {
t.Skipf("skipping acceptance testing: %s", err)
}

if len(instances) == 0 {
t.Skip("skipping acceptance testing: No SSO Instance found.")
}

if err != nil {
t.Fatalf("unexpected PreCheck error: %s", err)
}
}

func TestAccDataSourceAWSSSOAdminInstances_basic(t *testing.T) {
dataSourceName := "data.aws_ssoadmin_instances.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSSSOAdminInstances(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAWSSSOAdminInstancesConfigBasic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "arns.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "identity_store_ids.#", "1"),
testAccMatchResourceAttrGlobalARNNoAccount(dataSourceName, "arns.0", "sso", regexp.MustCompile("instance/(sso)?ins-[a-zA-Z0-9-.]{16}")),
resource.TestMatchResourceAttr(dataSourceName, "identity_store_ids.0", regexp.MustCompile("^[a-zA-Z0-9-]*")),
),
},
},
})
}

const testAccDataSourceAWSSSOAdminInstancesConfigBasic = `data "aws_ssoadmin_instances" "test" {}`
171 changes: 171 additions & 0 deletions aws/data_source_aws_ssoadmin_permission_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package aws

import (
"errors"
"fmt"
"regexp"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssoadmin"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsSsoAdminPermissionSet() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsSsoAdminPermissionSetRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateArn,
ExactlyOneOf: []string{"arn", "name"},
},

"created_date": {
Type: schema.TypeString,
Computed: true,
},

"description": {
Type: schema.TypeString,
Computed: true,
},

"instance_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},

"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 32),
validation.StringMatch(regexp.MustCompile(`[\w+=,.@-]+`), "must match [\\w+=,.@-]"),
),
ExactlyOneOf: []string{"name", "arn"},
},

"relay_state": {
Type: schema.TypeString,
Computed: true,
},

"session_duration": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsSsoAdminPermissionSetRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ssoadminconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

instanceArn := d.Get("instance_arn").(string)

var permissionSet *ssoadmin.PermissionSet

if v, ok := d.GetOk("arn"); ok {
arn := v.(string)

input := &ssoadmin.DescribePermissionSetInput{
InstanceArn: aws.String(instanceArn),
PermissionSetArn: aws.String(arn),
}

output, err := conn.DescribePermissionSet(input)
if err != nil {
return fmt.Errorf("error reading SSO Admin Permission Set (%s): %w", arn, err)
}

if output == nil {
return fmt.Errorf("error reading SSO Admin Permission Set (%s): empty output", arn)
}

permissionSet = output.PermissionSet
} else if v, ok := d.GetOk("name"); ok {
name := v.(string)
var describeErr error

input := &ssoadmin.ListPermissionSetsInput{
InstanceArn: aws.String(instanceArn),
}

err := conn.ListPermissionSetsPages(input, func(page *ssoadmin.ListPermissionSetsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, permissionSetArn := range page.PermissionSets {
if permissionSetArn == nil {
continue
}

output, describeErr := conn.DescribePermissionSet(&ssoadmin.DescribePermissionSetInput{
InstanceArn: aws.String(instanceArn),
PermissionSetArn: permissionSetArn,
})

if describeErr != nil {
return false
}

if output == nil || output.PermissionSet == nil {
continue
}

if aws.StringValue(output.PermissionSet.Name) == name {
permissionSet = output.PermissionSet
return false
}
}

return !lastPage
})

if err != nil {
return fmt.Errorf("error listing SSO Permission Sets: %w", err)
}

if describeErr != nil {
return fmt.Errorf("error reading SSO Permission Set (%s): %w", name, describeErr)
}
}

if permissionSet == nil {
return errors.New("error reading SSO Permission Set: not found")
}

arn := aws.StringValue(permissionSet.PermissionSetArn)

d.SetId(arn)
d.Set("arn", arn)
d.Set("created_date", permissionSet.CreatedDate.Format(time.RFC3339))
d.Set("description", permissionSet.Description)
d.Set("instance_arn", instanceArn)
d.Set("name", permissionSet.Name)
d.Set("session_duration", permissionSet.SessionDuration)
d.Set("relay_state", permissionSet.RelayState)

tags, err := keyvaluetags.SsoadminListTags(conn, arn, instanceArn)
if err != nil {
return fmt.Errorf("error listing tags for SSO Permission Set (%s): %w", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

return nil
}
Loading