Skip to content

Commit

Permalink
Add support for backup gateways, agent-level props
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiol committed Nov 12, 2024
1 parent a6780b5 commit c4d9436
Show file tree
Hide file tree
Showing 8 changed files with 874 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/service/ecloud/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,21 @@ type VPNGatewayUserNotFoundError struct {
func (e *VPNGatewayUserNotFoundError) Error() string {
return fmt.Sprintf("VPN gateway user not found with ID [%s]", e.ID)
}

// BackupGatewaySpecificationNotFoundError represents a VPN gateway specification not found error
type BackupGatewaySpecificationNotFoundError struct {
ID string
}

func (e *BackupGatewaySpecificationNotFoundError) Error() string {
return fmt.Sprintf("Backup gateway specification not found with ID [%s]", e.ID)
}

// BackupGatewayNotFoundError represents a backup gateway not found error
type BackupGatewayNotFoundError struct {
ID string
}

func (e *BackupGatewayNotFoundError) Error() string {
return fmt.Sprintf("Backup gateway not found with ID [%s]", e.ID)
}
25 changes: 25 additions & 0 deletions pkg/service/ecloud/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,28 @@ type VPNGatewayUser struct {
CreatedAt connection.DateTime `json:"created_at"`
UpdatedAt connection.DateTime `json:"updated_at"`
}

// BackupGatewaySpecification represents a Backup Gateway specification
type BackupGatewaySpecification struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CPU int `json:"cpu"`
RAM int `json:"ram"`
IopsID string `json:"iops_id"`
VolumeCapacity int `json:"volume_capacity"`
ImageID string `json:"image_id"`
}

// BackupGateway represents a Backup Gateway
type BackupGateway struct {
ID string `json:"id"`
VPCID string `json:"vpc_id"`
Name string `json:"name"`
AvailabilityZoneID string `json:"availability_zone_id"`
GatewaySpecID string `json:"gateway_spec_id"`
Sync ResourceSync `json:"sync"`
Task ResourceTask `json:"task"`
CreatedAt connection.DateTime `json:"created_at"`
UpdatedAt connection.DateTime `json:"updated_at"`
}
11 changes: 11 additions & 0 deletions pkg/service/ecloud/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,14 @@ type PatchVPNGatewayUserRequest struct {
Name string `json:"name,omitempty"`
Password string `json:"password,omitempty"`
}

type CreateBackupGatewayRequest struct {
Name string `json:"name,omitempty"`
VPCID string `json:"vpc_id"`
RouterID string `json:"router_id"`
GatewaySpecID string `json:"gateway_spec_id"`
}

type PatchBackupGatewayRequest struct {
Name string `json:"name"`
}
15 changes: 15 additions & 0 deletions pkg/service/ecloud/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,21 @@ type ECloudService interface {
GetIOPSTiers(parameters connection.APIRequestParameters) ([]IOPSTier, error)
GetIOPSTiersPaginated(parameters connection.APIRequestParameters) (*connection.Paginated[IOPSTier], error)
GetIOPSTier(iopsID string) (IOPSTier, error)

// Backup Gateway Specifications

Check notice on line 489 in pkg/service/ecloud/service.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Comment of exported element starts with the incorrect name

Comment should have the following format 'GetBackupGatewaySpecifications ...' (with an optional leading article)
GetBackupGatewaySpecifications(parameters connection.APIRequestParameters) ([]BackupGatewaySpecification, error)
GetBackupGatewaySpecificationsPaginated(parameters connection.APIRequestParameters) (*connection.Paginated[BackupGatewaySpecification], error)
GetBackupGatewaySpecification(specificationID string) (BackupGatewaySpecification, error)

// Backup Gateways

Check notice on line 494 in pkg/service/ecloud/service.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Comment of exported element starts with the incorrect name

Comment should have the following format 'GetBackupGateways ...' (with an optional leading article)
GetBackupGateways(parameters connection.APIRequestParameters) ([]BackupGateway, error)
GetBackupGatewaysPaginated(parameters connection.APIRequestParameters) (*connection.Paginated[BackupGateway], error)
GetBackupGateway(gatewayID string) (BackupGateway, error)
CreateBackupGateway(req CreateBackupGatewayRequest) (TaskReference, error)
PatchBackupGateway(gatewayID string, req PatchBackupGatewayRequest) (TaskReference, error)
DeleteBackupGateway(gatewayID string) (string, error)
GetBackupGatewayTasks(gatewayID string, parameters connection.APIRequestParameters) ([]Task, error)
GetBackupGatewayTasksPaginated(gatewayID string, parameters connection.APIRequestParameters) (*connection.Paginated[Task], error)
}

