-
Notifications
You must be signed in to change notification settings - Fork 3
router: add initial API client #95
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
Open
mweibel
wants to merge
6
commits into
master
Choose a base branch
from
router
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3939e3b
router: add initial API client
mweibel 288d4f2
router: use string instead of netip, replace WaitForDeleted with wait…
mweibel ce38056
router: adjust to new APIs
mweibel 4dca618
fix: delete interface before deleting router
mweibel 0021bdd
fix naming & length assertion
mweibel 4b5f03e
refactor: rename interface -> serverInterface
mweibel 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 hidden or 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 hidden or 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,110 @@ | ||
| package cloudscale | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
| ) | ||
|
|
||
| const routerBasePath = "v1/routers" | ||
|
|
||
| type Router struct { | ||
| ZonalResource | ||
| TaggedResource | ||
| HREF string `json:"href"` | ||
| UUID string `json:"uuid"` | ||
| Name string `json:"name"` | ||
| CreatedAt time.Time `json:"created_at"` | ||
| Status string `json:"status"` | ||
| InternetGateway bool `json:"internet_gateway"` | ||
| InternetGatewayAddresses []IPAddress `json:"internet_gateway_addresses,omitempty"` | ||
| Interfaces []RouterInterface `json:"interfaces,omitempty"` | ||
| } | ||
|
|
||
| type IPAddress struct { | ||
| Address string `json:"address"` | ||
| Subnet SubnetStub `json:"subnet"` | ||
| Version int `json:"version"` | ||
| ReversePTR *string `json:"reverse_ptr"` | ||
| } | ||
| type RouterInterface struct { | ||
| UUID string `json:"uuid"` | ||
| Network NetworkStub `json:"network"` | ||
| Addresses []IPAddress `json:"addresses"` | ||
| Type string `json:"type"` | ||
| MACAddress string `json:"mac_address"` | ||
| } | ||
|
|
||
| type RouterCreateRequest struct { | ||
| ZonalResourceRequest | ||
| TaggedResourceRequest | ||
| Name string `json:"name"` | ||
| InternetGateway bool `json:"internet_gateway"` | ||
| } | ||
|
|
||
| // RouterUpdateRequest is not implemented yet because the API is not implemented yet | ||
| type RouterUpdateRequest struct{} | ||
|
|
||
| type RouterService interface { | ||
| GenericCreateService[Router, RouterCreateRequest] | ||
| GenericGetService[Router] | ||
| GenericListService[Router] | ||
| // GenericUpdateService[Router, RouterUpdateRequest] | ||
| GenericDeleteService[Router] | ||
| GenericWaitForService[Router] | ||
| // CreateInterface creates a new interface attached to this router | ||
| CreateInterface(ctx context.Context, routerUUID string, createReq CreateInterfaceRequest) (*RouterInterface, error) | ||
| // DeleteInterface removes an interface attached to this router | ||
| DeleteInterface(ctx context.Context, routerUUID, interfaceUUID string) error | ||
| } | ||
|
|
||
| type CreateInterfaceRequest struct { | ||
| Network string `json:"network"` | ||
| Addresses []CreateAddressRequest `json:"addresses"` | ||
| } | ||
| type CreateAddressRequest struct { | ||
| Subnet string `json:"subnet"` | ||
| Address string `json:"address"` | ||
| } | ||
|
|
||
| type RouterServiceOperations struct { | ||
| GenericServiceOperations[Router, RouterCreateRequest, RouterUpdateRequest] | ||
| client *Client | ||
| } | ||
|
|
||
| func (r RouterServiceOperations) CreateInterface(ctx context.Context, routerUUID string, createReq CreateInterfaceRequest) (*RouterInterface, error) { | ||
| path := fmt.Sprintf("%s/%s/interfaces", routerBasePath, routerUUID) | ||
| ctx = WithOperationPath(ctx, routerBasePath+"/:id/interfaces") | ||
| req, err := r.client.NewRequest(ctx, http.MethodPost, path, createReq) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| res := &RouterInterface{} | ||
| if err := r.client.Do(ctx, req, res); err != nil { | ||
| return nil, err | ||
| } | ||
| return res, nil | ||
| } | ||
|
|
||
| func (r RouterServiceOperations) DeleteInterface(ctx context.Context, routerUUID, interfaceUUID string) error { | ||
| path := fmt.Sprintf("%s/%s/interfaces/%s", routerBasePath, routerUUID, interfaceUUID) | ||
| ctx = WithOperationPath(ctx, routerBasePath+"/:id/interfaces/:interface_id") | ||
|
|
||
| req, err := r.client.NewRequest(ctx, http.MethodDelete, path, nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return r.client.Do(ctx, req, nil) | ||
| } | ||
|
|
||
| const ( | ||
| RouterActive = "active" | ||
| ) | ||
|
|
||
| var RouterIsActive = func(router *Router) (bool, error) { | ||
| if router.Status == RouterActive { | ||
| return true, nil | ||
| } | ||
| return false, fmt.Errorf("waiting for status: %s, current status: %s", RouterActive, router.Status) | ||
| } | ||
Oops, something went wrong.
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.
As there will be a interface API at some point in the not so far future, is going for
RouterInterfacethe right direction? I assumed we would use a generic Interface Type.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 initially wanted to use interface but we have already an interface type (and, accordingly, an Address type):
cloudscale-go-sdk/servers.go
Lines 68 to 81 in b10972e
The question I guess is, what do we do. We can e.g. rename the existing Interface to ServerInterface or LegacyInterface because we plan to unify them, and we'll need a new major version anyway due to #91, or we find another name.
Your opinon?
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.
As discussed, keep
RouterInterfaceas submitted. On the server, rename theInterfacetoServerInterface. This will be a breaking change, but it keeps the naming consistent.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.
done.