Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staskraev/l3 agent scheduler #2501

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions openstack/networking/v2/extensions/agents/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,37 @@ Example to list dragents hosting specific bgp speaker
for _, a := range allAgents {
log.Printf("%+v", a)
}

Example to list routers scheduled to L3 agent

routers, err := agents.ListL3Routers(neutron, "655967f5-d6f3-4732-88f5-617b0ff5c356").Extract()
if err != nil {
log.Panic(err)
}

for _, r := range routers {
log.Printf("%+v", r)
}

Example to remove router from L3 agent

agentID := "0e1095ae-6f36-40f3-8322-8e1c9a5e68ca"
routerID := "e6fa0457-efc2-491d-ac12-17ab60417efd"
err = agents.RemoveL3Router(neutron, "0e1095ae-6f36-40f3-8322-8e1c9a5e68ca", "e6fa0457-efc2-491d-ac12-17ab60417efd").ExtractErr()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the intention of defining the agentID and routerID variables to use them here?

Suggested change
err = agents.RemoveL3Router(neutron, "0e1095ae-6f36-40f3-8322-8e1c9a5e68ca", "e6fa0457-efc2-491d-ac12-17ab60417efd").ExtractErr()
err = agents.RemoveL3Router(neutron, agentID, routerID).ExtractErr()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, thanks

if err != nil {
log.Panic(err)
}

Example to schedule router to L3 agent

agentID := "0e1095ae-6f36-40f3-8322-8e1c9a5e68ca"
routerID := "e6fa0457-efc2-491d-ac12-17ab60417efd"
err = agents.ScheduleL3Router(neutron, agentID, agents.ScheduleL3RouterOpts{routerID}).ExtractErr()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to be explicit:

Suggested change
err = agents.ScheduleL3Router(neutron, agentID, agents.ScheduleL3RouterOpts{routerID}).ExtractErr()
err = agents.ScheduleL3Router(neutron, agentID, agents.ScheduleL3RouterOpts{RouterID: routerID}).ExtractErr()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right

if err != nil {
log.Panic(err)
}


*/

package agents
46 changes: 46 additions & 0 deletions openstack/networking/v2/extensions/agents/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,49 @@ func ListDRAgentHostingBGPSpeakers(c *gophercloud.ServiceClient, bgpSpeakerID st
return AgentPage{pagination.LinkedPageBase{PageResult: r}}
})
}

// ListL3Routers returns a list of routers scheduled to a specific
// L3 agent.
func ListL3Routers(c *gophercloud.ServiceClient, id string) (r ListL3RoutersResult) {
resp, err := c.Get(listL3RoutersURL(c, id), &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}

// ScheduleL3RouterOptsBuilder allows extensions to add additional parameters
// to the ScheduleL3Router request.
type ScheduleL3RouterOptsBuilder interface {
ToAgentScheduleL3RouterMap() (map[string]interface{}, error)
}

// ScheduleL3RouterOpts represents the attributes used when scheduling a
// router to a L3 agent.
type ScheduleL3RouterOpts struct {
RouterID string `json:"router_id" required:"true"`
}

// ToAgentScheduleL3RouterMap builds a request body from ScheduleL3RouterOpts.
func (opts ScheduleL3RouterOpts) ToAgentScheduleL3RouterMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}

