-
Notifications
You must be signed in to change notification settings - Fork 2
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 #32 from Rey-Lane/add_floating_ips_endpoints
Add floating IPs create and delete endpoints
- Loading branch information
Showing
2 changed files
with
132 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package dbaas | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// FloatingIPsOpts represents create|delete options for creating|deleting | ||
// Floating IP in|from existed cluster. | ||
type FloatingIPsOpts struct { | ||
InstanceID string `json:"instance_id"` | ||
} | ||
|
||
const FloatingIPsURI = "/floating-ips" | ||
|
||
// CreateFloatingIP creates FloatingIP for provided instance of an existing datastore. | ||
func (api *API) CreateFloatingIP(ctx context.Context, opts FloatingIPsOpts) error { | ||
floatingIPsOpts := struct { | ||
FloatingIP FloatingIPsOpts `json:"floating-ip"` | ||
}{ | ||
FloatingIP: opts, | ||
} | ||
requestBody, err := json.Marshal(floatingIPsOpts) | ||
if err != nil { | ||
return fmt.Errorf("Error marshalling params to JSON, %w", err) | ||
} | ||
|
||
_, err = api.makeRequest(ctx, http.MethodPost, FloatingIPsURI, requestBody) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DeleteFloatingIP deletes FloatingIP from provided instance of an existing datastore. | ||
func (api *API) DeleteFloatingIP(ctx context.Context, opts FloatingIPsOpts) error { | ||
floatingIPsOpts := struct { | ||
FloatingIP FloatingIPsOpts `json:"floating-ip"` | ||
}{ | ||
FloatingIP: opts, | ||
} | ||
requestBody, err := json.Marshal(floatingIPsOpts) | ||
if err != nil { | ||
return fmt.Errorf("Error marshalling params to JSON, %w", err) | ||
} | ||
|
||
_, err = api.makeRequest(ctx, http.MethodDelete, FloatingIPsURI, requestBody) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,76 @@ | ||
package dbaas | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const testInstanceNotFoundResponse = `{ | ||
"error": { | ||
"code": 404, | ||
"title": "Not Found", | ||
"message": "instance %s not found." | ||
} | ||
}` | ||
|
||
const instanceID = "7d959e48-4d42-41cd-8515-aae0a8482d8d" | ||
|
||
func TestCreateFloatingIP(t *testing.T) { | ||
httpmock.Activate() | ||
testClient := SetupTestClient() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("POST", testClient.Endpoint+FloatingIPsURI, | ||
httpmock.NewStringResponder(200, "")) | ||
|
||
createFloatingIPOpts := FloatingIPsOpts{ | ||
InstanceID: instanceID, | ||
} | ||
|
||
err := testClient.CreateFloatingIP(context.Background(), createFloatingIPOpts) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func TestDeleteFloatingIP(t *testing.T) { | ||
httpmock.Activate() | ||
testClient := SetupTestClient() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("DELETE", testClient.Endpoint+FloatingIPsURI, | ||
httpmock.NewStringResponder(200, "")) | ||
|
||
deleteFloatingIPOpts := FloatingIPsOpts{ | ||
InstanceID: instanceID, | ||
} | ||
|
||
err := testClient.DeleteFloatingIP(context.Background(), deleteFloatingIPOpts) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func TestCreateFloatingIPInstanceNotFound(t *testing.T) { | ||
httpmock.Activate() | ||
testClient := SetupTestClient() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("DELETE", testClient.Endpoint+FloatingIPsURI, | ||
httpmock.NewStringResponder(404, testInstanceNotFoundResponse)) | ||
|
||
createFloatingIPOpts := FloatingIPsOpts{ | ||
InstanceID: instanceID, | ||
} | ||
|
||
expected := &DBaaSAPIError{} | ||
expected.APIError.Code = 404 | ||
expected.APIError.Title = ErrorNotFoundTitle | ||
expected.APIError.Message = fmt.Sprintf("instance %s not found.", instanceID) | ||
|
||
err := testClient.DeleteFloatingIP(context.Background(), createFloatingIPOpts) | ||
|
||
require.ErrorAs(t, err, &expected) | ||
} |