78 lines
3.0 KiB
Go
78 lines
3.0 KiB
Go
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 (s *PricingService) EstimateExistingInstance(ctx context.Context, region, instanceID string) (*PriceEstimate, error) {
|
|
var result PriceEstimate
|
|
err := s.client.do(ctx, http.MethodPost, instancesPath(region, instanceID)+"/price-estimate", map[string]any{}, &result, "")
|
|
return &result, err
|
|
}
|
|
func (s *PricingService) EstimateInstanceChange(ctx context.Context, region, instanceID string, input InstanceChangePriceRequest) (*InstanceChangePriceEstimate, error) {
|
|
var result InstanceChangePriceEstimate
|
|
err := s.client.do(ctx, http.MethodPost, instancesPath(region, instanceID)+"/change-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
|
|
}
|