|
| 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/base64" |
| 24 | + "fmt" |
| 25 | + "log" |
| 26 | + |
| 27 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 29 | +) |
| 30 | + |
| 31 | +func dataSourceCloudstackUserData() *schema.Resource { |
| 32 | + return &schema.Resource{ |
| 33 | + Read: dataSourceCloudstackUserDataRead, |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "filter": dataSourceFiltersSchema(), |
| 36 | + "account": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "account_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + }, |
| 44 | + "domain": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "domain_id": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + "project": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "project_id": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "userdata_id": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + "userdata": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "params": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func dataSourceCloudstackUserDataRead(d *schema.ResourceData, meta interface{}) error { |
| 77 | + cs := meta.(*cloudstack.CloudStackClient) |
| 78 | + |
| 79 | + name := d.Get("name").(string) |
| 80 | + p := cs.User.NewListUserDataParams() |
| 81 | + p.SetName(name) |
| 82 | + |
| 83 | + if v, ok := d.GetOk("account"); ok { |
| 84 | + p.SetAccount(v.(string)) |
| 85 | + } |
| 86 | + if v, ok := d.GetOk("domain_id"); ok { |
| 87 | + p.SetDomainid(v.(string)) |
| 88 | + } |
| 89 | + |
| 90 | + log.Printf("[DEBUG] Listing user data with name: %s", name) |
| 91 | + userdataList, err := cs.User.ListUserData(p) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("Error listing user data with name %s: %s", name, err) |
| 94 | + } |
| 95 | + |
| 96 | + if len(userdataList.UserData) == 0 { |
| 97 | + return fmt.Errorf("No user data found with name: %s", name) |
| 98 | + } |
| 99 | + if len(userdataList.UserData) > 1 { |
| 100 | + return fmt.Errorf("Multiple user data entries found with name: %s", name) |
| 101 | + } |
| 102 | + |
| 103 | + userdata := userdataList.UserData[0] |
| 104 | + |
| 105 | + d.SetId(userdata.Id) |
| 106 | + d.Set("name", userdata.Name) |
| 107 | + d.Set("account", userdata.Account) |
| 108 | + d.Set("account_id", userdata.Accountid) |
| 109 | + d.Set("domain", userdata.Domain) |
| 110 | + d.Set("domain_id", userdata.Domainid) |
| 111 | + d.Set("userdata_id", userdata.Id) |
| 112 | + d.Set("params", userdata.Params) |
| 113 | + |
| 114 | + if userdata.Project != "" { |
| 115 | + d.Set("project", userdata.Project) |
| 116 | + d.Set("project_id", userdata.Projectid) |
| 117 | + } |
| 118 | + |
| 119 | + if userdata.Userdata != "" { |
| 120 | + decoded, err := base64.StdEncoding.DecodeString(userdata.Userdata) |
| 121 | + if err != nil { |
| 122 | + d.Set("userdata", userdata.Userdata) // Fallback: use raw data |
| 123 | + } else { |
| 124 | + d.Set("userdata", string(decoded)) |
| 125 | + } |
| 126 | + } |
| 127 | + return nil |
| 128 | +} |
0 commit comments