Skip to content

Commit 6dd258e

Browse files
committed
update filtering capabilities to cloudstack domain data source
add test for invalidName
1 parent ea85903 commit 6dd258e

2 files changed

Lines changed: 109 additions & 7 deletions

File tree

cloudstack/data_source_cloudstack_domain.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,66 @@ func dataSourceCloudstackDomainRead(d *schema.ResourceData, meta interface{}) er
5858

5959
cs := meta.(*cloudstack.CloudStackClient)
6060
p := cs.Domain.NewListDomainsParams()
61-
csDomains, err := cs.Domain.ListDomains(p)
6261

62+
var filterName, filterValue string
63+
var filterByName, filterByID bool
64+
65+
// Apply filters if provided
66+
if filters, filtersOk := d.GetOk("filter"); filtersOk {
67+
for _, f := range filters.(*schema.Set).List() {
68+
m := f.(map[string]interface{})
69+
name := m["name"].(string)
70+
value := m["value"].(string)
71+
72+
switch name {
73+
case "name":
74+
p.SetName(value)
75+
filterName = value
76+
filterByName = true
77+
log.Printf("[DEBUG] Filtering by name: %s", value)
78+
case "id":
79+
p.SetId(value)
80+
filterValue = value
81+
filterByID = true
82+
log.Printf("[DEBUG] Filtering by ID: %s", value)
83+
}
84+
}
85+
}
86+
87+
csDomains, err := cs.Domain.ListDomains(p)
6388
if err != nil {
6489
return fmt.Errorf("failed to list domains: %s", err)
6590
}
6691

92+
log.Printf("[DEBUG] Found %d domains from CloudStack API", len(csDomains.Domains))
93+
6794
var domain *cloudstack.Domain
6895

69-
for _, d := range csDomains.Domains {
70-
if d.Name == "ROOT" {
71-
domain = d
72-
break
96+
// If we have results from the API call, select the appropriate domain
97+
if len(csDomains.Domains) > 0 {
98+
// If we filtered by ID or name through the API, we should have a specific result
99+
if filterByID || filterByName {
100+
// Since we used API filtering, the first result should be our match
101+
domain = csDomains.Domains[0]
102+
log.Printf("[DEBUG] Using API-filtered domain: %s", domain.Name)
103+
} else {
104+
// If no filters were applied, we need to handle this case
105+
// This shouldn't happen with the current schema as filters are required
106+
return fmt.Errorf("no filter criteria specified")
73107
}
74108
}
75109

76110
if domain == nil {
77-
return fmt.Errorf("no domain is matching with the specified name")
111+
if filterByName {
112+
return fmt.Errorf("no domain found with name: %s", filterName)
113+
} else if filterByID {
114+
return fmt.Errorf("no domain found with ID: %s", filterValue)
115+
} else {
116+
return fmt.Errorf("no domain found matching the specified criteria")
117+
}
78118
}
79119

80-
log.Printf("[DEBUG] Selected domain: %s\n", domain.Name)
120+
log.Printf("[DEBUG] Selected domain: %s (ID: %s)", domain.Name, domain.Id)
81121

82122
return domainDescriptionAttributes(d, domain)
83123
}

cloudstack/data_source_cloudstack_domain_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ package cloudstack
2020

2121
import (
2222
"fmt"
23+
"regexp"
2324
"testing"
2425

26+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
2527
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
2628
"github.com/hashicorp/terraform-plugin-testing/terraform"
2729
)
@@ -59,6 +61,19 @@ func testAccCheckCloudstackDomainDataSourceExists(n string) resource.TestCheckFu
5961
}
6062
}
6163

64+
func TestAccCloudstackDomainDataSource_invalidName(t *testing.T) {
65+
resource.Test(t, resource.TestCase{
66+
PreCheck: func() { testAccPreCheck(t) },
67+
Providers: testAccProviders,
68+
Steps: []resource.TestStep{
69+
{
70+
Config: testAccCloudstackDomainDataSource_invalidName(),
71+
ExpectError: regexp.MustCompile("no domain found with name: badgerbearocto"),
72+
},
73+
},
74+
})
75+
}
76+
6277
func testAccCloudstackDomainDataSource_basic() string {
6378
return `
6479
data "cloudstack_domain" "my_domain" {
@@ -69,3 +84,50 @@ data "cloudstack_domain" "my_domain" {
6984
}
7085
`
7186
}
87+
88+
func testAccCloudstackDomainDataSource_invalidName() string {
89+
return `
90+
data "cloudstack_domain" "my_domain" {
91+
filter {
92+
name = "name"
93+
value = "badgerbearocto"
94+
}
95+
}
96+
`
97+
}
98+
99+
func TestAccCloudstackDomainDataSource_byID(t *testing.T) {
100+
domainResourceName := "cloudstack_domain.test_domain"
101+
dataSourceName := "data.cloudstack_domain.my_domain_by_id"
102+
testDomainName := "test-domain-" + id.UniqueId()
103+
104+
resource.Test(t, resource.TestCase{
105+
PreCheck: func() { testAccPreCheck(t) },
106+
Providers: testAccProviders,
107+
Steps: []resource.TestStep{
108+
{
109+
Config: testAccCloudstackDomainDataSource_byID(testDomainName),
110+
Check: resource.ComposeTestCheckFunc(
111+
testAccCheckCloudstackDomainDataSourceExists(dataSourceName),
112+
resource.TestCheckResourceAttrPair(dataSourceName, "name", domainResourceName, "name"),
113+
resource.TestCheckResourceAttrPair(dataSourceName, "domain_id", domainResourceName, "id"),
114+
),
115+
},
116+
},
117+
})
118+
}
119+
120+
func testAccCloudstackDomainDataSource_byID(domainName string) string {
121+
return fmt.Sprintf(`
122+
resource "cloudstack_domain" "test_domain" {
123+
name = "%s"
124+
}
125+
126+
data "cloudstack_domain" "my_domain_by_id" {
127+
filter {
128+
name = "id"
129+
value = cloudstack_domain.test_domain.id
130+
}
131+
}
132+
`, domainName)
133+
}

0 commit comments

Comments
 (0)