github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/fake/adminrules_server.go (about) 1 //go:build go1.18 2 // +build go1.18 3 4 // Copyright (c) Microsoft Corporation. All rights reserved. 5 // Licensed under the MIT License. See License.txt in the project root for license information. 6 // Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT. 7 // Changes may cause incorrect behavior and will be lost if the code is regenerated. 8 9 package fake 10 11 import ( 12 "context" 13 "errors" 14 "fmt" 15 azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake" 16 "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake/server" 17 "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" 18 "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" 19 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6" 20 "net/http" 21 "net/url" 22 "regexp" 23 "strconv" 24 ) 25 26 // AdminRulesServer is a fake server for instances of the armnetwork.AdminRulesClient type. 27 type AdminRulesServer struct { 28 // CreateOrUpdate is the fake for method AdminRulesClient.CreateOrUpdate 29 // HTTP status codes to indicate success: http.StatusOK, http.StatusCreated 30 CreateOrUpdate func(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, adminRule armnetwork.BaseAdminRuleClassification, options *armnetwork.AdminRulesClientCreateOrUpdateOptions) (resp azfake.Responder[armnetwork.AdminRulesClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) 31 32 // BeginDelete is the fake for method AdminRulesClient.BeginDelete 33 // HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent 34 BeginDelete func(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, options *armnetwork.AdminRulesClientBeginDeleteOptions) (resp azfake.PollerResponder[armnetwork.AdminRulesClientDeleteResponse], errResp azfake.ErrorResponder) 35 36 // Get is the fake for method AdminRulesClient.Get 37 // HTTP status codes to indicate success: http.StatusOK 38 Get func(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, options *armnetwork.AdminRulesClientGetOptions) (resp azfake.Responder[armnetwork.AdminRulesClientGetResponse], errResp azfake.ErrorResponder) 39 40 // NewListPager is the fake for method AdminRulesClient.NewListPager 41 // HTTP status codes to indicate success: http.StatusOK 42 NewListPager func(resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, options *armnetwork.AdminRulesClientListOptions) (resp azfake.PagerResponder[armnetwork.AdminRulesClientListResponse]) 43 } 44 45 // NewAdminRulesServerTransport creates a new instance of AdminRulesServerTransport with the provided implementation. 46 // The returned AdminRulesServerTransport instance is connected to an instance of armnetwork.AdminRulesClient via the 47 // azcore.ClientOptions.Transporter field in the client's constructor parameters. 48 func NewAdminRulesServerTransport(srv *AdminRulesServer) *AdminRulesServerTransport { 49 return &AdminRulesServerTransport{ 50 srv: srv, 51 beginDelete: newTracker[azfake.PollerResponder[armnetwork.AdminRulesClientDeleteResponse]](), 52 newListPager: newTracker[azfake.PagerResponder[armnetwork.AdminRulesClientListResponse]](), 53 } 54 } 55 56 // AdminRulesServerTransport connects instances of armnetwork.AdminRulesClient to instances of AdminRulesServer. 57 // Don't use this type directly, use NewAdminRulesServerTransport instead. 58 type AdminRulesServerTransport struct { 59 srv *AdminRulesServer 60 beginDelete *tracker[azfake.PollerResponder[armnetwork.AdminRulesClientDeleteResponse]] 61 newListPager *tracker[azfake.PagerResponder[armnetwork.AdminRulesClientListResponse]] 62 } 63 64 // Do implements the policy.Transporter interface for AdminRulesServerTransport. 65 func (a *AdminRulesServerTransport) Do(req *http.Request) (*http.Response, error) { 66 rawMethod := req.Context().Value(runtime.CtxAPINameKey{}) 67 method, ok := rawMethod.(string) 68 if !ok { 69 return nil, nonRetriableError{errors.New("unable to dispatch request, missing value for CtxAPINameKey")} 70 } 71 72 var resp *http.Response 73 var err error 74 75 switch method { 76 case "AdminRulesClient.CreateOrUpdate": 77 resp, err = a.dispatchCreateOrUpdate(req) 78 case "AdminRulesClient.BeginDelete": 79 resp, err = a.dispatchBeginDelete(req) 80 case "AdminRulesClient.Get": 81 resp, err = a.dispatchGet(req) 82 case "AdminRulesClient.NewListPager": 83 resp, err = a.dispatchNewListPager(req) 84 default: 85 err = fmt.Errorf("unhandled API %s", method) 86 } 87 88 if err != nil { 89 return nil, err 90 } 91 92 return resp, nil 93 } 94 95 func (a *AdminRulesServerTransport) dispatchCreateOrUpdate(req *http.Request) (*http.Response, error) { 96 if a.srv.CreateOrUpdate == nil { 97 return nil, &nonRetriableError{errors.New("fake for method CreateOrUpdate not implemented")} 98 } 99 const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/networkManagers/(?P<networkManagerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/securityAdminConfigurations/(?P<configurationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/ruleCollections/(?P<ruleCollectionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/rules/(?P<ruleName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)` 100 regex := regexp.MustCompile(regexStr) 101 matches := regex.FindStringSubmatch(req.URL.EscapedPath()) 102 if matches == nil || len(matches) < 6 { 103 return nil, fmt.Errorf("failed to parse path %s", req.URL.Path) 104 } 105 raw, err := readRequestBody(req) 106 if err != nil { 107 return nil, err 108 } 109 body, err := unmarshalBaseAdminRuleClassification(raw) 110 if err != nil { 111 return nil, err 112 } 113 resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")]) 114 if err != nil { 115 return nil, err 116 } 117 networkManagerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("networkManagerName")]) 118 if err != nil { 119 return nil, err 120 } 121 configurationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("configurationName")]) 122 if err != nil { 123 return nil, err 124 } 125 ruleCollectionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ruleCollectionName")]) 126 if err != nil { 127 return nil, err 128 } 129 ruleNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ruleName")]) 130 if err != nil { 131 return nil, err 132 } 133 respr, errRespr := a.srv.CreateOrUpdate(req.Context(), resourceGroupNameParam, networkManagerNameParam, configurationNameParam, ruleCollectionNameParam, ruleNameParam, body, nil) 134 if respErr := server.GetError(errRespr, req); respErr != nil { 135 return nil, respErr 136 } 137 respContent := server.GetResponseContent(respr) 138 if !contains([]int{http.StatusOK, http.StatusCreated}, respContent.HTTPStatus) { 139 return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", respContent.HTTPStatus)} 140 } 141 resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).BaseAdminRuleClassification, req) 142 if err != nil { 143 return nil, err 144 } 145 return resp, nil 146 } 147 148 func (a *AdminRulesServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) { 149 if a.srv.BeginDelete == nil { 150 return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")} 151 } 152 beginDelete := a.beginDelete.get(req) 153 if beginDelete == nil { 154 const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/networkManagers/(?P<networkManagerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/securityAdminConfigurations/(?P<configurationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/ruleCollections/(?P<ruleCollectionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/rules/(?P<ruleName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)` 155 regex := regexp.MustCompile(regexStr) 156 matches := regex.FindStringSubmatch(req.URL.EscapedPath()) 157 if matches == nil || len(matches) < 6 { 158 return nil, fmt.Errorf("failed to parse path %s", req.URL.Path) 159 } 160 qp := req.URL.Query() 161 resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")]) 162 if err != nil { 163 return nil, err 164 } 165 networkManagerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("networkManagerName")]) 166 if err != nil { 167 return nil, err 168 } 169 configurationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("configurationName")]) 170 if err != nil { 171 return nil, err 172 } 173 ruleCollectionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ruleCollectionName")]) 174 if err != nil { 175 return nil, err 176 } 177 ruleNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ruleName")]) 178 if err != nil { 179 return nil, err 180 } 181 forceUnescaped, err := url.QueryUnescape(qp.Get("force")) 182 if err != nil { 183 return nil, err 184 } 185 forceParam, err := parseOptional(forceUnescaped, strconv.ParseBool) 186 if err != nil { 187 return nil, err 188 } 189 var options *armnetwork.AdminRulesClientBeginDeleteOptions 190 if forceParam != nil { 191 options = &armnetwork.AdminRulesClientBeginDeleteOptions{ 192 Force: forceParam, 193 } 194 } 195 respr, errRespr := a.srv.BeginDelete(req.Context(), resourceGroupNameParam, networkManagerNameParam, configurationNameParam, ruleCollectionNameParam, ruleNameParam, options) 196 if respErr := server.GetError(errRespr, req); respErr != nil { 197 return nil, respErr 198 } 199 beginDelete = &respr 200 a.beginDelete.add(req, beginDelete) 201 } 202 203 resp, err := server.PollerResponderNext(beginDelete, req) 204 if err != nil { 205 return nil, err 206 } 207 208 if !contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) { 209 a.beginDelete.remove(req) 210 return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted, http.StatusNoContent", resp.StatusCode)} 211 } 212 if !server.PollerResponderMore(beginDelete) { 213 a.beginDelete.remove(req) 214 } 215 216 return resp, nil 217 } 218 219 func (a *AdminRulesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) { 220 if a.srv.Get == nil { 221 return nil, &nonRetriableError{errors.New("fake for method Get not implemented")} 222 } 223 const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/networkManagers/(?P<networkManagerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/securityAdminConfigurations/(?P<configurationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/ruleCollections/(?P<ruleCollectionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/rules/(?P<ruleName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)` 224 regex := regexp.MustCompile(regexStr) 225 matches := regex.FindStringSubmatch(req.URL.EscapedPath()) 226 if matches == nil || len(matches) < 6 { 227 return nil, fmt.Errorf("failed to parse path %s", req.URL.Path) 228 } 229 resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")]) 230 if err != nil { 231 return nil, err 232 } 233 networkManagerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("networkManagerName")]) 234 if err != nil { 235 return nil, err 236 } 237 configurationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("configurationName")]) 238 if err != nil { 239 return nil, err 240 } 241 ruleCollectionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ruleCollectionName")]) 242 if err != nil { 243 return nil, err 244 } 245 ruleNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ruleName")]) 246 if err != nil { 247 return nil, err 248 } 249 respr, errRespr := a.srv.Get(req.Context(), resourceGroupNameParam, networkManagerNameParam, configurationNameParam, ruleCollectionNameParam, ruleNameParam, nil) 250 if respErr := server.GetError(errRespr, req); respErr != nil { 251 return nil, respErr 252 } 253 respContent := server.GetResponseContent(respr) 254 if !contains([]int{http.StatusOK}, respContent.HTTPStatus) { 255 return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)} 256 } 257 resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).BaseAdminRuleClassification, req) 258 if err != nil { 259 return nil, err 260 } 261 return resp, nil 262 } 263 264 func (a *AdminRulesServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) { 265 if a.srv.NewListPager == nil { 266 return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")} 267 } 268 newListPager := a.newListPager.get(req) 269 if newListPager == nil { 270 const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/networkManagers/(?P<networkManagerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/securityAdminConfigurations/(?P<configurationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/ruleCollections/(?P<ruleCollectionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/rules` 271 regex := regexp.MustCompile(regexStr) 272 matches := regex.FindStringSubmatch(req.URL.EscapedPath()) 273 if matches == nil || len(matches) < 5 { 274 return nil, fmt.Errorf("failed to parse path %s", req.URL.Path) 275 } 276 qp := req.URL.Query() 277 resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")]) 278 if err != nil { 279 return nil, err 280 } 281 networkManagerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("networkManagerName")]) 282 if err != nil { 283 return nil, err 284 } 285 configurationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("configurationName")]) 286 if err != nil { 287 return nil, err 288 } 289 ruleCollectionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ruleCollectionName")]) 290 if err != nil { 291 return nil, err 292 } 293 topUnescaped, err := url.QueryUnescape(qp.Get("$top")) 294 if err != nil { 295 return nil, err 296 } 297 topParam, err := parseOptional(topUnescaped, func(v string) (int32, error) { 298 p, parseErr := strconv.ParseInt(v, 10, 32) 299 if parseErr != nil { 300 return 0, parseErr 301 } 302 return int32(p), nil 303 }) 304 if err != nil { 305 return nil, err 306 } 307 skipTokenUnescaped, err := url.QueryUnescape(qp.Get("$skipToken")) 308 if err != nil { 309 return nil, err 310 } 311 skipTokenParam := getOptional(skipTokenUnescaped) 312 var options *armnetwork.AdminRulesClientListOptions 313 if topParam != nil || skipTokenParam != nil { 314 options = &armnetwork.AdminRulesClientListOptions{ 315 Top: topParam, 316 SkipToken: skipTokenParam, 317 } 318 } 319 resp := a.srv.NewListPager(resourceGroupNameParam, networkManagerNameParam, configurationNameParam, ruleCollectionNameParam, options) 320 newListPager = &resp 321 a.newListPager.add(req, newListPager) 322 server.PagerResponderInjectNextLinks(newListPager, req, func(page *armnetwork.AdminRulesClientListResponse, createLink func() string) { 323 page.NextLink = to.Ptr(createLink()) 324 }) 325 } 326 resp, err := server.PagerResponderNext(newListPager, req) 327 if err != nil { 328 return nil, err 329 } 330 if !contains([]int{http.StatusOK}, resp.StatusCode) { 331 a.newListPager.remove(req) 332 return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)} 333 } 334 if !server.PagerResponderMore(newListPager) { 335 a.newListPager.remove(req) 336 } 337 return resp, nil 338 }