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 IpBlock struct {
|
2017-08-10 09:15:53 -04:00
|
|
|
Id string `json:"id,omitempty"`
|
|
|
|
Type_ string `json:"type,omitempty"`
|
|
|
|
Href string `json:"href,omitempty"`
|
|
|
|
Metadata *Metadata `json:"metadata,omitempty"`
|
|
|
|
Properties IpBlockProperties `json:"properties,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 IpBlockProperties struct {
|
2017-08-10 09:15:53 -04:00
|
|
|
Name string `json:"name,omitempty"`
|
2016-08-01 07:15:21 -04:00
|
|
|
Ips []string `json:"ips,omitempty"`
|
|
|
|
Location string `json:"location,omitempty"`
|
|
|
|
Size int `json:"size,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type IpBlocks struct {
|
|
|
|
Id string `json:"id,omitempty"`
|
|
|
|
Type_ string `json:"type,omitempty"`
|
|
|
|
Href string `json:"href,omitempty"`
|
|
|
|
Items []IpBlock `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
|
|
|
}
|
|
|
|
|
|
|
|
// ListIpBlocks
|
2016-08-01 07:15:21 -04:00
|
|
|
func ListIpBlocks() IpBlocks {
|
2016-06-28 22:35:41 -04:00
|
|
|
path := ipblock_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)
|
|
|
|
return toIpBlocks(do(req))
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:15:21 -04:00
|
|
|
func ReserveIpBlock(request IpBlock) IpBlock {
|
2016-06-28 22:35:41 -04:00
|
|
|
obj, _ := json.Marshal(request)
|
|
|
|
path := ipblock_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 toIpBlock(do(req))
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
2016-08-01 07:15:21 -04:00
|
|
|
func GetIpBlock(ipblockid string) IpBlock {
|
2016-06-28 22:35:41 -04:00
|
|
|
path := ipblock_path(ipblockid)
|
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 toIpBlock(do(req))
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReleaseIpBlock(ipblockid string) Resp {
|
|
|
|
path := ipblock_path(ipblockid)
|
|
|
|
return is_delete(path)
|
|
|
|
}
|
2016-08-01 07:15:21 -04:00
|
|
|
|
|
|
|
func toIpBlock(resp Resp) IpBlock {
|
|
|
|
var obj IpBlock
|
|
|
|
json.Unmarshal(resp.Body, &obj)
|
|
|
|
obj.Response = string(resp.Body)
|
|
|
|
obj.Headers = &resp.Headers
|
|
|
|
obj.StatusCode = resp.StatusCode
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
func toIpBlocks(resp Resp) IpBlocks {
|
|
|
|
var col IpBlocks
|
|
|
|
json.Unmarshal(resp.Body, &col)
|
|
|
|
col.Response = string(resp.Body)
|
|
|
|
col.Headers = &resp.Headers
|
|
|
|
col.StatusCode = resp.StatusCode
|
|
|
|
return col
|
|
|
|
}
|