Skip to content

Commit c8aea2a

Browse files
committed
feat: add cloudstack_domain data source
1 parent 762f63f commit c8aea2a

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
//
18+
19+
package cloudstack
20+
21+
import (
22+
"fmt"
23+
"log"
24+
25+
"github.com/apache/cloudstack-go/v2/cloudstack"
26+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
27+
)
28+
29+
func dataSourceCloudstackDomain() *schema.Resource {
30+
return &schema.Resource{
31+
Read: dataSourceCloudstackDomainRead,
32+
Schema: map[string]*schema.Schema{
33+
"filter": dataSourceFiltersSchema(),
34+
35+
// Computed values
36+
"domain_id": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
"name": {
41+
Type: schema.TypeString,
42+
Computed: true,
43+
},
44+
"network_domain": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
48+
"parent_domain_id": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
},
53+
}
54+
}
55+
56+
func dataSourceCloudstackDomainRead(d *schema.ResourceData, meta interface{}) error {
57+
log.Printf("Domain Data Source Read Started")
58+
59+
cs := meta.(*cloudstack.CloudStackClient)
60+
p := cs.Domain.NewListDomainsParams()
61+
csDomains, err := cs.Domain.ListDomains(p)
62+
63+
if err != nil {
64+
return fmt.Errorf("failed to list domains: %s", err)
65+
}
66+
67+
var domain *cloudstack.Domain
68+
69+
for _, d := range csDomains.Domains {
70+
if d.Name == "ROOT" {
71+
domain = d
72+
break
73+
}
74+
}
75+
76+
if domain == nil {
77+
return fmt.Errorf("no domain is matching with the specified name")
78+
}
79+
80+
log.Printf("[DEBUG] Selected domain: %s\n", domain.Name)
81+
82+
return domainDescriptionAttributes(d, domain)
83+
}
84+
85+
func domainDescriptionAttributes(d *schema.ResourceData, domain *cloudstack.Domain) error {
86+
d.SetId(domain.Id)
87+
d.Set("domain_id", domain.Id)
88+
d.Set("name", domain.Name)
89+
d.Set("network_domain", domain.Networkdomain)
90+
d.Set("parent_domain_id", domain.Parentdomainid)
91+
92+
return nil
93+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
//
18+
19+
package cloudstack
20+
21+
import (
22+
"fmt"
23+
"testing"
24+
25+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
26+
"github.com/hashicorp/terraform-plugin-testing/terraform"
27+
)
28+
29+
func TestAccCloudstackDomainDataSource_basic(t *testing.T) {
30+
resourceName := "data.cloudstack_domain.my_domain"
31+
32+
resource.Test(t, resource.TestCase{
33+
PreCheck: func() { testAccPreCheck(t) },
34+
Providers: testAccProviders,
35+
Steps: []resource.TestStep{
36+
{
37+
Config: testAccCloudstackDomainDataSource_basic(),
38+
Check: resource.ComposeTestCheckFunc(
39+
testAccCheckCloudstackDomainDataSourceExists(resourceName),
40+
resource.TestCheckResourceAttr(resourceName, "name", "ROOT"),
41+
),
42+
},
43+
},
44+
})
45+
}
46+
47+
func testAccCheckCloudstackDomainDataSourceExists(n string) resource.TestCheckFunc {
48+
return func(s *terraform.State) error {
49+
rs, ok := s.RootModule().Resources[n]
50+
if !ok {
51+
return fmt.Errorf("Not found: %s", n)
52+
}
53+
54+
if rs.Primary.ID == "" {
55+
return fmt.Errorf("No Domain ID is set")
56+
}
57+
58+
return nil
59+
}
60+
}
61+
62+
func testAccCloudstackDomainDataSource_basic() string {
63+
return `
64+
data "cloudstack_domain" "my_domain" {
65+
filter {
66+
name = "name"
67+
value = "ROOT"
68+
}
69+
}
70+
`
71+
}

cloudstack/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func Provider() *schema.Provider {
9090
"cloudstack_user": dataSourceCloudstackUser(),
9191
"cloudstack_vpn_connection": dataSourceCloudstackVPNConnection(),
9292
"cloudstack_pod": dataSourceCloudstackPod(),
93+
"cloudstack_domain": dataSourceCloudstackDomain(),
9394
},
9495

9596
ResourcesMap: map[string]*schema.Resource{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: default
3+
page_title: "CloudStack: cloudstack_domain Data Source"
4+
sidebar_current: "docs-cloudstack-datasource-domain"
5+
description: |-
6+
Retrieves information about a Domain
7+
---
8+
9+
# CloudStack: cloudstack_domain Data Source
10+
11+
A `cloudstack_domain` data source retrieves information about a domain within CloudStack.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "cloudstack_domain" "my_domain" {
17+
filter {
18+
name = "name"
19+
value = "ROOT"
20+
}
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
The following arguments are supported:
27+
28+
* `filter` - (Required) A block to filter the domains. The filter block supports the following:
29+
* `name` - (Required) The name of the filter.
30+
* `value` - (Required) The value of the filter.
31+
32+
## Attributes Reference
33+
34+
The following attributes are exported:
35+
36+
* `id` - The ID of the domain.
37+
* `name` - The name of the domain.
38+
* `network_domain` - The network domain for the domain.
39+
* `parent_domain_id` - The ID of the parent domain.

0 commit comments

Comments
 (0)