-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Resell V2 licenses Get method and structure (#49)
Add basic License structure and Get method. Add doc example and tests.
- Loading branch information
1 parent
97b3b23
commit 9732e0e
Showing
5 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |