Skip to content

Commit 3a1f098

Browse files
authored
feat(ipam): add support for AttachIP, DetachIP, MoveIP for custom resource (#2175)
1 parent f1b5ded commit 3a1f098

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

api/ipam/v1/ipam_sdk.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type ResourceType string
8888

8989
const (
9090
ResourceTypeUnknownType = ResourceType("unknown_type")
91+
ResourceTypeCustom = ResourceType("custom")
9192
ResourceTypeInstanceServer = ResourceType("instance_server")
9293
ResourceTypeInstanceIP = ResourceType("instance_ip")
9394
ResourceTypeInstancePrivateNic = ResourceType("instance_private_nic")
@@ -115,6 +116,7 @@ func (enum ResourceType) String() string {
115116
func (enum ResourceType) Values() []ResourceType {
116117
return []ResourceType{
117118
"unknown_type",
119+
"custom",
118120
"instance_server",
119121
"instance_ip",
120122
"instance_private_nic",
@@ -187,6 +189,15 @@ type Source struct {
187189
SubnetID *string `json:"subnet_id,omitempty"`
188190
}
189191

192+
// CustomResource: custom resource.
193+
type CustomResource struct {
194+
// MacAddress: mAC address of the custom resource.
195+
MacAddress string `json:"mac_address"`
196+
197+
// Name: when the resource is in a Private Network, a DNS record is available to resolve the resource name.
198+
Name *string `json:"name"`
199+
}
200+
190201
// IP: ip.
191202
type IP struct {
192203
// ID: IP ID.
@@ -226,6 +237,18 @@ type IP struct {
226237
Zone *scw.Zone `json:"zone"`
227238
}
228239

240+
// AttachIPRequest: attach ip request.
241+
type AttachIPRequest struct {
242+
// Region: region to target. If none is passed will use default region from the config.
243+
Region scw.Region `json:"-"`
244+
245+
// IPID: IP ID.
246+
IPID string `json:"-"`
247+
248+
// Resource: custom resource to be attached to the IP.
249+
Resource *CustomResource `json:"resource"`
250+
}
251+
229252
// BookIPRequest: book ip request.
230253
type BookIPRequest struct {
231254
// Region: region to target. If none is passed will use default region from the config.
@@ -245,6 +268,18 @@ type BookIPRequest struct {
245268

246269
// Tags: tags for the IP.
247270
Tags []string `json:"tags"`
271+
272+
// Resource: custom resource to attach to the IP being booked. An example of a custom resource is a virtual machine hosted on an Elastic Metal server, or an additional user network interface on an Instance. Do not use this for attaching IP addresses to standard Scaleway resources, as it will fail - instead, see the relevant product API for an equivalent method.
273+
Resource *CustomResource `json:"resource,omitempty"`
274+
}
275+
276+
// DetachIPRequest: detach ip request.
277+
type DetachIPRequest struct {
278+
// Region: region to target. If none is passed will use default region from the config.
279+
Region scw.Region `json:"-"`
280+
281+
// IPID: IP ID.
282+
IPID string `json:"-"`
248283
}
249284

250285
// GetIPRequest: get ip request.
@@ -344,6 +379,18 @@ func (r *ListIPsResponse) UnsafeAppend(res interface{}) (uint64, error) {
344379
return uint64(len(results.IPs)), nil
345380
}
346381

382+
// MoveIPRequest: move ip request.
383+
type MoveIPRequest struct {
384+
// Region: region to target. If none is passed will use default region from the config.
385+
Region scw.Region `json:"-"`
386+
387+
// IPID: IP ID.
388+
IPID string `json:"-"`
389+
390+
// Resource: custom resource to be attached to the IP.
391+
Resource *CustomResource `json:"resource,omitempty"`
392+
}
393+
347394
// ReleaseIPRequest: release ip request.
348395
type ReleaseIPRequest struct {
349396
// Region: region to target. If none is passed will use default region from the config.
@@ -610,3 +657,111 @@ func (s *API) ListIPs(req *ListIPsRequest, opts ...scw.RequestOption) (*ListIPsR
610657
}
611658
return &resp, nil
612659
}
660+
661+
// AttachIP: Attach an existing IP from a Private Network subnet to a custom, named resource via its MAC address. An example of a custom resource is a virtual machine hosted on an Elastic Metal server, or an additional user network interface on an Instance. Do not use this method for attaching IP addresses to standard Scaleway resources as it will fail - see the relevant product API for an equivalent method.
662+
func (s *API) AttachIP(req *AttachIPRequest, opts ...scw.RequestOption) (*IP, error) {
663+
var err error
664+
665+
if req.Region == "" {
666+
defaultRegion, _ := s.client.GetDefaultRegion()
667+
req.Region = defaultRegion
668+
}
669+
670+
if fmt.Sprint(req.Region) == "" {
671+
return nil, errors.New("field Region cannot be empty in request")
672+
}
673+
674+
if fmt.Sprint(req.IPID) == "" {
675+
return nil, errors.New("field IPID cannot be empty in request")
676+
}
677+
678+
scwReq := &scw.ScalewayRequest{
679+
Method: "POST",
680+
Path: "/ipam/v1/regions/" + fmt.Sprint(req.Region) + "/ips/" + fmt.Sprint(req.IPID) + "/attach",
681+
}
682+
683+
err = scwReq.SetBody(req)
684+
if err != nil {
685+
return nil, err
686+
}
687+
688+
var resp IP
689+
690+
err = s.client.Do(scwReq, &resp, opts...)
691+
if err != nil {
692+
return nil, err
693+
}
694+
return &resp, nil
695+
}
696+
697+
// DetachIP: Detach a private IP from a custom resource. An example of a custom resource is a virtual machine hosted on an Elastic Metal server. Do not use this method for attaching IP addresses to standard Scaleway resources (e.g. Instances, Load Balancers) as it will fail - see the relevant product API for an equivalent method.
698+
func (s *API) DetachIP(req *DetachIPRequest, opts ...scw.RequestOption) (*IP, error) {
699+
var err error
700+
701+
if req.Region == "" {
702+
defaultRegion, _ := s.client.GetDefaultRegion()
703+
req.Region = defaultRegion
704+
}
705+
706+
if fmt.Sprint(req.Region) == "" {
707+
return nil, errors.New("field Region cannot be empty in request")
708+
}
709+
710+
if fmt.Sprint(req.IPID) == "" {
711+
return nil, errors.New("field IPID cannot be empty in request")
712+
}
713+
714+
scwReq := &scw.ScalewayRequest{
715+
Method: "POST",
716+
Path: "/ipam/v1/regions/" + fmt.Sprint(req.Region) + "/ips/" + fmt.Sprint(req.IPID) + "/detach",
717+
}
718+
719+
err = scwReq.SetBody(req)
720+
if err != nil {
721+
return nil, err
722+
}
723+
724+
var resp IP
725+
726+
err = s.client.Do(scwReq, &resp, opts...)
727+
if err != nil {
728+
return nil, err
729+
}
730+
return &resp, nil
731+
}
732+
733+
// MoveIP: Move an existing private IP from one custom resource (e.g. a virtual machine hosted on an Elastic Metal server) to another custom resource. This will detach it from the first resource, and attach it to the second. Do not use this method for moving IP addresses between standard Scaleway resources (e.g. Instances, Load Balancers) as it will fail - see the relevant product API for an equivalent method.
734+
func (s *API) MoveIP(req *MoveIPRequest, opts ...scw.RequestOption) (*IP, error) {
735+
var err error
736+
737+
if req.Region == "" {
738+
defaultRegion, _ := s.client.GetDefaultRegion()
739+
req.Region = defaultRegion
740+
}
741+
742+
if fmt.Sprint(req.Region) == "" {
743+
return nil, errors.New("field Region cannot be empty in request")
744+
}
745+
746+
if fmt.Sprint(req.IPID) == "" {
747+
return nil, errors.New("field IPID cannot be empty in request")
748+
}
749+
750+
scwReq := &scw.ScalewayRequest{
751+
Method: "POST",
752+
Path: "/ipam/v1/regions/" + fmt.Sprint(req.Region) + "/ips/" + fmt.Sprint(req.IPID) + "/move",
753+
}
754+
755+
err = scwReq.SetBody(req)
756+
if err != nil {
757+
return nil, err
758+
}
759+
760+
var resp IP
761+
762+
err = s.client.Do(scwReq, &resp, opts...)
763+
if err != nil {
764+
return nil, err
765+
}
766+
return &resp, nil
767+
}

api/ipam/v1alpha1/ipam_sdk.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type ResourceType string
8888

8989
const (
9090
ResourceTypeUnknownType = ResourceType("unknown_type")
91+
ResourceTypeCustom = ResourceType("custom")
9192
ResourceTypeInstanceServer = ResourceType("instance_server")
9293
ResourceTypeInstanceIP = ResourceType("instance_ip")
9394
ResourceTypeInstancePrivateNic = ResourceType("instance_private_nic")
@@ -115,6 +116,7 @@ func (enum ResourceType) String() string {
115116
func (enum ResourceType) Values() []ResourceType {
116117
return []ResourceType{
117118
"unknown_type",
119+
"custom",
118120
"instance_server",
119121
"instance_ip",
120122
"instance_private_nic",

0 commit comments

Comments
 (0)