// Service implements ECloudService for managing
Expand Down
168 changes: 168 additions & 0 deletions pkg/service/ecloud/service_backupgateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package ecloud

import (
"fmt"

"github.com/ans-group/sdk-go/pkg/connection"
)

// GetBackupGateways retrieves a list of backup gateways
func (s *Service) GetBackupGateways(parameters connection.APIRequestParameters) ([]BackupGateway, error) {
return connection.InvokeRequestAll(s.GetBackupGatewaysPaginated, parameters)
}

// GetBackupGatewaysPaginated retrieves a paginated list of backup gateways
func (s *Service) GetBackupGatewaysPaginated(parameters connection.APIRequestParameters) (*connection.Paginated[BackupGateway], error) {
body, err := s.getBackupGatewaysPaginatedResponseBody(parameters)
return connection.NewPaginated(body, parameters, s.GetBackupGatewaysPaginated), err
}

func (s *Service) getBackupGatewaysPaginatedResponseBody(parameters connection.APIRequestParameters) (*connection.APIResponseBodyData[[]BackupGateway], error) {
body := &connection.APIResponseBodyData[[]BackupGateway]{}

response, err := s.connection.Get("/ecloud/v2/backup-gateways", parameters)
if err != nil {
return body, err
}

return body, response.HandleResponse(body, nil)
}

// GetBackupGateway retrieves a single backup gateway by ID
func (s *Service) GetBackupGateway(gatewayID string) (BackupGateway, error) {
body, err := s.getBackupGatewayResponseBody(gatewayID)

return body.Data, err
}

func (s *Service) getBackupGatewayResponseBody(gatewayID string) (*connection.APIResponseBodyData[BackupGateway], error) {
body := &connection.APIResponseBodyData[BackupGateway]{}

if gatewayID == "" {
return body, fmt.Errorf("invalid backup gateway id")
}

response, err := s.connection.Get(fmt.Sprintf("/ecloud/v2/backup-gateways/%s", gatewayID), connection.APIRequestParameters{})
if err != nil {
return body, err
}

return body, response.HandleResponse(body, func(resp *connection.APIResponse) error {
if response.StatusCode == 404 {
return &BackupGatewayNotFoundError{ID: gatewayID}
}

return nil
})
}

// CreateBackupGateway creates a new backup gateway
func (s *Service) CreateBackupGateway(req CreateBackupGatewayRequest) (TaskReference, error) {
body, err := s.createBackupGatewayResponseBody(req)

return body.Data, err
}

func (s *Service) createBackupGatewayResponseBody(req CreateBackupGatewayRequest) (*connection.APIResponseBodyData[TaskReference], error) {
body := &connection.APIResponseBodyData[TaskReference]{}

response, err := s.connection.Post("/ecloud/v2/backup-gateways", &req)
if err != nil {
return body, err
}

return body, response.HandleResponse(body, nil)
}

// PatchBackupGateway patches a backup gateway
func (s *Service) PatchBackupGateway(gatewayID string, req PatchBackupGatewayRequest) (TaskReference, error) {
body, err := s.patchBackupGatewayResponseBody(gatewayID, req)

return body.Data, err
}

func (s *Service) patchBackupGatewayResponseBody(gatewayID string, req PatchBackupGatewayRequest) (*connection.APIResponseBodyData[TaskReference], error) {
body := &connection.APIResponseBodyData[TaskReference]{}

if gatewayID == "" {
return body, fmt.Errorf("invalid gateway id")
}

response, err := s.connection.Patch(fmt.Sprintf("/ecloud/v2/backup-gateways/%s", gatewayID), &req)
if err != nil {
return body, err
}

return body, response.HandleResponse(body, func(resp *connection.APIResponse) error {
if response.StatusCode == 404 {
return &BackupGatewayNotFoundError{ID: gatewayID}
}

return nil
})
}

// DeleteBackupGateway deletes a backup gateway
func (s *Service) DeleteBackupGateway(gatewayID string) (string, error) {
body, err := s.deleteBackupGatewayResponseBody(gatewayID)

return body.Data.TaskID, err
}

func (s *Service) deleteBackupGatewayResponseBody(gatewayID string) (*connection.APIResponseBodyData[TaskReference], error) {
body := &connection.APIResponseBodyData[TaskReference]{}

if gatewayID == "" {
return body, fmt.Errorf("invalid gateway id")
}

response, err := s.connection.Delete(fmt.Sprintf("/ecloud/v2/backup-gateways/%s", gatewayID), nil)
if err != nil {
return body, err
}

return body, response.HandleResponse(body, func(resp *connection.APIResponse) error {
if response.StatusCode == 404 {
return &BackupGatewayNotFoundError{ID: gatewayID}
}

return nil
})
}

