|
| 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 | + "log" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 28 | + "github.com/hashicorp/terraform/helper/schema" |
| 29 | +) |
| 30 | + |
| 31 | +func resourceCloudStackKubernetesVersion() *schema.Resource { |
| 32 | + return &schema.Resource{ |
| 33 | + Create: resourceCloudStackKubernetesVersionCreate, |
| 34 | + Read: resourceCloudStackKubernetesVersionRead, |
| 35 | + Update: resourceCloudStackKubernetesVersionUpdate, |
| 36 | + Delete: resourceCloudStackKubernetesVersionDelete, |
| 37 | + Importer: &schema.ResourceImporter{ |
| 38 | + State: importStatePassthrough, |
| 39 | + }, |
| 40 | + |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + |
| 43 | + "semantic_version": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + ForceNew: true, |
| 47 | + }, |
| 48 | + |
| 49 | + "url": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Required: true, |
| 52 | + ForceNew: true, |
| 53 | + }, |
| 54 | + |
| 55 | + "min_cpu": { |
| 56 | + Type: schema.TypeInt, |
| 57 | + Required: true, |
| 58 | + ForceNew: true, |
| 59 | + }, |
| 60 | + |
| 61 | + "min_memory": { |
| 62 | + Type: schema.TypeInt, |
| 63 | + Required: true, |
| 64 | + ForceNew: true, |
| 65 | + }, |
| 66 | + |
| 67 | + // Optional Params |
| 68 | + "name": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Optional: true, |
| 71 | + Computed: true, |
| 72 | + ForceNew: true, |
| 73 | + }, |
| 74 | + |
| 75 | + "zone": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Optional: true, |
| 78 | + Computed: true, |
| 79 | + ForceNew: true, |
| 80 | + }, |
| 81 | + |
| 82 | + "checksum": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Optional: true, |
| 85 | + }, |
| 86 | + |
| 87 | + "state": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Optional: true, |
| 90 | + Computed: true, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func resourceCloudStackKubernetesVersionCreate(d *schema.ResourceData, meta interface{}) error { |
| 97 | + cs := meta.(*cloudstack.CloudStackClient) |
| 98 | + |
| 99 | + // State is always Enabled when created |
| 100 | + if state, ok := d.GetOk("state"); ok { |
| 101 | + if state.(string) != "Enabled" { |
| 102 | + return fmt.Errorf("State must be 'Enabled' when first adding an ISO") |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + semanticVersion := d.Get("semantic_version").(string) |
| 107 | + url := d.Get("url").(string) |
| 108 | + minCpu := d.Get("min_cpu").(int) |
| 109 | + minMemory := d.Get("min_memory").(int) |
| 110 | + |
| 111 | + p := cs.Kubernetes.NewAddKubernetesSupportedVersionParams(minCpu, minMemory, semanticVersion) |
| 112 | + p.SetUrl(url) |
| 113 | + |
| 114 | + if name, ok := d.GetOk("name"); ok { |
| 115 | + p.SetName(name.(string)) |
| 116 | + } |
| 117 | + if checksum, ok := d.GetOk("checksum"); ok { |
| 118 | + p.SetName(checksum.(string)) |
| 119 | + } |
| 120 | + if zone, ok := d.GetOk("zone"); ok { |
| 121 | + zoneID, e := retrieveID(cs, "zone", zone.(string)) |
| 122 | + if e != nil { |
| 123 | + return e.Error() |
| 124 | + } |
| 125 | + p.SetZoneid(zoneID) |
| 126 | + } |
| 127 | + |
| 128 | + log.Printf("[DEBUG] Creating Kubernetes Version %s", semanticVersion) |
| 129 | + r, err := cs.Kubernetes.AddKubernetesSupportedVersion(p) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + log.Printf("[DEBUG] Kubernetes Version %s successfully created", semanticVersion) |
| 135 | + d.SetId(r.Id) |
| 136 | + return resourceCloudStackKubernetesVersionRead(d, meta) |
| 137 | +} |
| 138 | + |
| 139 | +func resourceCloudStackKubernetesVersionRead(d *schema.ResourceData, meta interface{}) error { |
| 140 | + cs := meta.(*cloudstack.CloudStackClient) |
| 141 | + |
| 142 | + log.Printf("[DEBUG] Retrieving Kubernetes Version %s", d.Get("semantic_version").(string)) |
| 143 | + |
| 144 | + // Get the Kubernetes Version details |
| 145 | + version, count, err := cs.Kubernetes.GetKubernetesSupportedVersionByID( |
| 146 | + d.Id(), |
| 147 | + ) |
| 148 | + if err != nil { |
| 149 | + if count == 0 { |
| 150 | + log.Printf("[DEBUG] Kubernetes Version %s does not longer exist", d.Get("semantic_version").(string)) |
| 151 | + d.SetId("") |
| 152 | + return nil |
| 153 | + } |
| 154 | + |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + // Update the config |
| 159 | + d.SetId(version.Id) |
| 160 | + d.Set("semantic_version", version.Semanticversion) |
| 161 | + d.Set("name", version.Name) |
| 162 | + d.Set("min_cpu", version.Mincpunumber) |
| 163 | + d.Set("min_memory", version.Minmemory) |
| 164 | + d.Set("state", version.State) |
| 165 | + |
| 166 | + setValueOrID(d, "zone", version.Zonename, version.Zoneid) |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func resourceCloudStackKubernetesVersionUpdate(d *schema.ResourceData, meta interface{}) error { |
| 171 | + cs := meta.(*cloudstack.CloudStackClient) |
| 172 | + d.Partial(true) |
| 173 | + |
| 174 | + if d.HasChange("state") { |
| 175 | + p := cs.Kubernetes.NewUpdateKubernetesSupportedVersionParams(d.Id(), d.Get("state").(string)) |
| 176 | + _, err := cs.Kubernetes.UpdateKubernetesSupportedVersion(p) |
| 177 | + if err != nil { |
| 178 | + return fmt.Errorf( |
| 179 | + "Error Updating Kubernetes Version %s: %s", d.Id(), err) |
| 180 | + } |
| 181 | + d.SetPartial("state") |
| 182 | + } |
| 183 | + |
| 184 | + d.Partial(false) |
| 185 | + return resourceCloudStackKubernetesVersionRead(d, meta) |
| 186 | +} |
| 187 | + |
| 188 | +func resourceCloudStackKubernetesVersionDelete(d *schema.ResourceData, meta interface{}) error { |
| 189 | + cs := meta.(*cloudstack.CloudStackClient) |
| 190 | + |
| 191 | + // Create a new parameter struct |
| 192 | + p := cs.Kubernetes.NewDeleteKubernetesSupportedVersionParams(d.Id()) |
| 193 | + |
| 194 | + // Delete the Kubernetes Version |
| 195 | + _, err := cs.Kubernetes.DeleteKubernetesSupportedVersion(p) |
| 196 | + if err != nil { |
| 197 | + // This is a very poor way to be told the ID does no longer exist :( |
| 198 | + if strings.Contains(err.Error(), fmt.Sprintf( |
| 199 | + "Invalid parameter id value=%s due to incorrect long value format, "+ |
| 200 | + "or entity does not exist", d.Id())) { |
| 201 | + return nil |
| 202 | + } |
| 203 | + |
| 204 | + return fmt.Errorf("Error deleting Kubernetes Version: %s", err) |
| 205 | + } |
| 206 | + |
| 207 | + return nil |
| 208 | +} |
0 commit comments