|
| 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 | + "strings" |
| 25 | + |
| 26 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 27 | + "github.com/hashicorp/terraform/helper/schema" |
| 28 | +) |
| 29 | + |
| 30 | +func resourceCloudStackPhysicalNetwork() *schema.Resource { |
| 31 | + return &schema.Resource{ |
| 32 | + Create: resourceCloudStackPhysicalNetworkCreate, |
| 33 | + Read: resourceCloudStackPhysicalNetworkRead, |
| 34 | + Update: resourceCloudStackPhysicalNetworkUpdate, |
| 35 | + Delete: resourceCloudStackPhysicalNetworkDelete, |
| 36 | + Importer: &schema.ResourceImporter{ |
| 37 | + State: schema.ImportStatePassthrough, |
| 38 | + }, |
| 39 | + Schema: map[string]*schema.Schema{ |
| 40 | + "broadcast_domain_range": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + }, |
| 44 | + "domainid": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + }, |
| 48 | + "isolation_methods": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + }, |
| 52 | + "name": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Required: true, |
| 55 | + }, |
| 56 | + "network_speed": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + }, |
| 60 | + "tags": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Optional: true, |
| 63 | + }, |
| 64 | + "vlan": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Optional: true, |
| 67 | + }, |
| 68 | + "zoneid": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Required: true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func resourceCloudStackPhysicalNetworkCreate(d *schema.ResourceData, meta interface{}) error { |
| 77 | + cs := meta.(*cloudstack.CloudStackClient) |
| 78 | + |
| 79 | + p := cs.Network.NewCreatePhysicalNetworkParams(d.Get("name").(string), d.Get("zoneid").(string)) |
| 80 | + if v, ok := d.GetOk("broadcast_domain_range"); ok { |
| 81 | + p.SetBroadcastdomainrange(strings.ToUpper(v.(string))) |
| 82 | + } |
| 83 | + if v, ok := d.GetOk("domainid"); ok { |
| 84 | + p.SetDomainid(v.(string)) |
| 85 | + } |
| 86 | + if v, ok := d.GetOk("isolation_methods"); ok { |
| 87 | + p.SetIsolationmethods([]string{v.(string)}) |
| 88 | + } |
| 89 | + if v, ok := d.GetOk("network_speed"); ok { |
| 90 | + p.SetNetworkspeed(v.(string)) |
| 91 | + } |
| 92 | + if v, ok := d.GetOk("tags"); ok { |
| 93 | + p.SetTags([]string{v.(string)}) |
| 94 | + } |
| 95 | + if v, ok := d.GetOk("vlan"); ok { |
| 96 | + p.SetVlan(v.(string)) |
| 97 | + } |
| 98 | + |
| 99 | + r, err := cs.Network.CreatePhysicalNetwork(p) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + d.SetId(r.Id) |
| 105 | + |
| 106 | + return resourceCloudStackPhysicalNetworkRead(d, meta) |
| 107 | +} |
| 108 | + |
| 109 | +func resourceCloudStackPhysicalNetworkRead(d *schema.ResourceData, meta interface{}) error { |
| 110 | + cs := meta.(*cloudstack.CloudStackClient) |
| 111 | + |
| 112 | + pn, _, err := cs.Network.GetPhysicalNetworkByID(d.Id()) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + d.Set("broadcast_domain_range", pn.Broadcastdomainrange) |
| 118 | + d.Set("domainid", pn.Domainid) |
| 119 | + d.Set("isolation_methods", pn.Isolationmethods) |
| 120 | + d.Set("name", pn.Name) |
| 121 | + d.Set("network_speed", pn.Networkspeed) |
| 122 | + d.Set("tags", pn.Tags) |
| 123 | + d.Set("vlan", pn.Vlan) |
| 124 | + d.Set("zoneid", pn.Zoneid) |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func resourceCloudStackPhysicalNetworkUpdate(d *schema.ResourceData, meta interface{}) error { |
| 130 | + cs := meta.(*cloudstack.CloudStackClient) |
| 131 | + |
| 132 | + p := cs.Network.NewUpdatePhysicalNetworkParams(d.Id()) |
| 133 | + if v, ok := d.GetOk("network_speed"); ok { |
| 134 | + p.SetNetworkspeed(v.(string)) |
| 135 | + } |
| 136 | + if v, ok := d.GetOk("tags"); ok { |
| 137 | + p.SetTags([]string{v.(string)}) |
| 138 | + } |
| 139 | + if v, ok := d.GetOk("vlan"); ok { |
| 140 | + p.SetVlan(v.(string)) |
| 141 | + } |
| 142 | + |
| 143 | + _, err := cs.Network.UpdatePhysicalNetwork(p) |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("Error deleting physical network: %s", err) |
| 146 | + } |
| 147 | + |
| 148 | + return resourceCloudStackPhysicalNetworkRead(d, meta) |
| 149 | +} |
| 150 | + |
| 151 | +func resourceCloudStackPhysicalNetworkDelete(d *schema.ResourceData, meta interface{}) error { |
| 152 | + cs := meta.(*cloudstack.CloudStackClient) |
| 153 | + |
| 154 | + p := cs.Network.NewDeletePhysicalNetworkParams(d.Id()) |
| 155 | + _, err := cs.Network.DeletePhysicalNetwork(p) |
| 156 | + if err != nil { |
| 157 | + return fmt.Errorf("Error deleting phsyical network: %s", err) |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
0 commit comments