-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8377782
commit a0f3e13
Showing
8 changed files
with
162 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package v5 | ||
|
||
import ( | ||
"github.com/luscis/openlan/cmd/api" | ||
"github.com/luscis/openlan/pkg/schema" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type Rate struct { | ||
Cmd | ||
} | ||
|
||
func (u Rate) Url(prefix, name string) string { | ||
return prefix + "/api/interface/" + name + "/rate" | ||
} | ||
|
||
func (u Rate) Tmpl() string { | ||
return `# total {{ len . }} | ||
{{ps -16 "device"}} {{ps -8 "speed"}} | ||
{{- range . }} | ||
{{ps -16 .Device}} {{pi .Speed}} | ||
{{- end }} | ||
` | ||
} | ||
|
||
func (u Rate) List(c *cli.Context) error { | ||
url := u.Url(c.String("url"), "") | ||
clt := u.NewHttp(c.String("token")) | ||
var items []schema.Rate | ||
if err := clt.GetJSON(url, &items); err != nil { | ||
return err | ||
} | ||
return u.Out(items, c.String("format"), u.Tmpl()) | ||
} | ||
|
||
func (u Rate) Add(c *cli.Context) error { | ||
name := c.String("device") | ||
rate := &schema.Rate{ | ||
Device: name, | ||
Speed: c.Int("speed"), | ||
} | ||
url := u.Url(c.String("url"), name) | ||
clt := u.NewHttp(c.String("token")) | ||
if err := clt.PostJSON(url, rate, nil); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (u Rate) Remove(c *cli.Context) error { | ||
name := c.String("device") | ||
|
||
url := u.Url(c.String("url"), name) | ||
clt := u.NewHttp(c.String("token")) | ||
if err := clt.DeleteJSON(url, nil, nil); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (u Rate) Commands(app *api.App) { | ||
app.Command(&cli.Command{ | ||
Name: "rate", | ||
Usage: "Rate Limit", | ||
Subcommands: []*cli.Command{ | ||
{ | ||
Name: "list", | ||
Usage: "Display all rate limits", | ||
Aliases: []string{"ls"}, | ||
Action: u.List, | ||
}, | ||
{ | ||
Name: "add", | ||
Usage: "Add a rate limit", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{Name: "device", Required: true}, | ||
&cli.StringFlag{Name: "speed", Required: true}, | ||
}, | ||
Action: u.Add, | ||
}, | ||
{ | ||
Name: "remove", | ||
Usage: "Remove a rate limit", | ||
Aliases: []string{"rm"}, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{Name: "device", Required: true}, | ||
}, | ||
Action: u.Remove, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/luscis/openlan/pkg/schema" | ||
) | ||
|
||
type Rate struct { | ||
Switcher Switcher | ||
} | ||
|
||
func (h Rate) Router(router *mux.Router) { | ||
router.HandleFunc("/api/interface/{id}/rate", h.Post).Methods("POST") | ||
router.HandleFunc("/api/interface/{id}/rate", h.Delete).Methods("DELETE") | ||
} | ||
|
||
func (h Rate) Post(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
device := vars["id"] | ||
|
||
rate := &schema.Rate{} | ||
if err := GetData(r, rate); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
h.Switcher.AddRate(device, rate.Speed) | ||
} | ||
|
||
func (h Rate) Delete(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
device := vars["id"] | ||
h.Switcher.DelRate(device) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package schema | ||
|
||
type Rate struct { | ||
Device string `json:"device"` | ||
Speed int `json:"speed"` // Mbit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters