|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +package cloudstack |
| 21 | + |
| 22 | +import ( |
| 23 | + "encoding/json" |
| 24 | + "fmt" |
| 25 | + "log" |
| 26 | + "regexp" |
| 27 | + "strings" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 32 | +) |
| 33 | + |
| 34 | +func dataSourceCloudstackProject() *schema.Resource { |
| 35 | + return &schema.Resource{ |
| 36 | + Read: datasourceCloudStackProjectRead, |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "filter": dataSourceFiltersSchema(), |
| 39 | + |
| 40 | + // Computed values |
| 41 | + "name": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + |
| 46 | + "displaytext": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + |
| 51 | + "domain": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + |
| 56 | + "account": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + |
| 61 | + "state": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + |
| 66 | + "tags": tagsSchema(), |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func datasourceCloudStackProjectRead(d *schema.ResourceData, meta interface{}) error { |
| 72 | + cs := meta.(*cloudstack.CloudStackClient) |
| 73 | + p := cs.Project.NewListProjectsParams() |
| 74 | + csProjects, err := cs.Project.ListProjects(p) |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("failed to list projects: %s", err) |
| 78 | + } |
| 79 | + |
| 80 | + filters := d.Get("filter") |
| 81 | + var projects []*cloudstack.Project |
| 82 | + |
| 83 | + for _, v := range csProjects.Projects { |
| 84 | + match, err := applyProjectFilters(v, filters.(*schema.Set)) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + if match { |
| 89 | + projects = append(projects, v) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if len(projects) == 0 { |
| 94 | + return fmt.Errorf("no project matches the specified filters") |
| 95 | + } |
| 96 | + |
| 97 | + // Return the latest project from the list of filtered projects according |
| 98 | + // to its creation date |
| 99 | + project, err := latestProject(projects) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + log.Printf("[DEBUG] Selected project: %s\n", project.Name) |
| 104 | + |
| 105 | + return projectDescriptionAttributes(d, project) |
| 106 | +} |
| 107 | + |
| 108 | +func projectDescriptionAttributes(d *schema.ResourceData, project *cloudstack.Project) error { |
| 109 | + d.SetId(project.Id) |
| 110 | + d.Set("name", project.Name) |
| 111 | + d.Set("displaytext", project.Displaytext) |
| 112 | + d.Set("domain", project.Domain) |
| 113 | + d.Set("state", project.State) |
| 114 | + |
| 115 | + // Handle account information safely |
| 116 | + if len(project.Owner) > 0 { |
| 117 | + for _, owner := range project.Owner { |
| 118 | + if account, ok := owner["account"]; ok { |
| 119 | + d.Set("account", account) |
| 120 | + break |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + d.Set("tags", tagsToMap(project.Tags)) |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +func latestProject(projects []*cloudstack.Project) (*cloudstack.Project, error) { |
| 131 | + var latest time.Time |
| 132 | + var project *cloudstack.Project |
| 133 | + |
| 134 | + for _, v := range projects { |
| 135 | + created, err := time.Parse("2006-01-02T15:04:05-0700", v.Created) |
| 136 | + if err != nil { |
| 137 | + return nil, fmt.Errorf("failed to parse creation date of a project: %s", err) |
| 138 | + } |
| 139 | + |
| 140 | + if created.After(latest) { |
| 141 | + latest = created |
| 142 | + project = v |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return project, nil |
| 147 | +} |
| 148 | + |
| 149 | +func applyProjectFilters(project *cloudstack.Project, filters *schema.Set) (bool, error) { |
| 150 | + var projectJSON map[string]interface{} |
| 151 | + k, _ := json.Marshal(project) |
| 152 | + err := json.Unmarshal(k, &projectJSON) |
| 153 | + if err != nil { |
| 154 | + return false, err |
| 155 | + } |
| 156 | + |
| 157 | + for _, f := range filters.List() { |
| 158 | + m := f.(map[string]interface{}) |
| 159 | + r, err := regexp.Compile(m["value"].(string)) |
| 160 | + if err != nil { |
| 161 | + return false, fmt.Errorf("invalid regex: %s", err) |
| 162 | + } |
| 163 | + |
| 164 | + // Handle special case for owner/account |
| 165 | + if m["name"].(string) == "account" { |
| 166 | + if len(project.Owner) == 0 { |
| 167 | + return false, nil |
| 168 | + } |
| 169 | + |
| 170 | + found := false |
| 171 | + for _, owner := range project.Owner { |
| 172 | + if account, ok := owner["account"]; ok { |
| 173 | + if r.MatchString(fmt.Sprintf("%v", account)) { |
| 174 | + found = true |
| 175 | + break |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if !found { |
| 181 | + return false, nil |
| 182 | + } |
| 183 | + continue |
| 184 | + } |
| 185 | + |
| 186 | + updatedName := strings.ReplaceAll(m["name"].(string), "_", "") |
| 187 | + |
| 188 | + // Handle fields that might not exist in the JSON |
| 189 | + fieldValue, exists := projectJSON[updatedName] |
| 190 | + if !exists { |
| 191 | + return false, nil |
| 192 | + } |
| 193 | + |
| 194 | + // Handle different types of fields |
| 195 | + switch v := fieldValue.(type) { |
| 196 | + case string: |
| 197 | + if !r.MatchString(v) { |
| 198 | + return false, nil |
| 199 | + } |
| 200 | + case float64: |
| 201 | + if !r.MatchString(fmt.Sprintf("%v", v)) { |
| 202 | + return false, nil |
| 203 | + } |
| 204 | + case bool: |
| 205 | + if !r.MatchString(fmt.Sprintf("%v", v)) { |
| 206 | + return false, nil |
| 207 | + } |
| 208 | + default: |
| 209 | + // Skip fields that aren't simple types |
| 210 | + continue |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + return true, nil |
| 215 | +} |
0 commit comments