Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resell V2 - add subnets List method #46

Merged
merged 1 commit into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions selvpcclient/resell/v2/subnets/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ Example of getting a single subnet referenced by its id
log.Fatal(err)
}
fmt.Println(subnet)

Example of getting all subnets

allSubnets, _, err := subnets.List(ctx, resellClient)
if err != nil {
log.Fatal(err)
}
for _, subnet := range allSubnet {
fmt.Println(subnet)
}
*/
package subnets
23 changes: 23 additions & 0 deletions selvpcclient/resell/v2/subnets/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ func Get(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*S

return result.Subnet, responseResult, nil
}

// List gets a list of subnets in the current domain.
func List(ctx context.Context, client *selvpcclient.ServiceClient) ([]*Subnet, *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 subnets from the response body.
var result struct {
Subnets []*Subnet `json:"subnets"`
}
err = responseResult.ExtractResult(&result)
if err != nil {
return nil, responseResult, err
}

return result.Subnets, responseResult, nil
}
52 changes: 50 additions & 2 deletions selvpcclient/resell/v2/subnets/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
const TestGetSubnetResponseRaw = `
{
"subnet": {
"cidr": "203.0.113.11/24",
"cidr": "203.0.113.0/24",
"id": 111122,
"network_id": "8233f12e-c47e-4f1c-953a-1ecd322a7119",
"project_id": "49338ac045f448e294b25d013f890317",
Expand Down Expand Up @@ -44,9 +44,57 @@ var TestGetSubnetResponse = &subnets.Subnet{
Updated: subnetServerTimeStamp,
},
},
CIDR: "203.0.113.11/24",
CIDR: "203.0.113.0/24",
NetworkID: "8233f12e-c47e-4f1c-953a-1ecd322a7119",
ProjectID: "49338ac045f448e294b25d013f890317",
Region: "ru-3",
SubnetID: "94425a6e-19cd-412d-9710-ff40b34a78f4",
}

// TestListSubnetsResponseRaw represents a raw response from the List request.
const TestListSubnetsResponseRaw = `
{
"subnets": [
{
"cidr": "203.0.113.0/24",
"id": 112233,
"project_id": "49338ac045f448e294b25d013f890317",
"region": "ru-3",
"status": "ACTIVE"
},
{
"cidr": "198.51.100.0/24",
"id": 112234,
"project_id": "9c97bdc75295493096cf5edcb8c37933",
"region": "ru-2",
"status": "ACTIVE"
}
]
}
`

// TestListSubnetsSingleResponseRaw represents a raw response with a single subnet from the List request.
const TestListSubnetsSingleResponseRaw = `
{
"subnets": [
{
"cidr": "203.0.113.0/24",
"id": 112233,
"project_id": "49338ac045f448e294b25d013f890317",
"region": "ru-3",
"status": "ACTIVE"
}
]
}
`

// TestListSubnetsSingleResponse represents the unmarshalled TestListSubnetsSingleResponseRaw response.
var TestListSubnetsSingleResponse = []*subnets.Subnet{
{
CIDR: "203.0.113.0/24",
ID: 112233,
ProjectID: "49338ac045f448e294b25d013f890317",
Region: "ru-3",
Status: "ACTIVE",
},
}
57 changes: 57 additions & 0 deletions selvpcclient/resell/v2/subnets/testing/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,60 @@ func TestGetSubnet(t *testing.T) {
t.Fatalf("expected %#v, but got %#v", expected, actual)
}
}

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

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

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

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

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

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

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

expected := TestListSubnetsSingleResponse

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