github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/defsecrules/requests.go (about) 1 package defsecrules 2 3 import ( 4 "strings" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/pagination" 8 ) 9 10 // List will return a collection of default rules. 11 func List(client *golangsdk.ServiceClient) pagination.Pager { 12 return pagination.NewPager(client, rootURL(client), func(r pagination.PageResult) pagination.Page { 13 return DefaultRulePage{pagination.SinglePageBase(r)} 14 }) 15 } 16 17 // CreateOpts represents the configuration for adding a new default rule. 18 type CreateOpts struct { 19 // The lower bound of the port range that will be opened. 20 FromPort int `json:"from_port"` 21 22 // The upper bound of the port range that will be opened. 23 ToPort int `json:"to_port"` 24 25 // The protocol type that will be allowed, e.g. TCP. 26 IPProtocol string `json:"ip_protocol" required:"true"` 27 28 // ONLY required if FromGroupID is blank. This represents the IP range that 29 // will be the source of network traffic to your security group. 30 // 31 // Use 0.0.0.0/0 to allow all IPv4 addresses. 32 // Use ::/0 to allow all IPv6 addresses. 33 CIDR string `json:"cidr,omitempty"` 34 } 35 36 // CreateOptsBuilder builds the create rule options into a serializable format. 37 type CreateOptsBuilder interface { 38 ToRuleCreateMap() (map[string]interface{}, error) 39 } 40 41 // ToRuleCreateMap builds the create rule options into a serializable format. 42 func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) { 43 if opts.FromPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" { 44 return nil, golangsdk.ErrMissingInput{Argument: "FromPort"} 45 } 46 if opts.ToPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" { 47 return nil, golangsdk.ErrMissingInput{Argument: "ToPort"} 48 } 49 return golangsdk.BuildRequestBody(opts, "security_group_default_rule") 50 } 51 52 // Create is the operation responsible for creating a new default rule. 53 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 54 b, err := opts.ToRuleCreateMap() 55 if err != nil { 56 r.Err = err 57 return 58 } 59 _, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{ 60 OkCodes: []int{200}, 61 }) 62 return 63 } 64 65 // Get will return details for a particular default rule. 66 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 67 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) 68 return 69 } 70 71 // Delete will permanently delete a rule the project's default security group. 72 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 73 _, r.Err = client.Delete(resourceURL(client, id), nil) 74 return 75 }