// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) Go Code Generator. DO NOT EDIT.

package armconnectedcache

import (
	"context"
	"errors"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"net/url"
	"strings"
)

// IspCacheNodesOperationsClient contains the methods for the IspCacheNodesOperations group.
// Don't use this type directly, use NewIspCacheNodesOperationsClient() instead.
type IspCacheNodesOperationsClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewIspCacheNodesOperationsClient creates a new instance of IspCacheNodesOperationsClient with the specified values.
//   - subscriptionID - The ID of the target subscription. The value must be an UUID.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - pass nil to accept the default values.
func NewIspCacheNodesOperationsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*IspCacheNodesOperationsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &IspCacheNodesOperationsClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// BeginCreateOrUpdate - This api creates an ispCacheNode with the specified create parameters
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - customerResourceName - Name of the Customer resource
//   - cacheNodeResourceName - Name of the ConnectedCache resource
//   - resource - Resource create parameters.
//   - options - IspCacheNodesOperationsClientBeginCreateOrUpdateOptions contains the optional parameters for the IspCacheNodesOperationsClient.BeginCreateOrUpdate
//     method.
func (client *IspCacheNodesOperationsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, resource IspCacheNodeResource, options *IspCacheNodesOperationsClientBeginCreateOrUpdateOptions) (*runtime.Poller[IspCacheNodesOperationsClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceGroupName, customerResourceName, cacheNodeResourceName, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[IspCacheNodesOperationsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[IspCacheNodesOperationsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - This api creates an ispCacheNode with the specified create parameters
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-05-01-preview
func (client *IspCacheNodesOperationsClient) createOrUpdate(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, resource IspCacheNodeResource, options *IspCacheNodesOperationsClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "IspCacheNodesOperationsClient.BeginCreateOrUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, customerResourceName, cacheNodeResourceName, resource, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *IspCacheNodesOperationsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, resource IspCacheNodeResource, _ *IspCacheNodesOperationsClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ConnectedCache/ispCustomers/{customerResourceName}/ispCacheNodes/{cacheNodeResourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if customerResourceName == "" {
		return nil, errors.New("parameter customerResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customerResourceName}", url.PathEscape(customerResourceName))
	if cacheNodeResourceName == "" {
		return nil, errors.New("parameter cacheNodeResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cacheNodeResourceName}", url.PathEscape(cacheNodeResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2023-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, resource); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginDelete - This api deletes an existing ispCacheNode resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - customerResourceName - Name of the Customer resource
//   - cacheNodeResourceName - Name of the ConnectedCache resource
//   - options - IspCacheNodesOperationsClientBeginDeleteOptions contains the optional parameters for the IspCacheNodesOperationsClient.BeginDelete
//     method.
func (client *IspCacheNodesOperationsClient) BeginDelete(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, options *IspCacheNodesOperationsClientBeginDeleteOptions) (*runtime.Poller[IspCacheNodesOperationsClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, customerResourceName, cacheNodeResourceName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[IspCacheNodesOperationsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[IspCacheNodesOperationsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - This api deletes an existing ispCacheNode resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-05-01-preview
func (client *IspCacheNodesOperationsClient) deleteOperation(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, options *IspCacheNodesOperationsClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "IspCacheNodesOperationsClient.BeginDelete"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteCreateRequest(ctx, resourceGroupName, customerResourceName, cacheNodeResourceName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// deleteCreateRequest creates the Delete request.
func (client *IspCacheNodesOperationsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, _ *IspCacheNodesOperationsClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ConnectedCache/ispCustomers/{customerResourceName}/ispCacheNodes/{cacheNodeResourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if customerResourceName == "" {
		return nil, errors.New("parameter customerResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customerResourceName}", url.PathEscape(customerResourceName))
	if cacheNodeResourceName == "" {
		return nil, errors.New("parameter cacheNodeResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cacheNodeResourceName}", url.PathEscape(cacheNodeResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2023-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	return req, nil
}

// Get - This api gets ispCacheNode resource information
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - customerResourceName - Name of the Customer resource
//   - cacheNodeResourceName - Name of the ConnectedCache resource
//   - options - IspCacheNodesOperationsClientGetOptions contains the optional parameters for the IspCacheNodesOperationsClient.Get
//     method.
func (client *IspCacheNodesOperationsClient) Get(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, options *IspCacheNodesOperationsClientGetOptions) (IspCacheNodesOperationsClientGetResponse, error) {
	var err error
	const operationName = "IspCacheNodesOperationsClient.Get"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getCreateRequest(ctx, resourceGroupName, customerResourceName, cacheNodeResourceName, options)
	if err != nil {
		return IspCacheNodesOperationsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return IspCacheNodesOperationsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return IspCacheNodesOperationsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *IspCacheNodesOperationsClient) getCreateRequest(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, _ *IspCacheNodesOperationsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ConnectedCache/ispCustomers/{customerResourceName}/ispCacheNodes/{cacheNodeResourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if customerResourceName == "" {
		return nil, errors.New("parameter customerResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customerResourceName}", url.PathEscape(customerResourceName))
	if cacheNodeResourceName == "" {
		return nil, errors.New("parameter cacheNodeResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cacheNodeResourceName}", url.PathEscape(cacheNodeResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2023-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getHandleResponse handles the Get response.
func (client *IspCacheNodesOperationsClient) getHandleResponse(resp *http.Response) (IspCacheNodesOperationsClientGetResponse, error) {
	result := IspCacheNodesOperationsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.IspCacheNodeResource); err != nil {
		return IspCacheNodesOperationsClientGetResponse{}, err
	}
	return result, nil
}

// GetBgpCidrs - This api gets ispCacheNode resource information
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - customerResourceName - Name of the Customer resource
//   - cacheNodeResourceName - Name of the ConnectedCache resource
//   - options - IspCacheNodesOperationsClientGetBgpCidrsOptions contains the optional parameters for the IspCacheNodesOperationsClient.GetBgpCidrs
//     method.
func (client *IspCacheNodesOperationsClient) GetBgpCidrs(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, options *IspCacheNodesOperationsClientGetBgpCidrsOptions) (IspCacheNodesOperationsClientGetBgpCidrsResponse, error) {
	var err error
	const operationName = "IspCacheNodesOperationsClient.GetBgpCidrs"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getBgpCidrsCreateRequest(ctx, resourceGroupName, customerResourceName, cacheNodeResourceName, options)
	if err != nil {
		return IspCacheNodesOperationsClientGetBgpCidrsResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return IspCacheNodesOperationsClientGetBgpCidrsResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return IspCacheNodesOperationsClientGetBgpCidrsResponse{}, err
	}
	resp, err := client.getBgpCidrsHandleResponse(httpResp)
	return resp, err
}

// getBgpCidrsCreateRequest creates the GetBgpCidrs request.
func (client *IspCacheNodesOperationsClient) getBgpCidrsCreateRequest(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, _ *IspCacheNodesOperationsClientGetBgpCidrsOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ConnectedCache/ispCustomers/{customerResourceName}/ispCacheNodes/{cacheNodeResourceName}/getBgpCidrs"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if customerResourceName == "" {
		return nil, errors.New("parameter customerResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customerResourceName}", url.PathEscape(customerResourceName))
	if cacheNodeResourceName == "" {
		return nil, errors.New("parameter cacheNodeResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cacheNodeResourceName}", url.PathEscape(cacheNodeResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2023-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getBgpCidrsHandleResponse handles the GetBgpCidrs response.
func (client *IspCacheNodesOperationsClient) getBgpCidrsHandleResponse(resp *http.Response) (IspCacheNodesOperationsClientGetBgpCidrsResponse, error) {
	result := IspCacheNodesOperationsClientGetBgpCidrsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.MccCacheNodeBgpCidrDetails); err != nil {
		return IspCacheNodesOperationsClientGetBgpCidrsResponse{}, err
	}
	return result, nil
}

// GetCacheNodeInstallDetails - This api gets secrets of the ispCacheNode resource install details
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - customerResourceName - Name of the Customer resource
//   - cacheNodeResourceName - Name of the ConnectedCache resource
//   - options - IspCacheNodesOperationsClientGetCacheNodeInstallDetailsOptions contains the optional parameters for the IspCacheNodesOperationsClient.GetCacheNodeInstallDetails
//     method.
func (client *IspCacheNodesOperationsClient) GetCacheNodeInstallDetails(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, options *IspCacheNodesOperationsClientGetCacheNodeInstallDetailsOptions) (IspCacheNodesOperationsClientGetCacheNodeInstallDetailsResponse, error) {
	var err error
	const operationName = "IspCacheNodesOperationsClient.GetCacheNodeInstallDetails"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getCacheNodeInstallDetailsCreateRequest(ctx, resourceGroupName, customerResourceName, cacheNodeResourceName, options)
	if err != nil {
		return IspCacheNodesOperationsClientGetCacheNodeInstallDetailsResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return IspCacheNodesOperationsClientGetCacheNodeInstallDetailsResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return IspCacheNodesOperationsClientGetCacheNodeInstallDetailsResponse{}, err
	}
	resp, err := client.getCacheNodeInstallDetailsHandleResponse(httpResp)
	return resp, err
}

// getCacheNodeInstallDetailsCreateRequest creates the GetCacheNodeInstallDetails request.
func (client *IspCacheNodesOperationsClient) getCacheNodeInstallDetailsCreateRequest(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, _ *IspCacheNodesOperationsClientGetCacheNodeInstallDetailsOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ConnectedCache/ispCustomers/{customerResourceName}/ispCacheNodes/{cacheNodeResourceName}/getCacheNodeInstallDetails"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if customerResourceName == "" {
		return nil, errors.New("parameter customerResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customerResourceName}", url.PathEscape(customerResourceName))
	if cacheNodeResourceName == "" {
		return nil, errors.New("parameter cacheNodeResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cacheNodeResourceName}", url.PathEscape(cacheNodeResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2023-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getCacheNodeInstallDetailsHandleResponse handles the GetCacheNodeInstallDetails response.
func (client *IspCacheNodesOperationsClient) getCacheNodeInstallDetailsHandleResponse(resp *http.Response) (IspCacheNodesOperationsClientGetCacheNodeInstallDetailsResponse, error) {
	result := IspCacheNodesOperationsClientGetCacheNodeInstallDetailsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.MccCacheNodeInstallDetails); err != nil {
		return IspCacheNodesOperationsClientGetCacheNodeInstallDetailsResponse{}, err
	}
	return result, nil
}

// NewListByIspCustomerResourcePager - This api retrieves information about all ispCacheNode resources under the given subscription
// and resource group
//
// Generated from API version 2023-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - customerResourceName - Name of the Customer resource
//   - options - IspCacheNodesOperationsClientListByIspCustomerResourceOptions contains the optional parameters for the IspCacheNodesOperationsClient.NewListByIspCustomerResourcePager
//     method.
func (client *IspCacheNodesOperationsClient) NewListByIspCustomerResourcePager(resourceGroupName string, customerResourceName string, options *IspCacheNodesOperationsClientListByIspCustomerResourceOptions) *runtime.Pager[IspCacheNodesOperationsClientListByIspCustomerResourceResponse] {
	return runtime.NewPager(runtime.PagingHandler[IspCacheNodesOperationsClientListByIspCustomerResourceResponse]{
		More: func(page IspCacheNodesOperationsClientListByIspCustomerResourceResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *IspCacheNodesOperationsClientListByIspCustomerResourceResponse) (IspCacheNodesOperationsClientListByIspCustomerResourceResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "IspCacheNodesOperationsClient.NewListByIspCustomerResourcePager")
			nextLink := ""
			if page != nil {
				nextLink = *page.NextLink
			}
			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
				return client.listByIspCustomerResourceCreateRequest(ctx, resourceGroupName, customerResourceName, options)
			}, nil)
			if err != nil {
				return IspCacheNodesOperationsClientListByIspCustomerResourceResponse{}, err
			}
			return client.listByIspCustomerResourceHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByIspCustomerResourceCreateRequest creates the ListByIspCustomerResource request.
func (client *IspCacheNodesOperationsClient) listByIspCustomerResourceCreateRequest(ctx context.Context, resourceGroupName string, customerResourceName string, _ *IspCacheNodesOperationsClientListByIspCustomerResourceOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ConnectedCache/ispCustomers/{customerResourceName}/ispCacheNodes"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if customerResourceName == "" {
		return nil, errors.New("parameter customerResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customerResourceName}", url.PathEscape(customerResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2023-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByIspCustomerResourceHandleResponse handles the ListByIspCustomerResource response.
func (client *IspCacheNodesOperationsClient) listByIspCustomerResourceHandleResponse(resp *http.Response) (IspCacheNodesOperationsClientListByIspCustomerResourceResponse, error) {
	result := IspCacheNodesOperationsClientListByIspCustomerResourceResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.IspCacheNodeResourceListResult); err != nil {
		return IspCacheNodesOperationsClientListByIspCustomerResourceResponse{}, err
	}
	return result, nil
}

// Update - This api updates an existing ispCacheNode resource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - customerResourceName - Name of the Customer resource
//   - cacheNodeResourceName - Name of the ConnectedCache resource
//   - properties - The resource properties to be updated.
//   - options - IspCacheNodesOperationsClientUpdateOptions contains the optional parameters for the IspCacheNodesOperationsClient.Update
//     method.
func (client *IspCacheNodesOperationsClient) Update(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, properties PatchResource, options *IspCacheNodesOperationsClientUpdateOptions) (IspCacheNodesOperationsClientUpdateResponse, error) {
	var err error
	const operationName = "IspCacheNodesOperationsClient.Update"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.updateCreateRequest(ctx, resourceGroupName, customerResourceName, cacheNodeResourceName, properties, options)
	if err != nil {
		return IspCacheNodesOperationsClientUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return IspCacheNodesOperationsClientUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return IspCacheNodesOperationsClientUpdateResponse{}, err
	}
	resp, err := client.updateHandleResponse(httpResp)
	return resp, err
}

// updateCreateRequest creates the Update request.
func (client *IspCacheNodesOperationsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, customerResourceName string, cacheNodeResourceName string, properties PatchResource, _ *IspCacheNodesOperationsClientUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ConnectedCache/ispCustomers/{customerResourceName}/ispCacheNodes/{cacheNodeResourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if customerResourceName == "" {
		return nil, errors.New("parameter customerResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{customerResourceName}", url.PathEscape(customerResourceName))
	if cacheNodeResourceName == "" {
		return nil, errors.New("parameter cacheNodeResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cacheNodeResourceName}", url.PathEscape(cacheNodeResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodPatch, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2023-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, properties); err != nil {
		return nil, err
	}
	return req, nil
}

// updateHandleResponse handles the Update response.
func (client *IspCacheNodesOperationsClient) updateHandleResponse(resp *http.Response) (IspCacheNodesOperationsClientUpdateResponse, error) {
	result := IspCacheNodesOperationsClientUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.IspCacheNodeResource); err != nil {
		return IspCacheNodesOperationsClientUpdateResponse{}, err
	}
	return result, nil
}
