-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 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,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 |
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,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 | ||
} |
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,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"` | ||
} |
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,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
69
selvpcclient/resell/v2/floatingips/testing/request_test.go
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,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) | ||
} | ||
} |