summaryrefslogtreecommitdiff
path: root/vendor/github.com/mitchellh/packer/vendor/github.com/profitbricks/profitbricks-sdk-go/firewallrule.go
blob: 1d5ef4ec4fc256fc41d912d8adace6f9b44e4d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package profitbricks

import (
	"bytes"
	"encoding/json"
	"net/http"
)

type FirewallRule struct {
	Id         string                 `json:"id,omitempty"`
	Type_      string                 `json:"type,omitempty"`
	Href       string                 `json:"href,omitempty"`
	Metadata   *Metadata              `json:"metadata,omitempty"`
	Properties FirewallruleProperties `json:"properties,omitempty"`
	Response   string                 `json:"Response,omitempty"`
	Headers    *http.Header           `json:"headers,omitempty"`
	StatusCode int                    `json:"headers,omitempty"`
}

type FirewallruleProperties struct {
	Name           string  `json:"name,omitempty"`
	Protocol       string  `json:"protocol,omitempty"`
	SourceMac      *string `json:"sourceMac,omitempty"`
	SourceIp       *string `json:"sourceIp,omitempty"`
	TargetIp       *string `json:"targetIp,omitempty"`
	IcmpCode       *int    `json:"icmpCode,omitempty"`
	IcmpType       *int    `json:"icmpType,omitempty"`
	PortRangeStart *int    `json:"portRangeStart,omitempty"`
	PortRangeEnd   *int    `json:"portRangeEnd,omitempty"`
}

type FirewallRules struct {
	Id         string         `json:"id,omitempty"`
	Type_      string         `json:"type,omitempty"`
	Href       string         `json:"href,omitempty"`
	Items      []FirewallRule `json:"items,omitempty"`
	Response   string         `json:"Response,omitempty"`
	Headers    *http.Header   `json:"headers,omitempty"`
	StatusCode int            `json:"headers,omitempty"`
}

func ListFirewallRules(dcId string, serverid string, nicId string) FirewallRules {
	path := fwrule_col_path(dcId, serverid, nicId)
	url := mk_url(path) + `?depth=` + Depth
	req, _ := http.NewRequest("GET", url, nil)
	req.Header.Add("Content-Type", FullHeader)
	resp := do(req)
	return toFirewallRules(resp)
}

func GetFirewallRule(dcid string, srvid string, nicId string, fwId string) FirewallRule {
	path := fwrule_path(dcid, srvid, nicId, fwId)
	url := mk_url(path) + `?depth=` + Depth
	req, _ := http.NewRequest("GET", url, nil)
	req.Header.Add("Content-Type", FullHeader)
	resp := do(req)
	return toFirewallRule(resp)
}

func CreateFirewallRule(dcid string, srvid string, nicId string, fw FirewallRule) FirewallRule {
	path := fwrule_col_path(dcid, srvid, nicId)
	url := mk_url(path) + `?depth=` + Depth
	obj, _ := json.Marshal(fw)
	req, _ := http.NewRequest("POST", url, bytes.NewBuffer(obj))
	req.Header.Add("Content-Type", FullHeader)
	return toFirewallRule(do(req))
}

func PatchFirewallRule(dcid string, srvid string, nicId string, fwId string, obj FirewallruleProperties) FirewallRule {
	jason_patch := []byte(MkJson(obj))
	path := fwrule_path(dcid, srvid, nicId, fwId)
	url := mk_url(path) + `?depth=` + Depth
	req, _ := http.NewRequest("PATCH", url, bytes.NewBuffer(jason_patch))
	req.Header.Add("Content-Type", PatchHeader)
	return toFirewallRule(do(req))
}

func DeleteFirewallRule(dcid string, srvid string, nicId string, fwId string) Resp {
	path := fwrule_path(dcid, srvid, nicId, fwId)
	return is_delete(path)
}

func toFirewallRule(resp Resp) FirewallRule {
	var dc FirewallRule
	json.Unmarshal(resp.Body, &dc)
	dc.Response = string(resp.Body)
	dc.Headers = &resp.Headers
	dc.StatusCode = resp.StatusCode
	return dc
}

func toFirewallRules(resp Resp) FirewallRules {
	var col FirewallRules
	json.Unmarshal(resp.Body, &col)
	col.Response = string(resp.Body)
	col.Headers = &resp.Headers
	col.StatusCode = resp.StatusCode
	return col
}