github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/waf_hw/v1/whiteblackip_rules/requests.go (about) 1 /* 2 Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 3 */ 4 5 package whiteblackip_rules 6 7 import ( 8 "fmt" 9 "github.com/chnsz/golangsdk" 10 ) 11 12 var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ 13 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 14 } 15 16 // CreateOptsBuilder allows extensions to add additional parameters to the 17 // Create request. 18 type CreateOptsBuilder interface { 19 ToWhiteBlackIPCreateMap() (map[string]interface{}, error) 20 } 21 22 // CreateOpts contains all the values needed to create a new whiteblackip rule. 23 type CreateOpts struct { 24 Name string `json:"name,omitempty"` 25 Addr string `json:"addr,omitempty"` 26 White int `json:"white"` 27 Description string `json:"description,omitempty"` 28 IPGroupID string `json:"ip_group_id,omitempty"` 29 } 30 31 // ToWhiteBlackIPCreateMap builds a create request body from CreateOpts. 32 func (opts CreateOpts) ToWhiteBlackIPCreateMap() (map[string]interface{}, error) { 33 return golangsdk.BuildRequestBody(opts, "") 34 } 35 36 // Create will create a new whiteblackip rule based on the values in CreateOpts. 37 func Create(c *golangsdk.ServiceClient, policyID string, opts CreateOptsBuilder) (r CreateResult) { 38 return CreateWithEpsId(c, opts, policyID, "") 39 } 40 41 func CreateWithEpsId(c *golangsdk.ServiceClient, opts CreateOptsBuilder, policyID, epsID string) (r CreateResult) { 42 b, err := opts.ToWhiteBlackIPCreateMap() 43 if err != nil { 44 r.Err = err 45 return 46 } 47 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 48 _, r.Err = c.Post(rootURL(c, policyID)+generateEpsIdQuery(epsID), b, &r.Body, reqOpt) 49 return 50 } 51 52 // UpdateOptsBuilder allows extensions to add additional parameters to the 53 // Update request. 54 type UpdateOptsBuilder interface { 55 ToWhiteBlackIPUpdateMap() (map[string]interface{}, error) 56 } 57 58 // UpdateOpts contains all the values needed to update a whiteblackip rule. 59 type UpdateOpts struct { 60 Name string `json:"name,omitempty"` 61 Addr string `json:"addr,omitempty"` 62 White *int `json:"white" required:"true"` 63 Description string `json:"description,omitempty"` 64 IPGroupID string `json:"ip_group_id,omitempty"` 65 } 66 67 // ToWhiteBlackIPUpdateMap builds a update request body from UpdateOpts. 68 func (opts UpdateOpts) ToWhiteBlackIPUpdateMap() (map[string]interface{}, error) { 69 return golangsdk.BuildRequestBody(opts, "") 70 } 71 72 // Update accepts a UpdateOpts struct and uses the values to update a rule.The response code from api is 200 73 func Update(c *golangsdk.ServiceClient, policyID, ruleID string, opts UpdateOptsBuilder) (r UpdateResult) { 74 return UpdateWithEpsId(c, opts, policyID, ruleID, "") 75 } 76 77 func UpdateWithEpsId(c *golangsdk.ServiceClient, opts UpdateOptsBuilder, 78 policyID, ruleID, epsID string) (r UpdateResult) { 79 b, err := opts.ToWhiteBlackIPUpdateMap() 80 if err != nil { 81 r.Err = err 82 return 83 } 84 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 85 _, r.Err = c.Put(resourceURL(c, policyID, ruleID)+generateEpsIdQuery(epsID), b, nil, reqOpt) 86 return 87 } 88 89 // Get retrieves a particular whiteblackip rule based on its unique ID. 90 func Get(c *golangsdk.ServiceClient, policyID, ruleID string) (r GetResult) { 91 return GetWithEpsId(c, policyID, ruleID, "") 92 } 93 94 func GetWithEpsId(c *golangsdk.ServiceClient, policyID, ruleID, epsID string) (r GetResult) { 95 reqOpt := &golangsdk.RequestOpts{ 96 OkCodes: []int{200}, 97 MoreHeaders: RequestOpts.MoreHeaders, 98 } 99 _, r.Err = c.Get(resourceURL(c, policyID, ruleID)+generateEpsIdQuery(epsID), &r.Body, reqOpt) 100 return 101 } 102 103 // Delete will permanently delete a particular whiteblackip rule based on its unique ID. 104 func Delete(c *golangsdk.ServiceClient, policyID, ruleID string) (r DeleteResult) { 105 return DeleteWithEpsId(c, policyID, ruleID, "") 106 } 107 108 func DeleteWithEpsId(c *golangsdk.ServiceClient, policyID, ruleID, epsID string) (r DeleteResult) { 109 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}, 110 MoreHeaders: RequestOpts.MoreHeaders} 111 _, r.Err = c.Delete(resourceURL(c, policyID, ruleID)+generateEpsIdQuery(epsID), reqOpt) 112 return 113 } 114 115 func generateEpsIdQuery(epsID string) string { 116 if len(epsID) == 0 { 117 return "" 118 } 119 return fmt.Sprintf("?enterprise_project_id=%s", epsID) 120 }