-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from ans-group/backup-props-change
Backup Gateway support
- Loading branch information
Showing
8 changed files
with
698 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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) { | ||
return connection.Get[[]BackupGateway](s.connection, "/ecloud/v2/backup-gateways", parameters) | ||
} | ||
|
||
// 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) { | ||
if gatewayID == "" { | ||
return &connection.APIResponseBodyData[BackupGateway]{}, fmt.Errorf("invalid backup gateway id") | ||
} | ||
|
||
return connection.Get[BackupGateway](s.connection, fmt.Sprintf("/ecloud/v2/backup-gateways/%s", gatewayID), connection.APIRequestParameters{}, connection.NotFoundResponseHandler(&BackupGatewayNotFoundError{ID: gatewayID})) | ||
} | ||
|
||
// 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) { | ||
return connection.Post[TaskReference](s.connection, "/ecloud/v2/backup-gateways", &req) | ||
} | ||
|
||
// 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) { | ||
if gatewayID == "" { | ||
return &connection.APIResponseBodyData[TaskReference]{}, fmt.Errorf("invalid gateway id") | ||
} | ||
|
||
return connection.Patch[TaskReference]( | ||
s.connection, | ||
fmt.Sprintf("/ecloud/v2/backup-gateways/%s", gatewayID), | ||
&req, | ||
connection.NotFoundResponseHandler(&BackupGatewayNotFoundError{ID: gatewayID}), | ||
) | ||
} | ||
|
||
// 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) { | ||
if gatewayID == "" { | ||
return &connection.APIResponseBodyData[TaskReference]{}, fmt.Errorf("invalid gateway id") | ||
} | ||
|
||
return connection.Delete[TaskReference]( | ||
s.connection, | ||
fmt.Sprintf("/ecloud/v2/backup-gateways/%s", gatewayID), | ||
nil, | ||
connection.NotFoundResponseHandler(&BackupGatewayNotFoundError{ID: gatewayID}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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) { | ||
return connection.Get[[]BackupGatewaySpecification](s.connection, "/ecloud/v2/backup-gateway-specs", parameters) | ||
} | ||
|
||
// 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) { | ||
if specificationID == "" { | ||
return &connection.APIResponseBodyData[BackupGatewaySpecification]{}, fmt.Errorf("invalid backup gateway specification id") | ||
} | ||
|
||
return connection.Get[BackupGatewaySpecification]( | ||
s.connection, | ||
fmt.Sprintf("/ecloud/v2/backup-gateway-specs/%s", specificationID), | ||
connection.APIRequestParameters{}, | ||
connection.NotFoundResponseHandler(&BackupGatewaySpecificationNotFoundError{ID: specificationID}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package ecloud | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/ans-group/sdk-go/pkg/connection" | ||
"github.com/ans-group/sdk-go/test/mocks" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetBackupGatewaySpecifications(t *testing.T) { | ||
t.Run("Single", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
c := mocks.NewMockConnection(mockCtrl) | ||
|
||
s := Service{ | ||
connection: c, | ||
} | ||
|
||
c.EXPECT().Get("/ecloud/v2/backup-gateway-specs", gomock.Any()).Return(&connection.APIResponse{ | ||
Response: &http.Response{ | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte("{\"data\":[{\"id\":\"bkupgs-abcdef12\"}],\"meta\":{\"pagination\":{\"total_pages\":1}}}"))), | ||
StatusCode: 200, | ||
}, | ||
}, nil).Times(1) | ||
|
||
specs, err := s.GetBackupGatewaySpecifications(connection.APIRequestParameters{}) | ||
|
||
assert.Nil(t, err) | ||
assert.Len(t, specs, 1) | ||
assert.Equal(t, "bkupgs-abcdef12", specs[0].ID) | ||
}) | ||
|
||
t.Run("ConnectionError_ReturnsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
c := mocks.NewMockConnection(mockCtrl) | ||
|
||
s := Service{ | ||
connection: c, | ||
} | ||
|
||
c.EXPECT().Get("/ecloud/v2/backup-gateway-specs", gomock.Any()).Return(&connection.APIResponse{}, errors.New("test error 1")) | ||
|
||
_, err := s.GetBackupGatewaySpecifications(connection.APIRequestParameters{}) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "test error 1", err.Error()) | ||
}) | ||
} | ||
|
||
func TestGetBackupGatewaySpecification(t *testing.T) { | ||
t.Run("Valid", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
c := mocks.NewMockConnection(mockCtrl) | ||
|
||
s := Service{ | ||
connection: c, | ||
} | ||
|
||
c.EXPECT().Get("/ecloud/v2/backup-gateway-specs/bkupgs-abcdef12", gomock.Any()).Return(&connection.APIResponse{ | ||
Response: &http.Response{ | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte("{\"data\":{\"id\":\"bkupgs-abcdef12\"}}"))), | ||
StatusCode: 200, | ||
}, | ||
}, nil).Times(1) | ||
|
||
spec, err := s.GetBackupGatewaySpecification("bkupgs-abcdef12") | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, "bkupgs-abcdef12", spec.ID) | ||
}) | ||
|
||
t.Run("ConnectionError_ReturnsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
c := mocks.NewMockConnection(mockCtrl) | ||
|
||
s := Service{ | ||
connection: c, | ||
} | ||
|
||
c.EXPECT().Get("/ecloud/v2/backup-gateway-specs/bkupgs-abcdef12", gomock.Any()).Return(&connection.APIResponse{}, errors.New("test error 1")).Times(1) | ||
|
||
_, err := s.GetBackupGatewaySpecification("bkupgs-abcdef12") | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "test error 1", err.Error()) | ||
}) | ||
|
||
t.Run("InvalidBackupGatewaySpecificationID_ReturnsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
c := mocks.NewMockConnection(mockCtrl) | ||
|
||
s := Service{ | ||
connection: c, | ||
} | ||
|
||
_, err := s.GetBackupGatewaySpecification("") | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "invalid backup gateway specification id", err.Error()) | ||
}) | ||
|
||
t.Run("404_ReturnsBackupGatewaySpecificationNotFoundError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
c := mocks.NewMockConnection(mockCtrl) | ||
|
||
s := Service{ | ||
connection: c, | ||
} | ||
|
||
c.EXPECT().Get("/ecloud/v2/backup-gateway-specs/bkupgs-abcdef12", gomock.Any()).Return(&connection.APIResponse{ | ||
Response: &http.Response{ | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), | ||
StatusCode: 404, | ||
}, | ||
}, nil).Times(1) | ||
|
||
_, err := s.GetBackupGatewaySpecification("bkupgs-abcdef12") | ||
|
||
assert.NotNil(t, err) | ||
assert.IsType(t, &BackupGatewaySpecificationNotFoundError{}, err) | ||
}) | ||
} |
Oops, something went wrong.