Adding create network operation
diff --git a/openstack/networking/v2/networks/requests.go b/openstack/networking/v2/networks/requests.go
index e27445d..7b647fe 100644
--- a/openstack/networking/v2/networks/requests.go
+++ b/openstack/networking/v2/networks/requests.go
@@ -1,26 +1,13 @@
package networks
import (
+ "encoding/json"
+ "fmt"
+
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
)
-// User-defined options sent to the API when creating or updating a network.
-type NetworkOpts struct {
- // The administrative state of the network, which is up (true) or down (false).
- AdminStateUp bool `json:"admin_state_up"`
- // The network name (optional)
- Name string `json:"name"`
- // Indicates whether this network is shared across all tenants. By default,
- // only administrative users can change this value.
- Shared bool `json:"shared"`
- // Admin-only. The UUID of the tenant that will own the network. This tenant
- // can be different from the tenant that makes the create network request.
- // However, only administrative users can specify a tenant ID other than their
- // own. You cannot change this value through authorization policies.
- TenantID string `json:"tenant_id"`
-}
-
func APIVersions(c *gophercloud.ServiceClient) (*APIVersionsList, error) {
var resp APIVersionsList
_, err := perigee.Request("GET", APIVersionsURL(c), perigee.Options{
@@ -79,3 +66,77 @@
}
return &n, nil
}
+
+type NetworkOpts struct {
+ AdminStateUp bool
+ Name string
+ Shared *bool
+ TenantID string
+}
+
+type NetworkProvider struct {
+ ProviderSegmentationID int `json:"provider:segmentation_id"`
+ ProviderPhysicalNetwork string `json:"provider:physical_network"`
+ ProviderNetworkType string `json:"provider:network_type"`
+}
+
+type NetworkResult struct {
+ Status string `json:"status"`
+ Subnets []interface{} `json:"subnets"`
+ Name string `json:"name"`
+ AdminStateUp bool `json:"admin_state_up"`
+ TenantID string `json:"tenant_id"`
+ Segments []NetworkProvider `json:"segments"`
+ Shared bool `json:"shared"`
+ PortSecurityEnabled bool `json:"port_security_enabled"`
+ ID string `json:"id"`
+}
+
+func Create(c *gophercloud.ServiceClient, opts NetworkOpts) (*NetworkResult, error) {
+ // Define structures
+ type network struct {
+ AdminStateUp bool `json:"admin_state_up"`
+ Name string `json:"name"`
+ Shared *bool `json:"shared,omitempty"`
+ TenantID *string `json:"tenant_id,omitempty"`
+ }
+ type request struct {
+ Network network `json:"network"`
+ }
+ type response struct {
+ Network *NetworkResult `json:"network"`
+ }
+
+ // Validate
+ if opts.Name == "" {
+ return nil, ErrNameRequired
+ }
+
+ // Populate request body
+ reqBody := request{Network: network{
+ AdminStateUp: opts.AdminStateUp,
+ Name: opts.Name,
+ Shared: opts.Shared,
+ }}
+
+ if opts.TenantID != "" {
+ reqBody.Network.TenantID = &opts.TenantID
+ }
+
+ j, _ := json.Marshal(reqBody)
+ fmt.Println(string(j))
+
+ // Send request to API
+ var res response
+ _, err := perigee.Request("POST", CreateURL(c), perigee.Options{
+ MoreHeaders: c.Provider.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ Results: &res,
+ OkCodes: []int{201},
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ return res.Network, nil
+}