github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/subscriptionnetworkmanagerconnections_client.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 armnetwork
    10  
    11  import (
    12  	"context"
    13  	"errors"
    14  	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
    15  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
    16  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
    17  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
    18  	"net/http"
    19  	"net/url"
    20  	"strconv"
    21  	"strings"
    22  )
    23  
    24  // SubscriptionNetworkManagerConnectionsClient contains the methods for the SubscriptionNetworkManagerConnections group.
    25  // Don't use this type directly, use NewSubscriptionNetworkManagerConnectionsClient() instead.
    26  type SubscriptionNetworkManagerConnectionsClient struct {
    27  	internal       *arm.Client
    28  	subscriptionID string
    29  }
    30  
    31  // NewSubscriptionNetworkManagerConnectionsClient creates a new instance of SubscriptionNetworkManagerConnectionsClient with the specified values.
    32  //   - subscriptionID - The subscription credentials which uniquely identify the Microsoft Azure subscription. The subscription
    33  //     ID forms part of the URI for every service call.
    34  //   - credential - used to authorize requests. Usually a credential from azidentity.
    35  //   - options - pass nil to accept the default values.
    36  func NewSubscriptionNetworkManagerConnectionsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*SubscriptionNetworkManagerConnectionsClient, error) {
    37  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	client := &SubscriptionNetworkManagerConnectionsClient{
    42  		subscriptionID: subscriptionID,
    43  		internal:       cl,
    44  	}
    45  	return client, nil
    46  }
    47  
    48  // CreateOrUpdate - Create a network manager connection on this subscription.
    49  // If the operation fails it returns an *azcore.ResponseError type.
    50  //
    51  // Generated from API version 2024-05-01
    52  //   - networkManagerConnectionName - Name for the network manager connection.
    53  //   - parameters - Network manager connection to be created/updated.
    54  //   - options - SubscriptionNetworkManagerConnectionsClientCreateOrUpdateOptions contains the optional parameters for the SubscriptionNetworkManagerConnectionsClient.CreateOrUpdate
    55  //     method.
    56  func (client *SubscriptionNetworkManagerConnectionsClient) CreateOrUpdate(ctx context.Context, networkManagerConnectionName string, parameters ManagerConnection, options *SubscriptionNetworkManagerConnectionsClientCreateOrUpdateOptions) (SubscriptionNetworkManagerConnectionsClientCreateOrUpdateResponse, error) {
    57  	var err error
    58  	const operationName = "SubscriptionNetworkManagerConnectionsClient.CreateOrUpdate"
    59  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
    60  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
    61  	defer func() { endSpan(err) }()
    62  	req, err := client.createOrUpdateCreateRequest(ctx, networkManagerConnectionName, parameters, options)
    63  	if err != nil {
    64  		return SubscriptionNetworkManagerConnectionsClientCreateOrUpdateResponse{}, err
    65  	}
    66  	httpResp, err := client.internal.Pipeline().Do(req)
    67  	if err != nil {
    68  		return SubscriptionNetworkManagerConnectionsClientCreateOrUpdateResponse{}, err
    69  	}
    70  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
    71  		err = runtime.NewResponseError(httpResp)
    72  		return SubscriptionNetworkManagerConnectionsClientCreateOrUpdateResponse{}, err
    73  	}
    74  	resp, err := client.createOrUpdateHandleResponse(httpResp)
    75  	return resp, err
    76  }
    77  
    78  // createOrUpdateCreateRequest creates the CreateOrUpdate request.
    79  func (client *SubscriptionNetworkManagerConnectionsClient) createOrUpdateCreateRequest(ctx context.Context, networkManagerConnectionName string, parameters ManagerConnection, options *SubscriptionNetworkManagerConnectionsClientCreateOrUpdateOptions) (*policy.Request, error) {
    80  	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Network/networkManagerConnections/{networkManagerConnectionName}"
    81  	if client.subscriptionID == "" {
    82  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
    83  	}
    84  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
    85  	if networkManagerConnectionName == "" {
    86  		return nil, errors.New("parameter networkManagerConnectionName cannot be empty")
    87  	}
    88  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerConnectionName}", url.PathEscape(networkManagerConnectionName))
    89  	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	reqQP := req.Raw().URL.Query()
    94  	reqQP.Set("api-version", "2024-05-01")
    95  	req.Raw().URL.RawQuery = reqQP.Encode()
    96  	req.Raw().Header["Accept"] = []string{"application/json"}
    97  	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
    98  		return nil, err
    99  	}
   100  	return req, nil
   101  }
   102  
   103  // createOrUpdateHandleResponse handles the CreateOrUpdate response.
   104  func (client *SubscriptionNetworkManagerConnectionsClient) createOrUpdateHandleResponse(resp *http.Response) (SubscriptionNetworkManagerConnectionsClientCreateOrUpdateResponse, error) {
   105  	result := SubscriptionNetworkManagerConnectionsClientCreateOrUpdateResponse{}
   106  	if err := runtime.UnmarshalAsJSON(resp, &result.ManagerConnection); err != nil {
   107  		return SubscriptionNetworkManagerConnectionsClientCreateOrUpdateResponse{}, err
   108  	}
   109  	return result, nil
   110  }
   111  
   112  // Delete - Delete specified connection created by this subscription.
   113  // If the operation fails it returns an *azcore.ResponseError type.
   114  //
   115  // Generated from API version 2024-05-01
   116  //   - networkManagerConnectionName - Name for the network manager connection.
   117  //   - options - SubscriptionNetworkManagerConnectionsClientDeleteOptions contains the optional parameters for the SubscriptionNetworkManagerConnectionsClient.Delete
   118  //     method.
   119  func (client *SubscriptionNetworkManagerConnectionsClient) Delete(ctx context.Context, networkManagerConnectionName string, options *SubscriptionNetworkManagerConnectionsClientDeleteOptions) (SubscriptionNetworkManagerConnectionsClientDeleteResponse, error) {
   120  	var err error
   121  	const operationName = "SubscriptionNetworkManagerConnectionsClient.Delete"
   122  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   123  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   124  	defer func() { endSpan(err) }()
   125  	req, err := client.deleteCreateRequest(ctx, networkManagerConnectionName, options)
   126  	if err != nil {
   127  		return SubscriptionNetworkManagerConnectionsClientDeleteResponse{}, err
   128  	}
   129  	httpResp, err := client.internal.Pipeline().Do(req)
   130  	if err != nil {
   131  		return SubscriptionNetworkManagerConnectionsClientDeleteResponse{}, err
   132  	}
   133  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
   134  		err = runtime.NewResponseError(httpResp)
   135  		return SubscriptionNetworkManagerConnectionsClientDeleteResponse{}, err
   136  	}
   137  	return SubscriptionNetworkManagerConnectionsClientDeleteResponse{}, nil
   138  }
   139  
   140  // deleteCreateRequest creates the Delete request.
   141  func (client *SubscriptionNetworkManagerConnectionsClient) deleteCreateRequest(ctx context.Context, networkManagerConnectionName string, options *SubscriptionNetworkManagerConnectionsClientDeleteOptions) (*policy.Request, error) {
   142  	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Network/networkManagerConnections/{networkManagerConnectionName}"
   143  	if client.subscriptionID == "" {
   144  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   145  	}
   146  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   147  	if networkManagerConnectionName == "" {
   148  		return nil, errors.New("parameter networkManagerConnectionName cannot be empty")
   149  	}
   150  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerConnectionName}", url.PathEscape(networkManagerConnectionName))
   151  	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  	reqQP := req.Raw().URL.Query()
   156  	reqQP.Set("api-version", "2024-05-01")
   157  	req.Raw().URL.RawQuery = reqQP.Encode()
   158  	req.Raw().Header["Accept"] = []string{"application/json"}
   159  	return req, nil
   160  }
   161  
   162  // Get - Get a specified connection created by this subscription.
   163  // If the operation fails it returns an *azcore.ResponseError type.
   164  //
   165  // Generated from API version 2024-05-01
   166  //   - networkManagerConnectionName - Name for the network manager connection.
   167  //   - options - SubscriptionNetworkManagerConnectionsClientGetOptions contains the optional parameters for the SubscriptionNetworkManagerConnectionsClient.Get
   168  //     method.
   169  func (client *SubscriptionNetworkManagerConnectionsClient) Get(ctx context.Context, networkManagerConnectionName string, options *SubscriptionNetworkManagerConnectionsClientGetOptions) (SubscriptionNetworkManagerConnectionsClientGetResponse, error) {
   170  	var err error
   171  	const operationName = "SubscriptionNetworkManagerConnectionsClient.Get"
   172  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   173  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   174  	defer func() { endSpan(err) }()
   175  	req, err := client.getCreateRequest(ctx, networkManagerConnectionName, options)
   176  	if err != nil {
   177  		return SubscriptionNetworkManagerConnectionsClientGetResponse{}, err
   178  	}
   179  	httpResp, err := client.internal.Pipeline().Do(req)
   180  	if err != nil {
   181  		return SubscriptionNetworkManagerConnectionsClientGetResponse{}, err
   182  	}
   183  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   184  		err = runtime.NewResponseError(httpResp)
   185  		return SubscriptionNetworkManagerConnectionsClientGetResponse{}, err
   186  	}
   187  	resp, err := client.getHandleResponse(httpResp)
   188  	return resp, err
   189  }
   190  
   191  // getCreateRequest creates the Get request.
   192  func (client *SubscriptionNetworkManagerConnectionsClient) getCreateRequest(ctx context.Context, networkManagerConnectionName string, options *SubscriptionNetworkManagerConnectionsClientGetOptions) (*policy.Request, error) {
   193  	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Network/networkManagerConnections/{networkManagerConnectionName}"
   194  	if client.subscriptionID == "" {
   195  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   196  	}
   197  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   198  	if networkManagerConnectionName == "" {
   199  		return nil, errors.New("parameter networkManagerConnectionName cannot be empty")
   200  	}
   201  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerConnectionName}", url.PathEscape(networkManagerConnectionName))
   202  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   203  	if err != nil {
   204  		return nil, err
   205  	}
   206  	reqQP := req.Raw().URL.Query()
   207  	reqQP.Set("api-version", "2024-05-01")
   208  	req.Raw().URL.RawQuery = reqQP.Encode()
   209  	req.Raw().Header["Accept"] = []string{"application/json"}
   210  	return req, nil
   211  }
   212  
   213  // getHandleResponse handles the Get response.
   214  func (client *SubscriptionNetworkManagerConnectionsClient) getHandleResponse(resp *http.Response) (SubscriptionNetworkManagerConnectionsClientGetResponse, error) {
   215  	result := SubscriptionNetworkManagerConnectionsClientGetResponse{}
   216  	if err := runtime.UnmarshalAsJSON(resp, &result.ManagerConnection); err != nil {
   217  		return SubscriptionNetworkManagerConnectionsClientGetResponse{}, err
   218  	}
   219  	return result, nil
   220  }
   221  
   222  // NewListPager - List all network manager connections created by this subscription.
   223  //
   224  // Generated from API version 2024-05-01
   225  //   - options - SubscriptionNetworkManagerConnectionsClientListOptions contains the optional parameters for the SubscriptionNetworkManagerConnectionsClient.NewListPager
   226  //     method.
   227  func (client *SubscriptionNetworkManagerConnectionsClient) NewListPager(options *SubscriptionNetworkManagerConnectionsClientListOptions) *runtime.Pager[SubscriptionNetworkManagerConnectionsClientListResponse] {
   228  	return runtime.NewPager(runtime.PagingHandler[SubscriptionNetworkManagerConnectionsClientListResponse]{
   229  		More: func(page SubscriptionNetworkManagerConnectionsClientListResponse) bool {
   230  			return page.NextLink != nil && len(*page.NextLink) > 0
   231  		},
   232  		Fetcher: func(ctx context.Context, page *SubscriptionNetworkManagerConnectionsClientListResponse) (SubscriptionNetworkManagerConnectionsClientListResponse, error) {
   233  			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "SubscriptionNetworkManagerConnectionsClient.NewListPager")
   234  			nextLink := ""
   235  			if page != nil {
   236  				nextLink = *page.NextLink
   237  			}
   238  			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
   239  				return client.listCreateRequest(ctx, options)
   240  			}, nil)
   241  			if err != nil {
   242  				return SubscriptionNetworkManagerConnectionsClientListResponse{}, err
   243  			}
   244  			return client.listHandleResponse(resp)
   245  		},
   246  		Tracer: client.internal.Tracer(),
   247  	})
   248  }
   249  
   250  // listCreateRequest creates the List request.
   251  func (client *SubscriptionNetworkManagerConnectionsClient) listCreateRequest(ctx context.Context, options *SubscriptionNetworkManagerConnectionsClientListOptions) (*policy.Request, error) {
   252  	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Network/networkManagerConnections"
   253  	if client.subscriptionID == "" {
   254  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   255  	}
   256  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   257  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   258  	if err != nil {
   259  		return nil, err
   260  	}
   261  	reqQP := req.Raw().URL.Query()
   262  	if options != nil && options.SkipToken != nil {
   263  		reqQP.Set("$skipToken", *options.SkipToken)
   264  	}
   265  	if options != nil && options.Top != nil {
   266  		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
   267  	}
   268  	reqQP.Set("api-version", "2024-05-01")
   269  	req.Raw().URL.RawQuery = reqQP.Encode()
   270  	req.Raw().Header["Accept"] = []string{"application/json"}
   271  	return req, nil
   272  }
   273  
   274  // listHandleResponse handles the List response.
   275  func (client *SubscriptionNetworkManagerConnectionsClient) listHandleResponse(resp *http.Response) (SubscriptionNetworkManagerConnectionsClientListResponse, error) {
   276  	result := SubscriptionNetworkManagerConnectionsClientListResponse{}
   277  	if err := runtime.UnmarshalAsJSON(resp, &result.ManagerConnectionListResult); err != nil {
   278  		return SubscriptionNetworkManagerConnectionsClientListResponse{}, err
   279  	}
   280  	return result, nil
   281  }