1+ package aquasec
2+
3+ import (
4+ "fmt"
5+ "testing"
6+
7+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+ "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+ )
10+
11+ func TestAquasecPermissionsSetSaasDatasource (t * testing.T ) {
12+ if ! isSaasEnv () {
13+ t .Skip ("Skipping permission set test because its not a SaaS environment" )
14+ }
15+
16+ resource .Test (t , resource.TestCase {
17+ PreCheck : func () { testAccPreCheck (t ) },
18+ Providers : testAccProviders ,
19+ Steps : []resource.TestStep {
20+ {
21+ Config : testAccCheckAquasecPermissionsSetSaasBasicConfig (),
22+ Check : resource .ComposeTestCheckFunc (
23+ // Check that the data source was created
24+ testAccCheckAquasecPermissionsSetSaasExists ("data.aquasec_permissions_sets_saas.test" ),
25+ // Check list attribute is populated
26+ resource .TestCheckResourceAttrSet ("data.aquasec_permissions_sets_saas.test" , "permissions_sets.#" ),
27+ // Custom check for data validity
28+ testAccCheckPermissionsSetSaasAttributes ("data.aquasec_permissions_sets_saas.test" ),
29+ ),
30+ },
31+ },
32+ })
33+ }
34+
35+ func TestAquasecPermissionsSetSaasDatasource_Structure (t * testing.T ) {
36+ if ! isSaasEnv () {
37+ t .Skip ("Skipping permission set test because its not a SaaS environment" )
38+ }
39+
40+ resource .Test (t , resource.TestCase {
41+ PreCheck : func () { testAccPreCheck (t ) },
42+ Providers : testAccProviders ,
43+ Steps : []resource.TestStep {
44+ {
45+ Config : testAccCheckAquasecPermissionsSetSaasBasicConfig (),
46+ Check : resource .ComposeTestCheckFunc (
47+ // Verify ID exists
48+ resource .TestCheckResourceAttrSet (
49+ "data.aquasec_permissions_sets_saas.test" ,
50+ "id" ,
51+ ),
52+ // Verify permissions_sets list exists
53+ resource .TestCheckResourceAttrSet (
54+ "data.aquasec_permissions_sets_saas.test" ,
55+ "permissions_sets.#" ,
56+ ),
57+ ),
58+ },
59+ },
60+ })
61+ }
62+
63+ // Basic Config
64+ func testAccCheckAquasecPermissionsSetSaasBasicConfig () string {
65+ return `
66+ data "aquasec_permissions_sets_saas" "test" {
67+ }
68+ `
69+ }
70+
71+ // Check data source exists
72+ func testAccCheckAquasecPermissionsSetSaasExists (n string ) resource.TestCheckFunc {
73+ return func (s * terraform.State ) error {
74+ rs , ok := s .RootModule ().Resources [n ]
75+ if ! ok {
76+ return fmt .Errorf ("Not found: %s" , n )
77+ }
78+
79+ if rs .Primary .ID == "" {
80+ return fmt .Errorf ("No ID is set" )
81+ }
82+
83+ return nil
84+ }
85+ }
86+
87+ // Check attributes match schema
88+ func testAccCheckPermissionsSetSaasAttributes (resourceName string ) resource.TestCheckFunc {
89+ return func (s * terraform.State ) error {
90+ rs , ok := s .RootModule ().Resources [resourceName ]
91+ if ! ok {
92+ return fmt .Errorf ("Not found: %s" , resourceName )
93+ }
94+
95+ numSets , ok := rs .Primary .Attributes ["permissions_sets.#" ]
96+ if ! ok {
97+ return fmt .Errorf ("No permissions_sets found" )
98+ }
99+
100+ // If we have permission sets, verify their structure
101+ if numSets != "0" {
102+ // Get the first permission set and verify required attributes
103+ if name , ok := rs .Primary .Attributes ["permissions_sets.0.name" ]; ! ok || name == "" {
104+ return fmt .Errorf ("permissions_sets.0.name is empty or missing" )
105+ }
106+
107+ if desc , exists := rs .Primary .Attributes ["permissions_sets.0.description" ]; exists {
108+ if desc == "" {
109+ return fmt .Errorf ("permissions_sets.0.description is empty" )
110+ }
111+ }
112+
113+ if _ , ok := rs .Primary .Attributes ["permissions_sets.0.actions.#" ]; ! ok {
114+ return fmt .Errorf ("permissions_sets.0.actions is missing" )
115+ }
116+ }
117+
118+ return nil
119+ }
120+ }
0 commit comments