-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add reservations to admin control subnet allocs #369
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -46,8 +46,6 @@ func jsonResponse(w http.ResponseWriter, code int, v interface{}) { | |
|
||
// GET /{network}/config | ||
func handleGetNetworkConfig(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
|
||
network := mux.Vars(r)["network"] | ||
if network == "_" { | ||
network = "" | ||
|
@@ -65,8 +63,6 @@ func handleGetNetworkConfig(ctx context.Context, sm subnet.Manager, w http.Respo | |
|
||
// POST /{network}/leases | ||
func handleAcquireLease(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
|
||
network := mux.Vars(r)["network"] | ||
if network == "_" { | ||
network = "" | ||
|
@@ -91,8 +87,6 @@ func handleAcquireLease(ctx context.Context, sm subnet.Manager, w http.ResponseW | |
|
||
// PUT /{network}/{lease.network} | ||
func handleRenewLease(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
|
||
network := mux.Vars(r)["network"] | ||
if network == "_" { | ||
network = "" | ||
|
@@ -115,8 +109,6 @@ func handleRenewLease(ctx context.Context, sm subnet.Manager, w http.ResponseWri | |
} | ||
|
||
func handleRevokeLease(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
|
||
network := mux.Vars(r)["network"] | ||
if network == "_" { | ||
network = "" | ||
|
@@ -134,8 +126,6 @@ func handleRevokeLease(ctx context.Context, sm subnet.Manager, w http.ResponseWr | |
fmt.Fprint(w, err) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func getCursor(u *url.URL) interface{} { | ||
|
@@ -148,8 +138,6 @@ func getCursor(u *url.URL) interface{} { | |
|
||
// GET /{network}/leases/subnet?next=cursor | ||
func handleWatchLease(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
|
||
network := mux.Vars(r)["network"] | ||
if network == "_" { | ||
network = "" | ||
|
@@ -186,8 +174,6 @@ func handleWatchLease(ctx context.Context, sm subnet.Manager, w http.ResponseWri | |
|
||
// GET /{network}/leases?next=cursor | ||
func handleWatchLeases(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
|
||
network := mux.Vars(r)["network"] | ||
if network == "_" { | ||
network = "" | ||
|
@@ -218,8 +204,6 @@ func handleWatchLeases(ctx context.Context, sm subnet.Manager, w http.ResponseWr | |
// GET /?next=cursor watches | ||
// GET / retrieves all networks | ||
func handleNetworks(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
|
||
cursor := getCursor(r.URL) | ||
wr, err := sm.WatchNetworks(ctx, cursor) | ||
if err != nil { | ||
|
@@ -241,6 +225,67 @@ func handleNetworks(ctx context.Context, sm subnet.Manager, w http.ResponseWrite | |
jsonResponse(w, http.StatusOK, wr) | ||
} | ||
|
||
// POST /{network}/reservations | ||
func handleAddReservation(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
network := mux.Vars(r)["network"] | ||
if network == "_" { | ||
network = "" | ||
} | ||
|
||
rsv := &subnet.Reservation{} | ||
if err := json.NewDecoder(r.Body).Decode(rsv); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprint(w, "JSON decoding error: ", err) | ||
return | ||
} | ||
|
||
if err := sm.AddReservation(ctx, network, rsv); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprint(w, fmt.Errorf("internal error: %v", err)) | ||
return | ||
} | ||
} | ||
|
||
// DELETE /{network}/reservations/{subnet} | ||
func handleRemoveReservation(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
network := mux.Vars(r)["network"] | ||
if network == "_" { | ||
network = "" | ||
} | ||
|
||
sn := subnet.ParseSubnetKey(mux.Vars(r)["subnet"]) | ||
if sn == nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprint(w, "bad subnet") | ||
return | ||
} | ||
|
||
if err := sm.RemoveReservation(ctx, network, *sn); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprint(w, err) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
// GET /{network}/reservations | ||
func handleListReservations(ctx context.Context, sm subnet.Manager, w http.ResponseWriter, r *http.Request) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also add a comment for this handler like the above for consistency? |
||
network := mux.Vars(r)["network"] | ||
if network == "_" { | ||
network = "" | ||
} | ||
|
||
leases, err := sm.ListReservations(ctx, network) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprint(w, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should return right after 305, right? |
||
return | ||
} | ||
|
||
jsonResponse(w, http.StatusOK, leases) | ||
} | ||
|
||
func bindHandler(h handler, ctx context.Context, sm subnet.Manager) http.HandlerFunc { | ||
return func(resp http.ResponseWriter, req *http.Request) { | ||
h(ctx, sm, resp, req) | ||
|
@@ -323,13 +368,18 @@ func RunServer(ctx context.Context, sm subnet.Manager, listenAddr, cafile, certf | |
|
||
r := mux.NewRouter() | ||
r.HandleFunc("/v1/{network}/config", bindHandler(handleGetNetworkConfig, ctx, sm)).Methods("GET") | ||
|
||
r.HandleFunc("/v1/{network}/leases", bindHandler(handleAcquireLease, ctx, sm)).Methods("POST") | ||
r.HandleFunc("/v1/{network}/leases/{subnet}", bindHandler(handleWatchLease, ctx, sm)).Methods("GET") | ||
r.HandleFunc("/v1/{network}/leases/{subnet}", bindHandler(handleRenewLease, ctx, sm)).Methods("PUT") | ||
r.HandleFunc("/v1/{network}/leases/{subnet}", bindHandler(handleRevokeLease, ctx, sm)).Methods("DELETE") | ||
r.HandleFunc("/v1/{network}/leases", bindHandler(handleWatchLeases, ctx, sm)).Methods("GET") | ||
r.HandleFunc("/v1/", bindHandler(handleNetworks, ctx, sm)).Methods("GET") | ||
|
||
r.HandleFunc("/v1/{network}/reservations", bindHandler(handleListReservations, ctx, sm)).Methods("GET") | ||
r.HandleFunc("/v1/{network}/reservations", bindHandler(handleAddReservation, ctx, sm)).Methods("POST") | ||
r.HandleFunc("/v1/{network}/reservations/{subnet}", bindHandler(handleRemoveReservation, ctx, sm)).Methods("DELETE") | ||
|
||
l, err := listener(listenAddr, cafile, certfile, keyfile) | ||
if err != nil { | ||
log.Errorf("Error listening on %v: %v", listenAddr, err) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error string does not need to be json format?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's a good point. I'll file an issue to evaluate changing a API on how the errors are reported.