diff --git a/selvpcclient/resell/v2/licenses/doc.go b/selvpcclient/resell/v2/licenses/doc.go index 43ca8f9..68d2ff0 100644 --- a/selvpcclient/resell/v2/licenses/doc.go +++ b/selvpcclient/resell/v2/licenses/doc.go @@ -8,6 +8,16 @@ Example of getting a single license referenced by its id if err != nil { log.Fatal(err) } - fmt.Println(license) + fmt.Println(license) + +Example of getting all licenses + + allLicenses, _, err := licenses.List(ctx, resellClient) + if err != nil { + log.Fatal(err) + } + for _, license := range allLicenses { + fmt.Println(license) + } */ package licenses diff --git a/selvpcclient/resell/v2/licenses/requests.go b/selvpcclient/resell/v2/licenses/requests.go index fc4a7a5..d17917c 100644 --- a/selvpcclient/resell/v2/licenses/requests.go +++ b/selvpcclient/resell/v2/licenses/requests.go @@ -31,3 +31,26 @@ func Get(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*L return result.License, responseResult, nil } + +// List gets a list of licenses in the current domain. +func List(ctx context.Context, client *selvpcclient.ServiceClient) ([]*License, *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 licenses from the response body. + var result struct { + Licenses []*License `json:"licenses"` + } + err = responseResult.ExtractResult(&result) + if err != nil { + return nil, responseResult, err + } + + return result.Licenses, responseResult, nil +} diff --git a/selvpcclient/resell/v2/licenses/schemas.go b/selvpcclient/resell/v2/licenses/schemas.go index cae0c2e..854ed13 100644 --- a/selvpcclient/resell/v2/licenses/schemas.go +++ b/selvpcclient/resell/v2/licenses/schemas.go @@ -5,7 +5,7 @@ import "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/servers" // License represents a single Resell Floating IP. type License struct { // ID is a unique id of the license. - ID string `json:"id"` + ID int `json:"id"` // ProjectID represents an associated Resell project. ProjectID string `json:"project_id"` diff --git a/selvpcclient/resell/v2/licenses/testing/fixtures.go b/selvpcclient/resell/v2/licenses/testing/fixtures.go index c292c37..20aff49 100644 --- a/selvpcclient/resell/v2/licenses/testing/fixtures.go +++ b/selvpcclient/resell/v2/licenses/testing/fixtures.go @@ -11,7 +11,7 @@ import ( const TestGetLicenseResponseRaw = ` { "license": { - "id": "123123", + "id": 123123, "project_id": "49338ac045f448e294b25d013f890317", "region": "ru-2", "servers": [ @@ -32,7 +32,7 @@ var licenseServerTimeStamp, _ = time.Parse(time.RFC3339, "2018-02-20T22:02:21Z") // TestGetLicenseResponse represents an unmarshalled TestGetLicenseResponseRaw. var TestGetLicenseResponse = &licenses.License{ - ID: "123123", + ID: 123123, ProjectID: "49338ac045f448e294b25d013f890317", Region: "ru-2", Status: "ACTIVE", @@ -46,3 +46,58 @@ var TestGetLicenseResponse = &licenses.License{ }, }, } + +// TestListLicensesResponseRaw represents a raw response from the List request. +const TestListLicensesResponseRaw = ` +{ + "licenses": [ + { + "id": 1123123, + "project_id": "49338ac045f448e294b25d013f890317", + "region": "ru-1", + "status": "DOWN", + "type": "license_windows_2012_standard" + }, + { + "id": 124123, + "project_id": "49338ac045f448e294b25d013f890317", + "region": "ru-3", + "status": "DOWN", + "type": "license_windows_2016_standard" + }, + { + "id": 13212, + "project_id": "49338ac045f448e294b25d013f890317", + "region": "ru-2", + "status": "DOWN", + "type": "license_windows_2016_standard" + } + ] +} +` + +// TestListLicensesSingleResponseRaw represents a raw response with a single license from the List request. +const TestListLicensesSingleResponseRaw = ` +{ + "licenses": [ + { + "id": 1123123, + "project_id": "49338ac045f448e294b25d013f890317", + "region": "ru-1", + "status": "DOWN", + "type": "license_windows_2012_standard" + } + ] +} +` + +// TestListLicensesSingleResponse represents the unmarshalled TestListLicensesSingleResponseRaw response. +var TestListLicensesSingleResponse = []*licenses.License{ + { + ID: 1123123, + ProjectID: "49338ac045f448e294b25d013f890317", + Region: "ru-1", + Status: "DOWN", + Type: "license_windows_2012_standard", + }, +} diff --git a/selvpcclient/resell/v2/licenses/testing/requests_test.go b/selvpcclient/resell/v2/licenses/testing/requests_test.go index b4bcafb..f224325 100644 --- a/selvpcclient/resell/v2/licenses/testing/requests_test.go +++ b/selvpcclient/resell/v2/licenses/testing/requests_test.go @@ -36,3 +36,60 @@ func TestGetLicense(t *testing.T) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } + +func TestListLicenses(t *testing.T) { + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testEnv.Mux.HandleFunc("/resell/v2/licenses", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + fmt.Fprintf(w, TestListLicensesResponseRaw) + + if r.Method != http.MethodGet { + t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) + } + }) + + ctx := context.Background() + actual, _, err := licenses.List(ctx, testEnv.Client) + if err != nil { + t.Fatal(err) + } + + if actual == nil { + t.Fatal("didn't get licenses") + } + actualKind := reflect.TypeOf(actual).Kind() + if actualKind != reflect.Slice { + t.Errorf("expected slice of pointers to licenses, but got %v", actualKind) + } + if len(actual) != 3 { + t.Errorf("expected 3 licenses, but got %d", len(actual)) + } +} + +func TestListLicensesSingle(t *testing.T) { + testEnv := testutils.SetupTestEnv() + defer testEnv.TearDownTestEnv() + testEnv.NewTestResellV2Client() + testEnv.Mux.HandleFunc("/resell/v2/licenses", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + fmt.Fprintf(w, TestListLicensesSingleResponseRaw) + + if r.Method != http.MethodGet { + t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) + } + }) + + ctx := context.Background() + actual, _, err := licenses.List(ctx, testEnv.Client) + if err != nil { + t.Fatal(err) + } + + expected := TestListLicensesSingleResponse + + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("expected %#v, but got %#v", expected, actual) + } +}