Skip to content

Commit

Permalink
Add Resell V2 licenses List method and fixes
Browse files Browse the repository at this point in the history
Fix License ID field type and add List method.
  • Loading branch information
ozerovandrei committed Apr 17, 2018
1 parent 9732e0e commit 6c669a3
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 4 deletions.
12 changes: 11 additions & 1 deletion selvpcclient/resell/v2/licenses/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions selvpcclient/resell/v2/licenses/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) (*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
}
2 changes: 1 addition & 1 deletion selvpcclient/resell/v2/licenses/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
59 changes: 57 additions & 2 deletions selvpcclient/resell/v2/licenses/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
const TestGetLicenseResponseRaw = `
{
"license": {
"id": "123123",
"id": 123123,
"project_id": "49338ac045f448e294b25d013f890317",
"region": "ru-2",
"servers": [
Expand All @@ -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",
Expand All @@ -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",
},
}
57 changes: 57 additions & 0 deletions selvpcclient/resell/v2/licenses/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 6c669a3

Please sign in to comment.