Skip to content

Commit bb4f6fb

Browse files
committed
Network v2: add listDHCPNetworks for agents extension
this adds the listDHCPNetworks call for agents, that fetches a list of networks scheduled to a specific agent.
1 parent ff98514 commit bb4f6fb

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

openstack/networking/v2/extensions/agents/requests.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,10 @@ func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
6464
_, r.Err = c.Get(getURL(c, id), &r.Body, nil)
6565
return
6666
}
67+
68+
// ListDHCPNetworks returns a list of networks scheduled to a specific
69+
// dhcp agent
70+
func ListDHCPNetworks(c *gophercloud.ServiceClient, id string) (r ListDHCPNetworksResult) {
71+
_, r.Err = c.Get(listDHCPNetworksURL(c, id), &r.Body, nil)
72+
return
73+
}

openstack/networking/v2/extensions/agents/results.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/gophercloud/gophercloud"
8+
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
89
"github.com/gophercloud/gophercloud/pagination"
910
)
1011

@@ -126,3 +127,19 @@ func ExtractAgents(r pagination.Page) ([]Agent, error) {
126127
err := (r.(AgentPage)).ExtractInto(&s)
127128
return s.Agents, err
128129
}
130+
131+
// ListDHCPNetworksResult is the response from a List operation.
132+
// Call its Extract method to interpret it as networks.
133+
type ListDHCPNetworksResult struct {
134+
gophercloud.Result
135+
}
136+
137+
// Extract interprets any ListDHCPNetworksResult as an array of networks.
138+
func (r ListDHCPNetworksResult) Extract() ([]networks.Network, error) {
139+
var s struct {
140+
Networks []networks.Network `json:"networks"`
141+
}
142+
143+
err := r.ExtractInto(&s)
144+
return s.Networks, err
145+
}

openstack/networking/v2/extensions/agents/testing/fixtures.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,41 @@ const AgentsGetResult = `
123123
}
124124
}
125125
`
126+
127+
// AgentDHCPNetworksListResult represents raw response for the ListDHCPNetworks request.
128+
const AgentDHCPNetworksListResult = `
129+
{
130+
"networks": [
131+
{
132+
"admin_state_up": true,
133+
"availability_zone_hints": [],
134+
"availability_zones": [
135+
"nova"
136+
],
137+
"created_at": "2016-03-08T20:19:41",
138+
"dns_domain": "my-domain.org.",
139+
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
140+
"ipv4_address_scope": null,
141+
"ipv6_address_scope": null,
142+
"l2_adjacency": false,
143+
"mtu": 1500,
144+
"name": "net1",
145+
"port_security_enabled": true,
146+
"project_id": "4fd44f30292945e481c7b8a0c8908869",
147+
"qos_policy_id": "6a8454ade84346f59e8d40665f878b2e",
148+
"revision_number": 1,
149+
"router:external": false,
150+
"shared": false,
151+
"status": "ACTIVE",
152+
"subnets": [
153+
"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
154+
],
155+
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
156+
"updated_at": "2016-03-08T20:19:41",
157+
"vlan_transparent": true,
158+
"description": "",
159+
"is_default": false
160+
}
161+
]
162+
}
163+
`

openstack/networking/v2/extensions/agents/testing/requests_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,35 @@ func TestGet(t *testing.T) {
8888
"enable_distributed_routing": false,
8989
})
9090
}
91+
92+
func TestListDHCPNetworks(t *testing.T) {
93+
th.SetupHTTP()
94+
defer th.TeardownHTTP()
95+
96+
th.Mux.HandleFunc("/v2.0/agents/43583cf5-472e-4dc8-af5b-6aed4c94ee3a/dhcp-networks", func(w http.ResponseWriter, r *http.Request) {
97+
th.TestMethod(t, r, "GET")
98+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
99+
100+
w.Header().Add("Content-Type", "application/json")
101+
w.WriteHeader(http.StatusOK)
102+
103+
fmt.Fprintf(w, AgentDHCPNetworksListResult)
104+
})
105+
106+
s, err := agents.ListDHCPNetworks(fake.ServiceClient(), "43583cf5-472e-4dc8-af5b-6aed4c94ee3a").Extract()
107+
th.AssertNoErr(t, err)
108+
109+
var nilSlice []string
110+
th.AssertEquals(t, len(s), 1)
111+
th.AssertEquals(t, s[0].ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
112+
th.AssertEquals(t, s[0].AdminStateUp, true)
113+
th.AssertEquals(t, s[0].ProjectID, "4fd44f30292945e481c7b8a0c8908869")
114+
th.AssertEquals(t, s[0].Shared, false)
115+
th.AssertEquals(t, s[0].Name, "net1")
116+
th.AssertEquals(t, s[0].Status, "ACTIVE")
117+
th.AssertDeepEquals(t, s[0].Tags, nilSlice)
118+
th.AssertEquals(t, s[0].TenantID, "4fd44f30292945e481c7b8a0c8908869")
119+
th.AssertDeepEquals(t, s[0].AvailabilityZoneHints, []string{})
120+
th.AssertDeepEquals(t, s[0].Subnets, []string{"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"})
121+
122+
}

openstack/networking/v2/extensions/agents/urls.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package agents
33
import "github.com/gophercloud/gophercloud"
44

55
const resourcePath = "agents"
6+
const dhcpNetworksResourcePath = "dhcp-networks"
67

78
func resourceURL(c *gophercloud.ServiceClient, id string) string {
89
return c.ServiceURL(resourcePath, id)
@@ -19,3 +20,11 @@ func listURL(c *gophercloud.ServiceClient) string {
1920
func getURL(c *gophercloud.ServiceClient, id string) string {
2021
return resourceURL(c, id)
2122
}
23+
24+
func dhcpNetworksURL(c *gophercloud.ServiceClient, id string) string {
25+
return c.ServiceURL(resourcePath, id, dhcpNetworksResourcePath)
26+
}
27+
28+
func listDHCPNetworksURL(c *gophercloud.ServiceClient, id string) string {
29+
return dhcpNetworksURL(c, id)
30+
}

0 commit comments

Comments
 (0)