github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/elb/v3/rules/requests.go (about) 1 package rules 2 3 import ( 4 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 6 ) 7 8 type RuleType string 9 type CompareType string 10 11 const ( 12 HostName RuleType = "HOST_NAME" 13 Path RuleType = "PATH" 14 15 EqualTo CompareType = "EQUAL_TO" 16 Regex CompareType = "REGEX" 17 StartsWith CompareType = "STARTS_WITH" 18 ) 19 20 type Condition struct { 21 // Specifies the key of match item. 22 Key string `json:"key"` 23 24 // Specifies the value of the match item. 25 Value string `json:"value" required:"true"` 26 } 27 28 type CreateOpts struct { 29 // Specifies the conditions contained in a forwarding rule. 30 // This parameter will take effect when enhance_l7policy_enable is set to true. 31 // If conditions is specified, key and value will not take effect, 32 // and the value of this parameter will contain all conditions configured for the forwarding rule. 33 // The keys in the list must be the same, whereas each value must be unique. 34 Conditions []Condition `json:"conditions,omitempty"` 35 // Specifies the match content. The value can be one of the following: 36 // 37 // HOST_NAME: A domain name will be used for matching. 38 // PATH: A URL will be used for matching. 39 // If type is set to HOST_NAME, PATH, METHOD, or SOURCE_IP, only one forwarding rule can be created for each type. 40 Type RuleType `json:"type" required:"true"` 41 42 // Specifies how requests are matched and forwarded. 43 // 44 // If type is set to HOST_NAME, this parameter can only be set to EQUAL_TO. Asterisks (*) can be used as wildcard characters. 45 // If type is set to PATH, this parameter can be set to REGEX, STARTS_WITH, or EQUAL_TO. 46 CompareType CompareType `json:"compare_type" required:"true"` 47 48 // Specifies the value of the match item. For example, if a domain name is used for matching, value is the domain name. 49 // 50 // If type is set to HOST_NAME, the value can contain letters, digits, hyphens (-), and periods (.) and must 51 // start with a letter or digit. If you want to use a wildcard domain name, enter an asterisk (*) as the leftmost 52 // label of the domain name. 53 // 54 // If type is set to PATH and compare_type to STARTS_WITH or EQUAL_TO, the value must start with a slash (/) and 55 // can contain only letters, digits, and special characters _~';@^-%#&$.*+?,=!:|/()[]{} 56 Value string `json:"value" required:"true"` 57 58 // Specifies the project ID. 59 ProjectID string `json:"project_id,omitempty"` 60 } 61 62 type CreateOptsBuilder interface { 63 ToRuleCreateMap() (map[string]interface{}, error) 64 } 65 66 func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) { 67 return golangsdk.BuildRequestBody(opts, "rule") 68 } 69 70 func Create(client *golangsdk.ServiceClient, policyID string, opts CreateOptsBuilder) (r CreateResult) { 71 b, err := opts.ToRuleCreateMap() 72 if err != nil { 73 r.Err = err 74 return 75 } 76 _, r.Err = client.Post(baseURL(client, policyID), b, &r.Body, nil) 77 return 78 } 79 80 func Get(client *golangsdk.ServiceClient, policyID, id string) (r GetResult) { 81 _, r.Err = client.Get(resourceURL(client, policyID, id), &r.Body, nil) 82 return 83 } 84 85 type UpdateOptsBuilder interface { 86 ToUpdateRuleMap() (map[string]interface{}, error) 87 } 88 89 type UpdateOpts struct { 90 CompareType CompareType `json:"compare_type,omitempty"` 91 Value string `json:"value,omitempty"` 92 Conditions []Condition `json:"conditions,omitempty"` 93 } 94 95 func (opts UpdateOpts) ToUpdateRuleMap() (map[string]interface{}, error) { 96 return golangsdk.BuildRequestBody(opts, "rule") 97 } 98 99 func Update(client *golangsdk.ServiceClient, policyID, id string, opts UpdateOptsBuilder) (r UpdateResult) { 100 b, err := opts.ToUpdateRuleMap() 101 if err != nil { 102 r.Err = err 103 return 104 } 105 _, r.Err = client.Put(resourceURL(client, policyID, id), b, &r.Body, &golangsdk.RequestOpts{ 106 OkCodes: []int{200, 201}, 107 }) 108 return 109 } 110 111 type ListOptsBuilder interface { 112 ToRuleListQuery() (string, error) 113 } 114 115 type ListOpts struct { 116 ID []string `q:"id"` 117 CompareType []CompareType `q:"compare_type"` 118 Value []string `q:"value"` 119 Type []RuleType `q:"type"` 120 121 Limit int `q:"limit"` 122 Marker string `q:"marker"` 123 PageReverse bool `q:"page_reverse"` 124 } 125 126 func List(client *golangsdk.ServiceClient, policyID string, opts ListOptsBuilder) pagination.Pager { 127 url := baseURL(client, policyID) 128 129 if opts != nil { 130 q, err := opts.ToRuleListQuery() 131 if err != nil { 132 return pagination.Pager{Err: err} 133 } 134 url += q 135 } 136 137 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 138 return RulePage{PageWithInfo: pagination.NewPageWithInfo(r)} 139 }) 140 } 141 142 func Delete(client *golangsdk.ServiceClient, policyID, id string) (r DeleteResult) { 143 _, r.Err = client.Delete(resourceURL(client, policyID, id), nil) 144 return 145 }