Skip to content

Commit 23b181c

Browse files
authored
Data Source filtering core w/ expository identity integration (#325)
* Assert assumptions regarding AD resource ordering Tests: make test run=TestDatasourceIdentity
1 parent 0e8b75a commit 23b181c

19 files changed

+794
-145
lines changed

data_source_obmcs_identity_availability_domains.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func AvailabilityDomainDatasource() *schema.Resource {
1515
return &schema.Resource{
1616
Read: readAvailabilityDomains,
1717
Schema: map[string]*schema.Schema{
18+
"filter": dataSourceFiltersSchema(),
1819
"compartment_id": {
1920
Type: schema.TypeString,
2021
Required: true,
@@ -60,17 +61,26 @@ func (s *AvailabilityDomainDatasourceCrud) Get() (e error) {
6061
}
6162

6263
func (s *AvailabilityDomainDatasourceCrud) SetData() {
63-
if s.Res != nil {
64-
s.D.SetId(time.Now().UTC().String())
65-
resources := []map[string]interface{}{}
66-
for _, v := range s.Res.AvailabilityDomains {
67-
res := map[string]interface{}{
68-
"name": v.Name,
69-
"compartment_id": v.CompartmentID,
70-
}
71-
resources = append(resources, res)
64+
if s.Res == nil {
65+
return
66+
}
67+
68+
s.D.SetId(time.Now().UTC().String())
69+
resources := []map[string]interface{}{}
70+
for _, v := range s.Res.AvailabilityDomains {
71+
res := map[string]interface{}{
72+
"name": v.Name,
73+
"compartment_id": v.CompartmentID,
7274
}
73-
s.D.Set("availability_domains", resources)
75+
resources = append(resources, res)
76+
}
77+
78+
if f, fOk := s.D.GetOk("filter"); fOk {
79+
resources = ApplyFilters(f.(*schema.Set), resources)
80+
}
81+
82+
if err := s.D.Set("availability_domains", resources); err != nil {
83+
panic(err)
7484
}
7585
return
7686
}

data_source_obmcs_identity_availability_domains_test.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package main
55
import (
66
"testing"
77

8+
"regexp"
9+
810
"github.com/hashicorp/terraform/helper/resource"
911
"github.com/hashicorp/terraform/terraform"
1012
"github.com/oracle/bmcs-go-sdk"
@@ -25,11 +27,7 @@ func (s *DatasourceIdentityAvailabilityDomainsTestSuite) SetupTest() {
2527
s.Client = testAccClient
2628
s.Provider = testAccProvider
2729
s.Providers = testAccProviders
28-
s.Config = testProviderConfig() + `
29-
data "oci_identity_availability_domains" "t" {
30-
compartment_id = "${var.compartment_id}"
31-
}`
32-
30+
s.Config = testProviderConfig()
3331
s.ResourceName = "data.oci_identity_availability_domains.t"
3432
}
3533

@@ -38,13 +36,33 @@ func (s *DatasourceIdentityAvailabilityDomainsTestSuite) TestAccIdentityAvailabi
3836
PreventPostDestroyRefresh: true,
3937
Providers: s.Providers,
4038
Steps: []resource.TestStep{
39+
// Verify expected number of ADs in expected order
40+
{
41+
Config: s.Config + `
42+
data "oci_identity_availability_domains" "t" {
43+
compartment_id = "${var.compartment_id}"
44+
}`,
45+
Check: resource.ComposeTestCheckFunc(
46+
resource.TestCheckResourceAttr(s.ResourceName, "availability_domains.#", "3"),
47+
resource.TestMatchResourceAttr(s.ResourceName, "availability_domains.0.name", regexp.MustCompile(`\w*:\w{3}-AD-1`)),
48+
resource.TestMatchResourceAttr(s.ResourceName, "availability_domains.1.name", regexp.MustCompile(`\w*:\w{3}-AD-2`)),
49+
resource.TestMatchResourceAttr(s.ResourceName, "availability_domains.2.name", regexp.MustCompile(`\w*:\w{3}-AD-3`)),
50+
),
51+
},
52+
// Verify regex filtering
4153
{
42-
ImportState: true,
43-
ImportStateVerify: true,
44-
Config: s.Config,
54+
Config: s.Config + `
55+
data "oci_identity_availability_domains" "t" {
56+
compartment_id = "${var.compartment_id}"
57+
filter {
58+
name = "name"
59+
values = ["\\w*:\\w{3}-AD-2"]
60+
regex = true
61+
}
62+
}`,
4563
Check: resource.ComposeTestCheckFunc(
46-
resource.TestCheckResourceAttrSet(s.ResourceName, "availability_domains.0.name"),
47-
resource.TestCheckResourceAttrSet(s.ResourceName, "availability_domains.1.name"),
64+
resource.TestCheckResourceAttr(s.ResourceName, "availability_domains.#", "1"),
65+
resource.TestMatchResourceAttr(s.ResourceName, "availability_domains.0.name", regexp.MustCompile(".*AD-2")),
4866
),
4967
},
5068
},

data_source_obmcs_identity_compartments.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func CompartmentDatasource() *schema.Resource {
1616
return &schema.Resource{
1717
Read: readCompartments,
1818
Schema: map[string]*schema.Schema{
19+
"filter": dataSourceFiltersSchema(),
1920
"compartment_id": {
2021
Type: schema.TypeString,
2122
Required: true,
@@ -65,24 +66,31 @@ func (s *CompartmentDatasourceCrud) Get() (e error) {
6566
}
6667

6768
func (s *CompartmentDatasourceCrud) SetData() {
68-
if s.Res != nil {
69-
s.D.SetId(time.Now().UTC().String())
70-
resources := []map[string]interface{}{}
71-
for _, v := range s.Res.Compartments {
72-
res := map[string]interface{}{
73-
"compartment_id": v.CompartmentID,
74-
"description": v.Description,
75-
"id": v.ID,
76-
"inactive_state": v.InactiveStatus,
77-
"name": v.Name,
78-
"state": v.State,
79-
"time_created": v.TimeCreated.String(),
80-
}
81-
resources = append(resources, res)
82-
}
83-
if err := s.D.Set("compartments", resources); err != nil {
84-
panic(err)
69+
if s.Res == nil {
70+
return
71+
}
72+
73+
s.D.SetId(time.Now().UTC().String())
74+
resources := []map[string]interface{}{}
75+
for _, v := range s.Res.Compartments {
76+
res := map[string]interface{}{
77+
"compartment_id": v.CompartmentID,
78+
"description": v.Description,
79+
"id": v.ID,
80+
"inactive_state": v.InactiveStatus,
81+
"name": v.Name,
82+
"state": v.State,
83+
"time_created": v.TimeCreated.String(),
8584
}
85+
resources = append(resources, res)
86+
}
87+
88+
if f, fOk := s.D.GetOk("filter"); fOk {
89+
resources = ApplyFilters(f.(*schema.Set), resources)
90+
}
91+
92+
if err := s.D.Set("compartments", resources); err != nil {
93+
panic(err)
8694
}
8795
return
8896
}

data_source_obmcs_identity_compartments_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,28 @@ func (s *DatasourceIdentityCompartmentsTestSuite) TestAccIdentityCompartments_ba
4747
compartment_id = "${var.compartment_id}"
4848
}`,
4949
Check: resource.ComposeTestCheckFunc(
50-
resource.TestCheckResourceAttrSet(s.ResourceName, "compartments.0.id"),
5150
resource.TestCheckResourceAttrSet(s.ResourceName, "compartments.#"),
5251
),
5352
},
53+
{
54+
Config: s.Config + `
55+
data "oci_identity_compartments" "t" {
56+
compartment_id = "${var.compartment_id}"
57+
filter {
58+
name = "name"
59+
values = ["-tf-compartment"]
60+
}
61+
}`,
62+
Check: resource.ComposeTestCheckFunc(
63+
resource.TestCheckResourceAttr(s.ResourceName, "compartments.#", "1"),
64+
resource.TestCheckResourceAttrSet(s.ResourceName, "compartments.0.id"),
65+
resource.TestCheckResourceAttrSet(s.ResourceName, "compartments.0.compartment_id"),
66+
resource.TestCheckResourceAttr(s.ResourceName, "compartments.0.name", "-tf-compartment"),
67+
resource.TestCheckResourceAttr(s.ResourceName, "compartments.0.description", "tf test compartment"),
68+
resource.TestCheckResourceAttrSet(s.ResourceName, "compartments.0.time_created"),
69+
resource.TestCheckResourceAttr(s.ResourceName, "compartments.0.inactive_state", "0"),
70+
),
71+
},
5472
},
5573
},
5674
)

data_source_obmcs_identity_groups.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func GroupDatasource() *schema.Resource {
1616
return &schema.Resource{
1717
Read: readGroups,
1818
Schema: map[string]*schema.Schema{
19+
"filter": dataSourceFiltersSchema(),
1920
"compartment_id": {
2021
Type: schema.TypeString,
2122
Required: true,
@@ -65,24 +66,33 @@ func (s *GroupDatasourceCrud) Get() (e error) {
6566
}
6667

6768
func (s *GroupDatasourceCrud) SetData() {
68-
if s.Res != nil {
69-
s.D.SetId(time.Now().UTC().String())
70-
resources := []map[string]interface{}{}
71-
for _, v := range s.Res.Groups {
72-
res := map[string]interface{}{
73-
"compartment_id": v.CompartmentID,
74-
"description": v.Description,
75-
"id": v.ID,
76-
"inactive_state": v.InactiveStatus,
77-
"name": v.Name,
78-
"state": v.State,
79-
"time_created": v.TimeCreated.String(),
80-
}
81-
resources = append(resources, res)
82-
}
83-
if err := s.D.Set("groups", resources); err != nil {
84-
panic(err)
69+
if s.Res == nil {
70+
return
71+
}
72+
73+
s.D.SetId(time.Now().UTC().String())
74+
resources := []map[string]interface{}{}
75+
for _, v := range s.Res.Groups {
76+
res := map[string]interface{}{
77+
"compartment_id": v.CompartmentID,
78+
"description": v.Description,
79+
"id": v.ID,
80+
"inactive_state": v.InactiveStatus,
81+
"name": v.Name,
82+
"state": v.State,
83+
"time_created": v.TimeCreated.String(),
8584
}
85+
86+
resources = append(resources, res)
8687
}
88+
89+
if f, fOk := s.D.GetOk("filter"); fOk {
90+
resources = ApplyFilters(f.(*schema.Set), resources)
91+
}
92+
93+
if err := s.D.Set("groups", resources); err != nil {
94+
panic(err)
95+
}
96+
8797
return
8898
}

data_source_obmcs_identity_groups_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ type DatasourceIdentityGroupsTestSuite struct {
1919
Provider terraform.ResourceProvider
2020
Providers map[string]terraform.ResourceProvider
2121
ResourceName string
22+
Token string
23+
TokenFn TokenFn
2224
}
2325

2426
func (s *DatasourceIdentityGroupsTestSuite) SetupTest() {
25-
_, tokenFn := tokenize()
27+
s.Token, s.TokenFn = tokenize()
2628
s.Client = testAccClient
2729
s.Provider = testAccProvider
2830
s.Providers = testAccProviders
29-
s.Config = testProviderConfig() + tokenFn(`
31+
s.Config = testProviderConfig() + s.TokenFn(`
3032
resource "oci_identity_group" "t" {
3133
name = "{{.token}}"
3234
description = "automated test group"
@@ -51,7 +53,31 @@ func (s *DatasourceIdentityGroupsTestSuite) TestAccDatasourceIdentityGroups_basi
5153
}`,
5254
Check: resource.ComposeTestCheckFunc(
5355
resource.TestCheckResourceAttrSet(s.ResourceName, "groups.#"),
56+
),
57+
},
58+
// Test cascading filters
59+
{
60+
Config: s.Config + s.TokenFn(`
61+
data "oci_identity_groups" "t" {
62+
compartment_id = "${var.compartment_id}"
63+
filter {
64+
name = "name"
65+
values = ["{{.token}}", "Administrators"]
66+
}
67+
filter {
68+
name = "description"
69+
values = ["automated test group"]
70+
}
71+
}`, nil),
72+
Check: resource.ComposeTestCheckFunc(
73+
resource.TestCheckResourceAttr(s.ResourceName, "groups.#", "1"),
5474
resource.TestCheckResourceAttrSet(s.ResourceName, "groups.0.id"),
75+
resource.TestCheckResourceAttrSet(s.ResourceName, "groups.0.compartment_id"),
76+
resource.TestCheckResourceAttrSet(s.ResourceName, "groups.0.time_created"),
77+
resource.TestCheckResourceAttr(s.ResourceName, "groups.0.name", s.Token),
78+
resource.TestCheckResourceAttr(s.ResourceName, "groups.0.description", "automated test group"),
79+
resource.TestCheckResourceAttr(s.ResourceName, "groups.0.state", "ACTIVE"),
80+
resource.TestCheckResourceAttr(s.ResourceName, "groups.0.inactive_state", "0"),
5581
),
5682
},
5783
},

data_source_obmcs_identity_policies.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func IdentityPolicyDatasource() *schema.Resource {
1616
return &schema.Resource{
1717
Read: readIdentityPolicies,
1818
Schema: map[string]*schema.Schema{
19+
"filter": dataSourceFiltersSchema(),
1920
"compartment_id": {
2021
Type: schema.TypeString,
2122
Required: true,
@@ -67,26 +68,33 @@ func (s *IdentityPolicyDatasourceCrud) Get() (e error) {
6768
}
6869

6970
func (s *IdentityPolicyDatasourceCrud) SetData() {
70-
if s.Res != nil {
71-
s.D.SetId(time.Now().UTC().String())
72-
resources := []map[string]interface{}{}
73-
for _, v := range s.Res.Policies {
74-
res := map[string]interface{}{
75-
"id": v.ID,
76-
"compartment_id": v.CompartmentID,
77-
"name": v.Name,
78-
"statements": v.Statements,
79-
"description": v.Description,
80-
"time_created": v.TimeCreated.String(),
81-
"state": v.State,
82-
"inactive_state": v.InactiveStatus,
83-
"version_date": v.VersionDate,
84-
}
85-
resources = append(resources, res)
86-
}
87-
if err := s.D.Set("policies", resources); err != nil {
88-
panic(err)
71+
if s.Res == nil {
72+
return
73+
}
74+
75+
s.D.SetId(time.Now().UTC().String())
76+
resources := []map[string]interface{}{}
77+
for _, v := range s.Res.Policies {
78+
res := map[string]interface{}{
79+
"id": v.ID,
80+
"compartment_id": v.CompartmentID,
81+
"name": v.Name,
82+
"statements": v.Statements,
83+
"description": v.Description,
84+
"time_created": v.TimeCreated.String(),
85+
"state": v.State,
86+
"inactive_state": v.InactiveStatus,
87+
"version_date": v.VersionDate,
8988
}
89+
resources = append(resources, res)
90+
}
91+
92+
if f, fOk := s.D.GetOk("filter"); fOk {
93+
resources = ApplyFilters(f.(*schema.Set), resources)
94+
}
95+
96+
if err := s.D.Set("policies", resources); err != nil {
97+
panic(err)
9098
}
9199
return
92100
}

0 commit comments

Comments
 (0)