Skip to content

Commit

Permalink
Merge pull request #32 from Rey-Lane/add_floating_ips_endpoints
Browse files Browse the repository at this point in the history
Add floating IPs create and delete endpoints
  • Loading branch information
Rey-Lane authored Mar 11, 2024
2 parents a02075e + 9a2ad81 commit 137f386
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
56 changes: 56 additions & 0 deletions floating_ips.go
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
}
76 changes: 76 additions & 0 deletions floating_ips_test.go
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)
}

0 comments on commit 137f386

Please sign in to comment.