github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/fake/inboundsecurityrule_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/resourcemanager/network/armnetwork/v6" 19 "net/http" 20 "net/url" 21 "regexp" 22 ) 23 24 // InboundSecurityRuleServer is a fake server for instances of the armnetwork.InboundSecurityRuleClient type. 25 type InboundSecurityRuleServer struct { 26 // BeginCreateOrUpdate is the fake for method InboundSecurityRuleClient.BeginCreateOrUpdate 27 // HTTP status codes to indicate success: http.StatusOK, http.StatusCreated 28 BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, networkVirtualApplianceName string, ruleCollectionName string, parameters armnetwork.InboundSecurityRule, options *armnetwork.InboundSecurityRuleClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armnetwork.InboundSecurityRuleClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) 29 30 // Get is the fake for method InboundSecurityRuleClient.Get 31 // HTTP status codes to indicate success: http.StatusOK 32 Get func(ctx context.Context, resourceGroupName string, networkVirtualApplianceName string, ruleCollectionName string, options *armnetwork.InboundSecurityRuleClientGetOptions) (resp azfake.Responder[armnetwork.InboundSecurityRuleClientGetResponse], errResp azfake.ErrorResponder) 33 } 34 35 // NewInboundSecurityRuleServerTransport creates a new instance of InboundSecurityRuleServerTransport with the provided implementation. 36 // The returned InboundSecurityRuleServerTransport instance is connected to an instance of armnetwork.InboundSecurityRuleClient via the 37 // azcore.ClientOptions.Transporter field in the client's constructor parameters. 38 func NewInboundSecurityRuleServerTransport(srv *InboundSecurityRuleServer) *InboundSecurityRuleServerTransport { 39 return &InboundSecurityRuleServerTransport{ 40 srv: srv, 41 beginCreateOrUpdate: newTracker[azfake.PollerResponder[armnetwork.InboundSecurityRuleClientCreateOrUpdateResponse]](), 42 } 43 } 44 45 // InboundSecurityRuleServerTransport connects instances of armnetwork.InboundSecurityRuleClient to instances of InboundSecurityRuleServer. 46 // Don't use this type directly, use NewInboundSecurityRuleServerTransport instead. 47 type InboundSecurityRuleServerTransport struct { 48 srv *InboundSecurityRuleServer 49 beginCreateOrUpdate *tracker[azfake.PollerResponder[armnetwork.InboundSecurityRuleClientCreateOrUpdateResponse]] 50 } 51 52 // Do implements the policy.Transporter interface for InboundSecurityRuleServerTransport. 53 func (i *InboundSecurityRuleServerTransport) Do(req *http.Request) (*http.Response, error) { 54 rawMethod := req.Context().Value(runtime.CtxAPINameKey{}) 55 method, ok := rawMethod.(string) 56 if !ok { 57 return nil, nonRetriableError{errors.New("unable to dispatch request, missing value for CtxAPINameKey")} 58 } 59 60 var resp *http.Response 61 var err error 62 63 switch method { 64 case "InboundSecurityRuleClient.BeginCreateOrUpdate": 65 resp, err = i.dispatchBeginCreateOrUpdate(req) 66 case "InboundSecurityRuleClient.Get": 67 resp, err = i.dispatchGet(req) 68 default: 69 err = fmt.Errorf("unhandled API %s", method) 70 } 71 72 if err != nil { 73 return nil, err 74 } 75 76 return resp, nil 77 } 78 79 func (i *InboundSecurityRuleServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) { 80 if i.srv.BeginCreateOrUpdate == nil { 81 return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")} 82 } 83 beginCreateOrUpdate := i.beginCreateOrUpdate.get(req) 84 if beginCreateOrUpdate == nil { 85 const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/networkVirtualAppliances/(?P<networkVirtualApplianceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/inboundSecurityRules/(?P<ruleCollectionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)` 86 regex := regexp.MustCompile(regexStr) 87 matches := regex.FindStringSubmatch(req.URL.EscapedPath()) 88 if matches == nil || len(matches) < 4 { 89 return nil, fmt.Errorf("failed to parse path %s", req.URL.Path) 90 } 91 body, err := server.UnmarshalRequestAsJSON[armnetwork.InboundSecurityRule](req) 92 if err != nil { 93 return nil, err 94 } 95 resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")]) 96 if err != nil { 97 return nil, err 98 } 99 networkVirtualApplianceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("networkVirtualApplianceName")]) 100 if err != nil { 101 return nil, err 102 } 103 ruleCollectionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ruleCollectionName")]) 104 if err != nil { 105 return nil, err 106 } 107 respr, errRespr := i.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, networkVirtualApplianceNameParam, ruleCollectionNameParam, body, nil) 108 if respErr := server.GetError(errRespr, req); respErr != nil { 109 return nil, respErr 110 } 111 beginCreateOrUpdate = &respr 112 i.beginCreateOrUpdate.add(req, beginCreateOrUpdate) 113 } 114 115 resp, err := server.PollerResponderNext(beginCreateOrUpdate, req) 116 if err != nil { 117 return nil, err 118 } 119 120 if !contains([]int{http.StatusOK, http.StatusCreated}, resp.StatusCode) { 121 i.beginCreateOrUpdate.remove(req) 122 return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", resp.StatusCode)} 123 } 124 if !server.PollerResponderMore(beginCreateOrUpdate) { 125 i.beginCreateOrUpdate.remove(req) 126 } 127 128 return resp, nil 129 } 130 131 func (i *InboundSecurityRuleServerTransport) dispatchGet(req *http.Request) (*http.Response, error) { 132 if i.srv.Get == nil { 133 return nil, &nonRetriableError{errors.New("fake for method Get not implemented")} 134 } 135 const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/networkVirtualAppliances/(?P<networkVirtualApplianceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/inboundSecurityRules/(?P<ruleCollectionName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)` 136 regex := regexp.MustCompile(regexStr) 137 matches := regex.FindStringSubmatch(req.URL.EscapedPath()) 138 if matches == nil || len(matches) < 4 { 139 return nil, fmt.Errorf("failed to parse path %s", req.URL.Path) 140 } 141 resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")]) 142 if err != nil { 143 return nil, err 144 } 145 networkVirtualApplianceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("networkVirtualApplianceName")]) 146 if err != nil { 147 return nil, err 148 } 149 ruleCollectionNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ruleCollectionName")]) 150 if err != nil { 151 return nil, err 152 } 153 respr, errRespr := i.srv.Get(req.Context(), resourceGroupNameParam, networkVirtualApplianceNameParam, ruleCollectionNameParam, nil) 154 if respErr := server.GetError(errRespr, req); respErr != nil { 155 return nil, respErr 156 } 157 respContent := server.GetResponseContent(respr) 158 if !contains([]int{http.StatusOK}, respContent.HTTPStatus) { 159 return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)} 160 } 161 resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).InboundSecurityRule, req) 162 if err != nil { 163 return nil, err 164 } 165 return resp, nil 166 }