|
| 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 | +} |
0 commit comments