-
Notifications
You must be signed in to change notification settings - Fork 48
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
ianc769
wants to merge
2
commits into
apache:main
Choose a base branch
from
ianc769:feature/data-cloudstack_domain
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameters needs to be set for
p
based on the provided input.