|
| 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 | + "errors" |
| 24 | + "fmt" |
| 25 | + |
| 26 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 27 | + "github.com/hashicorp/terraform/helper/schema" |
| 28 | +) |
| 29 | + |
| 30 | +func resourceCloudStackPod() *schema.Resource { |
| 31 | + return &schema.Resource{ |
| 32 | + Create: resourceCloudStackPodCreate, |
| 33 | + Read: resourceCloudStackPodRead, |
| 34 | + Update: resourceCloudStackPodUpdate, |
| 35 | + Delete: resourceCloudStackPodDelete, |
| 36 | + Importer: &schema.ResourceImporter{ |
| 37 | + State: schema.ImportStatePassthrough, |
| 38 | + }, |
| 39 | + Schema: map[string]*schema.Schema{ |
| 40 | + "allocation_state": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + }, |
| 44 | + "end_ip": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + }, |
| 48 | + "gateway": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Required: true, |
| 51 | + }, |
| 52 | + "name": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Required: true, |
| 55 | + }, |
| 56 | + "netmask": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Required: true, |
| 59 | + }, |
| 60 | + "start_ip": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Required: true, |
| 63 | + }, |
| 64 | + "zone_id": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Required: true, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func resourceCloudStackPodCreate(d *schema.ResourceData, meta interface{}) error { |
| 73 | + cs := meta.(*cloudstack.CloudStackClient) |
| 74 | + |
| 75 | + p := cs.Pod.NewCreatePodParams(d.Get("gateway").(string), d.Get("name").(string), d.Get("netmask").(string), d.Get("start_ip").(string), d.Get("zone_id").(string)) |
| 76 | + if v, ok := d.GetOk("allocation_state"); ok { |
| 77 | + p.SetAllocationstate(v.(string)) |
| 78 | + } |
| 79 | + if v, ok := d.GetOk("end_ip"); ok { |
| 80 | + p.SetEndip(v.(string)) |
| 81 | + } |
| 82 | + |
| 83 | + r, err := cs.Pod.CreatePod(p) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + d.SetId(r.Id) |
| 89 | + |
| 90 | + return resourceCloudStackPodRead(d, meta) |
| 91 | +} |
| 92 | + |
| 93 | +func resourceCloudStackPodRead(d *schema.ResourceData, meta interface{}) error { |
| 94 | + cs := meta.(*cloudstack.CloudStackClient) |
| 95 | + |
| 96 | + r, count, err := cs.Pod.GetPodByID(d.Id()) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + if count != 1 { |
| 102 | + return errors.New(fmt.Sprintf("Multiple pods. Invalid zone id: %s", d.Id())) |
| 103 | + } |
| 104 | + |
| 105 | + d.Set("allocation_state", r.Allocationstate) |
| 106 | + d.Set("end_ip", r.Endip) |
| 107 | + d.Set("gateway", r.Gateway) |
| 108 | + d.Set("name", r.Name) |
| 109 | + d.Set("netmask", r.Netmask) |
| 110 | + d.Set("start_ip", r.Startip) |
| 111 | + d.Set("zone_id", r.Zoneid) |
| 112 | + |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func resourceCloudStackPodUpdate(d *schema.ResourceData, meta interface{}) error { |
| 117 | + cs := meta.(*cloudstack.CloudStackClient) |
| 118 | + |
| 119 | + p := cs.Pod.NewUpdatePodParams(d.Id()) |
| 120 | + if v, ok := d.GetOk("allocation_state"); ok { |
| 121 | + p.SetAllocationstate(v.(string)) |
| 122 | + } |
| 123 | + if v, ok := d.GetOk("end_ip"); ok { |
| 124 | + p.SetEndip(v.(string)) |
| 125 | + } |
| 126 | + if v, ok := d.GetOk("gateway"); ok { |
| 127 | + p.SetGateway(v.(string)) |
| 128 | + } |
| 129 | + if v, ok := d.GetOk("name"); ok { |
| 130 | + p.SetName(v.(string)) |
| 131 | + } |
| 132 | + if v, ok := d.GetOk("netmask"); ok { |
| 133 | + p.SetNetmask(v.(string)) |
| 134 | + } |
| 135 | + if v, ok := d.GetOk("start_ip"); ok { |
| 136 | + p.SetStartip(v.(string)) |
| 137 | + } |
| 138 | + |
| 139 | + _, err := cs.Pod.UpdatePod(p) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + return resourceCloudStackPodRead(d, meta) |
| 145 | +} |
| 146 | + |
| 147 | +func resourceCloudStackPodDelete(d *schema.ResourceData, meta interface{}) error { |
| 148 | + cs := meta.(*cloudstack.CloudStackClient) |
| 149 | + |
| 150 | + _, err := cs.Pod.DeletePod(cs.Pod.NewDeletePodParams(d.Id())) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | +} |
0 commit comments