Skip to content

Commit

Permalink
Merge pull request #94 from ukfast/volume-groups
Browse files Browse the repository at this point in the history
add volume group crud operations
  • Loading branch information
Overglazed authored Sep 22, 2021
2 parents 01605d7 + a3ba4bd commit 21356b5
Show file tree
Hide file tree
Showing 8 changed files with 707 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/service/ecloud/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ func (e *NetworkRulePortNotFoundError) Error() string {
return fmt.Sprintf("Network rule port not found with ID [%s]", e.ID)
}

// VolumeGroupNotFoundError indicates a volume group was not found
type VolumeGroupNotFoundError struct {
ID string
}

func (e *VolumeGroupNotFoundError) Error() string {
return fmt.Sprintf("Volume group not found with ID [%s]", e.ID)
}

// VPNEndpointNotFoundError indicates a VPN endpoint was not found
type VPNEndpointNotFoundError struct {
ID string
Expand Down
18 changes: 18 additions & 0 deletions pkg/service/ecloud/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,24 @@ type NetworkRulePort struct {
UpdatedAt connection.DateTime `json:"updated_at"`
}

// VolumeGroup represents an eCloud volume group resource
// +genie:model_response
// +genie:model_paginated
type VolumeGroup struct {
ID string `json:"id"`
Name string `json:"name"`
VPCID string `json:"vpc_id"`
AvailabilityZoneID string `json:"availability_zone_id"`
Usage VolumeGroupUsage `json:"usage"`
Sync ResourceSync `json:"sync"`
CreatedAt connection.DateTime `json:"created_at"`
UpdatedAt connection.DateTime `json:"updated_at"`
}

type VolumeGroupUsage struct {
Volumes int `json:"volumes"`
}

// VPNProfileGroup represents an eCloud VPN profile group
// +genie:model_response
// +genie:model_paginated
Expand Down
14 changes: 14 additions & 0 deletions pkg/service/ecloud/model_paginated_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,20 @@ func NewPaginatedNetworkRulePort(getFunc connection.PaginatedGetFunc, parameters
}
}

// PaginatedVolumeGroup represents a paginated collection of VolumeGroup
type PaginatedVolumeGroup struct {
*connection.PaginatedBase
Items []VolumeGroup
}

// NewPaginatedVolumeGroup returns a pointer to an initialized PaginatedVolumeGroup struct
func NewPaginatedVolumeGroup(getFunc connection.PaginatedGetFunc, parameters connection.APIRequestParameters, pagination connection.APIResponseMetadataPagination, items []VolumeGroup) *PaginatedVolumeGroup {
return &PaginatedVolumeGroup{
Items: items,
PaginatedBase: connection.NewPaginatedBase(parameters, pagination, getFunc),
}
}