// ScheduleL3Router schedule a router to a L3 agent.
func ScheduleL3Router(c *gophercloud.ServiceClient, id string, opts ScheduleL3RouterOptsBuilder) (r ScheduleL3RouterResult) {
b, err := opts.ToAgentScheduleL3RouterMap()
if err != nil {
r.Err = err
return
}
resp, err := c.Post(scheduleL3RouterURL(c, id), b, nil, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}

// RemoveL3Router removes a router from a L3 agent.
func RemoveL3Router(c *gophercloud.ServiceClient, id string, routerID string) (r RemoveL3RouterResult) {
resp, err := c.Delete(removeL3RouterURL(c, id, routerID), nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
31 changes: 31 additions & 0 deletions openstack/networking/v2/extensions/agents/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/bgp/speakers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/pagination"
)
Expand Down Expand Up @@ -208,3 +209,33 @@ func ExtractBGPSpeakers(r pagination.Page) ([]speakers.BGPSpeaker, error) {
err := (r.(ListBGPSpeakersResult)).ExtractInto(&s)
return s.Speakers, err
}

// ListL3RoutersResult is the response from a List operation.
// Call its Extract method to interpret it as routers.
type ListL3RoutersResult struct {
gophercloud.Result
}

// ScheduleL3RouterResult represents the result of a schedule a router to
// a L3 agent operation. ExtractErr method to determine if the request
// succeeded or failed.
type ScheduleL3RouterResult struct {
gophercloud.ErrResult
}

// RemoveL3RouterResult represents the result of a remove a router from a
// L3 agent operation. ExtractErr method to determine if the request succeeded
// or failed.
type RemoveL3RouterResult struct {
gophercloud.ErrResult
}

// Extract interprets any ListL3RoutesResult as an array of routers.
func (r ListL3RoutersResult) Extract() ([]routers.Router, error) {
var s struct {
Routers []routers.Router `json:"routers"`
}

err := r.ExtractInto(&s)
return s.Routers, err
}
86 changes: 86 additions & 0 deletions openstack/networking/v2/extensions/agents/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,89 @@ const ListDRAgentHostingBGPSpeakersResult = `
]
}
`

// AgentL3ListListResult represents raw response for the ListL3Routers request.
const AgentL3RoutersListResult = `
{
"routers": [
{
"admin_state_up": true,
"availability_zone_hints": [],
"availability_zones": [
"nova"
],
"description": "",
"distributed": false,
"external_gateway_info": {
"enable_snat": true,
"external_fixed_ips": [
{
"ip_address": "172.24.4.3",
"subnet_id": "b930d7f6-ceb7-40a0-8b81-a425dd994ccf"
},
{
"ip_address": "2001:db8::c",
"subnet_id": "0c56df5d-ace5-46c8-8f4c-45fa4e334d18"
}
],
"network_id": "ae34051f-aa6c-4c75-abf5-50dc9ac99ef3"
},
"flavor_id": "f7b14d9a-b0dc-4fbe-bb14-a0f4970a69e0",
"ha": false,
"id": "915a14a6-867b-4af7-83d1-70efceb146f9",
"name": "router2",
"revision_number": 1,
"routes": [
{
"destination": "179.24.1.0/24",
"nexthop": "172.24.3.99"
}
],
"status": "ACTIVE",
"project_id": "0bd18306d801447bb457a46252d82d13",
"tenant_id": "0bd18306d801447bb457a46252d82d13",
"service_type_id": null
},
{
"admin_state_up": true,
"availability_zone_hints": [],
"availability_zones": [
"nova"
],
"description": "",
"distributed": false,
"external_gateway_info": {
"enable_snat": true,
"external_fixed_ips": [
{
"ip_address": "172.24.4.6",
"subnet_id": "b930d7f6-ceb7-40a0-8b81-a425dd994ccf"
},
{
"ip_address": "2001:db8::9",
"subnet_id": "0c56df5d-ace5-46c8-8f4c-45fa4e334d18"
}
],
"network_id": "ae34051f-aa6c-4c75-abf5-50dc9ac99ef3"
},
"flavor_id": "f7b14d9a-b0dc-4fbe-bb14-a0f4970a69e0",
"ha": false,
"id": "f8a44de0-fc8e-45df-93c7-f79bf3b01c95",
"name": "router1",
"revision_number": 1,
"routes": [],
"status": "ACTIVE",
"project_id": "0bd18306d801447bb457a46252d82d13",
"tenant_id": "0bd18306d801447bb457a46252d82d13",
"service_type_id": null
}
]
}
`

// ScheduleL3RouterRequest represents raw request for the ScheduleL3Router request.
const ScheduleL3RouterRequest = `
{
"router_id": "43e66290-79a4-415d-9eb9-7ff7919839e1"
}
`
98 changes: 98 additions & 0 deletions openstack/networking/v2/extensions/agents/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/agents"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
"github.com/gophercloud/gophercloud/pagination"
th "github.com/gophercloud/gophercloud/testhelper"
)
Expand Down Expand Up @@ -323,3 +324,100 @@ func TestListDRAgentHostingBGPSpeakers(t *testing.T) {
t.Errorf("Expected 1 page, got %d", count)
}
}

func TestListL3Routers(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()

th.Mux.HandleFunc("/v2.0/agents/43583cf5-472e-4dc8-af5b-6aed4c94ee3a/l3-routers", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

fmt.Fprintf(w, AgentL3RoutersListResult)
})

s, err := agents.ListL3Routers(fake.ServiceClient(), "43583cf5-472e-4dc8-af5b-6aed4c94ee3a").Extract()
th.AssertNoErr(t, err)

routes := []routers.Route{
{
"172.24.3.99",
"179.24.1.0/24",
},
}

var snat bool = true
gw := routers.GatewayInfo{
EnableSNAT: &snat,
NetworkID: "ae34051f-aa6c-4c75-abf5-50dc9ac99ef3",
ExternalFixedIPs: []routers.ExternalFixedIP{
{
IPAddress: "172.24.4.3",
SubnetID: "b930d7f6-ceb7-40a0-8b81-a425dd994ccf",
},

{
IPAddress: "2001:db8::c",
SubnetID: "0c56df5d-ace5-46c8-8f4c-45fa4e334d18",
},
},
}

