github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/scopeconnections_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  // ScopeConnectionsClient contains the methods for the ScopeConnections group.
    25  // Don't use this type directly, use NewScopeConnectionsClient() instead.
    26  type ScopeConnectionsClient struct {
    27  	internal       *arm.Client
    28  	subscriptionID string
    29  }
    30  
    31  // NewScopeConnectionsClient creates a new instance of ScopeConnectionsClient 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 NewScopeConnectionsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*ScopeConnectionsClient, error) {
    37  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	client := &ScopeConnectionsClient{
    42  		subscriptionID: subscriptionID,
    43  		internal:       cl,
    44  	}
    45  	return client, nil
    46  }
    47  
    48  // CreateOrUpdate - Creates or updates scope connection from Network Manager
    49  // If the operation fails it returns an *azcore.ResponseError type.
    50  //
    51  // Generated from API version 2024-05-01
    52  //   - resourceGroupName - The name of the resource group.
    53  //   - networkManagerName - The name of the network manager.
    54  //   - scopeConnectionName - Name for the cross-tenant connection.
    55  //   - parameters - Scope connection to be created/updated.
    56  //   - options - ScopeConnectionsClientCreateOrUpdateOptions contains the optional parameters for the ScopeConnectionsClient.CreateOrUpdate
    57  //     method.
    58  func (client *ScopeConnectionsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkManagerName string, scopeConnectionName string, parameters ScopeConnection, options *ScopeConnectionsClientCreateOrUpdateOptions) (ScopeConnectionsClientCreateOrUpdateResponse, error) {
    59  	var err error
    60  	const operationName = "ScopeConnectionsClient.CreateOrUpdate"
    61  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
    62  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
    63  	defer func() { endSpan(err) }()
    64  	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, networkManagerName, scopeConnectionName, parameters, options)
    65  	if err != nil {
    66  		return ScopeConnectionsClientCreateOrUpdateResponse{}, err
    67  	}
    68  	httpResp, err := client.internal.Pipeline().Do(req)
    69  	if err != nil {
    70  		return ScopeConnectionsClientCreateOrUpdateResponse{}, err
    71  	}
    72  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
    73  		err = runtime.NewResponseError(httpResp)
    74  		return ScopeConnectionsClientCreateOrUpdateResponse{}, err
    75  	}
    76  	resp, err := client.createOrUpdateHandleResponse(httpResp)
    77  	return resp, err
    78  }
    79  
    80  // createOrUpdateCreateRequest creates the CreateOrUpdate request.
    81  func (client *ScopeConnectionsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, scopeConnectionName string, parameters ScopeConnection, options *ScopeConnectionsClientCreateOrUpdateOptions) (*policy.Request, error) {
    82  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/scopeConnections/{scopeConnectionName}"
    83  	if client.subscriptionID == "" {
    84  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
    85  	}
    86  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
    87  	if resourceGroupName == "" {
    88  		return nil, errors.New("parameter resourceGroupName cannot be empty")
    89  	}
    90  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
    91  	if networkManagerName == "" {
    92  		return nil, errors.New("parameter networkManagerName cannot be empty")
    93  	}
    94  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
    95  	if scopeConnectionName == "" {
    96  		return nil, errors.New("parameter scopeConnectionName cannot be empty")
    97  	}
    98  	urlPath = strings.ReplaceAll(urlPath, "{scopeConnectionName}", url.PathEscape(scopeConnectionName))
    99  	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	reqQP := req.Raw().URL.Query()
   104  	reqQP.Set("api-version", "2024-05-01")
   105  	req.Raw().URL.RawQuery = reqQP.Encode()
   106  	req.Raw().Header["Accept"] = []string{"application/json"}
   107  	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
   108  		return nil, err
   109  	}
   110  	return req, nil
   111  }
   112  
   113  // createOrUpdateHandleResponse handles the CreateOrUpdate response.
   114  func (client *ScopeConnectionsClient) createOrUpdateHandleResponse(resp *http.Response) (ScopeConnectionsClientCreateOrUpdateResponse, error) {
   115  	result := ScopeConnectionsClientCreateOrUpdateResponse{}
   116  	if err := runtime.UnmarshalAsJSON(resp, &result.ScopeConnection); err != nil {
   117  		return ScopeConnectionsClientCreateOrUpdateResponse{}, err
   118  	}
   119  	return result, nil
   120  }
   121  
   122  // Delete - Delete the pending scope connection created by this network manager.
   123  // If the operation fails it returns an *azcore.ResponseError type.
   124  //
   125  // Generated from API version 2024-05-01
   126  //   - resourceGroupName - The name of the resource group.
   127  //   - networkManagerName - The name of the network manager.
   128  //   - scopeConnectionName - Name for the cross-tenant connection.
   129  //   - options - ScopeConnectionsClientDeleteOptions contains the optional parameters for the ScopeConnectionsClient.Delete method.
   130  func (client *ScopeConnectionsClient) Delete(ctx context.Context, resourceGroupName string, networkManagerName string, scopeConnectionName string, options *ScopeConnectionsClientDeleteOptions) (ScopeConnectionsClientDeleteResponse, error) {
   131  	var err error
   132  	const operationName = "ScopeConnectionsClient.Delete"
   133  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   134  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   135  	defer func() { endSpan(err) }()
   136  	req, err := client.deleteCreateRequest(ctx, resourceGroupName, networkManagerName, scopeConnectionName, options)
   137  	if err != nil {
   138  		return ScopeConnectionsClientDeleteResponse{}, err
   139  	}
   140  	httpResp, err := client.internal.Pipeline().Do(req)
   141  	if err != nil {
   142  		return ScopeConnectionsClientDeleteResponse{}, err
   143  	}
   144  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
   145  		err = runtime.NewResponseError(httpResp)
   146  		return ScopeConnectionsClientDeleteResponse{}, err
   147  	}
   148  	return ScopeConnectionsClientDeleteResponse{}, nil
   149  }
   150  
   151  // deleteCreateRequest creates the Delete request.
   152  func (client *ScopeConnectionsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, scopeConnectionName string, options *ScopeConnectionsClientDeleteOptions) (*policy.Request, error) {
   153  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/scopeConnections/{scopeConnectionName}"
   154  	if client.subscriptionID == "" {
   155  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   156  	}
   157  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   158  	if resourceGroupName == "" {
   159  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   160  	}
   161  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   162  	if networkManagerName == "" {
   163  		return nil, errors.New("parameter networkManagerName cannot be empty")
   164  	}
   165  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   166  	if scopeConnectionName == "" {
   167  		return nil, errors.New("parameter scopeConnectionName cannot be empty")
   168  	}
   169  	urlPath = strings.ReplaceAll(urlPath, "{scopeConnectionName}", url.PathEscape(scopeConnectionName))
   170  	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	reqQP := req.Raw().URL.Query()
   175  	reqQP.Set("api-version", "2024-05-01")
   176  	req.Raw().URL.RawQuery = reqQP.Encode()
   177  	req.Raw().Header["Accept"] = []string{"application/json"}
   178  	return req, nil
   179  }
   180  
   181  // Get - Get specified scope connection created by this Network Manager.
   182  // If the operation fails it returns an *azcore.ResponseError type.
   183  //
   184  // Generated from API version 2024-05-01
   185  //   - resourceGroupName - The name of the resource group.
   186  //   - networkManagerName - The name of the network manager.
   187  //   - scopeConnectionName - Name for the cross-tenant connection.
   188  //   - options - ScopeConnectionsClientGetOptions contains the optional parameters for the ScopeConnectionsClient.Get method.
   189  func (client *ScopeConnectionsClient) Get(ctx context.Context, resourceGroupName string, networkManagerName string, scopeConnectionName string, options *ScopeConnectionsClientGetOptions) (ScopeConnectionsClientGetResponse, error) {
   190  	var err error
   191  	const operationName = "ScopeConnectionsClient.Get"
   192  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   193  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   194  	defer func() { endSpan(err) }()
   195  	req, err := client.getCreateRequest(ctx, resourceGroupName, networkManagerName, scopeConnectionName, options)
   196  	if err != nil {
   197  		return ScopeConnectionsClientGetResponse{}, err
   198  	}
   199  	httpResp, err := client.internal.Pipeline().Do(req)
   200  	if err != nil {
   201  		return ScopeConnectionsClientGetResponse{}, err
   202  	}
   203  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   204  		err = runtime.NewResponseError(httpResp)
   205  		return ScopeConnectionsClientGetResponse{}, err
   206  	}
   207  	resp, err := client.getHandleResponse(httpResp)
   208  	return resp, err
   209  }
   210  
   211  // getCreateRequest creates the Get request.
   212  func (client *ScopeConnectionsClient) getCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, scopeConnectionName string, options *ScopeConnectionsClientGetOptions) (*policy.Request, error) {
   213  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/scopeConnections/{scopeConnectionName}"
   214  	if client.subscriptionID == "" {
   215  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   216  	}
   217  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   218  	if resourceGroupName == "" {
   219  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   220  	}
   221  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   222  	if networkManagerName == "" {
   223  		return nil, errors.New("parameter networkManagerName cannot be empty")
   224  	}
   225  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   226  	if scopeConnectionName == "" {
   227  		return nil, errors.New("parameter scopeConnectionName cannot be empty")
   228  	}
   229  	urlPath = strings.ReplaceAll(urlPath, "{scopeConnectionName}", url.PathEscape(scopeConnectionName))
   230  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   231  	if err != nil {
   232  		return nil, err
   233  	}
   234  	reqQP := req.Raw().URL.Query()
   235  	reqQP.Set("api-version", "2024-05-01")
   236  	req.Raw().URL.RawQuery = reqQP.Encode()
   237  	req.Raw().Header["Accept"] = []string{"application/json"}
   238  	return req, nil
   239  }
   240  
   241  // getHandleResponse handles the Get response.
   242  func (client *ScopeConnectionsClient) getHandleResponse(resp *http.Response) (ScopeConnectionsClientGetResponse, error) {
   243  	result := ScopeConnectionsClientGetResponse{}
   244  	if err := runtime.UnmarshalAsJSON(resp, &result.ScopeConnection); err != nil {
   245  		return ScopeConnectionsClientGetResponse{}, err
   246  	}
   247  	return result, nil
   248  }
   249  
   250  // NewListPager - List all scope connections created by this network manager.
   251  //
   252  // Generated from API version 2024-05-01
   253  //   - resourceGroupName - The name of the resource group.
   254  //   - networkManagerName - The name of the network manager.
   255  //   - options - ScopeConnectionsClientListOptions contains the optional parameters for the ScopeConnectionsClient.NewListPager
   256  //     method.
   257  func (client *ScopeConnectionsClient) NewListPager(resourceGroupName string, networkManagerName string, options *ScopeConnectionsClientListOptions) *runtime.Pager[ScopeConnectionsClientListResponse] {
   258  	return runtime.NewPager(runtime.PagingHandler[ScopeConnectionsClientListResponse]{
   259  		More: func(page ScopeConnectionsClientListResponse) bool {
   260  			return page.NextLink != nil && len(*page.NextLink) > 0
   261  		},
   262  		Fetcher: func(ctx context.Context, page *ScopeConnectionsClientListResponse) (ScopeConnectionsClientListResponse, error) {
   263  			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ScopeConnectionsClient.NewListPager")
   264  			nextLink := ""
   265  			if page != nil {
   266  				nextLink = *page.NextLink
   267  			}
   268  			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
   269  				return client.listCreateRequest(ctx, resourceGroupName, networkManagerName, options)
   270  			}, nil)
   271  			if err != nil {
   272  				return ScopeConnectionsClientListResponse{}, err
   273  			}
   274  			return client.listHandleResponse(resp)
   275  		},
   276  		Tracer: client.internal.Tracer(),
   277  	})
   278  }
   279  
   280  // listCreateRequest creates the List request.
   281  func (client *ScopeConnectionsClient) listCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, options *ScopeConnectionsClientListOptions) (*policy.Request, error) {
   282  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/scopeConnections"
   283  	if client.subscriptionID == "" {
   284  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   285  	}
   286  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   287  	if resourceGroupName == "" {
   288  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   289  	}
   290  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   291  	if networkManagerName == "" {
   292  		return nil, errors.New("parameter networkManagerName cannot be empty")
   293  	}
   294  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   295  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   296  	if err != nil {
   297  		return nil, err
   298  	}
   299  	reqQP := req.Raw().URL.Query()
   300  	if options != nil && options.SkipToken != nil {
   301  		reqQP.Set("$skipToken", *options.SkipToken)
   302  	}
   303  	if options != nil && options.Top != nil {
   304  		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
   305  	}
   306  	reqQP.Set("api-version", "2024-05-01")
   307  	req.Raw().URL.RawQuery = reqQP.Encode()
   308  	req.Raw().Header["Accept"] = []string{"application/json"}
   309  	return req, nil
   310  }
   311  
   312  // listHandleResponse handles the List response.
   313  func (client *ScopeConnectionsClient) listHandleResponse(resp *http.Response) (ScopeConnectionsClientListResponse, error) {
   314  	result := ScopeConnectionsClientListResponse{}
   315  	if err := runtime.UnmarshalAsJSON(resp, &result.ScopeConnectionListResult); err != nil {
   316  		return ScopeConnectionsClientListResponse{}, err
   317  	}
   318  	return result, nil
   319  }