github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/waf/v1/preciseprotection_rules/requests.go (about) 1 package preciseprotection_rules 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 ) 6 7 var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ 8 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 9 } 10 11 // CreateOptsBuilder allows extensions to add additional parameters to the 12 // Create request. 13 type CreateOptsBuilder interface { 14 ToPreciseCreateMap() (map[string]interface{}, error) 15 } 16 17 // CreateOpts contains all the values needed to create a new precise protection rule. 18 type CreateOpts struct { 19 Name string `json:"name" required:"true"` 20 Time bool `json:"time"` 21 Start int64 `json:"start,omitempty"` 22 End int64 `json:"end,omitempty"` 23 Conditions []Condition `json:"conditions" required:"true"` 24 Action Action `json:"action" required:"true"` 25 Priority *int `json:"priority,omitempty"` 26 } 27 28 type Condition struct { 29 Category string `json:"category" required:"true"` 30 Index string `json:"index,omitempty"` 31 Logic string `json:"logic" required:"true"` 32 Contents []string `json:"contents" required:"true"` 33 } 34 35 type Action struct { 36 Category string `json:"category" required:"true"` 37 } 38 39 // ToPreciseCreateMap builds a create request body from CreateOpts. 40 func (opts CreateOpts) ToPreciseCreateMap() (map[string]interface{}, error) { 41 return golangsdk.BuildRequestBody(opts, "") 42 } 43 44 // Create will create a new precise protection rule based on the values in CreateOpts. 45 func Create(c *golangsdk.ServiceClient, policyID string, opts CreateOptsBuilder) (r CreateResult) { 46 b, err := opts.ToPreciseCreateMap() 47 if err != nil { 48 r.Err = err 49 return 50 } 51 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 52 _, r.Err = c.Post(rootURL(c, policyID), b, &r.Body, reqOpt) 53 return 54 } 55 56 // Update will update a precise protection rule based on the values in CreateOpts. 57 // The response code from api is 200 58 func Update(c *golangsdk.ServiceClient, policyID, ruleID string, opts CreateOptsBuilder) (r UpdateResult) { 59 b, err := opts.ToPreciseCreateMap() 60 if err != nil { 61 r.Err = err 62 return 63 } 64 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 65 _, r.Err = c.Put(resourceURL(c, policyID, ruleID), b, &r.Body, reqOpt) 66 return 67 } 68 69 // Get retrieves a particular precise rule based on its unique ID. 70 func Get(c *golangsdk.ServiceClient, policyID, ruleID string) (r GetResult) { 71 reqOpt := &golangsdk.RequestOpts{ 72 MoreHeaders: RequestOpts.MoreHeaders, 73 } 74 75 _, r.Err = c.Get(resourceURL(c, policyID, ruleID), &r.Body, reqOpt) 76 return 77 } 78 79 // Delete will permanently delete a particular precise rule based on its unique ID. 80 func Delete(c *golangsdk.ServiceClient, policyID, ruleID string) (r DeleteResult) { 81 reqOpt := &golangsdk.RequestOpts{ 82 MoreHeaders: RequestOpts.MoreHeaders, 83 } 84 85 _, r.Err = c.Delete(resourceURL(c, policyID, ruleID), reqOpt) 86 return 87 }