Skip to content

Commit c9ab4e9

Browse files
authored
feat(vpc): add custom routes CRUD (#2136)
1 parent c148d81 commit c9ab4e9

File tree

1 file changed

+209
-4
lines changed

1 file changed

+209
-4
lines changed

api/vpc/v2/vpc_sdk.go

Lines changed: 209 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,23 +306,34 @@ type PrivateNetwork struct {
306306

307307
// Route: route.
308308
type Route struct {
309+
// ID: route ID.
309310
ID string `json:"id"`
310311

311-
CreatedAt *time.Time `json:"created_at"`
312+
// Description: route description.
313+
Description string `json:"description"`
312314

315+
// Tags: tags of the Route.
316+
Tags []string `json:"tags"`
317+
318+
// VpcID: vPC the Route belongs to.
313319
VpcID string `json:"vpc_id"`
314320

321+
// Destination: destination of the Route.
315322
Destination scw.IPNet `json:"destination"`
316323

324+
// NexthopResourceID: ID of the nexthop resource.
317325
NexthopResourceID *string `json:"nexthop_resource_id"`
318326

327+
// NexthopPrivateNetworkID: ID of the nexthop private network.
319328
NexthopPrivateNetworkID *string `json:"nexthop_private_network_id"`
320329

321-
Tags []string `json:"tags"`
330+
// CreatedAt: date the Route was created.
331+
CreatedAt *time.Time `json:"created_at"`
322332

323-
Description string `json:"description"`
333+
// UpdatedAt: date the Route was last modified.
334+
UpdatedAt *time.Time `json:"updated_at"`
324335

325-
// Region: region to target. If none is passed will use default region from the config.
336+
// Region: region of the Route.
326337
Region scw.Region `json:"region"`
327338
}
328339

@@ -416,6 +427,30 @@ type CreatePrivateNetworkRequest struct {
416427
VpcID *string `json:"vpc_id,omitempty"`
417428
}
418429

430+
// CreateRouteRequest: create route request.
431+
type CreateRouteRequest struct {
432+
// Region: region to target. If none is passed will use default region from the config.
433+
Region scw.Region `json:"-"`
434+
435+
// Description: route description.
436+
Description string `json:"description"`
437+
438+
// Tags: tags of the Route.
439+
Tags []string `json:"tags"`
440+
441+
// VpcID: vPC the Route belongs to.
442+
VpcID string `json:"vpc_id"`
443+
444+
// Destination: destination of the Route.
445+
Destination scw.IPNet `json:"destination"`
446+
447+
// NexthopResourceID: ID of the nexthop resource.
448+
NexthopResourceID *string `json:"nexthop_resource_id,omitempty"`
449+
450+
// NexthopPrivateNetworkID: ID of the nexthop private network.
451+
NexthopPrivateNetworkID *string `json:"nexthop_private_network_id,omitempty"`
452+
}
453+
419454
// CreateVPCRequest: create vpc request.
420455
type CreateVPCRequest struct {
421456
// Region: region to target. If none is passed will use default region from the config.
@@ -443,6 +478,15 @@ type DeletePrivateNetworkRequest struct {
443478
PrivateNetworkID string `json:"-"`
444479
}
445480

481+
// DeleteRouteRequest: delete route request.
482+
type DeleteRouteRequest struct {
483+
// Region: region to target. If none is passed will use default region from the config.
484+
Region scw.Region `json:"-"`
485+
486+
// RouteID: route ID.
487+
RouteID string `json:"-"`
488+
}
489+
446490
// DeleteSubnetsRequest: delete subnets request.
447491
type DeleteSubnetsRequest struct {
448492
// Region: region to target. If none is passed will use default region from the config.
@@ -495,6 +539,15 @@ type GetPrivateNetworkRequest struct {
495539
PrivateNetworkID string `json:"-"`
496540
}
497541

542+
// GetRouteRequest: get route request.
543+
type GetRouteRequest struct {
544+
// Region: region to target. If none is passed will use default region from the config.
545+
Region scw.Region `json:"-"`
546+
547+
// RouteID: route ID.
548+
RouteID string `json:"-"`
549+
}
550+
498551
// GetVPCRequest: get vpc request.
499552
type GetVPCRequest struct {
500553
// Region: region to target. If none is passed will use default region from the config.
@@ -796,6 +849,30 @@ type UpdatePrivateNetworkRequest struct {
796849
Tags *[]string `json:"tags,omitempty"`
797850
}
798851

852+
// UpdateRouteRequest: update route request.
853+
type UpdateRouteRequest struct {
854+
// Region: region to target. If none is passed will use default region from the config.
855+
Region scw.Region `json:"-"`
856+
857+
// RouteID: route ID.
858+
RouteID string `json:"-"`
859+
860+
// Description: route description.
861+
Description *string `json:"description,omitempty"`
862+
863+
// Tags: tags of the Route.
864+
Tags *[]string `json:"tags,omitempty"`
865+
866+
// Destination: destination of the Route.
867+
Destination *scw.IPNet `json:"destination,omitempty"`
868+
869+
// NexthopResourceID: ID of the nexthop resource.
870+
NexthopResourceID *string `json:"nexthop_resource_id,omitempty"`
871+
872+
// NexthopPrivateNetworkID: ID of the nexthop private network.
873+
NexthopPrivateNetworkID *string `json:"nexthop_private_network_id,omitempty"`
874+
}
875+
799876
// UpdateVPCRequest: update vpc request.
800877
type UpdateVPCRequest struct {
801878
// Region: region to target. If none is passed will use default region from the config.
@@ -1451,6 +1528,134 @@ func (s *API) DeleteSubnets(req *DeleteSubnetsRequest, opts ...scw.RequestOption
14511528
return &resp, nil
14521529
}
14531530

1531+
// CreateRoute: Create a new custom Route.
1532+
func (s *API) CreateRoute(req *CreateRouteRequest, opts ...scw.RequestOption) (*Route, error) {
1533+
var err error
1534+
1535+
if req.Region == "" {
1536+
defaultRegion, _ := s.client.GetDefaultRegion()
1537+
req.Region = defaultRegion
1538+
}
1539+
1540+
if fmt.Sprint(req.Region) == "" {
1541+
return nil, errors.New("field Region cannot be empty in request")
1542+
}
1543+
1544+
scwReq := &scw.ScalewayRequest{
1545+
Method: "POST",
1546+
Path: "/vpc/v2/regions/" + fmt.Sprint(req.Region) + "/routes",
1547+
}
1548+
1549+
err = scwReq.SetBody(req)
1550+
if err != nil {
1551+
return nil, err
1552+
}
1553+
1554+
var resp Route
1555+
1556+
err = s.client.Do(scwReq, &resp, opts...)
1557+
if err != nil {
1558+
return nil, err
1559+
}
1560+
return &resp, nil
1561+
}
1562+
1563+
// GetRoute: Retrieve details of an existing Route, specified by its Route ID.
1564+
func (s *API) GetRoute(req *GetRouteRequest, opts ...scw.RequestOption) (*Route, error) {
1565+
var err error
1566+
1567+
if req.Region == "" {
1568+
defaultRegion, _ := s.client.GetDefaultRegion()
1569+
req.Region = defaultRegion
1570+
}
1571+
1572+
if fmt.Sprint(req.Region) == "" {
1573+
return nil, errors.New("field Region cannot be empty in request")
1574+
}
1575+
1576+
if fmt.Sprint(req.RouteID) == "" {
1577+
return nil, errors.New("field RouteID cannot be empty in request")
1578+
}
1579+
1580+
scwReq := &scw.ScalewayRequest{
1581+
Method: "GET",
1582+
Path: "/vpc/v2/regions/" + fmt.Sprint(req.Region) + "/routes/" + fmt.Sprint(req.RouteID) + "",
1583+
}
1584+
1585+
var resp Route
1586+
1587+
err = s.client.Do(scwReq, &resp, opts...)
1588+
if err != nil {
1589+
return nil, err
1590+
}
1591+
return &resp, nil
1592+
}
1593+
1594+
// UpdateRoute: Update parameters of the specified Route.
1595+
func (s *API) UpdateRoute(req *UpdateRouteRequest, opts ...scw.RequestOption) (*Route, error) {
1596+
var err error
1597+
1598+
if req.Region == "" {
1599+
defaultRegion, _ := s.client.GetDefaultRegion()
1600+
req.Region = defaultRegion
1601+
}
1602+
1603+
if fmt.Sprint(req.Region) == "" {
1604+
return nil, errors.New("field Region cannot be empty in request")
1605+
}
1606+
1607+
if fmt.Sprint(req.RouteID) == "" {
1608+
return nil, errors.New("field RouteID cannot be empty in request")
1609+
}
1610+
1611+
scwReq := &scw.ScalewayRequest{
1612+
Method: "PATCH",
1613+
Path: "/vpc/v2/regions/" + fmt.Sprint(req.Region) + "/routes/" + fmt.Sprint(req.RouteID) + "",
1614+
}
1615+
1616+
err = scwReq.SetBody(req)
1617+
if err != nil {
1618+
return nil, err
1619+
}
1620+
1621+
var resp Route
1622+
1623+
err = s.client.Do(scwReq, &resp, opts...)
1624+
if err != nil {
1625+
return nil, err
1626+
}
1627+
return &resp, nil
1628+
}
1629+
1630+
// DeleteRoute: Delete a Route specified by its Route ID.
1631+
func (s *API) DeleteRoute(req *DeleteRouteRequest, opts ...scw.RequestOption) error {
1632+
var err error
1633+
1634+
if req.Region == "" {
1635+
defaultRegion, _ := s.client.GetDefaultRegion()
1636+
req.Region = defaultRegion
1637+
}
1638+
1639+
if fmt.Sprint(req.Region) == "" {
1640+
return errors.New("field Region cannot be empty in request")
1641+
}
1642+
1643+
if fmt.Sprint(req.RouteID) == "" {
1644+
return errors.New("field RouteID cannot be empty in request")
1645+
}
1646+
1647+
scwReq := &scw.ScalewayRequest{
1648+
Method: "DELETE",
1649+
Path: "/vpc/v2/regions/" + fmt.Sprint(req.Region) + "/routes/" + fmt.Sprint(req.RouteID) + "",
1650+
}
1651+
1652+
err = s.client.Do(scwReq, nil, opts...)
1653+
if err != nil {
1654+
return err
1655+
}
1656+
return nil
1657+
}
1658+
14541659
type RoutesWithNexthopAPI struct {
14551660
client *scw.Client
14561661
}

0 commit comments

Comments
 (0)