|
| 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 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 24 | + "github.com/hashicorp/terraform/helper/schema" |
| 25 | +) |
| 26 | + |
| 27 | +func resourceCloudStackCluster() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Create: resourceCloudStackClusterCreate, |
| 30 | + Read: resourceCloudStackClusterRead, |
| 31 | + Update: resourceCloudStackClusterUpdate, |
| 32 | + Delete: resourceCloudStackClusterDelete, |
| 33 | + Importer: &schema.ResourceImporter{ |
| 34 | + State: schema.ImportStatePassthrough, |
| 35 | + }, |
| 36 | + Schema: map[string]*schema.Schema{ |
| 37 | + "allocation_state": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + "cluster_name": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + }, |
| 46 | + "cluster_type": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Required: true, |
| 49 | + }, |
| 50 | + "guest_vswitch_name": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + ForceNew: true, |
| 54 | + }, |
| 55 | + "guest_vswitch_type": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + ForceNew: true, |
| 59 | + }, |
| 60 | + "hypervisor": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Required: true, |
| 63 | + }, |
| 64 | + "ovm3_cluster": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Optional: true, |
| 67 | + ForceNew: true, |
| 68 | + }, |
| 69 | + "ovm3_pool": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Optional: true, |
| 72 | + ForceNew: true, |
| 73 | + }, |
| 74 | + "ovm3_vip": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Optional: true, |
| 77 | + ForceNew: true, |
| 78 | + }, |
| 79 | + "password": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Optional: true, |
| 82 | + ForceNew: true, |
| 83 | + }, |
| 84 | + "public_vswitch_name": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Optional: true, |
| 87 | + ForceNew: true, |
| 88 | + }, |
| 89 | + "public_vswitch_type": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Optional: true, |
| 92 | + ForceNew: true, |
| 93 | + }, |
| 94 | + "pod_id": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Required: true, |
| 97 | + }, |
| 98 | + "url": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Optional: true, |
| 101 | + ForceNew: true, |
| 102 | + }, |
| 103 | + "username": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Optional: true, |
| 106 | + ForceNew: true, |
| 107 | + }, |
| 108 | + "vsm_ip_address": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Optional: true, |
| 111 | + ForceNew: true, |
| 112 | + }, |
| 113 | + "vsm_password": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Optional: true, |
| 116 | + ForceNew: true, |
| 117 | + }, |
| 118 | + "vsm_username": { |
| 119 | + Type: schema.TypeString, |
| 120 | + Optional: true, |
| 121 | + ForceNew: true, |
| 122 | + }, |
| 123 | + "zone_id": { |
| 124 | + Type: schema.TypeString, |
| 125 | + Required: true, |
| 126 | + }, |
| 127 | + }, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func resourceCloudStackClusterCreate(d *schema.ResourceData, meta interface{}) error { |
| 132 | + cs := meta.(*cloudstack.CloudStackClient) |
| 133 | + |
| 134 | + p := cs.Cluster.NewAddClusterParams(d.Get("cluster_name").(string), d.Get("cluster_type").(string), d.Get("hypervisor").(string), d.Get("pod_id").(string), d.Get("zone_id").(string)) |
| 135 | + if v, ok := d.GetOk("allocation_state"); ok { |
| 136 | + p.SetAllocationstate(v.(string)) |
| 137 | + } |
| 138 | + if v, ok := d.GetOk("guest_vswitch_name"); ok { |
| 139 | + p.SetGuestvswitchname(v.(string)) |
| 140 | + } |
| 141 | + if v, ok := d.GetOk("guest_vswitch_type"); ok { |
| 142 | + p.SetGuestvswitchtype(v.(string)) |
| 143 | + } |
| 144 | + if v, ok := d.GetOk("hypervisor"); ok { |
| 145 | + p.SetHypervisor(v.(string)) |
| 146 | + } |
| 147 | + if v, ok := d.GetOk("ovm3_cluster"); ok { |
| 148 | + p.SetOvm3cluster(v.(string)) |
| 149 | + } |
| 150 | + if v, ok := d.GetOk("ovm3_pool"); ok { |
| 151 | + p.SetOvm3pool(v.(string)) |
| 152 | + } |
| 153 | + if v, ok := d.GetOk("ovm3_vip"); ok { |
| 154 | + p.SetOvm3vip(v.(string)) |
| 155 | + } |
| 156 | + if v, ok := d.GetOk("password"); ok { |
| 157 | + p.SetPassword(v.(string)) |
| 158 | + } |
| 159 | + if v, ok := d.GetOk("public_vswitch_name"); ok { |
| 160 | + p.SetPublicvswitchname(v.(string)) |
| 161 | + } |
| 162 | + if v, ok := d.GetOk("public_vswitch_type"); ok { |
| 163 | + p.SetPublicvswitchtype(v.(string)) |
| 164 | + } |
| 165 | + if v, ok := d.GetOk("url"); ok { |
| 166 | + p.SetUrl(v.(string)) |
| 167 | + } |
| 168 | + if v, ok := d.GetOk("username"); ok { |
| 169 | + p.SetUsername(v.(string)) |
| 170 | + } |
| 171 | + if v, ok := d.GetOk("vsm_ip_address"); ok { |
| 172 | + p.SetVsmipaddress(v.(string)) |
| 173 | + } |
| 174 | + if v, ok := d.GetOk("vsm_password"); ok { |
| 175 | + p.SetVsmpassword(v.(string)) |
| 176 | + } |
| 177 | + if v, ok := d.GetOk("vsm_username"); ok { |
| 178 | + p.SetVsmusername(v.(string)) |
| 179 | + } |
| 180 | + |
| 181 | + r, err := cs.Cluster.AddCluster(p) |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + d.SetId(r.Id) |
| 186 | + |
| 187 | + return resourceCloudStackClusterRead(d, meta) |
| 188 | +} |
| 189 | + |
| 190 | +func resourceCloudStackClusterRead(d *schema.ResourceData, meta interface{}) error { |
| 191 | + cs := meta.(*cloudstack.CloudStackClient) |
| 192 | + |
| 193 | + r, _, err := cs.Cluster.GetClusterByID(d.Id()) |
| 194 | + if err != nil { |
| 195 | + return err |
| 196 | + } |
| 197 | + |
| 198 | + d.Set("allocation_state", r.Allocationstate) |
| 199 | + d.Set("cluster_type", r.Clustertype) |
| 200 | + d.Set("hypervisor", r.Hypervisortype) |
| 201 | + d.Set("cluster_name", r.Name) |
| 202 | + d.Set("ovm3_vip", r.Ovm3vip) |
| 203 | + d.Set("pod_id", r.Podid) |
| 204 | + d.Set("zone_id", r.Zoneid) |
| 205 | + |
| 206 | + return nil |
| 207 | +} |
| 208 | + |
| 209 | +func resourceCloudStackClusterUpdate(d *schema.ResourceData, meta interface{}) error { |
| 210 | + cs := meta.(*cloudstack.CloudStackClient) |
| 211 | + |
| 212 | + p := cs.Cluster.NewUpdateClusterParams(d.Id()) |
| 213 | + if v, ok := d.GetOk("allocation_state"); ok { |
| 214 | + p.SetAllocationstate(v.(string)) |
| 215 | + } |
| 216 | + if v, ok := d.GetOk("cluster_name"); ok { |
| 217 | + p.SetClustername(v.(string)) |
| 218 | + } |
| 219 | + if v, ok := d.GetOk("cluster_type"); ok { |
| 220 | + p.SetClustertype(v.(string)) |
| 221 | + } |
| 222 | + if v, ok := d.GetOk("hypervisor"); ok { |
| 223 | + p.SetHypervisor(v.(string)) |
| 224 | + } |
| 225 | + |
| 226 | + _, err := cs.Cluster.UpdateCluster(p) |
| 227 | + if err != nil { |
| 228 | + return err |
| 229 | + } |
| 230 | + |
| 231 | + return resourceCloudStackClusterRead(d, meta) |
| 232 | +} |
| 233 | + |
| 234 | +func resourceCloudStackClusterDelete(d *schema.ResourceData, meta interface{}) error { |
| 235 | + cs := meta.(*cloudstack.CloudStackClient) |
| 236 | + |
| 237 | + _, err := cs.Cluster.DeleteCluster(cs.Cluster.NewDeleteClusterParams(d.Id())) |
| 238 | + if err != nil { |
| 239 | + return err |
| 240 | + } |
| 241 | + |
| 242 | + return nil |
| 243 | +} |
0 commit comments