<commit_before>package aws

import (
	"fmt"
	"strings"
	"testing"

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

func TestAccAWSSSOPermissionSet_Basic(t *testing.T) {
	var permissionSet ssoadmin.PermissionSet
	resourceName := "aws_sso_permission_set.example"
	rName := acctest.RandomWithPrefix("tf-sso-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSSSOInstance(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSSOPermissionSetDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccSSOPermissionSetBasicConfig(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName, &permissionSet),
					resource.TestCheckResourceAttr(resourceName, "name", rName),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSSSOPermissionSet_ManagedPolicies(t *testing.T) {
	var permissionSet, updatedPermissionSet ssoadmin.PermissionSet
	resourceName := "aws_sso_permission_set.example"
	rName := acctest.RandomWithPrefix("tf-sso-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSSSOInstance(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSSOPermissionSetDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccSSOPermissionSetBasicConfig(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName, &permissionSet),
					resource.TestCheckResourceAttr(resourceName, "managed_policy_arns.#", "1"),
					resource.TestCheckTypeSetElemAttr(resourceName, "managed_policy_arns.*", "arn:aws:iam::aws:policy/ReadOnlyAccess"), // lintignore:AWSAT005
					resource.TestCheckResourceAttr(resourceName, "name", rName),
					resource.TestCheckResourceAttr(resourceName, "description", "Just a test"),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				Config: testAccSSOPermissionSetBasicConfigUpdated(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName, &updatedPermissionSet),
					resource.TestCheckResourceAttr(resourceName, "managed_policy_arns.#", "2"),
					resource.TestCheckTypeSetElemAttr(resourceName, "managed_policy_arns.*", "arn:aws:iam::aws:policy/ReadOnlyAccess"),              // lintignore:AWSAT005
					resource.TestCheckTypeSetElemAttr(resourceName, "managed_policy_arns.*", "arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"), // lintignore:AWSAT005
					resource.TestCheckResourceAttr(resourceName, "name", rName),
					resource.TestCheckResourceAttr(resourceName, "description", "Just a test update"),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
				),
			},
		},
	})
}

func TestAccAWSSSOPermissionSet_Disappears(t *testing.T) {
	var permissionSet ssoadmin.PermissionSet
	resourceName := "aws_sso_permission_set.example"
	rName := acctest.RandomWithPrefix("tf-sso-test")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSSSOInstance(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSSOPermissionSetDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccSSOPermissionSetBasicConfig(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName, &permissionSet),
					testAccCheckResourceDisappears(testAccProvider, resourceAwsSsoPermissionSet(), resourceName),
				),
				ExpectNonEmptyPlan: true,
			},
		},
	})
}

func TestAccAWSSSOPermissionSet_Tags(t *testing.T) {
	var permissionSet ssoadmin.PermissionSet
	resourceName := "aws_sso_permission_set.example"
	rName := acctest.RandomWithPrefix("tf-sso-test")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSSSOInstance(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSSOPermissionSetDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccSSOPermissionSetConfigTagsSingle(rName, "key1", "value1"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName, &permissionSet),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
					resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				Config: testAccSSOPermissionSetConfigTagsMultiple(rName, "key1", "updatedvalue1", "key2", "value2"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName, &permissionSet),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
					resource.TestCheckResourceAttr(resourceName, "tags.key1", "updatedvalue1"),
					resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
				),
			},
			{
				Config: testAccSSOPermissionSetConfigTagsSingle(rName, "key2", "value2"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName, &permissionSet),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
					resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
				),
			},
		},
	})
}

func testAccCheckAWSSSOPermissionSetExists(resourceName string, permissionSet *ssoadmin.PermissionSet) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[resourceName]
		if !ok {
			return fmt.Errorf("Not found: %s", resourceName)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("Resource (%s) ID not set", resourceName)
		}

		instanceArn, err := resourceAwsSsoPermissionSetParseID(rs.Primary.ID)

		if err != nil {
			return err
		}

		ssoadminconn := testAccProvider.Meta().(*AWSClient).ssoadminconn

		permissionSetResp, permissionSetErr := ssoadminconn.DescribePermissionSet(&ssoadmin.DescribePermissionSetInput{
			InstanceArn:      aws.String(instanceArn),
			PermissionSetArn: aws.String(rs.Primary.ID),
		})<commit_msg>remove unused parameter<commit_after>package aws

