diff --git a/cloudstack/data_source_cloudstack_domain.go b/cloudstack/data_source_cloudstack_domain.go new file mode 100644 index 00000000..59cb5454 --- /dev/null +++ b/cloudstack/data_source_cloudstack_domain.go @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package cloudstack + +import ( + "fmt" + "log" + + "github.com/apache/cloudstack-go/v2/cloudstack" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceCloudstackDomain() *schema.Resource { + return &schema.Resource{ + Read: dataSourceCloudstackDomainRead, + Schema: map[string]*schema.Schema{ + "filter": dataSourceFiltersSchema(), + + // Computed values + "domain_id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "network_domain": { + Type: schema.TypeString, + Computed: true, + }, + "parent_domain_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceCloudstackDomainRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("Domain Data Source Read Started") + + cs := meta.(*cloudstack.CloudStackClient) + p := cs.Domain.NewListDomainsParams() + csDomains, err := cs.Domain.ListDomains(p) + + if err != nil { + return fmt.Errorf("failed to list domains: %s", err) + } + + var domain *cloudstack.Domain + + for _, d := range csDomains.Domains { + if d.Name == "ROOT" { + domain = d + break + } + } + + if domain == nil { + return fmt.Errorf("no domain is matching with the specified name") + } + + log.Printf("[DEBUG] Selected domain: %s\n", domain.Name) + + return domainDescriptionAttributes(d, domain) +} + +func domainDescriptionAttributes(d *schema.ResourceData, domain *cloudstack.Domain) error { + d.SetId(domain.Id) + d.Set("domain_id", domain.Id) + d.Set("name", domain.Name) + d.Set("network_domain", domain.Networkdomain) + d.Set("parent_domain_id", domain.Parentdomainid) + + return nil +} diff --git a/cloudstack/data_source_cloudstack_domain_test.go b/cloudstack/data_source_cloudstack_domain_test.go new file mode 100644 index 00000000..4df9d808 --- /dev/null +++ b/cloudstack/data_source_cloudstack_domain_test.go @@ -0,0 +1,71 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package cloudstack + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +func TestAccCloudstackDomainDataSource_basic(t *testing.T) { + resourceName := "data.cloudstack_domain.my_domain" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCloudstackDomainDataSource_basic(), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudstackDomainDataSourceExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", "ROOT"), + ), + }, + }, + }) +} + +func testAccCheckCloudstackDomainDataSourceExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No Domain ID is set") + } + + return nil + } +} + +func testAccCloudstackDomainDataSource_basic() string { + return ` +data "cloudstack_domain" "my_domain" { + filter { + name = "name" + value = "ROOT" + } +} +` +} diff --git a/cloudstack/provider.go b/cloudstack/provider.go index a71df0e5..74cd0ce4 100644 --- a/cloudstack/provider.go +++ b/cloudstack/provider.go @@ -90,6 +90,7 @@ func Provider() *schema.Provider { "cloudstack_user": dataSourceCloudstackUser(), "cloudstack_vpn_connection": dataSourceCloudstackVPNConnection(), "cloudstack_pod": dataSourceCloudstackPod(), + "cloudstack_domain": dataSourceCloudstackDomain(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/website/docs/d/domain.html.markdown b/website/docs/d/domain.html.markdown new file mode 100644 index 00000000..d7318b04 --- /dev/null +++ b/website/docs/d/domain.html.markdown @@ -0,0 +1,39 @@ +--- +layout: default +page_title: "CloudStack: cloudstack_domain Data Source" +sidebar_current: "docs-cloudstack-datasource-domain" +description: |- + Retrieves information about a Domain +--- + +# CloudStack: cloudstack_domain Data Source + +A `cloudstack_domain` data source retrieves information about a domain within CloudStack. + +## Example Usage + +```hcl +data "cloudstack_domain" "my_domain" { + filter { + name = "name" + value = "ROOT" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `filter` - (Required) A block to filter the domains. The filter block supports the following: + * `name` - (Required) The name of the filter. + * `value` - (Required) The value of the filter. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the domain. +* `name` - The name of the domain. +* `network_domain` - The network domain for the domain. +* `parent_domain_id` - The ID of the parent domain.