packer-cn/vendor/github.com/profitbricks/profitbricks-sdk-go/lan.go

110 lines
3.2 KiB
Go
Raw Normal View History

2016-06-28 22:35:41 -04:00
package profitbricks
2016-08-01 07:15:21 -04:00
import (
"bytes"
"encoding/json"
"net/http"
)
2016-06-28 22:35:41 -04:00
2016-08-01 07:15:21 -04:00
type Lan struct {
Id string `json:"id,omitempty"`
Type_ string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Metadata *DatacenterElementMetadata `json:"metadata,omitempty"`
Properties LanProperties `json:"properties,omitempty"`
Entities *LanEntities `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
}
type LanProperties struct {
2016-08-01 07:15:21 -04:00
Name string `json:"name,omitempty"`
Public bool `json:"public,omitempty"`
}
type LanEntities struct {
Nics *LanNics `json:"nics,omitempty"`
}
type LanNics struct {
Id string `json:"id,omitempty"`
Type_ string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Nic `json:"items,omitempty"`
}
type Lans struct {
Id string `json:"id,omitempty"`
Type_ string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Lan `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
}
// ListLan returns a Collection for lans in the Datacenter
2016-08-01 07:15:21 -04:00
func ListLans(dcid string) Lans {
2016-06-28 22:35:41 -04:00
path := lan_col_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 toLans(do(req))
2016-06-28 22:35:41 -04:00
}
// CreateLan creates a lan in the datacenter
// from a jason []byte and returns a Instance struct
2016-08-01 07:15:21 -04:00
func CreateLan(dcid string, request Lan) Lan {
2016-06-28 22:35:41 -04:00
obj, _ := json.Marshal(request)
path := lan_col_path(dcid)
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 toLan(do(req))
2016-06-28 22:35:41 -04:00
}
// GetLan pulls data for the lan where id = lanid returns an Instance struct
2016-08-01 07:15:21 -04:00
func GetLan(dcid, lanid string) Lan {
2016-06-28 22:35:41 -04:00
path := lan_path(dcid, lanid)
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 toLan(do(req))
2016-06-28 22:35:41 -04:00
}
// PatchLan does a partial update to a lan using json from []byte jason
// returns a Instance struct
2016-08-01 07:15:21 -04:00
func PatchLan(dcid string, lanid string, obj LanProperties) Lan {
2016-06-28 22:35:41 -04:00
jason := []byte(MkJson(obj))
path := lan_path(dcid, lanid)
2016-08-01 07:15:21 -04:00
url := mk_url(path)
req, _ := http.NewRequest("PATCH", url, bytes.NewBuffer(jason))
req.Header.Add("Content-Type", PatchHeader)
return toLan(do(req))
2016-06-28 22:35:41 -04:00
}
// DeleteLan deletes a lan where id == lanid
func DeleteLan(dcid, lanid string) Resp {
path := lan_path(dcid, lanid)
return is_delete(path)
}
2016-08-01 07:15:21 -04:00
func toLan(resp Resp) Lan {
var lan Lan
json.Unmarshal(resp.Body, &lan)
lan.Response = string(resp.Body)
lan.Headers = &resp.Headers
lan.StatusCode = resp.StatusCode
return lan
}
func toLans(resp Resp) Lans {
var col Lans
json.Unmarshal(resp.Body, &col)
col.Response = string(resp.Body)
col.Headers = &resp.Headers
col.StatusCode = resp.StatusCode
return col
2016-06-28 22:35:41 -04:00
}