github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/fake/inboundnatrules_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  )
    24  
    25  // InboundNatRulesServer is a fake server for instances of the armnetwork.InboundNatRulesClient type.
    26  type InboundNatRulesServer struct {
    27  	// BeginCreateOrUpdate is the fake for method InboundNatRulesClient.BeginCreateOrUpdate
    28  	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
    29  	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, inboundNatRuleParameters armnetwork.InboundNatRule, options *armnetwork.InboundNatRulesClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armnetwork.InboundNatRulesClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)
    30  
    31  	// BeginDelete is the fake for method InboundNatRulesClient.BeginDelete
    32  	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
    33  	BeginDelete func(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, options *armnetwork.InboundNatRulesClientBeginDeleteOptions) (resp azfake.PollerResponder[armnetwork.InboundNatRulesClientDeleteResponse], errResp azfake.ErrorResponder)
    34  
    35  	// Get is the fake for method InboundNatRulesClient.Get
    36  	// HTTP status codes to indicate success: http.StatusOK
    37  	Get func(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, options *armnetwork.InboundNatRulesClientGetOptions) (resp azfake.Responder[armnetwork.InboundNatRulesClientGetResponse], errResp azfake.ErrorResponder)
    38  
    39  	// NewListPager is the fake for method InboundNatRulesClient.NewListPager
    40  	// HTTP status codes to indicate success: http.StatusOK
    41  	NewListPager func(resourceGroupName string, loadBalancerName string, options *armnetwork.InboundNatRulesClientListOptions) (resp azfake.PagerResponder[armnetwork.InboundNatRulesClientListResponse])
    42  }
    43  
    44  // NewInboundNatRulesServerTransport creates a new instance of InboundNatRulesServerTransport with the provided implementation.
    45  // The returned InboundNatRulesServerTransport instance is connected to an instance of armnetwork.InboundNatRulesClient via the
    46  // azcore.ClientOptions.Transporter field in the client's constructor parameters.
    47  func NewInboundNatRulesServerTransport(srv *InboundNatRulesServer) *InboundNatRulesServerTransport {
    48  	return &InboundNatRulesServerTransport{
    49  		srv:                 srv,
    50  		beginCreateOrUpdate: newTracker[azfake.PollerResponder[armnetwork.InboundNatRulesClientCreateOrUpdateResponse]](),
    51  		beginDelete:         newTracker[azfake.PollerResponder[armnetwork.InboundNatRulesClientDeleteResponse]](),
    52  		newListPager:        newTracker[azfake.PagerResponder[armnetwork.InboundNatRulesClientListResponse]](),
    53  	}
    54  }
    55  
    56  // InboundNatRulesServerTransport connects instances of armnetwork.InboundNatRulesClient to instances of InboundNatRulesServer.
    57  // Don't use this type directly, use NewInboundNatRulesServerTransport instead.
    58  type InboundNatRulesServerTransport struct {
    59  	srv                 *InboundNatRulesServer
    60  	beginCreateOrUpdate *tracker[azfake.PollerResponder[armnetwork.InboundNatRulesClientCreateOrUpdateResponse]]
    61  	beginDelete         *tracker[azfake.PollerResponder[armnetwork.InboundNatRulesClientDeleteResponse]]
    62  	newListPager        *tracker[azfake.PagerResponder[armnetwork.InboundNatRulesClientListResponse]]
    63  }
    64  
    65  // Do implements the policy.Transporter interface for InboundNatRulesServerTransport.
    66  func (i *InboundNatRulesServerTransport) Do(req *http.Request) (*http.Response, error) {
    67  	rawMethod := req.Context().Value(runtime.CtxAPINameKey{})
    68  	method, ok := rawMethod.(string)
    69  	if !ok {
    70  		return nil, nonRetriableError{errors.New("unable to dispatch request, missing value for CtxAPINameKey")}
    71  	}
    72  
    73  	var resp *http.Response
    74  	var err error
    75  
    76  	switch method {
    77  	case "InboundNatRulesClient.BeginCreateOrUpdate":
    78  		resp, err = i.dispatchBeginCreateOrUpdate(req)
    79  	case "InboundNatRulesClient.BeginDelete":
    80  		resp, err = i.dispatchBeginDelete(req)
    81  	case "InboundNatRulesClient.Get":
    82  		resp, err = i.dispatchGet(req)
    83  	case "InboundNatRulesClient.NewListPager":
    84  		resp, err = i.dispatchNewListPager(req)
    85  	default:
    86  		err = fmt.Errorf("unhandled API %s", method)
    87  	}
    88  
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	return resp, nil
    94  }
    95  
    96  func (i *InboundNatRulesServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
    97  	if i.srv.BeginCreateOrUpdate == nil {
    98  		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
    99  	}
   100  	beginCreateOrUpdate := i.beginCreateOrUpdate.get(req)
   101  	if beginCreateOrUpdate == nil {
   102  		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/loadBalancers/(?P<loadBalancerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/inboundNatRules/(?P<inboundNatRuleName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
   103  		regex := regexp.MustCompile(regexStr)
   104  		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   105  		if matches == nil || len(matches) < 4 {
   106  			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   107  		}
   108  		body, err := server.UnmarshalRequestAsJSON[armnetwork.InboundNatRule](req)
   109  		if err != nil {
   110  			return nil, err
   111  		}
   112  		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
   113  		if err != nil {
   114  			return nil, err
   115  		}
   116  		loadBalancerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("loadBalancerName")])
   117  		if err != nil {
   118  			return nil, err
   119  		}
   120  		inboundNatRuleNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("inboundNatRuleName")])
   121  		if err != nil {
   122  			return nil, err
   123  		}
   124  		respr, errRespr := i.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, loadBalancerNameParam, inboundNatRuleNameParam, body, nil)
   125  		if respErr := server.GetError(errRespr, req); respErr != nil {
   126  			return nil, respErr
   127  		}
   128  		beginCreateOrUpdate = &respr
   129  		i.beginCreateOrUpdate.add(req, beginCreateOrUpdate)
   130  	}
   131  
   132  	resp, err := server.PollerResponderNext(beginCreateOrUpdate, req)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	if !contains([]int{http.StatusOK, http.StatusCreated}, resp.StatusCode) {
   138  		i.beginCreateOrUpdate.remove(req)
   139  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", resp.StatusCode)}
   140  	}
   141  	if !server.PollerResponderMore(beginCreateOrUpdate) {
   142  		i.beginCreateOrUpdate.remove(req)
   143  	}
   144  
   145  	return resp, nil
   146  }
   147  
   148  func (i *InboundNatRulesServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
   149  	if i.srv.BeginDelete == nil {
   150  		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
   151  	}
   152  	beginDelete := i.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/loadBalancers/(?P<loadBalancerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/inboundNatRules/(?P<inboundNatRuleName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
   155  		regex := regexp.MustCompile(regexStr)
   156  		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   157  		if matches == nil || len(matches) < 4 {
   158  			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   159  		}
   160  		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
   161  		if err != nil {
   162  			return nil, err
   163  		}
   164  		loadBalancerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("loadBalancerName")])
   165  		if err != nil {
   166  			return nil, err
   167  		}
   168  		inboundNatRuleNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("inboundNatRuleName")])
   169  		if err != nil {
   170  			return nil, err
   171  		}
   172  		respr, errRespr := i.srv.BeginDelete(req.Context(), resourceGroupNameParam, loadBalancerNameParam, inboundNatRuleNameParam, nil)
   173  		if respErr := server.GetError(errRespr, req); respErr != nil {
   174  			return nil, respErr
   175  		}
   176  		beginDelete = &respr
   177  		i.beginDelete.add(req, beginDelete)
   178  	}
   179  
   180  	resp, err := server.PollerResponderNext(beginDelete, req)
   181  	if err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	if !contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
   186  		i.beginDelete.remove(req)
   187  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted, http.StatusNoContent", resp.StatusCode)}
   188  	}
   189  	if !server.PollerResponderMore(beginDelete) {
   190  		i.beginDelete.remove(req)
   191  	}
   192  
   193  	return resp, nil
   194  }
   195  
   196  func (i *InboundNatRulesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
   197  	if i.srv.Get == nil {
   198  		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
   199  	}
   200  	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/loadBalancers/(?P<loadBalancerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/inboundNatRules/(?P<inboundNatRuleName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
   201  	regex := regexp.MustCompile(regexStr)
   202  	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   203  	if matches == nil || len(matches) < 4 {
   204  		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   205  	}
   206  	qp := req.URL.Query()
   207  	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
   208  	if err != nil {
   209  		return nil, err
   210  	}
   211  	loadBalancerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("loadBalancerName")])
   212  	if err != nil {
   213  		return nil, err
   214  	}
   215  	inboundNatRuleNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("inboundNatRuleName")])
   216  	if err != nil {
   217  		return nil, err
   218  	}
   219  	expandUnescaped, err := url.QueryUnescape(qp.Get("$expand"))
   220  	if err != nil {
   221  		return nil, err
   222  	}
   223  	expandParam := getOptional(expandUnescaped)
   224  	var options *armnetwork.InboundNatRulesClientGetOptions
   225  	if expandParam != nil {
   226  		options = &armnetwork.InboundNatRulesClientGetOptions{
   227  			Expand: expandParam,
   228  		}
   229  	}
   230  	respr, errRespr := i.srv.Get(req.Context(), resourceGroupNameParam, loadBalancerNameParam, inboundNatRuleNameParam, options)
   231  	if respErr := server.GetError(errRespr, req); respErr != nil {
   232  		return nil, respErr
   233  	}
   234  	respContent := server.GetResponseContent(respr)
   235  	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
   236  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
   237  	}
   238  	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).InboundNatRule, req)
   239  	if err != nil {
   240  		return nil, err
   241  	}
   242  	return resp, nil
   243  }
   244  
   245  func (i *InboundNatRulesServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
   246  	if i.srv.NewListPager == nil {
   247  		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
   248  	}
   249  	newListPager := i.newListPager.get(req)
   250  	if newListPager == nil {
   251  		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/loadBalancers/(?P<loadBalancerName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/inboundNatRules`
   252  		regex := regexp.MustCompile(regexStr)
   253  		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   254  		if matches == nil || len(matches) < 3 {
   255  			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   256  		}
   257  		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
   258  		if err != nil {
   259  			return nil, err
   260  		}
   261  		loadBalancerNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("loadBalancerName")])
   262  		if err != nil {
   263  			return nil, err
   264  		}
   265  		resp := i.srv.NewListPager(resourceGroupNameParam, loadBalancerNameParam, nil)
   266  		newListPager = &resp
   267  		i.newListPager.add(req, newListPager)
   268  		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armnetwork.InboundNatRulesClientListResponse, createLink func() string) {
   269  			page.NextLink = to.Ptr(createLink())
   270  		})
   271  	}
   272  	resp, err := server.PagerResponderNext(newListPager, req)
   273  	if err != nil {
   274  		return nil, err
   275  	}
   276  	if !contains([]int{http.StatusOK}, resp.StatusCode) {
   277  		i.newListPager.remove(req)
   278  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
   279  	}
   280  	if !server.PagerResponderMore(newListPager) {
   281  		i.newListPager.remove(req)
   282  	}
   283  	return resp, nil
   284  }