github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/waf_hw/v1/policies/requests.go (about) 1 package policies 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/openstack/utils" 6 "github.com/chnsz/golangsdk/pagination" 7 ) 8 9 var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ 10 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 11 } 12 13 // CreateOptsBuilder allows extensions to add additional parameters to the 14 // Create request. 15 type CreateOptsBuilder interface { 16 ToPolicyCreateMap() (map[string]interface{}, error) 17 } 18 19 // CreateOpts contains all the values needed to create a new policy. 20 type CreateOpts struct { 21 //Policy name 22 Name string `json:"name" required:"true"` 23 EnterpriseProjectId string `q:"enterprise_project_id" json:"-"` 24 } 25 26 // ToPolicyCreateMap builds a create request body from CreateOpts. 27 func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) { 28 return golangsdk.BuildRequestBody(opts, "") 29 } 30 31 // Create will create a new policy based on the values in CreateOpts. 32 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 33 b, err := opts.ToPolicyCreateMap() 34 if err != nil { 35 r.Err = err 36 return 37 } 38 query, err := golangsdk.BuildQueryString(opts) 39 if err != nil { 40 r.Err = err 41 return 42 } 43 44 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 45 _, r.Err = c.Post(rootURL(c)+query.String(), b, &r.Body, reqOpt) 46 return 47 } 48 49 // UpdateOptsBuilder allows extensions to add additional parameters to the 50 // Update request. 51 type UpdateOptsBuilder interface { 52 ToPolicyUpdateMap() (map[string]interface{}, error) 53 } 54 55 // UpdateOpts contains all the values needed to update a policy. 56 type UpdateOpts struct { 57 FullDetection *bool `json:"full_detection,omitempty"` 58 RobotAction *Action `json:"robot_action,omitempty"` 59 Action *Action `json:"action,omitempty"` 60 Options *PolicyOption `json:"options,omitempty"` 61 Name string `json:"name,omitempty"` 62 Level int `json:"level,omitempty"` 63 EnterpriseProjectId string `q:"enterprise_project_id" json:"-"` 64 } 65 66 // ToPolicyUpdateMap builds a update request body from UpdateOpts. 67 func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) { 68 return golangsdk.BuildRequestBody(opts, "") 69 } 70 71 // Update accepts a UpdateOpts struct and uses the values to update a policy.The response code from api is 200 72 func Update(c *golangsdk.ServiceClient, policyID string, opts UpdateOptsBuilder) (r UpdateResult) { 73 b, err := opts.ToPolicyUpdateMap() 74 if err != nil { 75 r.Err = err 76 return 77 } 78 query, err := golangsdk.BuildQueryString(opts) 79 if err != nil { 80 r.Err = err 81 return 82 } 83 84 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 85 _, r.Err = c.Patch(resourceURL(c, policyID)+query.String(), b, nil, reqOpt) 86 return 87 } 88 89 // UpdateHostsOptsBuilder allows extensions to add additional parameters to the 90 // Update request. 91 type UpdateHostsOptsBuilder interface { 92 ToUpdateHostsQuery() (string, error) 93 } 94 95 // UpdateHostsOpts contains all the values needed to update a policy hosts. 96 type UpdateHostsOpts struct { 97 //Domain ID 98 Hosts []string `q:"hosts" required:"true"` 99 EnterpriseProjectId string `q:"enterprise_project_id"` 100 } 101 102 // ListPolicyOpts 103 type ListPolicyOpts struct { 104 Page int `q:"page"` 105 Pagesize int `q:"pagesize"` 106 // policy name 107 Name string `q:"name"` 108 EnterpriseProjectId string `q:"enterprise_project_id"` 109 } 110 111 // ToUpdateHostsQuery builds a update request query from UpdateHostsOpts. 112 func (opts UpdateHostsOpts) ToUpdateHostsQuery() (string, error) { 113 q, err := golangsdk.BuildQueryString(opts) 114 return q.String(), err 115 } 116 117 // UpdateHosts accepts a UpdateHostsOpts struct and uses the values to update a policy hosts.The response code from api is 200 118 func UpdateHosts(c *golangsdk.ServiceClient, policyId string, opts UpdateHostsOptsBuilder) (r UpdateResult) { 119 url := resourceURL(c, policyId) 120 if opts != nil { 121 var query string 122 query, r.Err = opts.ToUpdateHostsQuery() 123 if r.Err != nil { 124 return 125 } 126 url += query 127 } 128 reqOpt := &golangsdk.RequestOpts{ 129 OkCodes: []int{200}, 130 MoreHeaders: RequestOpts.MoreHeaders, 131 } 132 _, r.Err = c.Put(url, nil, r.Body, reqOpt) 133 return 134 } 135 136 // Get retrieves a particular policy based on its unique ID. 137 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 138 return GetWithEpsID(c, id, "") 139 } 140 141 func GetWithEpsID(c *golangsdk.ServiceClient, id, epsID string) (r GetResult) { 142 reqOpt := &golangsdk.RequestOpts{ 143 OkCodes: []int{200}, 144 MoreHeaders: RequestOpts.MoreHeaders, 145 } 146 _, r.Err = c.Get(resourceURL(c, id)+utils.GenerateEpsIDQuery(epsID), &r.Body, reqOpt) 147 return 148 } 149 150 // Delete will permanently delete a particular policy based on its unique ID. 151 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 152 return DeleteWithEpsID(c, id, "") 153 } 154 155 func DeleteWithEpsID(c *golangsdk.ServiceClient, id, epsID string) (r DeleteResult) { 156 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}, 157 MoreHeaders: RequestOpts.MoreHeaders} 158 _, r.Err = c.Delete(resourceURL(c, id)+utils.GenerateEpsIDQuery(epsID), reqOpt) 159 return 160 } 161 162 // ListPolicy retrieve waf policy by ListPolicyOpts 163 func ListPolicy(c *golangsdk.ServiceClient, opts ListPolicyOpts) (*ListPolicyRst, error) { 164 url := rootURL(c) 165 query, err := golangsdk.BuildQueryString(opts) 166 if err != nil { 167 return nil, err 168 } 169 url += query.String() 170 171 var rst golangsdk.Result 172 _, err = c.Get(url, &rst.Body, &golangsdk.RequestOpts{ 173 MoreHeaders: RequestOpts.MoreHeaders, 174 }) 175 176 if err == nil { 177 var r ListPolicyRst 178 rst.ExtractInto(&r) 179 return &r, nil 180 } 181 return nil, err 182 } 183 184 // List using to query all pages WAF policies 185 func List(c *golangsdk.ServiceClient, opts ListPolicyOpts) ([]Policy, error) { 186 url := rootURL(c) 187 query, err := golangsdk.BuildQueryString(opts) 188 if err != nil { 189 return nil, err 190 } 191 url += query.String() 192 193 pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 194 return PolicyPage{pagination.PageSizeBase{PageResult: r}} 195 }) 196 pager.Headers = RequestOpts.MoreHeaders 197 pages, err := pager.AllPages() 198 if err != nil { 199 return nil, err 200 } 201 return ExtractPolicies(pages) 202 }