-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ivan Denezhkin <idenezhkin@itkey.com>
- Loading branch information
Showing
26 changed files
with
4,823 additions
and
11 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,23 @@ | ||
package client | ||
|
||
import ( | ||
gcorecloud "github.com/G-Core/gcorelabscloud-go" | ||
"github.com/G-Core/gcorelabscloud-go/client/common" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func NewAIClusterClientV1(c *cli.Context) (*gcorecloud.ServiceClient, error) { | ||
return common.BuildClient(c, "ai/clusters", "v1") | ||
} | ||
|
||
func NewAIImageClientV1(c *cli.Context) (*gcorecloud.ServiceClient, error) { | ||
return common.BuildClient(c, "ai/images", "v1") | ||
} | ||
func NewAIFlavorClientV1(c *cli.Context) (*gcorecloud.ServiceClient, error) { | ||
return common.BuildClient(c, "ai/flavors", "v1") | ||
} | ||
|
||
|
||
|
||
|
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,12 @@ | ||
package client | ||
|
||
import ( | ||
gcorecloud "github.com/G-Core/gcorelabscloud-go" | ||
"github.com/G-Core/gcorelabscloud-go/client/common" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func NewAIClusterClientV2(c *cli.Context) (*gcorecloud.ServiceClient, error) { | ||
return common.BuildClient(c, "ai/clusters", "v2") | ||
} |
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
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
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
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,51 @@ | ||
package aiflavors | ||
|
||
import ( | ||
gcorecloud "github.com/G-Core/gcorelabscloud-go" | ||
"github.com/G-Core/gcorelabscloud-go/pagination" | ||
) | ||
|
||
|
||
type ListOptsBuilder interface { | ||
ToAIFlavorListQuery() (string, error) | ||
} | ||
|
||
type AIFlavorListOpts struct { | ||
Disabled bool `q:"disabled"` | ||
IncludeCapacity bool `q:"include_capacity"` | ||
IncludePrices bool `q:"include_prices"` | ||
} | ||
|
||
// ToAIFlavorListQuery formats a AIFlavorListOpts into a query string. | ||
func (opts AIFlavorListOpts) ToAIFlavorListQuery() (string, error) { | ||
q, err := gcorecloud.BuildQueryString(opts) | ||
if err != nil { | ||
return "", err | ||
} | ||
return q.String(), err | ||
} | ||
|
||
|
||
// List retrieves list of AI flavors | ||
func List(c *gcorecloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { | ||
url := listAIFlavorsURL(c) | ||
if opts != nil { | ||
query, err := opts.ToAIFlavorListQuery() | ||
if err != nil { | ||
return pagination.Pager{Err: err} | ||
} | ||
url += query | ||
} | ||
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { | ||
return AIFlavorPage{pagination.LinkedPageBase{PageResult: r}} | ||
}) | ||
} | ||
|
||
// ListAll retrieves list of all AI flavors | ||
func ListAll(c *gcorecloud.ServiceClient, opts ListOptsBuilder) ([]AIFlavor, error) { | ||
results, err := List(c, opts).AllPages() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ExtractAIFlavors(results) | ||
} |
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,91 @@ | ||
package aiflavors | ||
|
||
import ( | ||
gcorecloud "github.com/G-Core/gcorelabscloud-go" | ||
"github.com/G-Core/gcorelabscloud-go/pagination" | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
type commonResult struct { | ||
gcorecloud.Result | ||
} | ||
|
||
// Extract is a function that accepts a result and extracts a instance resource. | ||
func (r commonResult) Extract() (*AIFlavor, error) { | ||
var s AIFlavor | ||
err := r.ExtractInto(&s) | ||
return &s, err | ||
} | ||
|
||
func (r commonResult) ExtractInto(v interface{}) error { | ||
return r.Result.ExtractIntoStructPtr(v, "") | ||
} | ||
|
||
// AIFlavorPage is the page returned by a pager when traversing over a | ||
// collection of instances. | ||
type AIFlavorPage struct { | ||
pagination.LinkedPageBase | ||
} | ||
|
||
// NextPageURL is invoked when a paginated collection of flavors has reached | ||
// the end of a page and the pager seeks to traverse over a new one. In order | ||
// to do this, it needs to construct the next page's URL. | ||
func (r AIFlavorPage) NextPageURL() (string, error) { | ||
var s struct { | ||
Links []gcorecloud.Link `json:"links"` | ||
} | ||
err := r.ExtractInto(&s) | ||
if err != nil { | ||
return "", err | ||
} | ||
return gcorecloud.ExtractNextURL(s.Links) | ||
} | ||
|
||
// IsEmpty checks whether a FlavorPage struct is empty. | ||
func (r AIFlavorPage) IsEmpty() (bool, error) { | ||
is, err := ExtractAIFlavors(r) | ||
return len(is) == 0, err | ||
} | ||
|
||
// ExtractFlavor accepts a Page struct, specifically a FlavorPage struct, | ||
// and extracts the elements into a slice of Flavor structs. In other words, | ||
// a generic collection is mapped into a relevant slice. | ||
func ExtractAIFlavors(r pagination.Page) ([]AIFlavor, error) { | ||
var s []AIFlavor | ||
err := ExtractAIFlavorsInto(r, &s) | ||
return s, err | ||
} | ||
|
||
func ExtractAIFlavorsInto(r pagination.Page, v interface{}) error { | ||
return r.(AIFlavorPage).Result.ExtractIntoSlicePtr(v, "results") | ||
} | ||
|
||
|
||
type HardwareDescription struct { | ||
CPU string `json:"cpu,omitempty"` | ||
Disk string `json:"disk,omitempty"` | ||
Network string `json:"network,omitempty"` | ||
RAM string `json:"ram,omitempty"` | ||
Ephemeral string `json:"ephemeral,omitempty"` | ||
GPU string `json:"gpu,omitempty"` | ||
IPU string `json:"ipu,omitempty"` | ||
PoplarCount string `json:"poplar_count,omitempty"` | ||
SGXEPCSize string `json:"sgx_epc_size,omitempty"` | ||
} | ||
|
||
// Flavor represents a flavor structure. | ||
type AIFlavor struct { | ||
FlavorID string `json:"flavor_id"` | ||
FlavorName string `json:"flavor_name"` | ||
Disabled bool `json:"disabled"` | ||
ResourceClass string `json:"resource_class"` | ||
PriceStatus *string `json:"price_status,omitempty"` | ||
CurrencyCode *gcorecloud.Currency `json:"currency_code,omitempty"` | ||
PricePerHour *decimal.Decimal `json:"price_per_hour,omitempty"` | ||
PricePerMonth *decimal.Decimal `json:"price_per_month,omitempty"` | ||
HardwareDescription *HardwareDescription `json:"hardware_description,omitempty"` | ||
RAM *int `json:"ram,omitempty"` | ||
VCPUS *int `json:"vcpus,omitempty"` | ||
Capacity *int `json:"capacity,omitempty"` | ||
} | ||
|
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 @@ | ||
package testing |
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,39 @@ | ||
package testing | ||
|
||
import ( | ||
"github.com/G-Core/gcorelabscloud-go/gcore/ai/v1/aiflavors" | ||
) | ||
|
||
const ListResponse = ` | ||
{ | ||
"count": 1, | ||
"results": [ | ||
{ | ||
"resource_class": "bm1-ai-small", | ||
"hardware_description": { | ||
"network": "2x100G", | ||
"ipu": "vPOD-16 (Classic)", | ||
"poplar_count": "2" | ||
}, | ||
"disabled": false, | ||
"flavor_name": "bm1-ai-2xsmall-v1pod-16", | ||
"flavor_id": "bm1-ai-2xsmall-v1pod-16" | ||
} | ||
] | ||
} | ||
` | ||
|
||
var ( | ||
AIFlavor1 = aiflavors.AIFlavor{ | ||
FlavorID: "bm1-ai-2xsmall-v1pod-16", | ||
FlavorName: "bm1-ai-2xsmall-v1pod-16", | ||
Disabled: false, | ||
ResourceClass: "bm1-ai-small", | ||
HardwareDescription: &aiflavors.HardwareDescription{ | ||
Network: "2x100G", | ||
IPU: "vPOD-16 (Classic)", | ||
PoplarCount: "2", | ||
}, | ||
} | ||
ExpectedAIFlavorSlice = []aiflavors.AIFlavor{AIFlavor1} | ||
) |
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,59 @@ | ||
package testing | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/G-Core/gcorelabscloud-go/gcore/ai/v1/aiflavors" | ||
"github.com/G-Core/gcorelabscloud-go/pagination" | ||
th "github.com/G-Core/gcorelabscloud-go/testhelper" | ||
fake "github.com/G-Core/gcorelabscloud-go/testhelper/client" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func prepareListTestURLParams(version string, projectID int, regionID int) string { | ||
return fmt.Sprintf("/%s/ai/flavors/%d/%d", version, projectID, regionID) | ||
} | ||
|
||
func prepareListTestURL() string { | ||
return prepareListTestURLParams("v1", fake.ProjectID, fake.RegionID) | ||
} | ||
|
||
func TestList(t *testing.T) { | ||
th.SetupHTTP() | ||
defer th.TeardownHTTP() | ||
|
||
th.Mux.HandleFunc(prepareListTestURL(), func(w http.ResponseWriter, r *http.Request) { | ||
th.TestMethod(t, r, "GET") | ||
th.TestHeader(t, r, "Authorization", fmt.Sprintf("Bearer %s", fake.AccessToken)) | ||
w.Header().Add("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, err := fmt.Fprint(w, ListResponse) | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
}) | ||
|
||
client := fake.ServiceTokenClient("ai/flavors", "v1") | ||
count := 0 | ||
|
||
opts := aiflavors.AIFlavorListOpts{} | ||
|
||
err := aiflavors.List(client, opts).EachPage(func(page pagination.Page) (bool, error) { | ||
count++ | ||
actual, err := aiflavors.ExtractAIFlavors(page) | ||
require.NoError(t, err) | ||
ct := actual[0] | ||
require.Equal(t, AIFlavor1, ct) | ||
require.Equal(t, ExpectedAIFlavorSlice, actual) | ||
return true, nil | ||
}) | ||
|
||
th.AssertNoErr(t, err) | ||
|
||
if count != 1 { | ||
t.Errorf("Expected 1 page, got %d", count) | ||
} | ||
} |
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,9 @@ | ||
package aiflavors | ||
|
||
import ( | ||
gcorecloud "github.com/G-Core/gcorelabscloud-go" | ||
) | ||
|
||
func listAIFlavorsURL(c *gcorecloud.ServiceClient) string { | ||
return c.ServiceURL() | ||
} |
Oops, something went wrong.