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

112 lines
3.4 KiB
Go
Raw Normal View History

2016-06-28 22:35:41 -04:00
package profitbricks
import (
2016-08-01 07:15:21 -04:00
"bytes"
2016-06-28 22:35:41 -04:00
"encoding/json"
2016-08-01 07:15:21 -04:00
"net/http"
2016-06-28 22:35:41 -04:00
)
2016-08-01 07:15:21 -04:00
type Nic struct {
Id string `json:"id,omitempty"`
Type_ string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Properties *NicProperties `json:"properties,omitempty"`
Entities *NicEntities `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 NicProperties struct {
2016-08-01 07:15:21 -04:00
Name string `json:"name,omitempty"`
Mac string `json:"mac,omitempty"`
Ips []string `json:"ips,omitempty"`
Dhcp bool `json:"dhcp"`
2016-08-01 07:15:21 -04:00
Lan int `json:"lan,omitempty"`
FirewallActive bool `json:"firewallActive,omitempty"`
Nat bool `json:"nat,omitempty"`
2016-08-01 07:15:21 -04:00
}
type NicEntities struct {
Firewallrules *FirewallRules `json:"firewallrules,omitempty"`
}
type Nics struct {
Id string `json:"id,omitempty"`
Type_ string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Nic `json:"items,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"headers,omitempty"`
}
type NicCreateRequest struct {
NicProperties `json:"properties"`
2016-06-28 22:35:41 -04:00
}
// ListNics returns a Nics struct collection
2016-08-01 07:15:21 -04:00
func ListNics(dcid, srvid string) Nics {
path := nic_col_path(dcid, srvid) + `?depth=` + Depth
url := mk_url(path) + `?depth=` + Depth
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", FullHeader)
return toNics(do(req))
2016-06-28 22:35:41 -04:00
}
// CreateNic creates a nic on a server
// from a jason []byte and returns a Instance struct
func CreateNic(dcid string, srvid string, nic Nic) Nic {
obj, _ := json.Marshal(nic)
2016-06-28 22:35:41 -04:00
path := nic_col_path(dcid, srvid)
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 toNic(do(req))
2016-06-28 22:35:41 -04:00
}
// GetNic pulls data for the nic where id = srvid returns a Instance struct
2016-08-01 07:15:21 -04:00
func GetNic(dcid, srvid, nicid string) Nic {
2016-06-28 22:35:41 -04:00
path := nic_path(dcid, srvid, nicid)
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 toNic(do(req))
2016-06-28 22:35:41 -04:00
}
// PatchNic partial update of nic properties passed in as jason []byte
// Returns Instance struct
2016-08-01 07:15:21 -04:00
func PatchNic(dcid string, srvid string, nicid string, obj NicProperties) Nic {
2016-06-28 22:35:41 -04:00
jason := []byte(MkJson(obj))
path := nic_path(dcid, srvid, nicid)
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 toNic(do(req))
2016-06-28 22:35:41 -04:00
}
// DeleteNic deletes the nic where id=nicid and returns a Resp struct
func DeleteNic(dcid, srvid, nicid string) Resp {
path := nic_path(dcid, srvid, nicid)
return is_delete(path)
}
2016-08-01 07:15:21 -04:00
func toNic(resp Resp) Nic {
var obj Nic
json.Unmarshal(resp.Body, &obj)
obj.Response = string(resp.Body)
obj.Headers = &resp.Headers
obj.StatusCode = resp.StatusCode
return obj
}
func toNics(resp Resp) Nics {
var col Nics
json.Unmarshal(resp.Body, &col)
col.Response = string(resp.Body)
col.Headers = &resp.Headers
col.StatusCode = resp.StatusCode
return col
}