Adding get network operation
diff --git a/openstack/networking/v2/networks/requests.go b/openstack/networking/v2/networks/requests.go
index cc57540..e27445d 100644
--- a/openstack/networking/v2/networks/requests.go
+++ b/openstack/networking/v2/networks/requests.go
@@ -64,3 +64,18 @@
}
return &ext, nil
}
+
+func Get(c *gophercloud.ServiceClient, id string) (*Network, error) {
+ var n Network
+ _, err := perigee.Request("GET", NetworkURL(c, id), perigee.Options{
+ MoreHeaders: c.Provider.AuthenticatedHeaders(),
+ Results: &struct {
+ Network *Network `json:"network"`
+ }{&n},
+ OkCodes: []int{200},
+ })
+ if err != nil {
+ return nil, err
+ }
+ return &n, nil
+}
diff --git a/openstack/networking/v2/networks/requests_test.go b/openstack/networking/v2/networks/requests_test.go
index 85e3ce0..5682c33 100644
--- a/openstack/networking/v2/networks/requests_test.go
+++ b/openstack/networking/v2/networks/requests_test.go
@@ -27,6 +27,12 @@
}
}
+func DeepEquals(t *testing.T, actual, expected interface{}) {
+ if !reflect.DeepEqual(actual, expected) {
+ t.Fatalf("Expected %#v but got %#v", expected, actual)
+ }
+}
+
func CheckErr(t *testing.T, e error) {
if e != nil {
t.Fatalf("An error occurred: %#v", e)
@@ -204,3 +210,55 @@
Equals(t, ext.Description, "The agent management extension.")
})
}
+
+func TestGettingNetwork(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, `
+{
+ "network": {
+ "status": "ACTIVE",
+ "subnets": [
+ "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
+ ],
+ "name": "private-network",
+ "provider:physical_network": null,
+ "admin_state_up": true,
+ "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
+ "provider:network_type": "local",
+ "router:external": true,
+ "shared": true,
+ "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
+ "provider:segmentation_id": null
+ }
+}
+ `)
+ })
+
+ c := ServiceClient()
+
+ n, err := Get(c, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
+ if err != nil {
+ t.Fatalf("Unexpected error: %#v", err)
+ }
+
+ Equals(t, n.Status, "ACTIVE")
+ DeepEquals(t, n.Subnets, []string{"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"})
+ Equals(t, n.Name, "private-network")
+ Equals(t, n.ProviderPhysicalNetwork, "")
+ Equals(t, n.ProviderNetworkType, "local")
+ Equals(t, n.ProviderSegmentationID, "")
+ Equals(t, n.AdminStateUp, true)
+ Equals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869")
+ Equals(t, n.RouterExternal, true)
+ Equals(t, n.Shared, true)
+ Equals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
+}
diff --git a/openstack/networking/v2/networks/results.go b/openstack/networking/v2/networks/results.go
index 61d51b0..b48086c 100644
--- a/openstack/networking/v2/networks/results.go
+++ b/openstack/networking/v2/networks/results.go
@@ -10,7 +10,7 @@
// A Network represents a a virtual layer-2 broadcast domain.
type Network struct {
// Id is the unique identifier for the network.
- Id string `json:"id"`
+ ID string `json:"id"`
// Name is the (not necessarily unique) human-readable identifier for the network.
Name string `json:"name"`
// AdminStateUp is administrative state of the network. If false, network is down.
@@ -22,7 +22,7 @@
// Shared indicates whether the network can be accessed by any tenant or not.
Shared bool `json:"shared"`
// TenantId is the owner of the network. Admins may specify TenantId other than their own.
- TenantId string `json:"tenant_id"`
+ TenantID string `json:"tenant_id"`
// RouterExternal indicates if the network is connected to an external router.
RouterExternal bool `json:"router:external"`
// ProviderPhysicalNetwork is the name of the provider physical network.
@@ -30,7 +30,7 @@
// ProviderNetworkType is the type of provider network (eg "vlan").
ProviderNetworkType string `json:"provider:network_type"`
// ProviderSegmentationId is the provider network identifier (such as the vlan id).
- ProviderSegmentationId string `json:"provider:segmentation_id"`
+ ProviderSegmentationID string `json:"provider:segmentation_id"`
}
type APIVersion struct {
diff --git a/openstack/networking/v2/networks/urls.go b/openstack/networking/v2/networks/urls.go
index 8d350b1..70cc6e1 100644
--- a/openstack/networking/v2/networks/urls.go
+++ b/openstack/networking/v2/networks/urls.go
@@ -19,3 +19,7 @@
func ExtensionURL(c *gophercloud.ServiceClient, name string) string {
return c.ServiceURL(Version, "extensions", name)
}
+
+func NetworkURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL(Version, "networks", id)
+}
diff --git a/openstack/networking/v2/networks/urls_test.go b/openstack/networking/v2/networks/urls_test.go
index 34009ed..df8bd02 100644
--- a/openstack/networking/v2/networks/urls_test.go
+++ b/openstack/networking/v2/networks/urls_test.go
@@ -35,3 +35,11 @@
t.Fatalf("[%s] does not match expected [%s]", actual, expected)
}
}
+
+func TestNetworkURL(t *testing.T) {
+ actual := NetworkURL(EndpointClient(), "foo")
+ expected := Endpoint + "v2.0/networks/foo"
+ if expected != actual {
+ t.Fatalf("[%s] does not match expected [%s]", actual, expected)
+ }
+}