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