From f542c46a9883661ed1a086b2f63f699131d6642d Mon Sep 17 00:00:00 2001 From: Andrei Ozerov Date: Mon, 16 Apr 2018 08:15:26 +0300 Subject: [PATCH] Resell V2 - add subnets List method (#46) Add List method with tests and doc example. Also fix prefixes in testing fixtures. --- selvpcclient/resell/v2/subnets/doc.go | 10 ++++ selvpcclient/resell/v2/subnets/requests.go | 23 ++++++++ .../resell/v2/subnets/testing/fixtures.go | 52 ++++++++++++++++- .../resell/v2/subnets/testing/request_test.go | 57 +++++++++++++++++++ 4 files changed, 140 insertions(+), 2 deletions(-) diff --git a/selvpcclient/resell/v2/subnets/doc.go b/selvpcclient/resell/v2/subnets/doc.go index 4cb52b6..6c3bf64 100644 --- a/selvpcclient/resell/v2/subnets/doc.go +++ b/selvpcclient/resell/v2/subnets/doc.go @@ -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 diff --git a/selvpcclient/resell/v2/subnets/requests.go b/selvpcclient/resell/v2/subnets/requests.go index 9edbcd0..3330646 100644 --- a/selvpcclient/resell/v2/subnets/requests.go +++ b/selvpcclient/resell/v2/subnets/requests.go @@ -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 +} diff --git a/selvpcclient/resell/v2/subnets/testing/fixtures.go b/selvpcclient/resell/v2/subnets/testing/fixtures.go index f92bec8..0fe3b82 100644 --- a/selvpcclient/resell/v2/subnets/testing/fixtures.go +++ b/selvpcclient/resell/v2/subnets/testing/fixtures.go @@ -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", @@ -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", + }, +} diff --git a/selvpcclient/resell/v2/subnets/testing/request_test.go b/selvpcclient/resell/v2/subnets/testing/request_test.go index feb3c8a..bf0863e 100644 --- a/selvpcclient/resell/v2/subnets/testing/request_test.go +++ b/selvpcclient/resell/v2/subnets/testing/request_test.go @@ -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) + } +}