import (
	"fmt"
	"strings"
	"testing"

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

func TestAccAWSSSOPermissionSet_Basic(t *testing.T) {
	resourceName := "aws_sso_permission_set.example"
	rName := acctest.RandomWithPrefix("tf-sso-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSSSOInstance(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSSOPermissionSetDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccSSOPermissionSetBasicConfig(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName),
					resource.TestCheckResourceAttr(resourceName, "name", rName),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSSSOPermissionSet_ManagedPolicies(t *testing.T) {
	resourceName := "aws_sso_permission_set.example"
	rName := acctest.RandomWithPrefix("tf-sso-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSSSOInstance(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSSOPermissionSetDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccSSOPermissionSetBasicConfig(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName),
					resource.TestCheckResourceAttr(resourceName, "managed_policy_arns.#", "1"),
					resource.TestCheckTypeSetElemAttr(resourceName, "managed_policy_arns.*", "arn:aws:iam::aws:policy/ReadOnlyAccess"), // lintignore:AWSAT005
					resource.TestCheckResourceAttr(resourceName, "name", rName),
					resource.TestCheckResourceAttr(resourceName, "description", "Just a test"),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				Config: testAccSSOPermissionSetBasicConfigUpdated(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName),
					resource.TestCheckResourceAttr(resourceName, "managed_policy_arns.#", "2"),
					resource.TestCheckTypeSetElemAttr(resourceName, "managed_policy_arns.*", "arn:aws:iam::aws:policy/ReadOnlyAccess"),              // lintignore:AWSAT005
					resource.TestCheckTypeSetElemAttr(resourceName, "managed_policy_arns.*", "arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"), // lintignore:AWSAT005
					resource.TestCheckResourceAttr(resourceName, "name", rName),
					resource.TestCheckResourceAttr(resourceName, "description", "Just a test update"),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
				),
			},
		},
	})
}

func TestAccAWSSSOPermissionSet_Disappears(t *testing.T) {
	resourceName := "aws_sso_permission_set.example"
	rName := acctest.RandomWithPrefix("tf-sso-test")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSSSOInstance(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSSOPermissionSetDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccSSOPermissionSetBasicConfig(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName),
					testAccCheckResourceDisappears(testAccProvider, resourceAwsSsoPermissionSet(), resourceName),
				),
				ExpectNonEmptyPlan: true,
			},
		},
	})
}

func TestAccAWSSSOPermissionSet_Tags(t *testing.T) {
	resourceName := "aws_sso_permission_set.example"
	rName := acctest.RandomWithPrefix("tf-sso-test")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSSSOInstance(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSSOPermissionSetDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccSSOPermissionSetConfigTagsSingle(rName, "key1", "value1"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
					resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				Config: testAccSSOPermissionSetConfigTagsMultiple(rName, "key1", "updatedvalue1", "key2", "value2"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
					resource.TestCheckResourceAttr(resourceName, "tags.key1", "updatedvalue1"),
					resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
				),
			},
			{
				Config: testAccSSOPermissionSetConfigTagsSingle(rName, "key2", "value2"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSSOPermissionSetExists(resourceName),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
					resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
				),
			},
		},
	})
}

func testAccCheckAWSSSOPermissionSetExists(resourceName string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[resourceName]
		if !ok {
			return fmt.Errorf("Not found: %s", resourceName)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("Resource (%s) ID not set", resourceName)
		}

		instanceArn, err := resourceAwsSsoPermissionSetParseID(rs.Primary.ID)

		if err != nil {
			return err
		}

		ssoadminconn := testAccProvider.Meta().(*AWSClient).ssoadminconn

		permissionSetResp, permissionSetErr := ssoadminconn.DescribePermissionSet(&ssoadmin.DescribePermissionSetInput{
			InstanceArn:      aws.String(instanceArn),
			PermissionSetArn: aws.String(rs.Primary.ID),
		})
{'fraction_non_alphanumeric': 0.12123588456712672, 'fraction_numerical': 0.005489335006273526, 'mean_word_length': 5.190776699029127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6877558', 'n_tokens_mistral': 4768, 'n_tokens_neox': 4183, 'n_words': 552}