github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/fake/vipswap_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  // VipSwapServer is a fake server for instances of the armnetwork.VipSwapClient type.
    25  type VipSwapServer struct {
    26  	// BeginCreate is the fake for method VipSwapClient.BeginCreate
    27  	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
    28  	BeginCreate func(ctx context.Context, groupName string, resourceName string, parameters armnetwork.SwapResource, options *armnetwork.VipSwapClientBeginCreateOptions) (resp azfake.PollerResponder[armnetwork.VipSwapClientCreateResponse], errResp azfake.ErrorResponder)
    29  
    30  	// Get is the fake for method VipSwapClient.Get
    31  	// HTTP status codes to indicate success: http.StatusOK
    32  	Get func(ctx context.Context, groupName string, resourceName string, options *armnetwork.VipSwapClientGetOptions) (resp azfake.Responder[armnetwork.VipSwapClientGetResponse], errResp azfake.ErrorResponder)
    33  
    34  	// List is the fake for method VipSwapClient.List
    35  	// HTTP status codes to indicate success: http.StatusOK
    36  	List func(ctx context.Context, groupName string, resourceName string, options *armnetwork.VipSwapClientListOptions) (resp azfake.Responder[armnetwork.VipSwapClientListResponse], errResp azfake.ErrorResponder)
    37  }
    38  
    39  // NewVipSwapServerTransport creates a new instance of VipSwapServerTransport with the provided implementation.
    40  // The returned VipSwapServerTransport instance is connected to an instance of armnetwork.VipSwapClient via the
    41  // azcore.ClientOptions.Transporter field in the client's constructor parameters.
    42  func NewVipSwapServerTransport(srv *VipSwapServer) *VipSwapServerTransport {
    43  	return &VipSwapServerTransport{
    44  		srv:         srv,
    45  		beginCreate: newTracker[azfake.PollerResponder[armnetwork.VipSwapClientCreateResponse]](),
    46  	}
    47  }
    48  
    49  // VipSwapServerTransport connects instances of armnetwork.VipSwapClient to instances of VipSwapServer.
    50  // Don't use this type directly, use NewVipSwapServerTransport instead.
    51  type VipSwapServerTransport struct {
    52  	srv         *VipSwapServer
    53  	beginCreate *tracker[azfake.PollerResponder[armnetwork.VipSwapClientCreateResponse]]
    54  }
    55  
    56  // Do implements the policy.Transporter interface for VipSwapServerTransport.
    57  func (v *VipSwapServerTransport) Do(req *http.Request) (*http.Response, error) {
    58  	rawMethod := req.Context().Value(runtime.CtxAPINameKey{})
    59  	method, ok := rawMethod.(string)
    60  	if !ok {
    61  		return nil, nonRetriableError{errors.New("unable to dispatch request, missing value for CtxAPINameKey")}
    62  	}
    63  
    64  	var resp *http.Response
    65  	var err error
    66  
    67  	switch method {
    68  	case "VipSwapClient.BeginCreate":
    69  		resp, err = v.dispatchBeginCreate(req)
    70  	case "VipSwapClient.Get":
    71  		resp, err = v.dispatchGet(req)
    72  	case "VipSwapClient.List":
    73  		resp, err = v.dispatchList(req)
    74  	default:
    75  		err = fmt.Errorf("unhandled API %s", method)
    76  	}
    77  
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	return resp, nil
    83  }
    84  
    85  func (v *VipSwapServerTransport) dispatchBeginCreate(req *http.Request) (*http.Response, error) {
    86  	if v.srv.BeginCreate == nil {
    87  		return nil, &nonRetriableError{errors.New("fake for method BeginCreate not implemented")}
    88  	}
    89  	beginCreate := v.beginCreate.get(req)
    90  	if beginCreate == nil {
    91  		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<groupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Compute/cloudServices/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/cloudServiceSlots/(?P<singletonResource>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
    92  		regex := regexp.MustCompile(regexStr)
    93  		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
    94  		if matches == nil || len(matches) < 3 {
    95  			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
    96  		}
    97  		body, err := server.UnmarshalRequestAsJSON[armnetwork.SwapResource](req)
    98  		if err != nil {
    99  			return nil, err
   100  		}
   101  		groupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("groupName")])
   102  		if err != nil {
   103  			return nil, err
   104  		}
   105  		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
   106  		if err != nil {
   107  			return nil, err
   108  		}
   109  		respr, errRespr := v.srv.BeginCreate(req.Context(), groupNameParam, resourceNameParam, body, nil)
   110  		if respErr := server.GetError(errRespr, req); respErr != nil {
   111  			return nil, respErr
   112  		}
   113  		beginCreate = &respr
   114  		v.beginCreate.add(req, beginCreate)
   115  	}
   116  
   117  	resp, err := server.PollerResponderNext(beginCreate, req)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	if !contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
   123  		v.beginCreate.remove(req)
   124  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted", resp.StatusCode)}
   125  	}
   126  	if !server.PollerResponderMore(beginCreate) {
   127  		v.beginCreate.remove(req)
   128  	}
   129  
   130  	return resp, nil
   131  }
   132  
   133  func (v *VipSwapServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
   134  	if v.srv.Get == nil {
   135  		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
   136  	}
   137  	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<groupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Compute/cloudServices/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/cloudServiceSlots/(?P<singletonResource>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
   138  	regex := regexp.MustCompile(regexStr)
   139  	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   140  	if matches == nil || len(matches) < 3 {
   141  		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   142  	}
   143  	groupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("groupName")])
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  	resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	respr, errRespr := v.srv.Get(req.Context(), groupNameParam, resourceNameParam, nil)
   152  	if respErr := server.GetError(errRespr, req); respErr != nil {
   153  		return nil, respErr
   154  	}
   155  	respContent := server.GetResponseContent(respr)
   156  	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
   157  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
   158  	}
   159  	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).SwapResource, req)
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  	return resp, nil
   164  }
   165  
   166  func (v *VipSwapServerTransport) dispatchList(req *http.Request) (*http.Response, error) {
   167  	if v.srv.List == nil {
   168  		return nil, &nonRetriableError{errors.New("fake for method List not implemented")}
   169  	}
   170  	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<groupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Compute/cloudServices/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/cloudServiceSlots`
   171  	regex := regexp.MustCompile(regexStr)
   172  	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   173  	if matches == nil || len(matches) < 3 {
   174  		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   175  	}
   176  	groupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("groupName")])
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  	resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
   181  	if err != nil {
   182  		return nil, err
   183  	}
   184  	respr, errRespr := v.srv.List(req.Context(), groupNameParam, resourceNameParam, nil)
   185  	if respErr := server.GetError(errRespr, req); respErr != nil {
   186  		return nil, respErr
   187  	}
   188  	respContent := server.GetResponseContent(respr)
   189  	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
   190  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
   191  	}
   192  	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).SwapResourceListResult, req)
   193  	if err != nil {
   194  		return nil, err
   195  	}
   196  	return resp, nil
   197  }