|
| 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 | + "fmt" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 28 | +) |
| 29 | + |
| 30 | +func resourceCloudStackUserData() *schema.Resource { |
| 31 | + return &schema.Resource{ |
| 32 | + Create: resourceCloudStackUserDataCreate, |
| 33 | + Read: resourceCloudStackUserDataRead, |
| 34 | + Delete: resourceCloudStackUserDataDelete, |
| 35 | + Importer: &schema.ResourceImporter{ |
| 36 | + State: importStatePassthrough, |
| 37 | + }, |
| 38 | + |
| 39 | + Schema: map[string]*schema.Schema{ |
| 40 | + "name": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + ForceNew: true, |
| 44 | + Description: "Name of the user data", |
| 45 | + }, |
| 46 | + |
| 47 | + "userdata": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Required: true, |
| 50 | + ForceNew: true, |
| 51 | + Description: "The user data content to be registered", |
| 52 | + }, |
| 53 | + |
| 54 | + "account": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + ForceNew: true, |
| 58 | + Description: "An optional account for the user data. Must be used with domain_id.", |
| 59 | + }, |
| 60 | + |
| 61 | + "domain_id": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Optional: true, |
| 64 | + ForceNew: true, |
| 65 | + Description: "An optional domain ID for the user data. If the account parameter is used, domain_id must also be used.", |
| 66 | + }, |
| 67 | + |
| 68 | + "params": { |
| 69 | + Type: schema.TypeSet, |
| 70 | + Optional: true, |
| 71 | + ForceNew: true, |
| 72 | + Description: "Optional comma separated list of variables declared in user data content.", |
| 73 | + Elem: &schema.Schema{ |
| 74 | + Type: schema.TypeString, |
| 75 | + }, |
| 76 | + }, |
| 77 | + |
| 78 | + "project_id": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Optional: true, |
| 81 | + ForceNew: true, |
| 82 | + Description: "An optional project for the user data.", |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func resourceCloudStackUserDataCreate(d *schema.ResourceData, meta interface{}) error { |
| 89 | + cs := meta.(*cloudstack.CloudStackClient) |
| 90 | + |
| 91 | + p := cs.User.NewRegisterUserDataParams(d.Get("name").(string), d.Get("userdata").(string)) |
| 92 | + if v, ok := d.GetOk("account"); ok { |
| 93 | + p.SetAccount(v.(string)) |
| 94 | + } |
| 95 | + if v, ok := d.GetOk("domain_id"); ok { |
| 96 | + p.SetDomainid(v.(string)) |
| 97 | + } |
| 98 | + if v, ok := d.GetOk("project_id"); ok { |
| 99 | + p.SetProjectid(v.(string)) |
| 100 | + } |
| 101 | + if v, ok := d.GetOk("params"); ok { |
| 102 | + paramsList := v.(*schema.Set).List() |
| 103 | + var params []string |
| 104 | + for _, param := range paramsList { |
| 105 | + params = append(params, param.(string)) |
| 106 | + } |
| 107 | + p.SetParams(strings.Join(params, ",")) |
| 108 | + } |
| 109 | + |
| 110 | + userdata, err := cs.User.RegisterUserData(p) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("Error registering user data: %s", err) |
| 113 | + } |
| 114 | + |
| 115 | + d.SetId(userdata.Id) |
| 116 | + |
| 117 | + return resourceCloudStackUserDataRead(d, meta) |
| 118 | +} |
| 119 | + |
| 120 | +func resourceCloudStackUserDataRead(d *schema.ResourceData, meta interface{}) error { |
| 121 | + cs := meta.(*cloudstack.CloudStackClient) |
| 122 | + |
| 123 | + id := d.Id() |
| 124 | + |
| 125 | + p := cs.User.NewListUserDataParams() |
| 126 | + p.SetId(id) |
| 127 | + |
| 128 | + userdata, err := cs.User.ListUserData(p) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("Error retrieving user data with ID %s: %s", id, err) |
| 131 | + } |
| 132 | + |
| 133 | + d.Set("name", userdata.UserData[0].Name) |
| 134 | + d.Set("userdata", userdata.UserData[0]) |
| 135 | + if d.Get("account").(string) != "" { |
| 136 | + d.Set("account", userdata.UserData[0].Account) |
| 137 | + } |
| 138 | + if d.Get("domain_id").(string) != "" { |
| 139 | + d.Set("domain_id", userdata.UserData[0].Domainid) |
| 140 | + } |
| 141 | + if userdata.UserData[0].Params != "" { |
| 142 | + paramsList := strings.Split(userdata.UserData[0].Params, ",") |
| 143 | + var paramsSet []interface{} |
| 144 | + for _, param := range paramsList { |
| 145 | + paramsSet = append(paramsSet, param) |
| 146 | + } |
| 147 | + d.Set("params", schema.NewSet(schema.HashString, paramsSet)) |
| 148 | + } |
| 149 | + if userdata.UserData[0].Projectid != "" { |
| 150 | + d.Set("project_id", userdata.UserData[0].Projectid) |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func resourceCloudStackUserDataDelete(d *schema.ResourceData, meta interface{}) error { |
| 157 | + cs := meta.(*cloudstack.CloudStackClient) |
| 158 | + |
| 159 | + p := cs.User.NewDeleteUserDataParams(d.Id()) |
| 160 | + _, err := cs.User.DeleteUserData(p) |
| 161 | + if err != nil { |
| 162 | + return fmt.Errorf("Error deleting user data with ID %s: %s", d.Id(), err) |
| 163 | + } |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
0 commit comments