// PaginatedVPNProfileGroup represents a paginated collection of VPNProfileGroup
type PaginatedVPNProfileGroup struct {
*connection.PaginatedBase
Expand Down
12 changes: 12 additions & 0 deletions pkg/service/ecloud/model_response_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,18 @@ type GetNetworkRulePortResponseBody struct {
Data NetworkRulePort `json:"data"`
}

// GetVolumeGroupSliceResponseBody represents an API response body containing []VolumeGroup data
type GetVolumeGroupSliceResponseBody struct {
connection.APIResponseBody
Data []VolumeGroup `json:"data"`
}

// GetVolumeGroupResponseBody represents an API response body containing VolumeGroup data
type GetVolumeGroupResponseBody struct {
connection.APIResponseBody
Data VolumeGroup `json:"data"`
}

// GetVPNProfileGroupSliceResponseBody represents an API response body containing []VPNProfileGroup data
type GetVPNProfileGroupSliceResponseBody struct {
connection.APIResponseBody
Expand Down
12 changes: 12 additions & 0 deletions pkg/service/ecloud/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,15 @@ type PatchVPNServiceRequest struct {
type MigrateInstanceRequest struct {
HostGroupID string `json:"host_group_id,omitempty"`
}

// CreateVolumeGroupRequest represents a request to create a volume group
type CreateVolumeGroupRequest struct {
Name string `json:"name,omitempty"`
VPCID string `json:"vpc_id"`
AvailabilityZoneID string `json:"availability_zone_id"`
}

// PatchVolumeGroupRequest represents a request to patch a volume group
type PatchVolumeGroupRequest struct {
Name string `json:"name,omitempty"`
}
10 changes: 10 additions & 0 deletions pkg/service/ecloud/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,16 @@ type ECloudService interface {
PatchNetworkRulePort(ruleID string, req PatchNetworkRulePortRequest) (TaskReference, error)
DeleteNetworkRulePort(ruleID string) (string, error)

//Volume Groups
GetVolumeGroups(parameters connection.APIRequestParameters) ([]VolumeGroup, error)
GetVolumeGroupsPaginated(parameters connection.APIRequestParameters) (*PaginatedVolumeGroup, error)
GetVolumeGroup(groupID string) (VolumeGroup, error)
CreateVolumeGroup(req CreateVolumeGroupRequest) (TaskReference, error)
PatchVolumeGroup(groupID string, patch PatchVolumeGroupRequest) (TaskReference, error)
DeleteVolumeGroup(groupID string) (string, error)
GetVolumeGroupVolumes(groupID string, parameters connection.APIRequestParameters) ([]Volume, error)
GetVolumeGroupVolumesPaginated(groupID string, parameters connection.APIRequestParameters) (*PaginatedVolume, error)

// VPN Endpoint
GetVPNEndpoints(parameters connection.APIRequestParameters) ([]VPNEndpoint, error)
GetVPNEndpointsPaginated(parameters connection.APIRequestParameters) (*PaginatedVPNEndpoint, error)
Expand Down
193 changes: 193 additions & 0 deletions pkg/service/ecloud/service_volumegroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package ecloud

import (
"fmt"

"github.com/ukfast/sdk-go/pkg/connection"
)

// GetVolumeGroups retrieves a list of volume groups
func (s *Service) GetVolumeGroups(parameters connection.APIRequestParameters) ([]VolumeGroup, error) {
var volGroups []VolumeGroup

getFunc := func(p connection.APIRequestParameters) (connection.Paginated, error) {
return s.GetVolumeGroupsPaginated(p)
}

responseFunc := func(response connection.Paginated) {
for _, volumeGroup := range response.(*PaginatedVolumeGroup).Items {
volGroups = append(volGroups, volumeGroup)
}
}

return volGroups, connection.InvokeRequestAll(getFunc, responseFunc, parameters)
}

// GetVolumeGroupsPaginated retrieves a paginated list of volume groups
func (s *Service) GetVolumeGroupsPaginated(parameters connection.APIRequestParameters) (*PaginatedVolumeGroup, error) {
body, err := s.getVolumeGroupsPaginatedResponseBody(parameters)

return NewPaginatedVolumeGroup(func(p connection.APIRequestParameters) (connection.Paginated, error) {
return s.GetVolumeGroupsPaginated(p)
}, parameters, body.Metadata.Pagination, body.Data), err
}

func (s *Service) getVolumeGroupsPaginatedResponseBody(parameters connection.APIRequestParameters) (*GetVolumeGroupSliceResponseBody, error) {
body := &GetVolumeGroupSliceResponseBody{}

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

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

// GetVolumeGroup retrieves a single volumeGroup by id
func (s *Service) GetVolumeGroup(volumeGroupID string) (VolumeGroup, error) {
body, err := s.getVolumeGroupResponseBody(volumeGroupID)

return body.Data, err
}

func (s *Service) getVolumeGroupResponseBody(volumeGroupID string) (*GetVolumeGroupResponseBody, error) {
body := &GetVolumeGroupResponseBody{}

if volumeGroupID == "" {
return body, fmt.Errorf("invalid volume group id")
}

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

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

return nil
})
}

// CreateVolumeGroup creates a volumeGroup
func (s *Service) CreateVolumeGroup(req CreateVolumeGroupRequest) (TaskReference, error) {
body, err := s.createVolumeGroupResponseBody(req)

return body.Data, err
}

func (s *Service) createVolumeGroupResponseBody(req CreateVolumeGroupRequest) (*GetTaskReferenceResponseBody, error) {
body := &GetTaskReferenceResponseBody{}

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

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

// PatchVolumeGroup patches a volumeGroup
func (s *Service) PatchVolumeGroup(volumeGroupID string, req PatchVolumeGroupRequest) (TaskReference, error) {
body, err := s.patchVolumeGroupResponseBody(volumeGroupID, req)

return body.Data, err
}

func (s *Service) patchVolumeGroupResponseBody(volumeGroupID string, req PatchVolumeGroupRequest) (*GetTaskReferenceResponseBody, error) {
body := &GetTaskReferenceResponseBody{}

if volumeGroupID == "" {
return body, fmt.Errorf("invalid volume group id")
}

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

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

return nil
})
}

// DeleteVolumeGroup deletes a volumeGroup
func (s *Service) DeleteVolumeGroup(volumeGroupID string) (string, error) {
body, err := s.deleteVolumeGroupResponseBody(volumeGroupID)

return body.Data.TaskID, err
}

func (s *Service) deleteVolumeGroupResponseBody(volumeGroupID string) (*GetTaskReferenceResponseBody, error) {
body := &GetTaskReferenceResponseBody{}

if volumeGroupID == "" {
return body, fmt.Errorf("invalid volume group id")
}

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

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

return nil
})
}

// GetVolumeGroupVolumes retrieves a list of VolumeGroup volumes
func (s *Service) GetVolumeGroupVolumes(volumeGroupID string, parameters connection.APIRequestParameters) ([]Volume, error) {
var volumes []Volume

getFunc := func(p connection.APIRequestParameters) (connection.Paginated, error) {
return s.GetVolumeGroupVolumesPaginated(volumeGroupID, p)
}

responseFunc := func(response connection.Paginated) {
for _, volume := range response.(*PaginatedVolume).Items {
volumes = append(volumes, volume)
}
}

return volumes, connection.InvokeRequestAll(getFunc, responseFunc, parameters)
}

// GetVolumeGroupVolumesPaginated retrieves a paginated list of VolumeGroup volumes
func (s *Service) GetVolumeGroupVolumesPaginated(volumeGroupID string, parameters connection.APIRequestParameters) (*PaginatedVolume, error) {
body, err := s.getVolumeGroupVolumesPaginatedResponseBody(volumeGroupID, parameters)

return NewPaginatedVolume(func(p connection.APIRequestParameters) (connection.Paginated, error) {
return s.GetVolumeGroupVolumesPaginated(volumeGroupID, p)
}, parameters, body.Metadata.Pagination, body.Data), err
}

func (s *Service) getVolumeGroupVolumesPaginatedResponseBody(volumeGroupID string, parameters connection.APIRequestParameters) (*GetVolumeSliceResponseBody, error) {
body := &GetVolumeSliceResponseBody{}

if volumeGroupID == "" {
return body, fmt.Errorf("invalid volume group id")
}

response, err := s.connection.Get(fmt.Sprintf("/ecloud/v2/volume-groups/%s/volumes", volumeGroupID), parameters)
if err != nil {
return body, err
}

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

return nil
})
}
Loading

0 comments on commit 21356b5

Please sign in to comment.