github.com/1and1/oneandone-cloudserver-sdk-go@v1.4.1/oneandone.go (about)

     1  package oneandone
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  	"reflect"
     7  	"time"
     8  )
     9  
    10  // Struct to hold the required information for accessing the API.
    11  //
    12  // Instances of this type contain the URL of the endpoint to access the API as well as the API access token to be used.
    13  // They offer also all methods that allow to access the various objects that are returned by top level resources of
    14  // the API.
    15  type API struct {
    16  	Endpoint string
    17  	Client   *restClient
    18  }
    19  
    20  type ApiPtr struct {
    21  	api *API
    22  }
    23  
    24  type idField struct {
    25  	Id string `json:"id,omitempty"`
    26  }
    27  
    28  type typeField struct {
    29  	Type string `json:"type,omitempty"`
    30  }
    31  
    32  type nameField struct {
    33  	Name string `json:"name,omitempty"`
    34  }
    35  
    36  type descField struct {
    37  	Description string `json:"description,omitempty"`
    38  }
    39  
    40  type countField struct {
    41  	Count int `json:"count,omitempty"`
    42  }
    43  
    44  type serverIps struct {
    45  	ServerIps []string `json:"server_ips"`
    46  }
    47  
    48  type servers struct {
    49  	Servers []string `json:"servers"`
    50  }
    51  
    52  type ApiInstance interface {
    53  	GetState() (string, error)
    54  }
    55  
    56  const (
    57  	datacenterPathSegment        = "datacenters"
    58  	dvdIsoPathSegment            = "dvd_isos"
    59  	firewallPolicyPathSegment    = "firewall_policies"
    60  	imagePathSegment             = "images"
    61  	loadBalancerPathSegment      = "load_balancers"
    62  	logPathSegment               = "logs"
    63  	monitorCenterPathSegment     = "monitoring_center"
    64  	monitorPolicyPathSegment     = "monitoring_policies"
    65  	pingPathSegment              = "ping"
    66  	pingAuthPathSegment          = "ping_auth"
    67  	pricingPathSegment           = "pricing"
    68  	privateNetworkPathSegment    = "private_networks"
    69  	publicIpPathSegment          = "public_ips"
    70  	rolePathSegment              = "roles"
    71  	serverPathSegment            = "servers"
    72  	serverAppliancePathSegment   = "server_appliances"
    73  	sharedStoragePathSegment     = "shared_storages"
    74  	blockStoragePathSegment      = "block_storages"
    75  	sshkeyPathSegment            = "ssh_keys"
    76  	baremetalSegment             = "baremetal_models"
    77  	recoveryAppliancePathSegment = "recovery_appliances"
    78  	usagePathSegment             = "usages"
    79  	userPathSegment              = "users"
    80  	vpnPathSegment               = "vpns"
    81  )
    82  
    83  // Struct to hold the status of an API object.
    84  //
    85  // Values of this type are used to represent the status of API objects like servers, firewall policies and the like.
    86  //
    87  // The value of the "State" field can represent fixed states like "ACTIVE" or "POWERED_ON" but also transitional
    88  // states like "POWERING_ON" or "CONFIGURING".
    89  //
    90  // For fixed states the "Percent" field is empty where as for transitional states it contains the progress of the
    91  // transition in percent.
    92  type Status struct {
    93  	State   string `json:"state"`
    94  	Percent int    `json:"percent"`
    95  }
    96  
    97  type statusState struct {
    98  	State string `json:"state,omitempty"`
    99  }
   100  
   101  type Identity struct {
   102  	idField
   103  	nameField
   104  }
   105  
   106  type License struct {
   107  	nameField
   108  }
   109  
   110  // Creates a new API instance.
   111  //
   112  // Explanations about given token and url information can be found online under the following url TODO add url!
   113  func New(token string, url string) *API {
   114  	api := new(API)
   115  	api.Endpoint = url
   116  	api.Client = newRestClient(token)
   117  	return api
   118  }
   119  
   120  // Converts a given integer value into a pointer of the same type.
   121  func Int2Pointer(input int) *int {
   122  	result := new(int)
   123  	*result = input
   124  	return result
   125  }
   126  
   127  // Converts a given boolean value into a pointer of the same type.
   128  func Bool2Pointer(input bool) *bool {
   129  	result := new(bool)
   130  	*result = input
   131  	return result
   132  }
   133  
   134  // Performs busy-waiting for types that implement ApiInstance interface.
   135  func (api *API) WaitForState(in ApiInstance, state string, sec time.Duration, count int) error {
   136  	if in != nil {
   137  		for i := 0; i < count; i++ {
   138  			s, err := in.GetState()
   139  			if err != nil {
   140  				return err
   141  			}
   142  			if s == state {
   143  				return nil
   144  			}
   145  			time.Sleep(sec * time.Second)
   146  		}
   147  		return errors.New(reflect.ValueOf(in).Type().String() + " operation timeout.")
   148  	}
   149  	return nil
   150  }
   151  
   152  // Waits until instance is deleted for types that implement ApiInstance interface.
   153  func (api *API) WaitUntilDeleted(in ApiInstance) error {
   154  	var err error
   155  	for in != nil {
   156  		_, err = in.GetState()
   157  		if err != nil {
   158  			if apiError, ok := err.(ApiError); ok && apiError.httpStatusCode == http.StatusNotFound {
   159  				return nil
   160  			} else {
   161  				return err
   162  			}
   163  		}
   164  		time.Sleep(5 * time.Second)
   165  	}
   166  	return nil
   167  }