rackconnect cloud networks ops and unit tests
diff --git a/rackspace/rackconnect/v3/cloudnetworks/requests.go b/rackspace/rackconnect/v3/cloudnetworks/requests.go
new file mode 100644
index 0000000..33f5e04
--- /dev/null
+++ b/rackspace/rackconnect/v3/cloudnetworks/requests.go
@@ -0,0 +1,27 @@
+package cloudnetworks
+
+import (
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// List returns all cloud networks that are associated with RackConnect. The ID
+// returned for each network is the same as the ID returned by the networks package.
+func List(c *gophercloud.ServiceClient) pagination.Pager {
+ url := listURL(c)
+ createPage := func(r pagination.PageResult) pagination.Page {
+ return CloudNetworkPage{pagination.SinglePageBase(r)}
+ }
+ return pagination.NewPager(c, url, createPage)
+}
+
+// Get retrieves a specific cloud network (that is associated with RackConnect)
+// based on its unique ID.
+func Get(c *gophercloud.ServiceClient, id string) GetResult {
+ var res GetResult
+ _, res.Err = c.Request("GET", getURL(c, id), gophercloud.RequestOpts{
+ JSONResponse: &res.Body,
+ OkCodes: []int{200},
+ })
+ return res
+}
diff --git a/rackspace/rackconnect/v3/cloudnetworks/requests_test.go b/rackspace/rackconnect/v3/cloudnetworks/requests_test.go
new file mode 100644
index 0000000..10d15dd
--- /dev/null
+++ b/rackspace/rackconnect/v3/cloudnetworks/requests_test.go
@@ -0,0 +1,87 @@
+package cloudnetworks
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestListCloudNetworks(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ th.Mux.HandleFunc("/cloud_networks", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+
+ w.Header().Set("Content-Type", "application/json")
+ fmt.Fprintf(w, `[{
+ "cidr": "192.168.100.0/24",
+ "created": "2014-05-25T01:23:42Z",
+ "id": "07426958-1ebf-4c38-b032-d456820ca21a",
+ "name": "RC-CLOUD",
+ "updated": "2014-05-25T02:28:44Z"
+ }]`)
+ })
+
+ expected := []CloudNetwork{
+ CloudNetwork{
+ CIDR: "192.168.100.0/24",
+ CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC),
+ ID: "07426958-1ebf-4c38-b032-d456820ca21a",
+ Name: "RC-CLOUD",
+ UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC),
+ },
+ }
+
+ count := 0
+ err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractCloudNetworks(page)
+ th.AssertNoErr(t, err)
+
+ th.CheckDeepEquals(t, expected, actual)
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
+}
+
+func TestGetCloudNetwork(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ th.Mux.HandleFunc("/cloud_networks/07426958-1ebf-4c38-b032-d456820ca21a", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ fmt.Fprintf(w, `{
+ "cidr": "192.168.100.0/24",
+ "created": "2014-05-25T01:23:42Z",
+ "id": "07426958-1ebf-4c38-b032-d456820ca21a",
+ "name": "RC-CLOUD",
+ "updated": "2014-05-25T02:28:44Z"
+ }`)
+ })
+
+ expected := &CloudNetwork{
+ CIDR: "192.168.100.0/24",
+ CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC),
+ ID: "07426958-1ebf-4c38-b032-d456820ca21a",
+ Name: "RC-CLOUD",
+ UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC),
+ }
+
+ actual, err := Get(fake.ServiceClient(), "07426958-1ebf-4c38-b032-d456820ca21a").Extract()
+ th.AssertNoErr(t, err)
+
+ th.AssertDeepEquals(t, expected, actual)
+}
diff --git a/rackspace/rackconnect/v3/cloudnetworks/results.go b/rackspace/rackconnect/v3/cloudnetworks/results.go
new file mode 100644
index 0000000..282b9ce
--- /dev/null
+++ b/rackspace/rackconnect/v3/cloudnetworks/results.go
@@ -0,0 +1,114 @@
+package cloudnetworks
+
+import (
+ "fmt"
+ "reflect"
+ "time"
+
+ "github.com/jrperritt/gophercloud"
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// CloudNetwork represents a network associated with a RackConnect configuration.
+type CloudNetwork struct {
+ // Specifies the ID of the newtork.
+ ID string `mapstructure:"id"`
+ // Specifies the user-provided name of the network.
+ Name string `mapstructure:"name"`
+ // Specifies the IP range for this network.
+ CIDR string `mapstructure:"cidr"`
+ // Specifies the time the network was created.
+ CreatedAt time.Time `mapstructure:"-"`
+ // Specifies the time the network was last updated.
+ UpdatedAt time.Time `mapstructure:"-"`
+}
+
+// CloudNetworkPage is the page returned by a pager when traversing over a
+// collection of CloudNetworks.
+type CloudNetworkPage struct {
+ pagination.SinglePageBase
+}
+
+// IsEmpty returns true if a CloudNetworkPage contains no CloudNetworks.
+func (r CloudNetworkPage) IsEmpty() (bool, error) {
+ cns, err := ExtractCloudNetworks(r)
+ if err != nil {
+ return true, err
+ }
+ return len(cns) == 0, nil
+}
+
+// ExtractCloudNetworks extracts and returns CloudNetworks. It is used while iterating over
+// a cloudnetworks.List call.
+func ExtractCloudNetworks(page pagination.Page) ([]CloudNetwork, error) {
+ var res []CloudNetwork
+ casted := page.(CloudNetworkPage).Body
+
+ err := mapstructure.Decode(casted, &res)
+
+ var rawNets []interface{}
+ switch casted.(type) {
+ case interface{}:
+ rawNets = casted.([]interface{})
+ default:
+ return res, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
+ }
+
+ for i := range rawNets {
+ thisNet := (rawNets[i]).(map[string]interface{})
+
+ if t, ok := thisNet["created"].(string); ok && t != "" {
+ creationTime, err := time.Parse(time.RFC3339, t)
+ if err != nil {
+ return res, err
+ }
+ res[i].CreatedAt = creationTime
+ }
+
+ if t, ok := thisNet["updated"].(string); ok && t != "" {
+ updatedTime, err := time.Parse(time.RFC3339, t)
+ if err != nil {
+ return res, err
+ }
+ res[i].UpdatedAt = updatedTime
+ }
+ }
+
+ return res, err
+}
+
+// GetResult represents the result of a Get operation.
+type GetResult struct {
+ gophercloud.Result
+}
+
+// Extract is a function that extracts a CloudNetwork from a GetResult.
+func (r GetResult) Extract() (*CloudNetwork, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+ var res CloudNetwork
+
+ err := mapstructure.Decode(r.Body, &res)
+
+ b := r.Body.(map[string]interface{})
+
+ if date, ok := b["created"]; ok && date != nil {
+ t, err := time.Parse(time.RFC3339, date.(string))
+ if err != nil {
+ return nil, err
+ }
+ res.CreatedAt = t
+ }
+
+ if date, ok := b["updated"]; ok && date != nil {
+ t, err := time.Parse(time.RFC3339, date.(string))
+ if err != nil {
+ return nil, err
+ }
+ res.UpdatedAt = t
+ }
+
+ return &res, err
+}
diff --git a/rackspace/rackconnect/v3/cloudnetworks/urls.go b/rackspace/rackconnect/v3/cloudnetworks/urls.go
new file mode 100644
index 0000000..bd6b098
--- /dev/null
+++ b/rackspace/rackconnect/v3/cloudnetworks/urls.go
@@ -0,0 +1,11 @@
+package cloudnetworks
+
+import "github.com/rackspace/gophercloud"
+
+func listURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL("cloud_networks")
+}
+
+func getURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL("cloud_networks", id)
+}