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