var nilSlice []string
th.AssertEquals(t, len(s), 2)
th.AssertEquals(t, s[0].ID, "915a14a6-867b-4af7-83d1-70efceb146f9")
th.AssertEquals(t, s[0].AdminStateUp, true)
th.AssertEquals(t, s[0].ProjectID, "0bd18306d801447bb457a46252d82d13")
th.AssertEquals(t, s[0].Name, "router2")
th.AssertEquals(t, s[0].Status, "ACTIVE")
th.AssertEquals(t, s[0].TenantID, "0bd18306d801447bb457a46252d82d13")
th.AssertDeepEquals(t, s[0].AvailabilityZoneHints, []string{})
th.AssertDeepEquals(t, s[0].Routes, routes)
th.AssertDeepEquals(t, s[0].GatewayInfo, gw)
th.AssertDeepEquals(t, s[0].Tags, nilSlice)
th.AssertEquals(t, s[1].ID, "f8a44de0-fc8e-45df-93c7-f79bf3b01c95")
th.AssertEquals(t, s[1].Name, "router1")

}

func TestScheduleL3Router(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()

th.Mux.HandleFunc("/v2.0/agents/43583cf5-472e-4dc8-af5b-6aed4c94ee3a/l3-routers", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, ScheduleL3RouterRequest)

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
})

opts := &agents.ScheduleL3RouterOpts{
RouterID: "43e66290-79a4-415d-9eb9-7ff7919839e1",
}
err := agents.ScheduleL3Router(fake.ServiceClient(), "43583cf5-472e-4dc8-af5b-6aed4c94ee3a", opts).ExtractErr()
th.AssertNoErr(t, err)
}

func TestRemoveL3Router(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()

th.Mux.HandleFunc("/v2.0/agents/43583cf5-472e-4dc8-af5b-6aed4c94ee3a/l3-routers/43e66290-79a4-415d-9eb9-7ff7919839e1", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Accept", "application/json")

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusNoContent)
})

err := agents.RemoveL3Router(fake.ServiceClient(), "43583cf5-472e-4dc8-af5b-6aed4c94ee3a", "43e66290-79a4-415d-9eb9-7ff7919839e1").ExtractErr()
th.AssertNoErr(t, err)
}
19 changes: 19 additions & 0 deletions openstack/networking/v2/extensions/agents/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "github.com/gophercloud/gophercloud"

const resourcePath = "agents"
const dhcpNetworksResourcePath = "dhcp-networks"
const l3RoutersResourcePath = "l3-routers"
const bgpSpeakersResourcePath = "bgp-drinstances"
const bgpDRAgentSpeakersResourcePath = "bgp-speakers"
const bgpDRAgentAgentResourcePath = "bgp-dragents"
Expand Down Expand Up @@ -36,18 +37,36 @@ func dhcpNetworksURL(c *gophercloud.ServiceClient, id string) string {
return c.ServiceURL(resourcePath, id, dhcpNetworksResourcePath)
}

func l3RoutersURL(c *gophercloud.ServiceClient, id string) string {
return c.ServiceURL(resourcePath, id, l3RoutersResourcePath)
}

func listDHCPNetworksURL(c *gophercloud.ServiceClient, id string) string {
return dhcpNetworksURL(c, id)
}

func listL3RoutersURL(c *gophercloud.ServiceClient, id string) string {
// TODO
// hmm list should be the plain l3RoutersURL but dhcp example tell otherwise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover comment?

Both listL3RoutersURL and listDHCPNetworksURL look good to me. What was the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover from original research

return l3RoutersURL(c, id)
}

func scheduleDHCPNetworkURL(c *gophercloud.ServiceClient, id string) string {
return dhcpNetworksURL(c, id)
}

func scheduleL3RouterURL(c *gophercloud.ServiceClient, id string) string {
return l3RoutersURL(c, id)
}

func removeDHCPNetworkURL(c *gophercloud.ServiceClient, id string, networkID string) string {
return c.ServiceURL(resourcePath, id, dhcpNetworksResourcePath, networkID)
}

func removeL3RouterURL(c *gophercloud.ServiceClient, id string, routerID string) string {
return c.ServiceURL(resourcePath, id, l3RoutersResourcePath, routerID)
}

// return /v2.0/agents/{agent-id}/bgp-drinstances
func listBGPSpeakersURL(c *gophercloud.ServiceClient, agentID string) string {
return c.ServiceURL(resourcePath, agentID, bgpSpeakersResourcePath)
Expand Down