+67
@@ -0,0 +1,67 @@
|
||||
package awsserversdk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type CatalogService struct{ client *Client }
|
||||
type PricingService struct{ client *Client }
|
||||
|
||||
func (s *CatalogService) ListRegions(ctx context.Context) ([]Region, error) {
|
||||
var result struct {
|
||||
Items []Region `json:"items"`
|
||||
}
|
||||
err := s.client.do(ctx, http.MethodGet, "/regions", nil, &result, "")
|
||||
return result.Items, err
|
||||
}
|
||||
func (s *CatalogService) GetCreateOptions(ctx context.Context, region string) (*CreateOptions, error) {
|
||||
var result CreateOptions
|
||||
err := s.client.do(ctx, http.MethodGet, "/regions/"+escaped(region)+"/create-options", nil, &result, "")
|
||||
return &result, err
|
||||
}
|
||||
func (s *CatalogService) ListImages(ctx context.Context, region string, options ListOptions) (*Page[Image], error) {
|
||||
var result Page[Image]
|
||||
err := s.client.do(ctx, http.MethodGet, catalogPath(region, "images", options), nil, &result, "")
|
||||
return &result, err
|
||||
}
|
||||
func (s *CatalogService) ListInstanceTypes(ctx context.Context, region string, options ListOptions) (*Page[InstanceType], error) {
|
||||
var result Page[InstanceType]
|
||||
err := s.client.do(ctx, http.MethodGet, catalogPath(region, "instance-types", options), nil, &result, "")
|
||||
return &result, err
|
||||
}
|
||||
func (s *CatalogService) ListPlacements(ctx context.Context, region string) ([]Placement, error) {
|
||||
var result struct {
|
||||
Items []Placement `json:"items"`
|
||||
}
|
||||
err := s.client.do(ctx, http.MethodGet, "/regions/"+escaped(region)+"/placements", nil, &result, "")
|
||||
return result.Items, err
|
||||
}
|
||||
func (s *PricingService) EstimateInstance(ctx context.Context, region string, input CreateInstanceRequest) (*PriceEstimate, error) {
|
||||
var result PriceEstimate
|
||||
err := s.client.do(ctx, http.MethodPost, "/regions/"+escaped(region)+"/price-estimate", input, &result, newID())
|
||||
return &result, err
|
||||
}
|
||||
func catalogPath(region, resource string, options ListOptions) string {
|
||||
values := url.Values{}
|
||||
if options.Query != "" {
|
||||
values.Set("q", options.Query)
|
||||
}
|
||||
if options.CategoryID != "" {
|
||||
values.Set("category_id", options.CategoryID)
|
||||
}
|
||||
if options.Limit > 0 {
|
||||
values.Set("limit", strconv.Itoa(options.Limit))
|
||||
}
|
||||
if options.Offset > 0 {
|
||||
values.Set("offset", strconv.Itoa(options.Offset))
|
||||
}
|
||||
path := fmt.Sprintf("/regions/%s/%s", escaped(region), resource)
|
||||
if encoded := values.Encode(); encoded != "" {
|
||||
path += "?" + encoded
|
||||
}
|
||||
return path
|
||||
}
|
||||
Reference in New Issue
Block a user