github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/hashcode" 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/rackspace/gophercloud/openstack/networking/v2/subnets" 10 ) 11 12 func resourceNetworkingSubnetV2() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceNetworkingSubnetV2Create, 15 Read: resourceNetworkingSubnetV2Read, 16 Update: resourceNetworkingSubnetV2Update, 17 Delete: resourceNetworkingSubnetV2Delete, 18 19 Schema: map[string]*schema.Schema{ 20 "region": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 DefaultFunc: envDefaultFuncAllowMissing("OS_REGION_NAME"), 25 }, 26 "network_id": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 "cidr": &schema.Schema{ 32 Type: schema.TypeString, 33 Required: true, 34 ForceNew: true, 35 }, 36 "name": &schema.Schema{ 37 Type: schema.TypeString, 38 Optional: true, 39 ForceNew: false, 40 }, 41 "tenant_id": &schema.Schema{ 42 Type: schema.TypeString, 43 Optional: true, 44 ForceNew: true, 45 }, 46 "allocation_pools": &schema.Schema{ 47 Type: schema.TypeList, 48 Optional: true, 49 ForceNew: true, 50 Elem: &schema.Resource{ 51 Schema: map[string]*schema.Schema{ 52 "start": &schema.Schema{ 53 Type: schema.TypeString, 54 Required: true, 55 }, 56 "end": &schema.Schema{ 57 Type: schema.TypeString, 58 Required: true, 59 }, 60 }, 61 }, 62 }, 63 "gateway_ip": &schema.Schema{ 64 Type: schema.TypeString, 65 Optional: true, 66 ForceNew: false, 67 }, 68 "ip_version": &schema.Schema{ 69 Type: schema.TypeInt, 70 Required: true, 71 ForceNew: true, 72 }, 73 "enable_dhcp": &schema.Schema{ 74 Type: schema.TypeBool, 75 Optional: true, 76 ForceNew: false, 77 }, 78 "dns_nameservers": &schema.Schema{ 79 Type: schema.TypeSet, 80 Optional: true, 81 ForceNew: false, 82 Elem: &schema.Schema{Type: schema.TypeString}, 83 Set: func(v interface{}) int { 84 return hashcode.String(v.(string)) 85 }, 86 }, 87 "host_routes": &schema.Schema{ 88 Type: schema.TypeList, 89 Optional: true, 90 ForceNew: false, 91 Elem: &schema.Resource{ 92 Schema: map[string]*schema.Schema{ 93 "destination_cidr": &schema.Schema{ 94 Type: schema.TypeString, 95 Required: true, 96 }, 97 "next_hop": &schema.Schema{ 98 Type: schema.TypeString, 99 Required: true, 100 }, 101 }, 102 }, 103 }, 104 }, 105 } 106 } 107 108 func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{}) error { 109 config := meta.(*Config) 110 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 111 if err != nil { 112 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 113 } 114 115 createOpts := subnets.CreateOpts{ 116 NetworkID: d.Get("network_id").(string), 117 CIDR: d.Get("cidr").(string), 118 Name: d.Get("name").(string), 119 TenantID: d.Get("tenant_id").(string), 120 AllocationPools: resourceSubnetAllocationPoolsV2(d), 121 GatewayIP: d.Get("gateway_ip").(string), 122 IPVersion: d.Get("ip_version").(int), 123 DNSNameservers: resourceSubnetDNSNameserversV2(d), 124 HostRoutes: resourceSubnetHostRoutesV2(d), 125 } 126 127 if raw, ok := d.GetOk("enable_dhcp"); ok { 128 value := raw.(bool) 129 createOpts.EnableDHCP = &value 130 } 131 132 log.Printf("[DEBUG] Create Options: %#v", createOpts) 133 s, err := subnets.Create(networkingClient, createOpts).Extract() 134 if err != nil { 135 return fmt.Errorf("Error creating OpenStack Neutron subnet: %s", err) 136 } 137 log.Printf("[INFO] Subnet ID: %s", s.ID) 138 139 d.SetId(s.ID) 140 141 return resourceNetworkingSubnetV2Read(d, meta) 142 } 143 144 func resourceNetworkingSubnetV2Read(d *schema.ResourceData, meta interface{}) error { 145 config := meta.(*Config) 146 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 147 if err != nil { 148 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 149 } 150 151 s, err := subnets.Get(networkingClient, d.Id()).Extract() 152 if err != nil { 153 return CheckDeleted(d, err, "subnet") 154 } 155 156 log.Printf("[DEBUG] Retreived Subnet %s: %+v", d.Id(), s) 157 158 d.Set("newtork_id", s.NetworkID) 159 d.Set("cidr", s.CIDR) 160 d.Set("ip_version", s.IPVersion) 161 d.Set("name", s.Name) 162 d.Set("tenant_id", s.TenantID) 163 d.Set("allocation_pools", s.AllocationPools) 164 d.Set("gateway_ip", s.GatewayIP) 165 d.Set("enable_dhcp", s.EnableDHCP) 166 d.Set("dns_nameservers", s.DNSNameservers) 167 d.Set("host_routes", s.HostRoutes) 168 169 return nil 170 } 171 172 func resourceNetworkingSubnetV2Update(d *schema.ResourceData, meta interface{}) error { 173 config := meta.(*Config) 174 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 175 if err != nil { 176 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 177 } 178 179 var updateOpts subnets.UpdateOpts 180 181 if d.HasChange("name") { 182 updateOpts.Name = d.Get("name").(string) 183 } 184 185 if d.HasChange("gateway_ip") { 186 updateOpts.GatewayIP = d.Get("gateway_ip").(string) 187 } 188 189 if d.HasChange("dns_nameservers") { 190 updateOpts.DNSNameservers = resourceSubnetDNSNameserversV2(d) 191 } 192 193 if d.HasChange("host_routes") { 194 updateOpts.HostRoutes = resourceSubnetHostRoutesV2(d) 195 } 196 197 if d.HasChange("enable_dhcp") { 198 v := d.Get("enable_dhcp").(bool) 199 updateOpts.EnableDHCP = &v 200 } 201 202 log.Printf("[DEBUG] Updating Subnet %s with options: %+v", d.Id(), updateOpts) 203 204 _, err = subnets.Update(networkingClient, d.Id(), updateOpts).Extract() 205 if err != nil { 206 return fmt.Errorf("Error updating OpenStack Neutron Subnet: %s", err) 207 } 208 209 return resourceNetworkingSubnetV2Read(d, meta) 210 } 211 212 func resourceNetworkingSubnetV2Delete(d *schema.ResourceData, meta interface{}) error { 213 config := meta.(*Config) 214 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 215 if err != nil { 216 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 217 } 218 219 err = subnets.Delete(networkingClient, d.Id()).ExtractErr() 220 if err != nil { 221 return fmt.Errorf("Error deleting OpenStack Neutron Subnet: %s", err) 222 } 223 224 d.SetId("") 225 return nil 226 } 227 228 func resourceSubnetAllocationPoolsV2(d *schema.ResourceData) []subnets.AllocationPool { 229 rawAPs := d.Get("allocation_pools").([]interface{}) 230 aps := make([]subnets.AllocationPool, len(rawAPs)) 231 for i, raw := range rawAPs { 232 rawMap := raw.(map[string]interface{}) 233 aps[i] = subnets.AllocationPool{ 234 Start: rawMap["start"].(string), 235 End: rawMap["end"].(string), 236 } 237 } 238 return aps 239 } 240 241 func resourceSubnetDNSNameserversV2(d *schema.ResourceData) []string { 242 rawDNSN := d.Get("dns_nameservers").(*schema.Set) 243 dnsn := make([]string, rawDNSN.Len()) 244 for i, raw := range rawDNSN.List() { 245 dnsn[i] = raw.(string) 246 } 247 return dnsn 248 } 249 250 func resourceSubnetHostRoutesV2(d *schema.ResourceData) []subnets.HostRoute { 251 rawHR := d.Get("host_routes").([]interface{}) 252 hr := make([]subnets.HostRoute, len(rawHR)) 253 for i, raw := range rawHR { 254 rawMap := raw.(map[string]interface{}) 255 hr[i] = subnets.HostRoute{ 256 DestinationCIDR: rawMap["destination_cidr"].(string), 257 NextHop: rawMap["next_hop"].(string), 258 } 259 } 260 return hr 261 }