@@ -15,30 +15,26 @@ import (
1515
1616type ResourceIdentityPolicyTestSuite struct {
1717 suite.Suite
18- Providers map [string ]terraform.ResourceProvider
19- Config string
20- ResourceName string
21- Token string
22- TokenFn func (string , map [string ]string ) string
18+ Providers map [string ]terraform.ResourceProvider
19+ Config string
20+ ResourceName string
21+ DataSourceName string
22+ Token string
23+ TokenFn func (string , map [string ]string ) string
2324}
2425
2526func (s * ResourceIdentityPolicyTestSuite ) SetupTest () {
2627 s .Token , s .TokenFn = tokenize ()
2728 s .Providers = testAccProviders
2829 testAccPreCheck (s .T ())
2930 s .Config = legacyTestProviderConfig () + s .TokenFn (`
30- resource "oci_identity_compartment" "t" {
31- name = "-tf-compartment"
32- description = "tf test compartment"
33- compartment_id = "${var.tenancy_ocid}"
34- }
35-
3631 resource "oci_identity_group" "t" {
3732 name = "{{.token}}"
3833 description = "automated test group"
3934 compartment_id = "${var.tenancy_ocid}"
4035 }` , nil )
4136 s .ResourceName = "oci_identity_policy.p"
37+ s .DataSourceName = "data.oci_identity_policies.p"
4238}
4339
4440func (s * ResourceIdentityPolicyTestSuite ) TestAccResourceIdentityPolicy_basic () {
@@ -50,11 +46,11 @@ func (s *ResourceIdentityPolicyTestSuite) TestAccResourceIdentityPolicy_basic()
5046 {
5147 Config : s .Config + s .TokenFn (`
5248 resource "oci_identity_policy" "p" {
53- compartment_id = "${oci_identity_compartment.t.id }"
49+ compartment_id = "${var.tenancy_ocid }"
5450 name = "p1-{{.token}}"
5551 description = "automated test policy"
5652 version_date = "2018-04-17"
57- statements = ["Allow group ${oci_identity_group.t.name} to read instances in compartment ${oci_identity_compartment.t.name} "]
53+ statements = ["Allow group ${oci_identity_group.t.name} to read instances in tenancy "]
5854 }` , nil ),
5955 Check : resource .ComposeAggregateTestCheckFunc (
6056 resource .TestCheckResourceAttrSet (s .ResourceName , "id" ),
@@ -79,17 +75,17 @@ func (s *ResourceIdentityPolicyTestSuite) TestAccResourceIdentityPolicy_basic()
7975 {
8076 Config : s .Config + s .TokenFn (`
8177 resource "oci_identity_policy" "p" {
82- compartment_id = "${oci_identity_compartment.t.id }"
83- name = "p2- {{.token}}"
78+ compartment_id = "${var.tenancy_ocid }"
79+ name = "{{.token}}"
8480 description = "automated test policy (updated)"
8581 version_date = "2018-04-18"
8682 statements = [
87- "Allow group ${oci_identity_group.t.name} to inspect instances in compartment ${oci_identity_compartment.t.name} ",
88- "Allow group ${oci_identity_group.t.name} to read instances in compartment ${oci_identity_compartment.t.name} "
83+ "Allow group ${oci_identity_group.t.name} to inspect instances in tenancy ",
84+ "Allow group ${oci_identity_group.t.name} to read instances in tenancy "
8985 ]
9086 }` , nil ),
9187 Check : resource .ComposeAggregateTestCheckFunc (
92- resource .TestCheckResourceAttr (s .ResourceName , "name" , "p2-" + s .Token ),
88+ resource .TestCheckResourceAttr (s .ResourceName , "name" , s .Token ),
9389 resource .TestCheckResourceAttr (s .ResourceName , "description" , "automated test policy (updated)" ),
9490 resource .TestCheckResourceAttr (s .ResourceName , "version_date" , "2018-04-18" ),
9591 resource .TestCheckResourceAttr (s .ResourceName , "statements.#" , "2" ),
@@ -102,6 +98,39 @@ func (s *ResourceIdentityPolicyTestSuite) TestAccResourceIdentityPolicy_basic()
10298 },
10399 ),
104100 },
101+ // verify data source, + filtering against array of items
102+ {
103+ Config : s .Config + s .TokenFn (`
104+ resource "oci_identity_policy" "p" {
105+ compartment_id = "${var.tenancy_ocid}"
106+ name = "{{.token}}"
107+ description = "automated test policy (updated)"
108+ version_date = "2018-04-18"
109+ statements = [
110+ "Allow group ${oci_identity_group.t.name} to inspect instances in tenancy",
111+ "Allow group ${oci_identity_group.t.name} to read instances in tenancy"
112+ ]
113+ }
114+ data "oci_identity_policies" "p" {
115+ compartment_id = "${var.tenancy_ocid}"
116+ filter {
117+ name = "statements"
118+ values = ["Allow group ${oci_identity_group.t.name} to inspect instances in tenancy"]
119+ }
120+ }` , nil ),
121+ Check : resource .ComposeAggregateTestCheckFunc (
122+ resource .TestCheckResourceAttr (s .DataSourceName , "policies.#" , "1" ),
123+ resource .TestCheckResourceAttrSet (s .DataSourceName , "policies.0.id" ),
124+ resource .TestCheckResourceAttr (s .DataSourceName , "policies.0.name" , s .Token ),
125+ resource .TestCheckResourceAttr (s .DataSourceName , "policies.0.description" , "automated test policy (updated)" ),
126+ resource .TestCheckResourceAttr (s .DataSourceName , "policies.0.state" , string (identity .PolicyLifecycleStateActive )),
127+ // TODO: This field is not being returned by the service call but is still showing up in the datasource
128+ // resource.TestCheckNoResourceAttr(s.ResourceName, "policies.0.inactive_state"),
129+ resource .TestCheckResourceAttr (s .DataSourceName , "policies.0.statements.#" , "2" ),
130+ resource .TestCheckResourceAttrSet (s .DataSourceName , "policies.0.time_created" ),
131+ resource .TestCheckResourceAttr (s .DataSourceName , "policies.0.version_date" , "2018-04-18" ),
132+ ),
133+ },
105134 },
106135 },
107136 )
@@ -115,14 +144,14 @@ func (s *ResourceIdentityPolicyTestSuite) TestAccResourceIdentityPolicy_emptySta
115144 {
116145 Config : s .Config + s .TokenFn (`
117146 resource "oci_identity_policy" "p" {
118- compartment_id = "${oci_identity_compartment.t.id }"
147+ compartment_id = "${var.tenancy_ocid }"
119148 name = "p1-{{.token}}"
120149 description = "automated test policy"
121150 version_date = "2018-04-17"
122151 statements = [
123- "Allow group ${oci_identity_group.t.name} to inspect instances in compartment ${oci_identity_compartment.t.name} ",
152+ "Allow group ${oci_identity_group.t.name} to inspect instances in tenancy ",
124153"",
125- "Allow group ${oci_identity_group.t.name} to inspect instances in compartment ${oci_identity_compartment.t.name} "]
154+ "Allow group ${oci_identity_group.t.name} to inspect instances in tenancy "]
126155 }` , nil ),
127156 ExpectError : regexp .MustCompile ("Service error:InvalidParameter" ),
128157 },
@@ -140,15 +169,15 @@ func (s *ResourceIdentityPolicyTestSuite) TestAccResourceIdentityPolicy_formatti
140169 {
141170 Config : s .Config + s .TokenFn (`
142171 resource "oci_identity_policy" "p" {
143- compartment_id = "${oci_identity_compartment.t.id }"
172+ compartment_id = "${var.tenancy_ocid }"
144173 name = "{{.token}}"
145174 description = "automated test policy"
146- statements = ["Allow group ${oci_identity_group.t.name} to read instances in >> compartment ${oci_identity_compartment.t.name} "]
175+ statements = ["Allow group ${oci_identity_group.t.name} to read instances in >> tenancy "]
147176 }` , nil ),
148177 Check : resource .ComposeAggregateTestCheckFunc (
149178 // policy statements may or may not have invalid characters stripped (">>" above), accommodate this uncertainty as specifically as possible
150179 resource .TestMatchResourceAttr (s .ResourceName , "statements.0" ,
151- regexp .MustCompile (`Allow group ` + s .Token + ` to read instances in (>> )?compartment.+ ` )),
180+ regexp .MustCompile (`Allow group ` + s .Token + ` to read instances in (>> )?tenancy ` )),
152181 func (s * terraform.State ) (err error ) {
153182 if policyHash , err = fromInstanceState (s , "oci_identity_policy.p" , "policyHash" ); err == nil {
154183 lastUpdateETag , err = fromInstanceState (s , "oci_identity_policy.p" , "lastUpdateETag" )
@@ -161,10 +190,10 @@ func (s *ResourceIdentityPolicyTestSuite) TestAccResourceIdentityPolicy_formatti
161190 {
162191 Config : s .Config + s .TokenFn (`
163192 resource "oci_identity_policy" "p" {
164- compartment_id = "${oci_identity_compartment.t.id }"
193+ compartment_id = "${var.tenancy_ocid }"
165194 name = "{{.token}}"
166195 description = "automated test policy"
167- statements = ["Allow group ${oci_identity_group.t.name} to read instances in >> compartment ${oci_identity_compartment.t.name} "]
196+ statements = ["Allow group ${oci_identity_group.t.name} to read instances in >> tenancy "]
168197 }` , nil ),
169198 Check : resource .ComposeAggregateTestCheckFunc (
170199 func (s * terraform.State ) (err error ) {
0 commit comments