github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/fake/ipallocations_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  // IPAllocationsServer is a fake server for instances of the armnetwork.IPAllocationsClient type.
    26  type IPAllocationsServer struct {
    27  	// BeginCreateOrUpdate is the fake for method IPAllocationsClient.BeginCreateOrUpdate
    28  	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
    29  	BeginCreateOrUpdate func(ctx context.Context, resourceGroupName string, ipAllocationName string, parameters armnetwork.IPAllocation, options *armnetwork.IPAllocationsClientBeginCreateOrUpdateOptions) (resp azfake.PollerResponder[armnetwork.IPAllocationsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)
    30  
    31  	// BeginDelete is the fake for method IPAllocationsClient.BeginDelete
    32  	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
    33  	BeginDelete func(ctx context.Context, resourceGroupName string, ipAllocationName string, options *armnetwork.IPAllocationsClientBeginDeleteOptions) (resp azfake.PollerResponder[armnetwork.IPAllocationsClientDeleteResponse], errResp azfake.ErrorResponder)
    34  
    35  	// Get is the fake for method IPAllocationsClient.Get
    36  	// HTTP status codes to indicate success: http.StatusOK
    37  	Get func(ctx context.Context, resourceGroupName string, ipAllocationName string, options *armnetwork.IPAllocationsClientGetOptions) (resp azfake.Responder[armnetwork.IPAllocationsClientGetResponse], errResp azfake.ErrorResponder)
    38  
    39  	// NewListPager is the fake for method IPAllocationsClient.NewListPager
    40  	// HTTP status codes to indicate success: http.StatusOK
    41  	NewListPager func(options *armnetwork.IPAllocationsClientListOptions) (resp azfake.PagerResponder[armnetwork.IPAllocationsClientListResponse])
    42  
    43  	// NewListByResourceGroupPager is the fake for method IPAllocationsClient.NewListByResourceGroupPager
    44  	// HTTP status codes to indicate success: http.StatusOK
    45  	NewListByResourceGroupPager func(resourceGroupName string, options *armnetwork.IPAllocationsClientListByResourceGroupOptions) (resp azfake.PagerResponder[armnetwork.IPAllocationsClientListByResourceGroupResponse])
    46  
    47  	// UpdateTags is the fake for method IPAllocationsClient.UpdateTags
    48  	// HTTP status codes to indicate success: http.StatusOK
    49  	UpdateTags func(ctx context.Context, resourceGroupName string, ipAllocationName string, parameters armnetwork.TagsObject, options *armnetwork.IPAllocationsClientUpdateTagsOptions) (resp azfake.Responder[armnetwork.IPAllocationsClientUpdateTagsResponse], errResp azfake.ErrorResponder)
    50  }
    51  
    52  // NewIPAllocationsServerTransport creates a new instance of IPAllocationsServerTransport with the provided implementation.
    53  // The returned IPAllocationsServerTransport instance is connected to an instance of armnetwork.IPAllocationsClient via the
    54  // azcore.ClientOptions.Transporter field in the client's constructor parameters.
    55  func NewIPAllocationsServerTransport(srv *IPAllocationsServer) *IPAllocationsServerTransport {
    56  	return &IPAllocationsServerTransport{
    57  		srv:                         srv,
    58  		beginCreateOrUpdate:         newTracker[azfake.PollerResponder[armnetwork.IPAllocationsClientCreateOrUpdateResponse]](),
    59  		beginDelete:                 newTracker[azfake.PollerResponder[armnetwork.IPAllocationsClientDeleteResponse]](),
    60  		newListPager:                newTracker[azfake.PagerResponder[armnetwork.IPAllocationsClientListResponse]](),
    61  		newListByResourceGroupPager: newTracker[azfake.PagerResponder[armnetwork.IPAllocationsClientListByResourceGroupResponse]](),
    62  	}
    63  }
    64  
    65  // IPAllocationsServerTransport connects instances of armnetwork.IPAllocationsClient to instances of IPAllocationsServer.
    66  // Don't use this type directly, use NewIPAllocationsServerTransport instead.
    67  type IPAllocationsServerTransport struct {
    68  	srv                         *IPAllocationsServer
    69  	beginCreateOrUpdate         *tracker[azfake.PollerResponder[armnetwork.IPAllocationsClientCreateOrUpdateResponse]]
    70  	beginDelete                 *tracker[azfake.PollerResponder[armnetwork.IPAllocationsClientDeleteResponse]]
    71  	newListPager                *tracker[azfake.PagerResponder[armnetwork.IPAllocationsClientListResponse]]
    72  	newListByResourceGroupPager *tracker[azfake.PagerResponder[armnetwork.IPAllocationsClientListByResourceGroupResponse]]
    73  }
    74  
    75  // Do implements the policy.Transporter interface for IPAllocationsServerTransport.
    76  func (i *IPAllocationsServerTransport) Do(req *http.Request) (*http.Response, error) {
    77  	rawMethod := req.Context().Value(runtime.CtxAPINameKey{})
    78  	method, ok := rawMethod.(string)
    79  	if !ok {
    80  		return nil, nonRetriableError{errors.New("unable to dispatch request, missing value for CtxAPINameKey")}
    81  	}
    82  
    83  	var resp *http.Response
    84  	var err error
    85  
    86  	switch method {
    87  	case "IPAllocationsClient.BeginCreateOrUpdate":
    88  		resp, err = i.dispatchBeginCreateOrUpdate(req)
    89  	case "IPAllocationsClient.BeginDelete":
    90  		resp, err = i.dispatchBeginDelete(req)
    91  	case "IPAllocationsClient.Get":
    92  		resp, err = i.dispatchGet(req)
    93  	case "IPAllocationsClient.NewListPager":
    94  		resp, err = i.dispatchNewListPager(req)
    95  	case "IPAllocationsClient.NewListByResourceGroupPager":
    96  		resp, err = i.dispatchNewListByResourceGroupPager(req)
    97  	case "IPAllocationsClient.UpdateTags":
    98  		resp, err = i.dispatchUpdateTags(req)
    99  	default:
   100  		err = fmt.Errorf("unhandled API %s", method)
   101  	}
   102  
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	return resp, nil
   108  }
   109  
   110  func (i *IPAllocationsServerTransport) dispatchBeginCreateOrUpdate(req *http.Request) (*http.Response, error) {
   111  	if i.srv.BeginCreateOrUpdate == nil {
   112  		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOrUpdate not implemented")}
   113  	}
   114  	beginCreateOrUpdate := i.beginCreateOrUpdate.get(req)
   115  	if beginCreateOrUpdate == nil {
   116  		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/IpAllocations/(?P<ipAllocationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
   117  		regex := regexp.MustCompile(regexStr)
   118  		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   119  		if matches == nil || len(matches) < 3 {
   120  			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   121  		}
   122  		body, err := server.UnmarshalRequestAsJSON[armnetwork.IPAllocation](req)
   123  		if err != nil {
   124  			return nil, err
   125  		}
   126  		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
   127  		if err != nil {
   128  			return nil, err
   129  		}
   130  		ipAllocationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ipAllocationName")])
   131  		if err != nil {
   132  			return nil, err
   133  		}
   134  		respr, errRespr := i.srv.BeginCreateOrUpdate(req.Context(), resourceGroupNameParam, ipAllocationNameParam, body, nil)
   135  		if respErr := server.GetError(errRespr, req); respErr != nil {
   136  			return nil, respErr
   137  		}
   138  		beginCreateOrUpdate = &respr
   139  		i.beginCreateOrUpdate.add(req, beginCreateOrUpdate)
   140  	}
   141  
   142  	resp, err := server.PollerResponderNext(beginCreateOrUpdate, req)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	if !contains([]int{http.StatusOK, http.StatusCreated}, resp.StatusCode) {
   148  		i.beginCreateOrUpdate.remove(req)
   149  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", resp.StatusCode)}
   150  	}
   151  	if !server.PollerResponderMore(beginCreateOrUpdate) {
   152  		i.beginCreateOrUpdate.remove(req)
   153  	}
   154  
   155  	return resp, nil
   156  }
   157  
   158  func (i *IPAllocationsServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
   159  	if i.srv.BeginDelete == nil {
   160  		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
   161  	}
   162  	beginDelete := i.beginDelete.get(req)
   163  	if beginDelete == nil {
   164  		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/IpAllocations/(?P<ipAllocationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
   165  		regex := regexp.MustCompile(regexStr)
   166  		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   167  		if matches == nil || len(matches) < 3 {
   168  			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   169  		}
   170  		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
   171  		if err != nil {
   172  			return nil, err
   173  		}
   174  		ipAllocationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ipAllocationName")])
   175  		if err != nil {
   176  			return nil, err
   177  		}
   178  		respr, errRespr := i.srv.BeginDelete(req.Context(), resourceGroupNameParam, ipAllocationNameParam, nil)
   179  		if respErr := server.GetError(errRespr, req); respErr != nil {
   180  			return nil, respErr
   181  		}
   182  		beginDelete = &respr
   183  		i.beginDelete.add(req, beginDelete)
   184  	}
   185  
   186  	resp, err := server.PollerResponderNext(beginDelete, req)
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  
   191  	if !contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
   192  		i.beginDelete.remove(req)
   193  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted, http.StatusNoContent", resp.StatusCode)}
   194  	}
   195  	if !server.PollerResponderMore(beginDelete) {
   196  		i.beginDelete.remove(req)
   197  	}
   198  
   199  	return resp, nil
   200  }
   201  
   202  func (i *IPAllocationsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
   203  	if i.srv.Get == nil {
   204  		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
   205  	}
   206  	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/IpAllocations/(?P<ipAllocationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
   207  	regex := regexp.MustCompile(regexStr)
   208  	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   209  	if matches == nil || len(matches) < 3 {
   210  		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   211  	}
   212  	qp := req.URL.Query()
   213  	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
   214  	if err != nil {
   215  		return nil, err
   216  	}
   217  	ipAllocationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ipAllocationName")])
   218  	if err != nil {
   219  		return nil, err
   220  	}
   221  	expandUnescaped, err := url.QueryUnescape(qp.Get("$expand"))
   222  	if err != nil {
   223  		return nil, err
   224  	}
   225  	expandParam := getOptional(expandUnescaped)
   226  	var options *armnetwork.IPAllocationsClientGetOptions
   227  	if expandParam != nil {
   228  		options = &armnetwork.IPAllocationsClientGetOptions{
   229  			Expand: expandParam,
   230  		}
   231  	}
   232  	respr, errRespr := i.srv.Get(req.Context(), resourceGroupNameParam, ipAllocationNameParam, options)
   233  	if respErr := server.GetError(errRespr, req); respErr != nil {
   234  		return nil, respErr
   235  	}
   236  	respContent := server.GetResponseContent(respr)
   237  	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
   238  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
   239  	}
   240  	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).IPAllocation, req)
   241  	if err != nil {
   242  		return nil, err
   243  	}
   244  	return resp, nil
   245  }
   246  
   247  func (i *IPAllocationsServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
   248  	if i.srv.NewListPager == nil {
   249  		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
   250  	}
   251  	newListPager := i.newListPager.get(req)
   252  	if newListPager == nil {
   253  		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/IpAllocations`
   254  		regex := regexp.MustCompile(regexStr)
   255  		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   256  		if matches == nil || len(matches) < 1 {
   257  			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   258  		}
   259  		resp := i.srv.NewListPager(nil)
   260  		newListPager = &resp
   261  		i.newListPager.add(req, newListPager)
   262  		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armnetwork.IPAllocationsClientListResponse, createLink func() string) {
   263  			page.NextLink = to.Ptr(createLink())
   264  		})
   265  	}
   266  	resp, err := server.PagerResponderNext(newListPager, req)
   267  	if err != nil {
   268  		return nil, err
   269  	}
   270  	if !contains([]int{http.StatusOK}, resp.StatusCode) {
   271  		i.newListPager.remove(req)
   272  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
   273  	}
   274  	if !server.PagerResponderMore(newListPager) {
   275  		i.newListPager.remove(req)
   276  	}
   277  	return resp, nil
   278  }
   279  
   280  func (i *IPAllocationsServerTransport) dispatchNewListByResourceGroupPager(req *http.Request) (*http.Response, error) {
   281  	if i.srv.NewListByResourceGroupPager == nil {
   282  		return nil, &nonRetriableError{errors.New("fake for method NewListByResourceGroupPager not implemented")}
   283  	}
   284  	newListByResourceGroupPager := i.newListByResourceGroupPager.get(req)
   285  	if newListByResourceGroupPager == nil {
   286  		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/IpAllocations`
   287  		regex := regexp.MustCompile(regexStr)
   288  		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   289  		if matches == nil || len(matches) < 2 {
   290  			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   291  		}
   292  		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
   293  		if err != nil {
   294  			return nil, err
   295  		}
   296  		resp := i.srv.NewListByResourceGroupPager(resourceGroupNameParam, nil)
   297  		newListByResourceGroupPager = &resp
   298  		i.newListByResourceGroupPager.add(req, newListByResourceGroupPager)
   299  		server.PagerResponderInjectNextLinks(newListByResourceGroupPager, req, func(page *armnetwork.IPAllocationsClientListByResourceGroupResponse, createLink func() string) {
   300  			page.NextLink = to.Ptr(createLink())
   301  		})
   302  	}
   303  	resp, err := server.PagerResponderNext(newListByResourceGroupPager, req)
   304  	if err != nil {
   305  		return nil, err
   306  	}
   307  	if !contains([]int{http.StatusOK}, resp.StatusCode) {
   308  		i.newListByResourceGroupPager.remove(req)
   309  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
   310  	}
   311  	if !server.PagerResponderMore(newListByResourceGroupPager) {
   312  		i.newListByResourceGroupPager.remove(req)
   313  	}
   314  	return resp, nil
   315  }
   316  
   317  func (i *IPAllocationsServerTransport) dispatchUpdateTags(req *http.Request) (*http.Response, error) {
   318  	if i.srv.UpdateTags == nil {
   319  		return nil, &nonRetriableError{errors.New("fake for method UpdateTags not implemented")}
   320  	}
   321  	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Network/IpAllocations/(?P<ipAllocationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
   322  	regex := regexp.MustCompile(regexStr)
   323  	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
   324  	if matches == nil || len(matches) < 3 {
   325  		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
   326  	}
   327  	body, err := server.UnmarshalRequestAsJSON[armnetwork.TagsObject](req)
   328  	if err != nil {
   329  		return nil, err
   330  	}
   331  	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
   332  	if err != nil {
   333  		return nil, err
   334  	}
   335  	ipAllocationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("ipAllocationName")])
   336  	if err != nil {
   337  		return nil, err
   338  	}
   339  	respr, errRespr := i.srv.UpdateTags(req.Context(), resourceGroupNameParam, ipAllocationNameParam, body, nil)
   340  	if respErr := server.GetError(errRespr, req); respErr != nil {
   341  		return nil, respErr
   342  	}
   343  	respContent := server.GetResponseContent(respr)
   344  	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
   345  		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
   346  	}
   347  	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).IPAllocation, req)
   348  	if err != nil {
   349  		return nil, err
   350  	}
   351  	return resp, nil
   352  }