|
| 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 dataSourceCloudstackAutoscaleVMGroup() *schema.Resource { |
| 31 | + return &schema.Resource{ |
| 32 | + Read: dataSourceCloudstackAutoscaleVMGroupRead, |
| 33 | + |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "id": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + |
| 41 | + "name": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Optional: true, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + |
| 47 | + "lbrule_id": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + |
| 52 | + "min_members": { |
| 53 | + Type: schema.TypeInt, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + |
| 57 | + "max_members": { |
| 58 | + Type: schema.TypeInt, |
| 59 | + Computed: true, |
| 60 | + }, |
| 61 | + |
| 62 | + "vm_profile_id": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + |
| 67 | + "scaleup_policy_ids": { |
| 68 | + Type: schema.TypeSet, |
| 69 | + Computed: true, |
| 70 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 71 | + }, |
| 72 | + |
| 73 | + "scaledown_policy_ids": { |
| 74 | + Type: schema.TypeSet, |
| 75 | + Computed: true, |
| 76 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 77 | + }, |
| 78 | + |
| 79 | + "state": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Computed: true, |
| 82 | + }, |
| 83 | + |
| 84 | + "interval": { |
| 85 | + Type: schema.TypeInt, |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + |
| 89 | + "available_virtual_machine_count": { |
| 90 | + Type: schema.TypeInt, |
| 91 | + Computed: true, |
| 92 | + }, |
| 93 | + |
| 94 | + "account_name": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + }, |
| 98 | + |
| 99 | + "domain_id": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + |
| 104 | + "project_id": { |
| 105 | + Type: schema.TypeString, |
| 106 | + Computed: true, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func dataSourceCloudstackAutoscaleVMGroupRead(d *schema.ResourceData, meta interface{}) error { |
| 113 | + cs := meta.(*cloudstack.CloudStackClient) |
| 114 | + |
| 115 | + id, idOk := d.GetOk("id") |
| 116 | + name, nameOk := d.GetOk("name") |
| 117 | + |
| 118 | + if !idOk && !nameOk { |
| 119 | + return fmt.Errorf("either 'id' or 'name' must be specified") |
| 120 | + } |
| 121 | + |
| 122 | + var group *cloudstack.AutoScaleVmGroup |
| 123 | + |
| 124 | + if idOk { |
| 125 | + p := cs.AutoScale.NewListAutoScaleVmGroupsParams() |
| 126 | + p.SetId(id.(string)) |
| 127 | + |
| 128 | + resp, err := cs.AutoScale.ListAutoScaleVmGroups(p) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("failed to list autoscale VM groups: %s", err) |
| 131 | + } |
| 132 | + |
| 133 | + if resp.Count == 0 { |
| 134 | + return fmt.Errorf("autoscale VM group with ID %s not found", id.(string)) |
| 135 | + } |
| 136 | + |
| 137 | + group = resp.AutoScaleVmGroups[0] |
| 138 | + } else { |
| 139 | + p := cs.AutoScale.NewListAutoScaleVmGroupsParams() |
| 140 | + |
| 141 | + resp, err := cs.AutoScale.ListAutoScaleVmGroups(p) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("failed to list autoscale VM groups: %s", err) |
| 144 | + } |
| 145 | + |
| 146 | + for _, grp := range resp.AutoScaleVmGroups { |
| 147 | + if grp.Name == name.(string) { |
| 148 | + group = grp |
| 149 | + break |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if group == nil { |
| 154 | + return fmt.Errorf("autoscale VM group with name %s not found", name.(string)) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + log.Printf("[DEBUG] Found autoscale VM group: %s", group.Name) |
| 159 | + |
| 160 | + d.SetId(group.Id) |
| 161 | + d.Set("name", group.Name) |
| 162 | + d.Set("lbrule_id", group.Lbruleid) |
| 163 | + d.Set("min_members", group.Minmembers) |
| 164 | + d.Set("max_members", group.Maxmembers) |
| 165 | + d.Set("vm_profile_id", group.Vmprofileid) |
| 166 | + d.Set("state", group.State) |
| 167 | + d.Set("interval", group.Interval) |
| 168 | + d.Set("available_virtual_machine_count", group.Availablevirtualmachinecount) |
| 169 | + d.Set("account_name", group.Account) |
| 170 | + d.Set("domain_id", group.Domainid) |
| 171 | + if group.Projectid != "" { |
| 172 | + d.Set("project_id", group.Projectid) |
| 173 | + } |
| 174 | + |
| 175 | + scaleupPolicyIds := make([]string, len(group.Scaleuppolicies)) |
| 176 | + for i, policy := range group.Scaleuppolicies { |
| 177 | + scaleupPolicyIds[i] = policy.Id |
| 178 | + } |
| 179 | + d.Set("scaleup_policy_ids", scaleupPolicyIds) |
| 180 | + |
| 181 | + scaledownPolicyIds := make([]string, len(group.Scaledownpolicies)) |
| 182 | + for i, policy := range group.Scaledownpolicies { |
| 183 | + scaledownPolicyIds[i] = policy.Id |
| 184 | + } |
| 185 | + d.Set("scaledown_policy_ids", scaledownPolicyIds) |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
0 commit comments