|
| 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 | + |
| 62 | + var filterName, filterValue string |
| 63 | + var filterByName, filterByID bool |
| 64 | + |
| 65 | + // Apply filters if provided |
| 66 | + if filters, filtersOk := d.GetOk("filter"); filtersOk { |
| 67 | + for _, f := range filters.(*schema.Set).List() { |
| 68 | + m := f.(map[string]interface{}) |
| 69 | + name := m["name"].(string) |
| 70 | + value := m["value"].(string) |
| 71 | + |
| 72 | + switch name { |
| 73 | + case "name": |
| 74 | + p.SetName(value) |
| 75 | + filterName = value |
| 76 | + filterByName = true |
| 77 | + log.Printf("[DEBUG] Filtering by name: %s", value) |
| 78 | + case "id": |
| 79 | + p.SetId(value) |
| 80 | + filterValue = value |
| 81 | + filterByID = true |
| 82 | + log.Printf("[DEBUG] Filtering by ID: %s", value) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + csDomains, err := cs.Domain.ListDomains(p) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("failed to list domains: %s", err) |
| 90 | + } |
| 91 | + |
| 92 | + log.Printf("[DEBUG] Found %d domains from CloudStack API", len(csDomains.Domains)) |
| 93 | + |
| 94 | + var domain *cloudstack.Domain |
| 95 | + |
| 96 | + // If we have results from the API call, select the appropriate domain |
| 97 | + if len(csDomains.Domains) > 0 { |
| 98 | + // If we filtered by ID or name through the API, we should have a specific result |
| 99 | + if filterByID || filterByName { |
| 100 | + // Since we used API filtering, the first result should be our match |
| 101 | + domain = csDomains.Domains[0] |
| 102 | + log.Printf("[DEBUG] Using API-filtered domain: %s", domain.Name) |
| 103 | + } else { |
| 104 | + // If no filters were applied, we need to handle this case |
| 105 | + // This shouldn't happen with the current schema as filters are required |
| 106 | + return fmt.Errorf("no filter criteria specified") |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if domain == nil { |
| 111 | + if filterByName { |
| 112 | + return fmt.Errorf("no domain found with name: %s", filterName) |
| 113 | + } else if filterByID { |
| 114 | + return fmt.Errorf("no domain found with ID: %s", filterValue) |
| 115 | + } else { |
| 116 | + return fmt.Errorf("no domain found matching the specified criteria") |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + log.Printf("[DEBUG] Selected domain: %s (ID: %s)", domain.Name, domain.Id) |
| 121 | + |
| 122 | + return domainDescriptionAttributes(d, domain) |
| 123 | +} |
| 124 | + |
| 125 | +func domainDescriptionAttributes(d *schema.ResourceData, domain *cloudstack.Domain) error { |
| 126 | + d.SetId(domain.Id) |
| 127 | + d.Set("domain_id", domain.Id) |
| 128 | + d.Set("name", domain.Name) |
| 129 | + d.Set("network_domain", domain.Networkdomain) |
| 130 | + d.Set("parent_domain_id", domain.Parentdomainid) |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments