2016-06-28 22:35:41 -04:00
|
|
|
package profitbricks
|
|
|
|
|
2016-07-08 13:40:36 -04:00
|
|
|
import (
|
2016-08-01 07:15:21 -04:00
|
|
|
"bytes"
|
2016-07-08 13:40:36 -04:00
|
|
|
"encoding/json"
|
2016-08-01 07:15:21 -04:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2016-07-08 13:40:36 -04:00
|
|
|
)
|
2016-06-28 22:35:41 -04:00
|
|
|
|
2016-08-01 07:15:21 -04:00
|
|
|
type Datacenter struct {
|
|
|
|
Id string `json:"id,omitempty"`
|
|
|
|
Type_ string `json:"type,omitempty"`
|
|
|
|
Href string `json:"href,omitempty"`
|
|
|
|
Metadata *DatacenterElementMetadata `json:"metadata,omitempty"`
|
|
|
|
Properties DatacenterProperties `json:"properties,omitempty"`
|
|
|
|
Entities DatacenterEntities `json:"entities,omitempty"`
|
|
|
|
Response string `json:"Response,omitempty"`
|
|
|
|
Headers *http.Header `json:"headers,omitempty"`
|
|
|
|
StatusCode int `json:"headers,omitempty"`
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:15:21 -04:00
|
|
|
type DatacenterElementMetadata struct {
|
|
|
|
CreatedDate time.Time `json:"createdDate,omitempty"`
|
|
|
|
CreatedBy string `json:"createdBy,omitempty"`
|
|
|
|
Etag string `json:"etag,omitempty"`
|
|
|
|
LastModifiedDate time.Time `json:"lastModifiedDate,omitempty"`
|
|
|
|
LastModifiedBy string `json:"lastModifiedBy,omitempty"`
|
|
|
|
State string `json:"state,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DatacenterProperties struct {
|
2016-06-28 22:35:41 -04:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Location string `json:"location,omitempty"`
|
2016-08-01 07:15:21 -04:00
|
|
|
Version int32 `json:"version,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DatacenterEntities struct {
|
|
|
|
Servers *Servers `json:"servers,omitempty"`
|
|
|
|
Volumes *Volumes `json:"volumes,omitempty"`
|
|
|
|
Loadbalancers *Loadbalancers `json:"loadbalancers,omitempty"`
|
|
|
|
Lans *Lans `json:"lans,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Datacenters struct {
|
|
|
|
Id string `json:"id,omitempty"`
|
|
|
|
Type_ string `json:"type,omitempty"`
|
|
|
|
Href string `json:"href,omitempty"`
|
|
|
|
Items []Datacenter `json:"items,omitempty"`
|
|
|
|
Response string `json:"Response,omitempty"`
|
|
|
|
Headers *http.Header `json:"headers,omitempty"`
|
|
|
|
StatusCode int `json:"headers,omitempty"`
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:15:21 -04:00
|
|
|
func ListDatacenters() Datacenters {
|
2016-06-28 22:35:41 -04:00
|
|
|
path := dc_col_path()
|
2016-08-01 07:15:21 -04:00
|
|
|
url := mk_url(path) + `?depth=` + Depth
|
|
|
|
req, _ := http.NewRequest("GET", url, nil)
|
|
|
|
req.Header.Add("Content-Type", FullHeader)
|
|
|
|
resp := do(req)
|
|
|
|
return toDataCenters(resp)
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:15:21 -04:00
|
|
|
func CreateDatacenter(dc Datacenter) Datacenter {
|
2016-06-28 22:35:41 -04:00
|
|
|
obj, _ := json.Marshal(dc)
|
|
|
|
path := dc_col_path()
|
2016-08-01 07:15:21 -04:00
|
|
|
url := mk_url(path)
|
|
|
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(obj))
|
|
|
|
req.Header.Add("Content-Type", FullHeader)
|
|
|
|
|
|
|
|
return toDataCenter(do(req))
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:15:21 -04:00
|
|
|
func CompositeCreateDatacenter(datacenter Datacenter) Datacenter {
|
2016-07-08 13:40:36 -04:00
|
|
|
obj, _ := json.Marshal(datacenter)
|
|
|
|
path := dc_col_path()
|
2016-08-01 07:15:21 -04:00
|
|
|
url := mk_url(path) + `?depth=` + Depth
|
|
|
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(obj))
|
|
|
|
req.Header.Add("Content-Type", FullHeader)
|
|
|
|
return toDataCenter(do(req))
|
2016-07-08 13:40:36 -04:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:15:21 -04:00
|
|
|
func GetDatacenter(dcid string) Datacenter {
|
2016-06-28 22:35:41 -04:00
|
|
|
path := dc_path(dcid)
|
2016-08-01 07:15:21 -04:00
|
|
|
url := mk_url(path) + `?depth=` + Depth
|
|
|
|
req, _ := http.NewRequest("GET", url, nil)
|
|
|
|
req.Header.Add("Content-Type", FullHeader)
|
|
|
|
return toDataCenter(do(req))
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:15:21 -04:00
|
|
|
func PatchDatacenter(dcid string, obj DatacenterProperties) Datacenter {
|
2016-06-28 22:35:41 -04:00
|
|
|
jason_patch := []byte(MkJson(obj))
|
|
|
|
path := dc_path(dcid)
|
2016-08-01 07:15:21 -04:00
|
|
|
url := mk_url(path) + `?depth=` + Depth
|
|
|
|
req, _ := http.NewRequest("PATCH", url, bytes.NewBuffer(jason_patch))
|
|
|
|
req.Header.Add("Content-Type", PatchHeader)
|
|
|
|
return toDataCenter(do(req))
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteDatacenter(dcid string) Resp {
|
|
|
|
path := dc_path(dcid)
|
|
|
|
return is_delete(path)
|
|
|
|
}
|
2016-08-01 07:15:21 -04:00
|
|
|
|
|
|
|
func toDataCenter(resp Resp) Datacenter {
|
|
|
|
var dc Datacenter
|
|
|
|
json.Unmarshal(resp.Body, &dc)
|
|
|
|
dc.Response = string(resp.Body)
|
|
|
|
dc.Headers = &resp.Headers
|
|
|
|
dc.StatusCode = resp.StatusCode
|
|
|
|
return dc
|
|
|
|
}
|
|
|
|
|
|
|
|
func toDataCenters(resp Resp) Datacenters {
|
|
|
|
var col Datacenters
|
|
|
|
json.Unmarshal(resp.Body, &col)
|
|
|
|
col.Response = string(resp.Body)
|
|
|
|
col.Headers = &resp.Headers
|
|
|
|
col.StatusCode = resp.StatusCode
|
|
|
|
return col
|
|
|
|
}
|