From ee56f1d075b41f4a39bdc9cf86f4c88d5be9b729 Mon Sep 17 00:00:00 2001 From: Gavin Williams Date: Fri, 12 Aug 2016 18:13:20 +0100 Subject: [PATCH] provieder/openstack: Add 'value_specs' support for openstack_networking_subnet_v2 provider. Updated provider documentation to support. --- ...resource_openstack_networking_subnet_v2.go | 92 ++++++++++++++++++- .../r/networking_subnet_v2.html.markdown | 2 + 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go b/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go index 2ef42c78c..140b29f02 100644 --- a/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go +++ b/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go @@ -114,10 +114,91 @@ func resourceNetworkingSubnetV2() *schema.Resource { }, }, }, + "value_specs": &schema.Schema{ + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, }, } } +// SubnetCreateOpts represents the attributes used when creating a new subnet. +type SubnetCreateOpts struct { + // Required + NetworkID string + CIDR string + // Optional + Name string + TenantID string + AllocationPools []subnets.AllocationPool + GatewayIP string + NoGateway bool + IPVersion int + EnableDHCP *bool + DNSNameservers []string + HostRoutes []subnets.HostRoute + ValueSpecs map[string]string +} + +// ToSubnetCreateMap casts a CreateOpts struct to a map. +func (opts SubnetCreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) { + s := make(map[string]interface{}) + + if opts.NetworkID == "" { + return nil, fmt.Errorf("A network ID is required") + } + if opts.CIDR == "" { + return nil, fmt.Errorf("A valid CIDR is required") + } + if opts.IPVersion != 0 && opts.IPVersion != subnets.IPv4 && opts.IPVersion != subnets.IPv6 { + return nil, fmt.Errorf("An IP type must either be 4 or 6") + } + + // Both GatewayIP and NoGateway should not be set + if opts.GatewayIP != "" && opts.NoGateway { + return nil, fmt.Errorf("Both disabling the gateway and specifying a gateway is not allowed") + } + + s["network_id"] = opts.NetworkID + s["cidr"] = opts.CIDR + + if opts.EnableDHCP != nil { + s["enable_dhcp"] = &opts.EnableDHCP + } + if opts.Name != "" { + s["name"] = opts.Name + } + if opts.GatewayIP != "" { + s["gateway_ip"] = opts.GatewayIP + } else if opts.NoGateway { + s["gateway_ip"] = nil + } + if opts.TenantID != "" { + s["tenant_id"] = opts.TenantID + } + if opts.IPVersion != 0 { + s["ip_version"] = opts.IPVersion + } + if len(opts.AllocationPools) != 0 { + s["allocation_pools"] = opts.AllocationPools + } + if len(opts.DNSNameservers) != 0 { + s["dns_nameservers"] = opts.DNSNameservers + } + if len(opts.HostRoutes) != 0 { + s["host_routes"] = opts.HostRoutes + } + + if opts.ValueSpecs != nil { + for k, v := range opts.ValueSpecs { + s[k] = v + } + } + + return map[string]interface{}{"subnet": s}, nil +} + func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkingClient, err := config.networkingV2Client(d.Get("region").(string)) @@ -133,7 +214,7 @@ func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{}) enableDHCP := d.Get("enable_dhcp").(bool) - createOpts := subnets.CreateOpts{ + createOpts := SubnetCreateOpts{ NetworkID: d.Get("network_id").(string), CIDR: d.Get("cidr").(string), Name: d.Get("name").(string), @@ -145,6 +226,7 @@ func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{}) DNSNameservers: resourceSubnetDNSNameserversV2(d), HostRoutes: resourceSubnetHostRoutesV2(d), EnableDHCP: &enableDHCP, + ValueSpecs: subnetValueSpecs(d), } log.Printf("[DEBUG] Create Options: %#v", createOpts) @@ -354,3 +436,11 @@ func waitForSubnetDelete(networkingClient *gophercloud.ServiceClient, subnetId s return s, "ACTIVE", nil } } + +func subnetValueSpecs(d *schema.ResourceData) map[string]string { + m := make(map[string]string) + for key, val := range d.Get("value_specs").(map[string]interface{}) { + m[key] = val.(string) + } + return m +} diff --git a/website/source/docs/providers/openstack/r/networking_subnet_v2.html.markdown b/website/source/docs/providers/openstack/r/networking_subnet_v2.html.markdown index 072c49b16..ecc5f6b99 100644 --- a/website/source/docs/providers/openstack/r/networking_subnet_v2.html.markdown +++ b/website/source/docs/providers/openstack/r/networking_subnet_v2.html.markdown @@ -73,6 +73,8 @@ The following arguments are supported: object structure is documented below. Changing this updates the host routes for the existing subnet. +* `value_specs` - (Optional) Map of additional options. + The `allocation_pools` block supports: * `start` - (Required) The starting address.