Skip to content

Commit

Permalink
feat(network): static_routes field to router (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
villevsv-upcloud committed Sep 27, 2023
1 parent 9fc8e4d commit 7dd13dd
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 80 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
## [Unreleased]

### Added

- network: `dhcp_routes` field to IP network for additional DHCP classless static routes to be delivered if the DHCP is enabled
- network: `static_routes` field to router for defining static routes

## [6.6.0]

Expand Down
8 changes: 8 additions & 0 deletions upcloud/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ type Router struct {
Type string `json:"type"`
UUID string `json:"uuid"`
Labels []Label `json:"labels"`
StaticRoutes []StaticRoute `json:"static_routes"`
}

// StaticRoute represents a Static route
type StaticRoute struct {
Name string `json:"name,omitempty"`
Route string `json:"route"`
Nexthop string `json:"nexthop"`
}

// UnmarshalJSON is a custom unmarshaller that deals with
Expand Down
14 changes: 14 additions & 0 deletions upcloud/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ func TestUnmarshalRouters(t *testing.T) {
]
},
"name": "Example router",
"static_routes": [
{
"route": "0.0.0.0/0",
"nexthop": "10.0.0.100",
"name": "static_route_0"
}
],
"type": "normal",
"uuid": "04c0df35-2658-4b0c-8ac7-962090f4e92a"
}
Expand All @@ -473,6 +480,13 @@ func TestUnmarshalRouters(t *testing.T) {
},
Name: "Example router",
Type: "normal",
StaticRoutes: []StaticRoute{
{
Name: "static_route_0",
Route: "0.0.0.0/0",
Nexthop: "10.0.0.100",
},
},
UUID: "04c0df35-2658-4b0c-8ac7-962090f4e92a",
},
}
Expand Down
12 changes: 7 additions & 5 deletions upcloud/request/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,9 @@ func (r *GetRouterDetailsRequest) RequestURL() string {

// CreateRouterRequest represents a request to create a new router.
type CreateRouterRequest struct {
Name string `json:"name"`
Labels []upcloud.Label `json:"labels,omitempty"`
Name string `json:"name"`
Labels []upcloud.Label `json:"labels,omitempty"`
StaticRoutes []upcloud.StaticRoute `json:"static_routes,omitempty"`
}

// RequestURL implements the Request interface.
Expand All @@ -302,9 +303,10 @@ func (r CreateRouterRequest) MarshalJSON() ([]byte, error) {

// ModifyRouterRequest represents a request to modify an existing router.
type ModifyRouterRequest struct {
UUID string `json:"-"`
Name string `json:"name"`
Labels *[]upcloud.Label `json:"labels,omitempty"`
UUID string `json:"-"`
Name string `json:"name"`
Labels *[]upcloud.Label `json:"labels,omitempty"`
StaticRoutes *[]upcloud.StaticRoute `json:"static_routes,omitempty"`
}

// RequestURL implements the Request interface.
Expand Down
36 changes: 32 additions & 4 deletions upcloud/request/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,26 @@ func TestMarshalGetRouterDetailsRequest(t *testing.T) {
func TestMarshalCreateRouterRequest(t *testing.T) {
request := CreateRouterRequest{
Name: "Example router",
StaticRoutes: []upcloud.StaticRoute{
{
Name: "example_static_route",
Route: "0.0.0.0/0",
Nexthop: "10.0.0.100",
},
},
}

expectedJSON := `
{
"router": {
"name": "Example router"
"name": "Example router",
"static_routes": [
{
"route": "0.0.0.0/0",
"nexthop": "10.0.0.100",
"name": "example_static_route"
}
]
}
}
`
Expand All @@ -383,13 +397,27 @@ func TestMarshalCreateRouterRequest(t *testing.T) {
func TestMarshalModifyRouterRequest(t *testing.T) {
request := ModifyRouterRequest{
Name: "Modified router",
StaticRoutes: &[]upcloud.StaticRoute{
{
Name: "example_static_route",
Route: "0.0.0.0/0",
Nexthop: "10.0.0.100",
},
},
UUID: "foo",
}

expectedJSON := `
{
"router": {
"name": "Modified router"
"name": "Modified router",
"static_routes": [
{
"route": "0.0.0.0/0",
"nexthop": "10.0.0.100",
"name": "example_static_route"
}
]
}
}
`
Expand All @@ -403,14 +431,14 @@ func TestMarshalModifyRouterRequest(t *testing.T) {

request = ModifyRouterRequest{
UUID: "",
Name: "Modified router",
Name: "Modified router name",
Labels: &[]upcloud.Label{},
}

expectedJSON = `
{
"router": {
"name": "Modified router",
"name": "Modified router name",
"labels": []
}
}
Expand Down
Loading

0 comments on commit 7dd13dd

Please sign in to comment.