Skip to content

Commit

Permalink
Merge 01e8c82 into 39a224e
Browse files Browse the repository at this point in the history
  • Loading branch information
ozerovandrei committed Apr 12, 2018
2 parents 39a224e + 01e8c82 commit d3e68b8
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
15 changes: 15 additions & 0 deletions selvpcclient/resell/v2/floatingips/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Package floatingips provides the ability to retrieve and manage floatingips through
the Resell v2 API.
Example of getting all floatingips
allFloatingIPs, _, err := floatingips.List(ctx, resellClient)
if err != nil {
log.Fatal(err)
}
for _, floatingIP := range floatingips {
fmt.Println(floatingIP)
}
*/
package floatingips
33 changes: 33 additions & 0 deletions selvpcclient/resell/v2/floatingips/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package floatingips

import (
"context"
"strings"

"github.com/selectel/go-selvpcclient/selvpcclient"
)

const resourceURL = "floatingips"

// List gets a list of floating ips in the current domain.
func List(ctx context.Context, client *selvpcclient.ServiceClient) ([]*FloatingIP, *selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL}, "/")
responseResult, err := client.DoRequest(ctx, "GET", url, nil)
if err != nil {
return nil, nil, err
}
if responseResult.Err != nil {
return nil, responseResult, responseResult.Err
}

// Extract users from the response body.
var result struct {
FloatingIPs []*FloatingIP `json:"floatingips"`
}
err = responseResult.ExtractResult(&result)
if err != nil {
return nil, responseResult, err
}

return result.FloatingIPs, responseResult, nil
}
19 changes: 19 additions & 0 deletions selvpcclient/resell/v2/floatingips/schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package floatingips

// FloatingIP represents a single Resell Floating IP.
type FloatingIP struct {
// FloatingIPAddress represents IP address.
FloatingIPAddress string `json:"floating_ip_address"`

// ID is a unique id of the Floating IP.
ID string `json:"id"`

// ProjectID represents an associated Resell project.
ProjectID string `json:"project_id"`

// Region represents a region of where the Floating IP resides.
Region string `json:"region"`

// Status represents a current status of the Floating IP.
Status string `json:"status"`
}
58 changes: 58 additions & 0 deletions selvpcclient/resell/v2/floatingips/testing/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package testing

import "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/floatingips"

// TestListFloatingIPsResponseRaw represents a raw response from the List request.
const TestListFloatingIPsResponseRaw = `
{
"floatingips": [
{
"floating_ip_address": "203.0.113.11",
"id": "5232d5f3-4950-454b-bd41-78c5295622cd",
"project_id": "49338ac045f448e294b25d013f890317",
"region": "ru-2",
"status": "ACTIVE"
},
{
"floating_ip_address": "203.0.113.12",
"id": "94425a6e-19cd-412d-9710-ff40b34a78f4",
"project_id": "9c97bdc75295493096cf5edcb8c37933",
"region": "ru-1",
"status": "DOWN"
},
{
"floating_ip_address": "203.0.113.13",
"id": "8233f12e-c47e-4f1c-953a-1ecd322a7119",
"project_id": "9c97bdc75295493096cf5edcb8c37933",
"region": "ru-3",
"status": "ACTIVE"
}
]
}
`

// TestListFloatingIPsSingleResponseRaw represents a raw response with a single floating ip from the List request.
const TestListFloatingIPsSingleResponseRaw = `
{
"floatingips": [
{
"floating_ip_address": "203.0.113.11",
"id": "5232d5f3-4950-454b-bd41-78c5295622cd",
"project_id": "49338ac045f448e294b25d013f890317",
"region": "ru-2",
"status": "ACTIVE"
}
]
}
`

// TestListFloatingIPsSingleResponse represents the unmarshalled TestListUsersSingleUserResponseRaw response.
var TestListFloatingIPsSingleResponse = []*floatingips.FloatingIP{
{
FloatingIPAddress: "203.0.113.11",
ID: "5232d5f3-4950-454b-bd41-78c5295622cd",
ProjectID: "49338ac045f448e294b25d013f890317",
Region: "ru-2",
Status: "ACTIVE",
},
}
69 changes: 69 additions & 0 deletions selvpcclient/resell/v2/floatingips/testing/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package testing

import (
"context"
"fmt"
"net/http"
"reflect"
"testing"

"github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/floatingips"
"github.com/selectel/go-selvpcclient/selvpcclient/testutils"
)

func TestListFloatingIPs(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/floatingips", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestListFloatingIPsResponseRaw)

if r.Method != http.MethodGet {
t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method)
}
})

ctx := context.Background()
actual, _, err := floatingips.List(ctx, testEnv.Client)
if err != nil {
t.Fatal(err)
}

if actual == nil {
t.Fatal("didn't get floating ips")
}
actualKind := reflect.TypeOf(actual).Kind()
if actualKind != reflect.Slice {
t.Errorf("expected slice of pointers to floating ips, but got %v", actualKind)
}
if len(actual) != 3 {
t.Errorf("expected 3 floating ips, but got %d", len(actual))
}
}

func TestListFloatingIPsSingle(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/floatingips", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestListFloatingIPsSingleResponseRaw)

if r.Method != http.MethodGet {
t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method)
}
})

ctx := context.Background()
actual, _, err := floatingips.List(ctx, testEnv.Client)
if err != nil {
t.Fatal(err)
}

expected := TestListFloatingIPsSingleResponse

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected %#v, but got %#v", expected, actual)
}
}

0 comments on commit d3e68b8

Please sign in to comment.