Skip to content

Add cloudstack_domain as a data source #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions cloudstack/data_source_cloudstack_domain.go
Original file line number Diff line number Diff line change
@@ -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
}
71 changes: 71 additions & 0 deletions cloudstack/data_source_cloudstack_domain_test.go
Original file line number Diff line number Diff line change
@@ -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"
}
}
`
}
1 change: 1 addition & 0 deletions cloudstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/domain.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
Loading