|
| 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 | + |
| 26 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 28 | +) |
| 29 | + |
| 30 | +func resourceCloudStackAutoScalePolicy() *schema.Resource { |
| 31 | + return &schema.Resource{ |
| 32 | + Create: resourceCloudStackAutoScalePolicyCreate, |
| 33 | + Read: resourceCloudStackAutoScalePolicyRead, |
| 34 | + Update: resourceCloudStackAutoScalePolicyUpdate, |
| 35 | + Delete: resourceCloudStackAutoScalePolicyDelete, |
| 36 | + |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "name": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + Description: "the name of the autoscale policy", |
| 42 | + }, |
| 43 | + "action": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + Description: "the action to be executed if all the conditions evaluate to true for the specified duration", |
| 47 | + ForceNew: true, |
| 48 | + }, |
| 49 | + "duration": { |
| 50 | + Type: schema.TypeInt, |
| 51 | + Required: true, |
| 52 | + Description: "the duration in which the conditions have to be true before action is taken", |
| 53 | + }, |
| 54 | + "quiet_time": { |
| 55 | + Type: schema.TypeInt, |
| 56 | + Optional: true, |
| 57 | + Description: "the cool down period in which the policy should not be evaluated after the action has been taken", |
| 58 | + }, |
| 59 | + "condition_ids": { |
| 60 | + Type: schema.TypeSet, |
| 61 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 62 | + Required: true, |
| 63 | + Description: "the list of IDs of the conditions that are being evaluated on every interval", |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func resourceCloudStackAutoScalePolicyCreate(d *schema.ResourceData, meta interface{}) error { |
| 70 | + cs := meta.(*cloudstack.CloudStackClient) |
| 71 | + |
| 72 | + action := d.Get("action").(string) |
| 73 | + duration := d.Get("duration").(int) |
| 74 | + |
| 75 | + // Get condition IDs from the set |
| 76 | + conditionIds := []string{} |
| 77 | + if v, ok := d.GetOk("condition_ids"); ok { |
| 78 | + conditionSet := v.(*schema.Set) |
| 79 | + for _, id := range conditionSet.List() { |
| 80 | + conditionIds = append(conditionIds, id.(string)) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Create a new parameter struct |
| 85 | + p := cs.AutoScale.NewCreateAutoScalePolicyParams(action, conditionIds, duration) |
| 86 | + |
| 87 | + // Set optional parameters |
| 88 | + if v, ok := d.GetOk("name"); ok { |
| 89 | + p.SetName(v.(string)) |
| 90 | + } |
| 91 | + if v, ok := d.GetOk("quiet_time"); ok { |
| 92 | + p.SetQuiettime(v.(int)) |
| 93 | + } |
| 94 | + |
| 95 | + log.Printf("[DEBUG] Creating autoscale policy") |
| 96 | + resp, err := cs.AutoScale.CreateAutoScalePolicy(p) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("Error creating autoscale policy: %s", err) |
| 99 | + } |
| 100 | + |
| 101 | + d.SetId(resp.Id) |
| 102 | + log.Printf("[DEBUG] Autoscale policy created with ID: %s", resp.Id) |
| 103 | + |
| 104 | + return resourceCloudStackAutoScalePolicyRead(d, meta) |
| 105 | +} |
| 106 | + |
| 107 | +func resourceCloudStackAutoScalePolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 108 | + cs := meta.(*cloudstack.CloudStackClient) |
| 109 | + |
| 110 | + // Get the autoscale policy details |
| 111 | + p := cs.AutoScale.NewListAutoScalePoliciesParams() |
| 112 | + p.SetId(d.Id()) |
| 113 | + |
| 114 | + resp, err := cs.AutoScale.ListAutoScalePolicies(p) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("Error retrieving autoscale policy: %s", err) |
| 117 | + } |
| 118 | + |
| 119 | + if resp.Count == 0 { |
| 120 | + log.Printf("[DEBUG] Autoscale policy %s no longer exists", d.Id()) |
| 121 | + d.SetId("") |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + policy := resp.AutoScalePolicies[0] |
| 126 | + d.Set("name", policy.Name) |
| 127 | + d.Set("action", policy.Action) |
| 128 | + d.Set("duration", policy.Duration) |
| 129 | + d.Set("quiet_time", policy.Quiettime) |
| 130 | + |
| 131 | + // Convert condition IDs to set |
| 132 | + conditionIds := schema.NewSet(schema.HashString, []interface{}{}) |
| 133 | + for _, conditionId := range policy.Conditions { |
| 134 | + conditionIds.Add(conditionId) |
| 135 | + } |
| 136 | + d.Set("condition_ids", conditionIds) |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func resourceCloudStackAutoScalePolicyUpdate(d *schema.ResourceData, meta interface{}) error { |
| 142 | + cs := meta.(*cloudstack.CloudStackClient) |
| 143 | + |
| 144 | + // Check if any updateable fields have changed |
| 145 | + if d.HasChange("name") || d.HasChange("condition_ids") || d.HasChange("duration") || d.HasChange("quiet_time") { |
| 146 | + log.Printf("[DEBUG] Updating autoscale policy: %s", d.Id()) |
| 147 | + |
| 148 | + // Create a new parameter struct |
| 149 | + p := cs.AutoScale.NewUpdateAutoScalePolicyParams(d.Id()) |
| 150 | + |
| 151 | + // Set fields that can be updated |
| 152 | + if d.HasChange("name") { |
| 153 | + if v, ok := d.GetOk("name"); ok { |
| 154 | + p.SetName(v.(string)) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if d.HasChange("duration") { |
| 159 | + duration := d.Get("duration").(int) |
| 160 | + p.SetDuration(duration) |
| 161 | + } |
| 162 | + |
| 163 | + if d.HasChange("quiet_time") { |
| 164 | + if v, ok := d.GetOk("quiet_time"); ok { |
| 165 | + p.SetQuiettime(v.(int)) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if d.HasChange("condition_ids") { |
| 170 | + // Get condition IDs from the set |
| 171 | + conditionIds := []string{} |
| 172 | + if v, ok := d.GetOk("condition_ids"); ok { |
| 173 | + conditionSet := v.(*schema.Set) |
| 174 | + for _, id := range conditionSet.List() { |
| 175 | + conditionIds = append(conditionIds, id.(string)) |
| 176 | + } |
| 177 | + } |
| 178 | + p.SetConditionids(conditionIds) |
| 179 | + } |
| 180 | + |
| 181 | + _, err := cs.AutoScale.UpdateAutoScalePolicy(p) |
| 182 | + if err != nil { |
| 183 | + return fmt.Errorf("Error updating autoscale policy: %s", err) |
| 184 | + } |
| 185 | + |
| 186 | + log.Printf("[DEBUG] Autoscale policy updated successfully: %s", d.Id()) |
| 187 | + } |
| 188 | + |
| 189 | + return resourceCloudStackAutoScalePolicyRead(d, meta) |
| 190 | +} |
| 191 | + |
| 192 | +func resourceCloudStackAutoScalePolicyDelete(d *schema.ResourceData, meta interface{}) error { |
| 193 | + cs := meta.(*cloudstack.CloudStackClient) |
| 194 | + |
| 195 | + // Create a new parameter struct |
| 196 | + p := cs.AutoScale.NewDeleteAutoScalePolicyParams(d.Id()) |
| 197 | + |
| 198 | + log.Printf("[DEBUG] Deleting autoscale policy: %s", d.Id()) |
| 199 | + _, err := cs.AutoScale.DeleteAutoScalePolicy(p) |
| 200 | + if err != nil { |
| 201 | + return fmt.Errorf("Error deleting autoscale policy: %s", err) |
| 202 | + } |
| 203 | + |
| 204 | + return nil |
| 205 | +} |
0 commit comments