99 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1111 "fmt"
12+ "os"
1213)
1314
1415func TestAccTFEDataRetentionPolicy_basic (t * testing.T ) {
@@ -21,10 +22,9 @@ func TestAccTFEDataRetentionPolicy_basic(t *testing.T) {
2122 CheckDestroy : testAccCheckTFEDataRetentionPolicyDestroy ,
2223 Steps : []resource.TestStep {
2324 {
24- Config : testAccTFEDataRetentionPolicy_basic (rInt ),
25+ Config : testAccTFEDataRetentionPolicy_basic (rInt , 42 ),
2526 Check : resource .ComposeTestCheckFunc (
2627 testAccCheckTFEDataRetentionPolicyExists ("tfe_data_retention_policy.foobar" , policy ),
27- testAccCheckTFEDataRetentionPolicyAttributes (policy ),
2828 resource .TestCheckResourceAttr (
2929 "tfe_data_retention_policy.foobar" , "delete_older_than.days" , "42" ),
3030 ),
@@ -33,7 +33,68 @@ func TestAccTFEDataRetentionPolicy_basic(t *testing.T) {
3333 })
3434}
3535
36- func testAccTFEDataRetentionPolicy_basic (rInt int ) string {
36+ func TestAccTFEDataRetentionPolicy_update (t * testing.T ) {
37+ policy := & tfe.DataRetentionPolicyChoice {}
38+ rInt := rand .New (rand .NewSource (time .Now ().UnixNano ())).Int ()
39+
40+ resource .Test (t , resource.TestCase {
41+ PreCheck : func () { testAccPreCheck (t ) },
42+ ProtoV5ProviderFactories : testAccMuxedProviders ,
43+ CheckDestroy : testAccCheckTFEDataRetentionPolicyDestroy ,
44+ Steps : []resource.TestStep {
45+ {
46+ Config : testAccTFEDataRetentionPolicy_basic (rInt , 42 ),
47+ Check : resource .ComposeTestCheckFunc (
48+ testAccCheckTFEDataRetentionPolicyExists ("tfe_data_retention_policy.foobar" , policy ),
49+ resource .TestCheckResourceAttr (
50+ "tfe_data_retention_policy.foobar" , "delete_older_than.days" , "42" ),
51+ ),
52+ },
53+
54+ {
55+ Config : testAccTFEDataRetentionPolicy_basic (rInt , 1337 ),
56+ Check : resource .ComposeTestCheckFunc (
57+ testAccCheckTFEDataRetentionPolicyExists ("tfe_data_retention_policy.foobar" , policy ),
58+ resource .TestCheckResourceAttr (
59+ "tfe_data_retention_policy.foobar" , "delete_older_than.days" , "1337" ),
60+ ),
61+ },
62+ },
63+ })
64+ }
65+
66+ func TestAccTFEDataRetentionPolicy_organization_level (t * testing.T ) {
67+ policy := & tfe.DataRetentionPolicyChoice {}
68+ defaultOrgName , _ := setupDefaultOrganization (t )
69+
70+ os .Setenv ("TFE_ORGANIZATION" , defaultOrgName )
71+
72+ resource .Test (t , resource.TestCase {
73+ PreCheck : func () { testAccPreCheck (t ) },
74+ ProtoV5ProviderFactories : testAccMuxedProviders ,
75+ CheckDestroy : testAccCheckTFEDataRetentionPolicyDestroy ,
76+ Steps : []resource.TestStep {
77+ {
78+ Config : testAccTFEDataRetentionPolicy_implicit_organization (42 ),
79+ Check : resource .ComposeTestCheckFunc (
80+ testAccCheckTFEDataRetentionPolicyExists ("tfe_data_retention_policy.foobar" , policy ),
81+ resource .TestCheckResourceAttr (
82+ "tfe_data_retention_policy.foobar" , "delete_older_than.days" , "42" ),
83+ ),
84+ },
85+ {
86+ Config : testAccTFEDataRetentionPolicy_implicit_organization (1337 ),
87+ Check : resource .ComposeTestCheckFunc (
88+ testAccCheckTFEDataRetentionPolicyExists ("tfe_data_retention_policy.foobar" , policy ),
89+ resource .TestCheckResourceAttr (
90+ "tfe_data_retention_policy.foobar" , "delete_older_than.days" , "1337" ),
91+ ),
92+ },
93+ },
94+ })
95+ }
96+
97+ func testAccTFEDataRetentionPolicy_basic (rInt int , deleteOlderThan int ) string {
3798 return fmt .Sprintf (`
3899resource "tfe_organization" "foobar" {
39100 name = "tst-terraform-%d"
@@ -49,9 +110,18 @@ resource "tfe_data_retention_policy" "foobar" {
49110 workspace_id = tfe_workspace.foobar.id
50111
51112 delete_older_than {
52- days = 42
113+ days = %d
53114 }
54- }` , rInt )
115+ }` , rInt , deleteOlderThan )
116+ }
117+
118+ func testAccTFEDataRetentionPolicy_implicit_organization (deleteOlderThan int ) string {
119+ return fmt .Sprintf (`
120+ resource "tfe_data_retention_policy" "foobar" {
121+ delete_older_than {
122+ days = %d
123+ }
124+ }` , deleteOlderThan )
55125}
56126
57127func testAccCheckTFEDataRetentionPolicyExists (
@@ -69,33 +139,31 @@ func testAccCheckTFEDataRetentionPolicyExists(
69139 }
70140
71141 wsID := rs .Primary .Attributes ["workspace_id" ]
72- ws , err := config .Client .Workspaces .ReadByID (ctx , wsID )
73- if err != nil {
74- return fmt .Errorf (
75- "Error retrieving workspace %s: %w" , wsID , err )
76- }
77142
78- drp , err := config .Client .Workspaces .ReadDataRetentionPolicyChoice (ctx , ws .ID )
79- if err != nil {
80- return fmt .Errorf (
81- "Error retrieving data retention policy for workspace %s: %w" , ws .ID , err )
82- }
143+ if wsID != "" {
144+ ws , err := config .Client .Workspaces .ReadByID (ctx , wsID )
145+ if err != nil {
146+ return fmt .Errorf (
147+ "Error retrieving workspace %s: %w" , wsID , err )
148+ }
83149
84- * policy = * drp
150+ drp , err := config .Client .Workspaces .ReadDataRetentionPolicyChoice (ctx , ws .ID )
151+ if err != nil {
152+ return fmt .Errorf (
153+ "Error retrieving data retention policy for workspace %s: %w" , ws .ID , err )
154+ }
85155
86- return nil
87- }
88- }
156+ * policy = * drp
157+ } else {
158+ orgName := rs . Primary . Attributes [ "organization" ]
89159
90- func testAccCheckTFEDataRetentionPolicyAttributes (
91- policy * tfe.DataRetentionPolicyChoice ) resource.TestCheckFunc {
92- return func (s * terraform.State ) error {
93- if policy .DataRetentionPolicyDeleteOlder == nil {
94- return fmt .Errorf ("policy wasn't of type 'delete_older'" )
95- }
160+ drp , err := config .Client .Organizations .ReadDataRetentionPolicyChoice (ctx , orgName )
161+ if err != nil {
162+ return fmt .Errorf (
163+ "Error retrieving data retention policy for organization %s: %w" , orgName , err )
164+ }
96165
97- if policy .DataRetentionPolicyDeleteOlder .DeleteOlderThanNDays != 42 {
98- return fmt .Errorf ("bad delete_older_than_n_days: %d" , policy .DataRetentionPolicyDeleteOlder .DeleteOlderThanNDays )
166+ * policy = * drp
99167 }
100168
101169 return nil
0 commit comments