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

     1  package oneandone
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  type Image struct {
     8  	idField
     9  	nameField
    10  	descField
    11  	MinHddSize   int         `json:"min_hdd_size"`
    12  	Architecture *int        `json:"architecture,omitempty"`
    13  	NumImages    *int        `json:"num_images,omitempty"`
    14  	Frequency    string      `json:"frequency,omitempty"`
    15  	ServerId     string      `json:"server_id,omitempty"`
    16  	CreationDate string      `json:"creation_date,omitempty"`
    17  	State        string      `json:"state,omitempty"`
    18  	OsImageType  string      `json:"os_image_type,omitempty"`
    19  	Os           string      `json:"os,omitempty"`
    20  	OsFamily     string      `json:"os_family,omitempty"`
    21  	OsVersion    string      `json:"os_version,omitempty"`
    22  	Type         string      `json:"type,omitempty"`
    23  	Licenses     []License   `json:"licenses,omitempty"`
    24  	Hdds         []Hdd       `json:"hdds,omitempty"`
    25  	Datacenter   *Datacenter `json:"datacenter,omitempty"`
    26  	ApiPtr
    27  }
    28  
    29  type ImageRequest struct {
    30  	Name         string `json:"name,omitempty"`
    31  	Description  string `json:"description,omitempty"`
    32  	Frequency    string `json:"frequency,omitempty"`
    33  	ServerId     string `json:"server_id,omitempty"`
    34  	DatacenterId string `json:"datacenter_id,omitempty"`
    35  	Source       string `json:"source,omitempty"`
    36  	Url          string `json:"url,omitempty"`
    37  	OsId         string `json:"os_id,omitempty"`
    38  	Type         string `json:"type,omitempty"`
    39  	NumImages    *int   `json:"num_images,omitempty"`
    40  }
    41  
    42  type UpdateImageRequest struct {
    43  	Name        string `json:"name,omitempty"`
    44  	Description string `json:"description,omitempty"`
    45  	Frequency   string `json:"frequency,omitempty"`
    46  }
    47  
    48  type ImageOs struct {
    49  	idField
    50  	Architecture *int   `json:"architecture,omitempty"`
    51  	Os           string `json:"os,omitempty"`
    52  	OsFamily     string `json:"os_family,omitempty"`
    53  	OsVersion    string `json:"os_version,omitempty"`
    54  }
    55  
    56  // GET /images
    57  func (api *API) ListImages(args ...interface{}) ([]Image, error) {
    58  	url, err := processQueryParams(createUrl(api, imagePathSegment), args...)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	result := []Image{}
    63  	err = api.Client.Get(url, &result, http.StatusOK)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	for index, _ := range result {
    68  		result[index].api = api
    69  	}
    70  	return result, nil
    71  }
    72  
    73  // GET /images/os
    74  func (api *API) ListImageOs(args ...interface{}) ([]ImageOs, error) {
    75  	url, err := processQueryParams(createUrl(api, imagePathSegment, "os"), args...)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	result := []ImageOs{}
    80  	err = api.Client.Get(url, &result, http.StatusOK)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return result, nil
    86  }
    87  
    88  // POST /images
    89  func (api *API) CreateImage(request *ImageRequest) (string, *Image, error) {
    90  	res := new(Image)
    91  	url := createUrl(api, imagePathSegment)
    92  	err := api.Client.Post(url, &request, &res, http.StatusAccepted)
    93  	if err != nil {
    94  		return "", nil, err
    95  	}
    96  	res.api = api
    97  	return res.Id, res, nil
    98  }
    99  
   100  // GET /images/{id}
   101  func (api *API) GetImage(img_id string) (*Image, error) {
   102  	result := new(Image)
   103  	url := createUrl(api, imagePathSegment, img_id)
   104  	err := api.Client.Get(url, &result, http.StatusOK)
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  	result.api = api
   109  	return result, nil
   110  }
   111  
   112  // DELETE /images/{id}
   113  func (api *API) DeleteImage(img_id string) (*Image, error) {
   114  	result := new(Image)
   115  	url := createUrl(api, imagePathSegment, img_id)
   116  	err := api.Client.Delete(url, nil, &result, http.StatusAccepted)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	result.api = api
   121  	return result, nil
   122  }
   123  
   124  // PUT /images/{id}
   125  func (api *API) UpdateImage(img_id string, request *UpdateImageRequest) (*Image, error) {
   126  	result := new(Image)
   127  	url := createUrl(api, imagePathSegment, img_id)
   128  	err := api.Client.Put(url, &request, &result, http.StatusOK)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	result.api = api
   133  	return result, nil
   134  }
   135  
   136  func (im *Image) GetState() (string, error) {
   137  	in, err := im.api.GetImage(im.Id)
   138  	if in == nil {
   139  		return "", err
   140  	}
   141  	return in.State, err
   142  }