blob: e2ac8538dcbfc16e0c6ee43d8da52350590dfe7d [file] [log] [blame]
Jamie Hannaford924c09d2014-11-19 12:05:38 +01001package secgroups
2
3import (
4 "github.com/mitchellh/mapstructure"
5
Jamie Hannaforda493e642014-11-19 12:40:30 +01006 "github.com/rackspace/gophercloud"
Jamie Hannaford924c09d2014-11-19 12:05:38 +01007 "github.com/rackspace/gophercloud/pagination"
8)
9
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010010// SecurityGroup represents a security group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010011type SecurityGroup struct {
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010012 // The unique ID of the group.
Jamie Hannaford558572f2014-11-24 14:31:57 +010013 ID int
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010014
15 // The human-readable name of the group, which needs to be unique.
16 Name string
17
18 // The human-readable description of the group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010019 Description string
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010020
21 // The rules which determine how this security group operates.
22 Rules []Rule
23
Jamie Hannaford04abbc72014-11-21 11:27:57 +010024 // The ID of the tenant to which this security group belongs.
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010025 TenantID string `mapstructure:"tenant_id"`
Jamie Hannaford924c09d2014-11-19 12:05:38 +010026}
27
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010028// Rule represents a security group rule, a policy which determines how a
29// security group operates and what inbound traffic it allows in.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010030type Rule struct {
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010031 // The unique ID
Jamie Hannaford558572f2014-11-24 14:31:57 +010032 ID int
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010033
34 // The lower bound of the port range which this security group should open up
35 FromPort int `mapstructure:"from_port"`
36
37 // The upper bound of the port range which this security group should open up
38 ToPort int `mapstructure:"to_port"`
39
40 // The IP protocol (e.g. TCP) which the security group accepts
41 IPProtocol string `mapstructure:"ip_protocol"`
42
43 // The CIDR IP range whose traffic can be received
44 IPRange IPRange `mapstructure:"ip_range"`
45
Jamie Hannaford04abbc72014-11-21 11:27:57 +010046 // The security group ID to which this rule belongs
Jamie Hannaford558572f2014-11-24 14:31:57 +010047 ParentGroupID int `mapstructure:"parent_group_id"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010048
49 // Not documented.
50 Group Group
Jamie Hannaford924c09d2014-11-19 12:05:38 +010051}
52
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010053// IPRange represents the IP range whose traffic will be accepted by the
54// security group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010055type IPRange struct {
56 CIDR string
57}
58
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010059// Group represents a group.
Jamie Hannafordb38dd312014-11-19 13:02:11 +010060type Group struct {
61 TenantID string `mapstructure:"tenant_id"`
62 Name string
63}
64
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010065// SecurityGroupPage is a single page of a SecurityGroup collection.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010066type SecurityGroupPage struct {
67 pagination.SinglePageBase
68}
69
70// IsEmpty determines whether or not a page of Security Groups contains any results.
71func (page SecurityGroupPage) IsEmpty() (bool, error) {
72 users, err := ExtractSecurityGroups(page)
73 if err != nil {
74 return false, err
75 }
76 return len(users) == 0, nil
77}
78
79// ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results.
80func ExtractSecurityGroups(page pagination.Page) ([]SecurityGroup, error) {
81 casted := page.(SecurityGroupPage).Body
82 var response struct {
83 SecurityGroups []SecurityGroup `mapstructure:"security_groups"`
84 }
85
86 err := mapstructure.Decode(casted, &response)
87 return response.SecurityGroups, err
88}
Jamie Hannaforda493e642014-11-19 12:40:30 +010089
90type commonResult struct {
91 gophercloud.Result
92}
93
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010094// CreateResult represents the result of a create operation.
Jamie Hannaforda493e642014-11-19 12:40:30 +010095type CreateResult struct {
96 commonResult
97}
98
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010099// GetResult represents the result of a get operation.
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100100type GetResult struct {
101 commonResult
102}
103
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100104// UpdateResult represents the result of an update operation.
Jamie Hannaford30c74662014-11-19 15:37:34 +0100105type UpdateResult struct {
106 commonResult
107}
108
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100109// Extract will extract a SecurityGroup struct from most responses.
Jamie Hannaforda493e642014-11-19 12:40:30 +0100110func (r commonResult) Extract() (*SecurityGroup, error) {
111 if r.Err != nil {
112 return nil, r.Err
113 }
114
115 var response struct {
116 SecurityGroup SecurityGroup `mapstructure:"security_group"`
117 }
118
119 err := mapstructure.Decode(r.Body, &response)
120
121 return &response.SecurityGroup, err
122}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100123
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100124// CreateRuleResult represents the result when adding rules to a security group.
125type CreateRuleResult struct {
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100126 gophercloud.Result
127}
128
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100129// Extract will extract a Rule struct from a CreateRuleResult.
130func (r CreateRuleResult) Extract() (*Rule, error) {
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100131 if r.Err != nil {
132 return nil, r.Err
133 }
134
135 var response struct {
136 Rule Rule `mapstructure:"security_group_rule"`
137 }
138
139 err := mapstructure.Decode(r.Body, &response)
140
141 return &response.Rule, err
142}