Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 1 | package defsecrules |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 4 | "errors" |
| 5 | |
| 6 | "github.com/racker/perigee" |
| 7 | |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | ) |
| 11 | |
| 12 | // List will return a collection of default rules. |
| 13 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 14 | createPage := func(r pagination.PageResult) pagination.Page { |
| 15 | return DefaultRulePage{pagination.SinglePageBase(r)} |
| 16 | } |
| 17 | |
| 18 | return pagination.NewPager(client, rootURL(client), createPage) |
| 19 | } |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 20 | |
| 21 | // CreateOpts represents the configuration for adding a new default rule. |
| 22 | type CreateOpts struct { |
| 23 | // Required - the lower bound of the port range that will be opened. |
| 24 | FromPort int `json:"from_port"` |
| 25 | |
| 26 | // Required - the upper bound of the port range that will be opened. |
| 27 | ToPort int `json:"to_port"` |
| 28 | |
| 29 | // Required - the protocol type that will be allowed, e.g. TCP. |
| 30 | IPProtocol string `json:"ip_protocol"` |
| 31 | |
| 32 | // ONLY required if FromGroupID is blank. This represents the IP range that |
| 33 | // will be the source of network traffic to your security group. Use |
| 34 | // 0.0.0.0/0 to allow all IP addresses. |
| 35 | CIDR string `json:"cidr,omitempty"` |
| 36 | } |
| 37 | |
| 38 | // CreateRuleOptsBuilder builds the create rule options into a serializable format. |
| 39 | type CreateOptsBuilder interface { |
| 40 | ToRuleCreateMap() (map[string]interface{}, error) |
| 41 | } |
| 42 | |
| 43 | // ToRuleCreateMap builds the create rule options into a serializable format. |
| 44 | func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) { |
| 45 | rule := make(map[string]interface{}) |
| 46 | |
| 47 | if opts.FromPort == 0 { |
| 48 | return rule, errors.New("A FromPort must be set") |
| 49 | } |
| 50 | if opts.ToPort == 0 { |
| 51 | return rule, errors.New("A ToPort must be set") |
| 52 | } |
| 53 | if opts.IPProtocol == "" { |
| 54 | return rule, errors.New("A IPProtocol must be set") |
| 55 | } |
| 56 | if opts.CIDR == "" { |
| 57 | return rule, errors.New("A CIDR must be set") |
| 58 | } |
| 59 | |
| 60 | rule["from_port"] = opts.FromPort |
| 61 | rule["to_port"] = opts.ToPort |
| 62 | rule["ip_protocol"] = opts.IPProtocol |
| 63 | rule["cidr"] = opts.CIDR |
| 64 | |
| 65 | return map[string]interface{}{"security_group_default_rule": rule}, nil |
| 66 | } |
| 67 | |
| 68 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 69 | var result CreateResult |
| 70 | |
| 71 | reqBody, err := opts.ToRuleCreateMap() |
| 72 | if err != nil { |
| 73 | result.Err = err |
| 74 | return result |
| 75 | } |
| 76 | |
| 77 | _, result.Err = perigee.Request("POST", rootURL(client), perigee.Options{ |
| 78 | Results: &result.Body, |
| 79 | ReqBody: &reqBody, |
| 80 | MoreHeaders: client.AuthenticatedHeaders(), |
| 81 | OkCodes: []int{200}, |
| 82 | }) |
| 83 | |
| 84 | return result |
| 85 | } |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame^] | 86 | |
| 87 | // Get will return details for a particular default rule. |
| 88 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 89 | var result GetResult |
| 90 | |
| 91 | _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{ |
| 92 | Results: &result.Body, |
| 93 | MoreHeaders: client.AuthenticatedHeaders(), |
| 94 | OkCodes: []int{200}, |
| 95 | }) |
| 96 | |
| 97 | return result |
| 98 | } |