From a86ff60313bee80a525b55a0b3ab8a66927627da Mon Sep 17 00:00:00 2001 From: Scaleway Bot Date: Thu, 3 Nov 2022 18:12:48 +0100 Subject: [PATCH] feat(baremetal): add support for private network (#1451) --- api/baremetal/v1/baremetal_sdk.go | 334 ++++++++++++++++++++++++++++++ 1 file changed, 334 insertions(+) diff --git a/api/baremetal/v1/baremetal_sdk.go b/api/baremetal/v1/baremetal_sdk.go index cdccd1f3d..a49a6f3e1 100644 --- a/api/baremetal/v1/baremetal_sdk.go +++ b/api/baremetal/v1/baremetal_sdk.go @@ -151,6 +151,42 @@ func (enum *ListServerEventsRequestOrderBy) UnmarshalJSON(data []byte) error { return nil } +type ListServerPrivateNetworksRequestOrderBy string + +const ( + // ListServerPrivateNetworksRequestOrderByCreatedAtAsc is [insert doc]. + ListServerPrivateNetworksRequestOrderByCreatedAtAsc = ListServerPrivateNetworksRequestOrderBy("created_at_asc") + // ListServerPrivateNetworksRequestOrderByCreatedAtDesc is [insert doc]. + ListServerPrivateNetworksRequestOrderByCreatedAtDesc = ListServerPrivateNetworksRequestOrderBy("created_at_desc") + // ListServerPrivateNetworksRequestOrderByUpdatedAtAsc is [insert doc]. + ListServerPrivateNetworksRequestOrderByUpdatedAtAsc = ListServerPrivateNetworksRequestOrderBy("updated_at_asc") + // ListServerPrivateNetworksRequestOrderByUpdatedAtDesc is [insert doc]. + ListServerPrivateNetworksRequestOrderByUpdatedAtDesc = ListServerPrivateNetworksRequestOrderBy("updated_at_desc") +) + +func (enum ListServerPrivateNetworksRequestOrderBy) String() string { + if enum == "" { + // return default value if empty + return "created_at_asc" + } + return string(enum) +} + +func (enum ListServerPrivateNetworksRequestOrderBy) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, enum)), nil +} + +func (enum *ListServerPrivateNetworksRequestOrderBy) UnmarshalJSON(data []byte) error { + tmp := "" + + if err := json.Unmarshal(data, &tmp); err != nil { + return err + } + + *enum = ListServerPrivateNetworksRequestOrderBy(ListServerPrivateNetworksRequestOrderBy(tmp).String()) + return nil +} + type ListServersRequestOrderBy string const ( @@ -427,6 +463,46 @@ func (enum *ServerPingStatus) UnmarshalJSON(data []byte) error { return nil } +type ServerPrivateNetworkStatus string + +const ( + // ServerPrivateNetworkStatusUnknown is [insert doc]. + ServerPrivateNetworkStatusUnknown = ServerPrivateNetworkStatus("unknown") + // ServerPrivateNetworkStatusAttaching is [insert doc]. + ServerPrivateNetworkStatusAttaching = ServerPrivateNetworkStatus("attaching") + // ServerPrivateNetworkStatusAttached is [insert doc]. + ServerPrivateNetworkStatusAttached = ServerPrivateNetworkStatus("attached") + // ServerPrivateNetworkStatusError is [insert doc]. + ServerPrivateNetworkStatusError = ServerPrivateNetworkStatus("error") + // ServerPrivateNetworkStatusDetaching is [insert doc]. + ServerPrivateNetworkStatusDetaching = ServerPrivateNetworkStatus("detaching") + // ServerPrivateNetworkStatusLocked is [insert doc]. + ServerPrivateNetworkStatusLocked = ServerPrivateNetworkStatus("locked") +) + +func (enum ServerPrivateNetworkStatus) String() string { + if enum == "" { + // return default value if empty + return "unknown" + } + return string(enum) +} + +func (enum ServerPrivateNetworkStatus) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, enum)), nil +} + +func (enum *ServerPrivateNetworkStatus) UnmarshalJSON(data []byte) error { + tmp := "" + + if err := json.Unmarshal(data, &tmp); err != nil { + return err + } + + *enum = ServerPrivateNetworkStatus(ServerPrivateNetworkStatus(tmp).String()) + return nil +} + type ServerStatus string const ( @@ -619,6 +695,12 @@ type ListServerEventsResponse struct { Events []*ServerEvent `json:"events"` } +type ListServerPrivateNetworksResponse struct { + ServerPrivateNetworks []*ServerPrivateNetwork `json:"server_private_networks"` + + TotalCount uint32 `json:"total_count"` +} + // ListServersResponse: list servers response type ListServersResponse struct { // TotalCount: total count of matching servers @@ -869,6 +951,28 @@ type ServerOption struct { ExpiresAt *time.Time `json:"expires_at"` } +// ServerPrivateNetwork: server private network +type ServerPrivateNetwork struct { + // ID: the private network ID + ID string `json:"id"` + // ProjectID: the private network project ID + ProjectID string `json:"project_id"` + // ServerID: the server ID + ServerID string `json:"server_id"` + // PrivateNetworkID: the private network ID + PrivateNetworkID string `json:"private_network_id"` + // Vlan: the VLAN ID associated to the private network + Vlan *uint32 `json:"vlan"` + // Status: the configuration status of the private network + // + // Default value: unknown + Status ServerPrivateNetworkStatus `json:"status"` + // CreatedAt: the private network creation date + CreatedAt *time.Time `json:"created_at"` + // UpdatedAt: the date the private network was last modified + UpdatedAt *time.Time `json:"updated_at"` +} + // ServerRescueServer: server. rescue server type ServerRescueServer struct { // User: rescue user name @@ -877,6 +981,10 @@ type ServerRescueServer struct { Password string `json:"password"` } +type SetServerPrivateNetworksResponse struct { + ServerPrivateNetworks []*ServerPrivateNetwork `json:"server_private_networks"` +} + // Setting: setting type Setting struct { // ID: ID of the setting @@ -2200,6 +2308,213 @@ func (s *API) GetOS(req *GetOSRequest, opts ...scw.RequestOption) (*OS, error) { return &resp, nil } +type AddServerPrivateNetworkRequest struct { + // Zone: + // + // Zone to target. If none is passed will use default zone from the config + Zone scw.Zone `json:"-"` + // ServerID: the ID of the server + ServerID string `json:"-"` + // PrivateNetworkID: the ID of the private network + PrivateNetworkID string `json:"private_network_id"` +} + +// AddServerPrivateNetwork: add a server to a private network +func (s *API) AddServerPrivateNetwork(req *AddServerPrivateNetworkRequest, opts ...scw.RequestOption) (*ServerPrivateNetwork, error) { + var err error + + if req.Zone == "" { + defaultZone, _ := s.client.GetDefaultZone() + req.Zone = defaultZone + } + + if fmt.Sprint(req.Zone) == "" { + return nil, errors.New("field Zone cannot be empty in request") + } + + if fmt.Sprint(req.ServerID) == "" { + return nil, errors.New("field ServerID cannot be empty in request") + } + + scwReq := &scw.ScalewayRequest{ + Method: "POST", + Path: "/baremetal/v1/zones/" + fmt.Sprint(req.Zone) + "/servers/" + fmt.Sprint(req.ServerID) + "/private-networks", + Headers: http.Header{}, + } + + err = scwReq.SetBody(req) + if err != nil { + return nil, err + } + + var resp ServerPrivateNetwork + + err = s.client.Do(scwReq, &resp, opts...) + if err != nil { + return nil, err + } + return &resp, nil +} + +type SetServerPrivateNetworksRequest struct { + // Zone: + // + // Zone to target. If none is passed will use default zone from the config + Zone scw.Zone `json:"-"` + // ServerID: the ID of the server + ServerID string `json:"-"` + // PrivateNetworkIDs: the IDs of the private networks + PrivateNetworkIDs []string `json:"private_network_ids"` +} + +// SetServerPrivateNetworks: set multiple private networks on a server +func (s *API) SetServerPrivateNetworks(req *SetServerPrivateNetworksRequest, opts ...scw.RequestOption) (*SetServerPrivateNetworksResponse, error) { + var err error + + if req.Zone == "" { + defaultZone, _ := s.client.GetDefaultZone() + req.Zone = defaultZone + } + + if fmt.Sprint(req.Zone) == "" { + return nil, errors.New("field Zone cannot be empty in request") + } + + if fmt.Sprint(req.ServerID) == "" { + return nil, errors.New("field ServerID cannot be empty in request") + } + + scwReq := &scw.ScalewayRequest{ + Method: "PUT", + Path: "/baremetal/v1/zones/" + fmt.Sprint(req.Zone) + "/servers/" + fmt.Sprint(req.ServerID) + "/private-networks", + Headers: http.Header{}, + } + + err = scwReq.SetBody(req) + if err != nil { + return nil, err + } + + var resp SetServerPrivateNetworksResponse + + err = s.client.Do(scwReq, &resp, opts...) + if err != nil { + return nil, err + } + return &resp, nil +} + +type ListServerPrivateNetworksRequest struct { + // Zone: + // + // Zone to target. If none is passed will use default zone from the config + Zone scw.Zone `json:"-"` + // OrderBy: the sort order for the returned private networks + // + // Default value: created_at_asc + OrderBy ListServerPrivateNetworksRequestOrderBy `json:"-"` + // Page: the page number for the returned private networks + Page *int32 `json:"-"` + // PageSize: the maximum number of private networks per page + PageSize *uint32 `json:"-"` + // ServerID: filter private networks by server ID + ServerID *string `json:"-"` + // PrivateNetworkID: filter private networks by private network ID + PrivateNetworkID *string `json:"-"` + // OrganizationID: filter private networks by organization ID + OrganizationID *string `json:"-"` + // ProjectID: filter private networks by project ID + ProjectID *string `json:"-"` +} + +// ListServerPrivateNetworks: list the private networks of a server +func (s *API) ListServerPrivateNetworks(req *ListServerPrivateNetworksRequest, opts ...scw.RequestOption) (*ListServerPrivateNetworksResponse, error) { + var err error + + if req.Zone == "" { + defaultZone, _ := s.client.GetDefaultZone() + req.Zone = defaultZone + } + + defaultPageSize, exist := s.client.GetDefaultPageSize() + if (req.PageSize == nil || *req.PageSize == 0) && exist { + req.PageSize = &defaultPageSize + } + + query := url.Values{} + parameter.AddToQuery(query, "order_by", req.OrderBy) + parameter.AddToQuery(query, "page", req.Page) + parameter.AddToQuery(query, "page_size", req.PageSize) + parameter.AddToQuery(query, "server_id", req.ServerID) + parameter.AddToQuery(query, "private_network_id", req.PrivateNetworkID) + parameter.AddToQuery(query, "organization_id", req.OrganizationID) + parameter.AddToQuery(query, "project_id", req.ProjectID) + + if fmt.Sprint(req.Zone) == "" { + return nil, errors.New("field Zone cannot be empty in request") + } + + scwReq := &scw.ScalewayRequest{ + Method: "GET", + Path: "/baremetal/v1/zones/" + fmt.Sprint(req.Zone) + "/server-private-networks", + Query: query, + Headers: http.Header{}, + } + + var resp ListServerPrivateNetworksResponse + + err = s.client.Do(scwReq, &resp, opts...) + if err != nil { + return nil, err + } + return &resp, nil +} + +type DeleteServerPrivateNetworkRequest struct { + // Zone: + // + // Zone to target. If none is passed will use default zone from the config + Zone scw.Zone `json:"-"` + // ServerID: the ID of the server + ServerID string `json:"-"` + // PrivateNetworkID: the ID of the private network + PrivateNetworkID string `json:"-"` +} + +// DeleteServerPrivateNetwork: delete a private network +func (s *API) DeleteServerPrivateNetwork(req *DeleteServerPrivateNetworkRequest, opts ...scw.RequestOption) error { + var err error + + if req.Zone == "" { + defaultZone, _ := s.client.GetDefaultZone() + req.Zone = defaultZone + } + + if fmt.Sprint(req.Zone) == "" { + return errors.New("field Zone cannot be empty in request") + } + + if fmt.Sprint(req.ServerID) == "" { + return errors.New("field ServerID cannot be empty in request") + } + + if fmt.Sprint(req.PrivateNetworkID) == "" { + return errors.New("field PrivateNetworkID cannot be empty in request") + } + + scwReq := &scw.ScalewayRequest{ + Method: "DELETE", + Path: "/baremetal/v1/zones/" + fmt.Sprint(req.Zone) + "/servers/" + fmt.Sprint(req.ServerID) + "/private-networks/" + fmt.Sprint(req.PrivateNetworkID) + "", + Headers: http.Header{}, + } + + err = s.client.Do(scwReq, nil, opts...) + if err != nil { + return err + } + return nil +} + // UnsafeGetTotalCount should not be used // Internal usage only func (r *ListServersResponse) UnsafeGetTotalCount() uint32 { @@ -2313,3 +2628,22 @@ func (r *ListOSResponse) UnsafeAppend(res interface{}) (uint32, error) { r.TotalCount += uint32(len(results.Os)) return uint32(len(results.Os)), nil } + +// UnsafeGetTotalCount should not be used +// Internal usage only +func (r *ListServerPrivateNetworksResponse) UnsafeGetTotalCount() uint32 { + return r.TotalCount +} + +// UnsafeAppend should not be used +// Internal usage only +func (r *ListServerPrivateNetworksResponse) UnsafeAppend(res interface{}) (uint32, error) { + results, ok := res.(*ListServerPrivateNetworksResponse) + if !ok { + return 0, errors.New("%T type cannot be appended to type %T", res, r) + } + + r.ServerPrivateNetworks = append(r.ServerPrivateNetworks, results.ServerPrivateNetworks...) + r.TotalCount += uint32(len(results.ServerPrivateNetworks)) + return uint32(len(results.ServerPrivateNetworks)), nil +}