Debian Codegraph @

cloudapi/firewalls.go

raw symbols SCIP
1 package cloudapi
2
3 import (
4 "net/http"
5
6 "github.com/joyent/gocommon/client"
7 "github.com/joyent/gocommon/errors"
8 )
9
10 // FirewallRule represent a firewall rule that can be specifed for a machine.
11 type FirewallRule struct {
12 Id string // Unique identifier for the rule
13 Enabled bool // Whether the rule is enabled or not
14 Rule string // Firewall rule in the form 'FROM <target a> TO <target b> <action> <protocol> <port>'
15 }
16
17 // CreateFwRuleOpts represent the option that can be specified
18 // when creating a new firewall rule.
19 type CreateFwRuleOpts struct {
20 Enabled bool `json:"enabled"` // Whether to enable the rule or not
21 Rule string `json:"rule"` // Firewall rule in the form 'FROM <target a> TO <target b> <action> <protocol> <port>'
22 }
23
24 // ListFirewallRules lists all the firewall rules on record for a specified account.
25 // See API docs: http://apidocs.joyent.com/cloudapi/#ListFirewallRules
26 func (c *Client) ListFirewallRules() ([]FirewallRule, error) {
27 var resp []FirewallRule
28 req := request{
29 method: client.GET,
30 url: apiFirewallRules,
31 resp: &resp,
32 }
33 if _, err := c.sendRequest(req); err != nil {
34 return nil, errors.Newf(err, "failed to get list of firewall rules")
35 }
36 return resp, nil
37 }
38
39 // GetFirewallRule returns the specified firewall rule.
40 // See API docs: http://apidocs.joyent.com/cloudapi/#GetFirewallRule
41 func (c *Client) GetFirewallRule(fwRuleID string) (*FirewallRule, error) {
42 var resp FirewallRule
43 req := request{
44 method: client.GET,
45 url: makeURL(apiFirewallRules, fwRuleID),
46 resp: &resp,
47 }
48 if _, err := c.sendRequest(req); err != nil {
49 return nil, errors.Newf(err, "failed to get firewall rule with id %s", fwRuleID)
50 }
51 return &resp, nil
52 }
53
54 // CreateFirewallRule creates the firewall rule with the specified options.
55 // See API docs: http://apidocs.joyent.com/cloudapi/#CreateFirewallRule
56 func (c *Client) CreateFirewallRule(opts CreateFwRuleOpts) (*FirewallRule, error) {
57 var resp FirewallRule
58 req := request{
59 method: client.POST,
60 url: apiFirewallRules,
61 reqValue: opts,
62 resp: &resp,
63 expectedStatus: http.StatusCreated,
64 }
65 if _, err := c.sendRequest(req); err != nil {
66 return nil, errors.Newf(err, "failed to create firewall rule: %s", opts.Rule)
67 }
68 return &resp, nil
69 }
70
71 // UpdateFirewallRule updates the specified firewall rule.
72 // See API docs: http://apidocs.joyent.com/cloudapi/#UpdateFirewallRule
73 func (c *Client) UpdateFirewallRule(fwRuleID string, opts CreateFwRuleOpts) (*FirewallRule, error) {
74 var resp FirewallRule
75 req := request{
76 method: client.POST,
77 url: makeURL(apiFirewallRules, fwRuleID),
78 reqValue: opts,
79 resp: &resp,
80 }
81 if _, err := c.sendRequest(req); err != nil {
82 return nil, errors.Newf(err, "failed to update firewall rule with id %s to %s", fwRuleID, opts.Rule)
83 }
84 return &resp, nil
85 }
86
87 // EnableFirewallRule enables the given firewall rule record if it is disabled.
88 // See API docs: http://apidocs.joyent.com/cloudapi/#EnableFirewallRule
89 func (c *Client) EnableFirewallRule(fwRuleID string) (*FirewallRule, error) {
90 var resp FirewallRule
91 req := request{
92 method: client.POST,
93 url: makeURL(apiFirewallRules, fwRuleID, apiFirewallRulesEnable),
94 resp: &resp,
95 }
96 if _, err := c.sendRequest(req); err != nil {
97 return nil, errors.Newf(err, "failed to enable firewall rule with id %s", fwRuleID)
98 }
99 return &resp, nil
100 }
101
102 // DisableFirewallRule disables the given firewall rule record if it is enabled.
103 // See API docs: http://apidocs.joyent.com/cloudapi/#DisableFirewallRule
104 func (c *Client) DisableFirewallRule(fwRuleID string) (*FirewallRule, error) {
105 var resp FirewallRule
106 req := request{
107 method: client.POST,
108 url: makeURL(apiFirewallRules, fwRuleID, apiFirewallRulesDisable),
109 resp: &resp,
110 }
111 if _, err := c.sendRequest(req); err != nil {
112 return nil, errors.Newf(err, "failed to disable firewall rule with id %s", fwRuleID)
113 }
114 return &resp, nil
115 }
116
117 // DeleteFirewallRule removes the given firewall rule record from all the required account machines.
118 // See API docs: http://apidocs.joyent.com/cloudapi/#DeleteFirewallRule
119 func (c *Client) DeleteFirewallRule(fwRuleID string) error {
120 req := request{
121 method: client.DELETE,
122 url: makeURL(apiFirewallRules, fwRuleID),
123 expectedStatus: http.StatusNoContent,
124 }
125 if _, err := c.sendRequest(req); err != nil {
126 return errors.Newf(err, "failed to delete firewall rule with id %s", fwRuleID)
127 }
128 return nil
129 }
130
131 // ListFirewallRuleMachines return the list of machines affected by the given firewall rule.
132 // See API docs: http://apidocs.joyent.com/cloudapi/#ListFirewallRuleMachines
133 func (c *Client) ListFirewallRuleMachines(fwRuleID string) ([]Machine, error) {
134 var resp []Machine
135 req := request{
136 method: client.GET,
137 url: makeURL(apiFirewallRules, fwRuleID, apiMachines),
138 resp: &resp,
139 }
140 if _, err := c.sendRequest(req); err != nil {
141 return nil, errors.Newf(err, "failed to get list of machines affected by firewall rule wit id %s", fwRuleID)
142 }
143 return resp, nil
144 }