|
| 1 | +package security_group_rules |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/nttcom/eclcloud/v3" |
| 5 | + "github.com/nttcom/eclcloud/v3/pagination" |
| 6 | +) |
| 7 | + |
| 8 | +// ListOptsBuilder allows extensions to add additional parameters to the |
| 9 | +// List request. |
| 10 | +type ListOptsBuilder interface { |
| 11 | + ToSecurityGroupRuleListQuery() (string, error) |
| 12 | +} |
| 13 | + |
| 14 | +// ListOpts allows the filtering and sorting of paginated collections through |
| 15 | +// the API. Filtering is achieved by passing in struct field values that map to |
| 16 | +// the security group rule attributes you want to see returned. |
| 17 | +type ListOpts struct { |
| 18 | + Description string `q:"description"` |
| 19 | + Direction string `q:"direction"` |
| 20 | + Ethertype string `q:"ethertype"` |
| 21 | + ID string `q:"id"` |
| 22 | + PortRangeMax int `q:"port_range_max"` |
| 23 | + PortRangeMin int `q:"port_range_min"` |
| 24 | + Protocol string `q:"protocol"` |
| 25 | + RemoteGroupID string `q:"remote_group_id"` |
| 26 | + RemoteIPPrefix string `q:"remote_ip_prefix"` |
| 27 | + SecurityGroupID string `q:"security_group_id"` |
| 28 | + TenantID string `q:"tenant_id"` |
| 29 | +} |
| 30 | + |
| 31 | +// ToSecurityGroupRuleListQuery formats a ListOpts into a query string. |
| 32 | +func (opts ListOpts) ToSecurityGroupRuleListQuery() (string, error) { |
| 33 | + q, err := eclcloud.BuildQueryString(opts) |
| 34 | + return q.String(), err |
| 35 | +} |
| 36 | + |
| 37 | +// List returns a Pager which allows you to iterate over a collection of |
| 38 | +// security group rules. It accepts a ListOpts struct, which allows you to filter |
| 39 | +// and sort the returned collection for greater efficiency. |
| 40 | +func List(c *eclcloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 41 | + url := listURL(c) |
| 42 | + if opts != nil { |
| 43 | + query, err := opts.ToSecurityGroupRuleListQuery() |
| 44 | + if err != nil { |
| 45 | + return pagination.Pager{Err: err} |
| 46 | + } |
| 47 | + url += query |
| 48 | + } |
| 49 | + return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 50 | + return SecurityGroupRulePage{pagination.LinkedPageBase{PageResult: r}} |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +// Get retrieves a specific security group rule based on its unique ID. |
| 55 | +func Get(c *eclcloud.ServiceClient, id string) (r GetResult) { |
| 56 | + _, r.Err = c.Get(getURL(c, id), &r.Body, nil) |
| 57 | + return |
| 58 | +} |
| 59 | + |
| 60 | +// CreateOptsBuilder allows extensions to add additional parameters to the |
| 61 | +// Create request. |
| 62 | +type CreateOptsBuilder interface { |
| 63 | + ToSecurityGroupRuleCreateMap() (map[string]interface{}, error) |
| 64 | +} |
| 65 | + |
| 66 | +// CreateOpts represents options used to create a security group rule. |
| 67 | +type CreateOpts struct { |
| 68 | + Description string `json:"description,omitempty"` |
| 69 | + Direction string `json:"direction" required:"true"` |
| 70 | + Ethertype string `json:"ethertype,omitempty"` |
| 71 | + PortRangeMax *int `json:"port_range_max,omitempty"` |
| 72 | + PortRangeMin *int `json:"port_range_min,omitempty"` |
| 73 | + Protocol string `json:"protocol,omitempty"` |
| 74 | + RemoteGroupID *string `json:"remote_group_id,omitempty"` |
| 75 | + RemoteIPPrefix *string `json:"remote_ip_prefix,omitempty"` |
| 76 | + SecurityGroupID string `json:"security_group_id" required:"true"` |
| 77 | + TenantID string `json:"tenant_id,omitempty"` |
| 78 | +} |
| 79 | + |
| 80 | +// ToSecurityGroupRuleCreateMap builds a request body from CreateOpts. |
| 81 | +func (opts CreateOpts) ToSecurityGroupRuleCreateMap() (map[string]interface{}, error) { |
| 82 | + return eclcloud.BuildRequestBody(opts, "security_group_rule") |
| 83 | +} |
| 84 | + |
| 85 | +// Create accepts a CreateOpts struct and creates a new security group rule. |
| 86 | +func Create(c *eclcloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 87 | + b, err := opts.ToSecurityGroupRuleCreateMap() |
| 88 | + if err != nil { |
| 89 | + r.Err = err |
| 90 | + return |
| 91 | + } |
| 92 | + _, r.Err = c.Post(createURL(c), b, &r.Body, nil) |
| 93 | + return |
| 94 | +} |
| 95 | + |
| 96 | +// Delete accepts a unique ID and deletes the security group rule associated with it. |
| 97 | +func Delete(c *eclcloud.ServiceClient, id string) (r DeleteResult) { |
| 98 | + _, r.Err = c.Delete(deleteURL(c, id), nil) |
| 99 | + return |
| 100 | +} |
0 commit comments