github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/secgroups/results.go (about) 1 package secgroups 2 3 import ( 4 "encoding/json" 5 "strconv" 6 7 "github.com/huaweicloud/golangsdk" 8 "github.com/huaweicloud/golangsdk/pagination" 9 ) 10 11 // SecurityGroup represents a security group. 12 type SecurityGroup struct { 13 // The unique ID of the group. If Neutron is installed, this ID will be 14 // represented as a string UUID; if Neutron is not installed, it will be a 15 // numeric ID. For the sake of consistency, we always cast it to a string. 16 ID string `json:"-"` 17 18 // The human-readable name of the group, which needs to be unique. 19 Name string `json:"name"` 20 21 // The human-readable description of the group. 22 Description string `json:"description"` 23 24 // The rules which determine how this security group operates. 25 Rules []Rule `json:"rules"` 26 27 // The ID of the tenant to which this security group belongs. 28 TenantID string `json:"tenant_id"` 29 } 30 31 func (r *SecurityGroup) UnmarshalJSON(b []byte) error { 32 type tmp SecurityGroup 33 var s struct { 34 tmp 35 ID interface{} `json:"id"` 36 } 37 err := json.Unmarshal(b, &s) 38 if err != nil { 39 return err 40 } 41 42 *r = SecurityGroup(s.tmp) 43 44 switch t := s.ID.(type) { 45 case float64: 46 r.ID = strconv.FormatFloat(t, 'f', -1, 64) 47 case string: 48 r.ID = t 49 } 50 51 return err 52 } 53 54 // Rule represents a security group rule, a policy which determines how a 55 // security group operates and what inbound traffic it allows in. 56 type Rule struct { 57 // The unique ID. If Neutron is installed, this ID will be 58 // represented as a string UUID; if Neutron is not installed, it will be a 59 // numeric ID. For the sake of consistency, we always cast it to a string. 60 ID string `json:"-"` 61 62 // The lower bound of the port range which this security group should open up. 63 FromPort int `json:"from_port"` 64 65 // The upper bound of the port range which this security group should open up. 66 ToPort int `json:"to_port"` 67 68 // The IP protocol (e.g. TCP) which the security group accepts. 69 IPProtocol string `json:"ip_protocol"` 70 71 // The CIDR IP range whose traffic can be received. 72 IPRange IPRange `json:"ip_range"` 73 74 // The security group ID to which this rule belongs. 75 ParentGroupID string `json:"-"` 76 77 // Not documented. 78 Group Group 79 } 80 81 func (r *Rule) UnmarshalJSON(b []byte) error { 82 type tmp Rule 83 var s struct { 84 tmp 85 ID interface{} `json:"id"` 86 ParentGroupID interface{} `json:"parent_group_id"` 87 } 88 err := json.Unmarshal(b, &s) 89 if err != nil { 90 return err 91 } 92 93 *r = Rule(s.tmp) 94 95 switch t := s.ID.(type) { 96 case float64: 97 r.ID = strconv.FormatFloat(t, 'f', -1, 64) 98 case string: 99 r.ID = t 100 } 101 102 switch t := s.ParentGroupID.(type) { 103 case float64: 104 r.ParentGroupID = strconv.FormatFloat(t, 'f', -1, 64) 105 case string: 106 r.ParentGroupID = t 107 } 108 109 return err 110 } 111 112 // IPRange represents the IP range whose traffic will be accepted by the 113 // security group. 114 type IPRange struct { 115 CIDR string 116 } 117 118 // Group represents a group. 119 type Group struct { 120 TenantID string `json:"tenant_id"` 121 Name string 122 } 123 124 // SecurityGroupPage is a single page of a SecurityGroup collection. 125 type SecurityGroupPage struct { 126 pagination.SinglePageBase 127 } 128 129 // IsEmpty determines whether or not a page of Security Groups contains any 130 // results. 131 func (page SecurityGroupPage) IsEmpty() (bool, error) { 132 users, err := ExtractSecurityGroups(page) 133 return len(users) == 0, err 134 } 135 136 // ExtractSecurityGroups returns a slice of SecurityGroups contained in a 137 // single page of results. 138 func ExtractSecurityGroups(r pagination.Page) ([]SecurityGroup, error) { 139 var s struct { 140 SecurityGroups []SecurityGroup `json:"security_groups"` 141 } 142 err := (r.(SecurityGroupPage)).ExtractInto(&s) 143 return s.SecurityGroups, err 144 } 145 146 type commonResult struct { 147 golangsdk.Result 148 } 149 150 // CreateResult represents the result of a create operation. Call its Extract 151 // method to interpret the result as a SecurityGroup. 152 type CreateResult struct { 153 commonResult 154 } 155 156 // GetResult represents the result of a get operation. Call its Extract 157 // method to interpret the result as a SecurityGroup. 158 type GetResult struct { 159 commonResult 160 } 161 162 // UpdateResult represents the result of an update operation. Call its Extract 163 // method to interpret the result as a SecurityGroup. 164 type UpdateResult struct { 165 commonResult 166 } 167 168 // Extract will extract a SecurityGroup struct from most responses. 169 func (r commonResult) Extract() (*SecurityGroup, error) { 170 var s struct { 171 SecurityGroup *SecurityGroup `json:"security_group"` 172 } 173 err := r.ExtractInto(&s) 174 return s.SecurityGroup, err 175 } 176 177 // CreateRuleResult represents the result when adding rules to a security group. 178 // Call its Extract method to interpret the result as a Rule. 179 type CreateRuleResult struct { 180 golangsdk.Result 181 } 182 183 // Extract will extract a Rule struct from a CreateRuleResult. 184 func (r CreateRuleResult) Extract() (*Rule, error) { 185 var s struct { 186 Rule *Rule `json:"security_group_rule"` 187 } 188 err := r.ExtractInto(&s) 189 return s.Rule, err 190 } 191 192 // DeleteResult is the response from delete operation. Call its ExtractErr 193 // method to determine if the request succeeded or failed. 194 type DeleteResult struct { 195 golangsdk.ErrResult 196 } 197 198 // DeleteRuleResult is the response from a DeleteRule operation. Call its 199 // ExtractErr method to determine if the request succeeded or failed. 200 type DeleteRuleResult struct { 201 golangsdk.ErrResult 202 } 203 204 // AddServerResult is the response from an AddServer operation. Call its 205 // ExtractErr method to determine if the request succeeded or failed. 206 type AddServerResult struct { 207 golangsdk.ErrResult 208 } 209 210 // RemoveServerResult is the response from a RemoveServer operation. Call its 211 // ExtractErr method to determine if the request succeeded or failed. 212 type RemoveServerResult struct { 213 golangsdk.ErrResult 214 }