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

Add Resell V2 licenses Get method and structure #49

Merged
merged 1 commit into from
Apr 17, 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
13 changes: 13 additions & 0 deletions selvpcclient/resell/v2/licenses/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Package licenses provides the ability to retrieve and manage licenses through
the Resell v2 API.

Example of getting a single license referenced by its id

license, _, err := licenses.Get(context, resellClient, licenseID)
if err != nil {
log.Fatal(err)
}
fmt.Println(license)
*/
package licenses
33 changes: 33 additions & 0 deletions selvpcclient/resell/v2/licenses/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package licenses

import (
"context"
"strings"

"github.com/selectel/go-selvpcclient/selvpcclient"
)

const resourceURL = "licenses"

// Get returns a single license by its id.
func Get(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*License, *selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL, id}, "/")
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 a license from the response body.
var result struct {
License *License `json:"license"`
}
err = responseResult.ExtractResult(&result)
if err != nil {
return nil, responseResult, err
}

return result.License, responseResult, nil
}
24 changes: 24 additions & 0 deletions selvpcclient/resell/v2/licenses/schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package licenses

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"`

// ProjectID represents an associated Resell project.
ProjectID string `json:"project_id"`

// Region represents a region of where the license resides.
Region string `json:"region"`

// Servers contains info about servers to which license is associated to.
Servers []servers.Server `json:"servers"`

// Status represents a current status of the license.
Status string `json:"status"`

// Type represent a license type.
Type string `json:"type"`
}
48 changes: 48 additions & 0 deletions selvpcclient/resell/v2/licenses/testing/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package testing

import (
"time"

"github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/licenses"
"github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/servers"
)

// TestGetLicenseResponseRaw represents a raw response from the Get request.
const TestGetLicenseResponseRaw = `
{
"license": {
"id": "123123",
"project_id": "49338ac045f448e294b25d013f890317",
"region": "ru-2",
"servers": [
{
"id": "253b680c-89f6-4c85-afbf-c9a67c92d3fe",
"name": "Node00",
"status": "ACTIVE",
"updated": "2018-02-20T22:02:21Z"
}
],
"status": "ACTIVE",
"type": "license_windows_2012_standard"
}
}
`

var licenseServerTimeStamp, _ = time.Parse(time.RFC3339, "2018-02-20T22:02:21Z")

// TestGetLicenseResponse represents an unmarshalled TestGetLicenseResponseRaw.
var TestGetLicenseResponse = &licenses.License{
ID: "123123",
ProjectID: "49338ac045f448e294b25d013f890317",
Region: "ru-2",
Status: "ACTIVE",
Type: "license_windows_2012_standard",
Servers: []servers.Server{
{
ID: "253b680c-89f6-4c85-afbf-c9a67c92d3fe",
Name: "Node00",
Status: "ACTIVE",
Updated: licenseServerTimeStamp,
},
},
}
38 changes: 38 additions & 0 deletions selvpcclient/resell/v2/licenses/testing/requests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package testing

import (
"context"
"fmt"
"net/http"
"reflect"
"testing"

"github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/licenses"
"github.com/selectel/go-selvpcclient/selvpcclient/testutils"
)

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

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

ctx := context.Background()
actual, _, err := licenses.Get(ctx, testEnv.Client, "123123")
if err != nil {
t.Fatal(err)
}

expected := TestGetLicenseResponse

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