diff --git a/openstack/waf/v1/preciseprotection_rules/requests.go b/openstack/waf/v1/preciseprotection_rules/requests.go new file mode 100644 index 000000000..fe5ab8ff8 --- /dev/null +++ b/openstack/waf/v1/preciseprotection_rules/requests.go @@ -0,0 +1,68 @@ +package preciseprotection_rules + +import ( + "github.com/huaweicloud/golangsdk" +) + +var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ + MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, +} + +// CreateOptsBuilder allows extensions to add additional parameters to the +// Create request. +type CreateOptsBuilder interface { + ToPreciseCreateMap() (map[string]interface{}, error) +} + +// CreateOpts contains all the values needed to create a new precise protection rule. +type CreateOpts struct { + Name string `json:"name" required:"true"` + Time bool `json:"time,omitempty"` + Start int64 `json:"start,omitempty"` + End int64 `json:"end,omitempty"` + Conditions Conditions `json:"conditions" required:"true"` + Action Action `json:"action" required:"true"` + Priority *int `json:"priority,omitempty"` +} + +type Conditions struct { + Category string `json:"category" required:"true"` + Index string `json:"index,omitempty"` + Logic int `json:"logic" required:"true"` + Contents []string `json:"contents" required:"true"` +} + +type Action struct { + Category string `json:"category" required:"true"` +} + +// ToPreciseCreateMap builds a create request body from CreateOpts. +func (opts CreateOpts) ToPreciseCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "") +} + +// Create will create a new precise protection rule based on the values in CreateOpts. +func Create(c *golangsdk.ServiceClient, policyID string, opts CreateOptsBuilder) (r CreateResult) { + b, err := opts.ToPreciseCreateMap() + if err != nil { + r.Err = err + return + } + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} + _, r.Err = c.Post(rootURL(c, policyID), b, &r.Body, reqOpt) + return +} + +// Get retrieves a particular precise rule based on its unique ID. +func Get(c *golangsdk.ServiceClient, policyID, ruleID string) (r GetResult) { + _, r.Err = c.Get(resourceURL(c, policyID, ruleID), &r.Body, &RequestOpts) + return +} + +// Delete will permanently delete a particular precise rule based on its unique ID. +func Delete(c *golangsdk.ServiceClient, policyID, ruleID string) (r DeleteResult) { + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}, + MoreHeaders: RequestOpts.MoreHeaders} + _, r.Err = c.Delete(resourceURL(c, policyID, ruleID), reqOpt) + return +} diff --git a/openstack/waf/v1/preciseprotection_rules/results.go b/openstack/waf/v1/preciseprotection_rules/results.go new file mode 100644 index 000000000..2acf7179a --- /dev/null +++ b/openstack/waf/v1/preciseprotection_rules/results.go @@ -0,0 +1,46 @@ +package preciseprotection_rules + +import ( + "github.com/huaweicloud/golangsdk" +) + +type Precise struct { + Id string `json:"id"` + PolicyID string `json:"policyid"` + Name string `json:"name"` + Time bool `json:"time"` + Start int64 `json:"start"` + End int64 `json:"end"` + Conditions Conditions `json:"conditions"` + Action Action `json:"action"` + Priority int `json:"priority"` +} + +type commonResult struct { + golangsdk.Result +} + +// Extract is a function that accepts a result and extracts a precise protection rule. +func (r commonResult) Extract() (*Precise, error) { + var response Precise + err := r.ExtractInto(&response) + return &response, err +} + +// CreateResult represents the result of a create operation. Call its Extract +// method to interpret it as a precise protection rule. +type CreateResult struct { + commonResult +} + +// GetResult represents the result of a get operation. Call its Extract +// method to interpret it as a precise protection rule. +type GetResult struct { + commonResult +} + +// DeleteResult represents the result of a delete operation. Call its ExtractErr +// method to determine if the request succeeded or failed. +type DeleteResult struct { + golangsdk.ErrResult +} diff --git a/openstack/waf/v1/preciseprotection_rules/urls.go b/openstack/waf/v1/preciseprotection_rules/urls.go new file mode 100644 index 000000000..10a8bbd46 --- /dev/null +++ b/openstack/waf/v1/preciseprotection_rules/urls.go @@ -0,0 +1,11 @@ +package preciseprotection_rules + +import "github.com/huaweicloud/golangsdk" + +func rootURL(c *golangsdk.ServiceClient, policy_id string) string { + return c.ServiceURL("policy", policy_id, "custom") +} + +func resourceURL(c *golangsdk.ServiceClient, policy_id, id string) string { + return c.ServiceURL("policy", policy_id, "custom", id) +}