114 lines
5.0 KiB
Go
114 lines
5.0 KiB
Go
package awsserversdk
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
type InstancesService struct{ client *Client }
|
|
type InstanceListOptions struct {
|
|
State string
|
|
Name string
|
|
Limit int
|
|
Offset int
|
|
}
|
|
type ResizeInstanceRequest struct {
|
|
InstanceType string `json:"instance_type"`
|
|
}
|
|
type RebuildInstanceRequest struct {
|
|
ImageID string `json:"image_id"`
|
|
InstanceType string `json:"instance_type,omitempty"`
|
|
AssociateEIP bool `json:"associate_eip,omitempty"`
|
|
InstallAgent *bool `json:"install_agent,omitempty"`
|
|
ExpectedConfigurationFingerprint string `json:"expected_configuration_fingerprint,omitempty"`
|
|
}
|
|
type RefreshDetailsRequest struct {
|
|
Sections []string `json:"sections,omitempty"`
|
|
}
|
|
|
|
func (s *InstancesService) Create(ctx context.Context, region string, input CreateInstanceRequest, options ...RequestOption) (*Operation, error) {
|
|
requestOptions := mutationOptions(options)
|
|
var result Operation
|
|
err := s.client.do(ctx, http.MethodPost, instancesPath(region, ""), input, &result, requestOptions.idempotencyKey)
|
|
result.client = s.client
|
|
return &result, err
|
|
}
|
|
func (s *InstancesService) List(ctx context.Context, region string, options InstanceListOptions) (*Page[Instance], error) {
|
|
values := url.Values{}
|
|
if options.State != "" {
|
|
values.Set("state", options.State)
|
|
}
|
|
if options.Name != "" {
|
|
values.Set("name", options.Name)
|
|
}
|
|
if options.Limit > 0 {
|
|
values.Set("limit", strconv.Itoa(options.Limit))
|
|
}
|
|
if options.Offset > 0 {
|
|
values.Set("offset", strconv.Itoa(options.Offset))
|
|
}
|
|
path := instancesPath(region, "")
|
|
if encoded := values.Encode(); encoded != "" {
|
|
path += "?" + encoded
|
|
}
|
|
var result Page[Instance]
|
|
err := s.client.do(ctx, http.MethodGet, path, nil, &result, "")
|
|
return &result, err
|
|
}
|
|
func (s *InstancesService) Get(ctx context.Context, region, instanceID string) (*Instance, error) {
|
|
var result Instance
|
|
err := s.client.do(ctx, http.MethodGet, instancesPath(region, instanceID), nil, &result, "")
|
|
return &result, err
|
|
}
|
|
func (s *InstancesService) GetDetails(ctx context.Context, region, instanceID string) (InstanceDetails, error) {
|
|
result := InstanceDetails{}
|
|
err := s.client.do(ctx, http.MethodGet, instancesPath(region, instanceID)+"/details", nil, &result, "")
|
|
return result, err
|
|
}
|
|
func (s *InstancesService) RefreshDetails(ctx context.Context, region, instanceID string, input RefreshDetailsRequest, options ...RequestOption) (*Operation, error) {
|
|
return s.operation(ctx, http.MethodPost, instancesPath(region, instanceID)+"/details/refresh", input, options...)
|
|
}
|
|
func (s *InstancesService) Start(ctx context.Context, region, instanceID string, options ...RequestOption) (*Operation, error) {
|
|
return s.action(ctx, region, instanceID, "start", options...)
|
|
}
|
|
func (s *InstancesService) Stop(ctx context.Context, region, instanceID string, options ...RequestOption) (*Operation, error) {
|
|
return s.action(ctx, region, instanceID, "stop", options...)
|
|
}
|
|
func (s *InstancesService) Reboot(ctx context.Context, region, instanceID string, options ...RequestOption) (*Operation, error) {
|
|
return s.action(ctx, region, instanceID, "reboot", options...)
|
|
}
|
|
func (s *InstancesService) Delete(ctx context.Context, region, instanceID string, options ...RequestOption) (*Operation, error) {
|
|
return s.operation(ctx, http.MethodDelete, instancesPath(region, instanceID), nil, options...)
|
|
}
|
|
func (s *InstancesService) RetryCreate(ctx context.Context, region, instanceID string, options ...RequestOption) (*Operation, error) {
|
|
return s.operation(ctx, http.MethodPost, instancesPath(region, instanceID)+"/retry", map[string]any{}, options...)
|
|
}
|
|
func (s *InstancesService) Resize(ctx context.Context, region, instanceID string, input ResizeInstanceRequest, options ...RequestOption) (*Operation, error) {
|
|
return s.operation(ctx, http.MethodPost, instancesPath(region, instanceID)+"/resize", input, options...)
|
|
}
|
|
func (s *InstancesService) Rebuild(ctx context.Context, region, instanceID string, input RebuildInstanceRequest, options ...RequestOption) (*Operation, error) {
|
|
return s.operation(ctx, http.MethodPost, instancesPath(region, instanceID)+"/rebuild", input, options...)
|
|
}
|
|
func (s *InstancesService) action(ctx context.Context, region, instanceID, action string, options ...RequestOption) (*Operation, error) {
|
|
return s.operation(ctx, http.MethodPost, instancesPath(region, instanceID)+"/actions", map[string]string{"action": action}, options...)
|
|
}
|
|
func (s *InstancesService) operation(ctx context.Context, method, path string, input any, options ...RequestOption) (*Operation, error) {
|
|
requestOptions := mutationOptions(options)
|
|
var result Operation
|
|
err := s.client.do(ctx, method, path, input, &result, requestOptions.idempotencyKey)
|
|
if result.TaskID == "" {
|
|
result.TaskID = result.CommandID
|
|
}
|
|
result.client = s.client
|
|
return &result, err
|
|
}
|
|
func instancesPath(region, instanceID string) string {
|
|
path := "/regions/" + escaped(region) + "/instances"
|
|
if instanceID != "" {
|
|
path += "/" + escaped(instanceID)
|
|
}
|
|
return path
|
|
}
|