github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/apigw/v2/responses/requests.go (about) 1 package responses 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/golangsdk/pagination" 6 ) 7 8 // ResponseOpts allows to create a new custom response or update the existing custom response using given parameters. 9 type ResponseOpts struct { 10 // APIG group name, which can contain 1 to 64 characters, only letters, digits, hyphens (-) and 11 // underscores (_) are allowed. 12 Name string `json:"name" required:"true"` 13 // Response type definition, which includes a key and value. Options of the key: 14 // AUTH_FAILURE: Authentication failed. 15 // AUTH_HEADER_MISSING: The identity source is missing. 16 // AUTHORIZER_FAILURE: Custom authentication failed. 17 // AUTHORIZER_CONF_FAILURE: There has been a custom authorizer error. 18 // AUTHORIZER_IDENTITIES_FAILURE: The identity source of the custom authorizer is invalid. 19 // BACKEND_UNAVAILABLE: The backend service is unavailable. 20 // BACKEND_TIMEOUT: Communication with the backend service timed out. 21 // THROTTLED: The request was rejected due to request throttling. 22 // UNAUTHORIZED: The app you are using has not been authorized to call the API. 23 // ACCESS_DENIED: Access denied. 24 // NOT_FOUND: No API is found. 25 // REQUEST_PARAMETERS_FAILURE: The request parameters are incorrect. 26 // DEFAULT_4XX: Another 4XX error occurred. 27 // DEFAULT_5XX: Another 5XX error occurred. 28 // Each error type is in JSON format. 29 Responses map[string]ResponseInfo `json:"responses,omitempty"` 30 // APIG dedicated instance ID. 31 InstanceId string `json:"-"` 32 // APIG group ID. 33 GroupId string `json:"-"` 34 } 35 36 type ResponseInfo struct { 37 // Response body template. 38 Body string `json:"body" required:"true"` 39 // HTTP status code of the response. If omitted, the status will be cancelled. 40 Status int `json:"status,omitempty"` 41 // Indicates whether the response is the default response. 42 // Only the response of the API call is supported. 43 IsDefault bool `json:"default,omitempty"` 44 } 45 46 type ResponseOptsBuilder interface { 47 ToResponseOptsMap() (map[string]interface{}, error) 48 GetInstanceId() string 49 GetGroupId() string 50 } 51 52 func (opts ResponseOpts) ToResponseOptsMap() (map[string]interface{}, error) { 53 return golangsdk.BuildRequestBody(opts, "") 54 } 55 56 func (opts ResponseOpts) GetInstanceId() string { 57 return opts.InstanceId 58 } 59 60 func (opts ResponseOpts) GetGroupId() string { 61 return opts.GroupId 62 } 63 64 // Create is a method by which to create function that create a new custom response. 65 func Create(client *golangsdk.ServiceClient, opts ResponseOptsBuilder) (r CreateResult) { 66 reqBody, err := opts.ToResponseOptsMap() 67 if err != nil { 68 r.Err = err 69 return 70 } 71 _, r.Err = client.Post(rootURL(client, buildResponsesPath(opts.GetInstanceId(), opts.GetGroupId())), 72 reqBody, &r.Body, nil) 73 return 74 } 75 76 // Update is a method by which to create function that udpate the existing custom response. 77 func Update(client *golangsdk.ServiceClient, respId string, opts ResponseOptsBuilder) (r UpdateResult) { 78 reqBody, err := opts.ToResponseOptsMap() 79 if err != nil { 80 r.Err = err 81 return 82 } 83 _, r.Err = client.Put(resourceURL(client, buildResponsesPath(opts.GetInstanceId(), opts.GetGroupId()), respId), 84 reqBody, &r.Body, &golangsdk.RequestOpts{ 85 OkCodes: []int{200}, 86 }) 87 return 88 } 89 90 // Get is a method to obtain the specified custom response according to the instanceId, appId and respId. 91 func Get(client *golangsdk.ServiceClient, instanceId, groupId, respId string) (r GetResult) { 92 _, r.Err = client.Get(resourceURL(client, buildResponsesPath(instanceId, groupId), respId), &r.Body, nil) 93 return 94 } 95 96 // ListOpts allows to filter list data using given parameters. 97 type ListOpts struct { 98 // Offset from which the query starts. 99 // If the offset is less than 0, the value is automatically converted to 0. Default to 0. 100 Offset int `q:"offset"` 101 // Number of items displayed on each page. The valid values are range form 1 to 500, default to 20. 102 Limit int `q:"limit"` 103 // APIG dedicated instance ID. 104 InstanceId string `json:"-"` 105 // APIG group ID. 106 GroupId string `json:"-"` 107 } 108 109 type ListOptsBuilder interface { 110 ToListQuery() (string, error) 111 GetInstanceId() string 112 GetGroupId() string 113 } 114 115 func (opts ListOpts) GetInstanceId() string { 116 return opts.InstanceId 117 } 118 119 func (opts ListOpts) GetGroupId() string { 120 return opts.GroupId 121 } 122 123 func (opts ListOpts) ToListQuery() (string, error) { 124 q, err := golangsdk.BuildQueryString(opts) 125 if err != nil { 126 return "", err 127 } 128 return q.String(), err 129 } 130 131 // List is a method to obtain an array of one or more custom reponses according to the query parameters. 132 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 133 url := rootURL(client, buildResponsesPath(opts.GetInstanceId(), opts.GetGroupId())) 134 if opts != nil { 135 query, err := opts.ToListQuery() 136 if err != nil { 137 return pagination.Pager{Err: err} 138 } 139 url += query 140 } 141 142 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 143 return ResponsePage{pagination.SinglePageBase(r)} 144 }) 145 } 146 147 // Delete is a method to delete the existing custom response. 148 func Delete(client *golangsdk.ServiceClient, instanceId, groupId, respId string) (r DeleteResult) { 149 _, r.Err = client.Delete(resourceURL(client, buildResponsesPath(instanceId, groupId), respId), nil) 150 return 151 } 152 153 // SpecRespOpts is used to build the APIG response url. All parameters are required. 154 type SpecRespOpts struct { 155 InstanceId string 156 GroupId string 157 RespId string 158 } 159 160 type SpecRespOptsBuilder interface { 161 GetInstanceId() string 162 GetGroupId() string 163 GetResponseId() string 164 } 165 166 func (opts SpecRespOpts) GetInstanceId() string { 167 return opts.InstanceId 168 } 169 170 func (opts SpecRespOpts) GetGroupId() string { 171 return opts.GroupId 172 } 173 174 func (opts SpecRespOpts) GetResponseId() string { 175 return opts.RespId 176 } 177 178 // GetSpecResp is a method to get the specifies custom response configuration from an group. 179 func GetSpecResp(client *golangsdk.ServiceClient, respType string, opts SpecRespOptsBuilder) (r GetSpecRespResult) { 180 _, r.Err = client.Get(specResponsesURL(client, buildResponsesPath(opts.GetInstanceId(), opts.GetGroupId()), 181 opts.GetResponseId(), respType), &r.Body, nil) 182 return 183 } 184 185 type UpdateSpecRespBuilder interface { 186 ToSpecRespUpdateMap() (map[string]interface{}, error) 187 } 188 189 func (opts ResponseInfo) ToSpecRespUpdateMap() (map[string]interface{}, error) { 190 return golangsdk.BuildRequestBody(opts, "") 191 } 192 193 // UpdateSpecResp is a method to update an existing custom response configuration from an group. 194 func UpdateSpecResp(client *golangsdk.ServiceClient, respType string, specOpts SpecRespOptsBuilder, 195 respOpts UpdateSpecRespBuilder) (r UpdateSpecRespResult) { 196 reqBody, err := respOpts.ToSpecRespUpdateMap() 197 if err != nil { 198 r.Err = err 199 return 200 } 201 _, r.Err = client.Put(specResponsesURL(client, buildResponsesPath(specOpts.GetInstanceId(), specOpts.GetGroupId()), 202 specOpts.GetResponseId(), respType), reqBody, &r.Body, &golangsdk.RequestOpts{ 203 OkCodes: []int{200}, 204 }) 205 return 206 } 207 208 // DeleteSpecResp is a method to delete an existing custom response configuration from an group. 209 func DeleteSpecResp(client *golangsdk.ServiceClient, respType string, specOpts SpecRespOptsBuilder) (r DeleteResult) { 210 _, r.Err = client.Delete(specResponsesURL(client, buildResponsesPath(specOpts.GetInstanceId(), 211 specOpts.GetGroupId()), specOpts.GetResponseId(), respType), nil) 212 return 213 }