// GetBackupGatewayTasks retrieves a list of backup gateway tasks
func (s *Service) GetBackupGatewayTasks(gatewayID string, parameters connection.APIRequestParameters) ([]Task, error) {
return connection.InvokeRequestAll(func(p connection.APIRequestParameters) (*connection.Paginated[Task], error) {
return s.GetBackupGatewayTasksPaginated(gatewayID, p)
}, parameters)
}

// GetBackupGatewayTasksPaginated retrieves a paginated list of backup gateway tasks
func (s *Service) GetBackupGatewayTasksPaginated(gatewayID string, parameters connection.APIRequestParameters) (*connection.Paginated[Task], error) {
body, err := s.getBackupGatewayTasksPaginatedResponseBody(gatewayID, parameters)

return connection.NewPaginated(body, parameters, func(p connection.APIRequestParameters) (*connection.Paginated[Task], error) {
return s.GetBackupGatewayTasksPaginated(gatewayID, p)
}), err
}

func (s *Service) getBackupGatewayTasksPaginatedResponseBody(gatewayID string, parameters connection.APIRequestParameters) (*connection.APIResponseBodyData[[]Task], error) {
body := &connection.APIResponseBodyData[[]Task]{}

if gatewayID == "" {
return body, fmt.Errorf("invalid backup gateway id")
}

response, err := s.connection.Get(fmt.Sprintf("/ecloud/v2/backup-gateways/%s/tasks", gatewayID), parameters)
if err != nil {
return body, err
}

return body, response.HandleResponse(body, func(resp *connection.APIResponse) error {
if response.StatusCode == 404 {
return &BackupGatewayNotFoundError{ID: gatewayID}
}

return nil
})
}
57 changes: 57 additions & 0 deletions pkg/service/ecloud/service_backupgateway_specs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ecloud

import (
"fmt"

"github.com/ans-group/sdk-go/pkg/connection"
)

// GetBackupGatewaySpecifications retrieves a list of Backup gateway specifications
func (s *Service) GetBackupGatewaySpecifications(parameters connection.APIRequestParameters) ([]BackupGatewaySpecification, error) {
return connection.InvokeRequestAll(s.GetBackupGatewaySpecificationsPaginated, parameters)
}

// GetBackupGatewaySpecificationsPaginated retrieves a paginated list of Backup gateway specifications
func (s *Service) GetBackupGatewaySpecificationsPaginated(parameters connection.APIRequestParameters) (*connection.Paginated[BackupGatewaySpecification], error) {
body, err := s.getBackupGatewaySpecificationsPaginatedResponseBody(parameters)
return connection.NewPaginated(body, parameters, s.GetBackupGatewaySpecificationsPaginated), err
}

func (s *Service) getBackupGatewaySpecificationsPaginatedResponseBody(parameters connection.APIRequestParameters) (*connection.APIResponseBodyData[[]BackupGatewaySpecification], error) {
body := &connection.APIResponseBodyData[[]BackupGatewaySpecification]{}

response, err := s.connection.Get("/ecloud/v2/backup-gateway-specs", parameters)
if err != nil {
return body, err
}

return body, response.HandleResponse(body, nil)
}

// GetBackupGatewaySpecification retrieves a single Backup gateway specification by ID
func (s *Service) GetBackupGatewaySpecification(specificationID string) (BackupGatewaySpecification, error) {
body, err := s.getBackupGatewaySpecificationResponseBody(specificationID)

return body.Data, err
}

func (s *Service) getBackupGatewaySpecificationResponseBody(specificationID string) (*connection.APIResponseBodyData[BackupGatewaySpecification], error) {
body := &connection.APIResponseBodyData[BackupGatewaySpecification]{}

if specificationID == "" {
return body, fmt.Errorf("invalid backup gateway specification id")
}

response, err := s.connection.Get(fmt.Sprintf("/ecloud/v2/backup-gateway-specs/%s", specificationID), connection.APIRequestParameters{})
if err != nil {
return body, err
}

return body, response.HandleResponse(body, func(resp *connection.APIResponse) error {
if response.StatusCode == 404 {
return &BackupGatewaySpecificationNotFoundError{ID: specificationID}
}

return nil
})
}
Loading

0 comments on commit c4d9436

Please sign in to comment.