// 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 armpurestorageblock

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"
)

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

// NewAvsStorageContainerVolumesClient creates a new instance of AvsStorageContainerVolumesClient 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 NewAvsStorageContainerVolumesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*AvsStorageContainerVolumesClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &AvsStorageContainerVolumesClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// BeginDelete - Delete a volume in an AVS storage container
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-11-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - storagePoolName - Name of the storage pool
//   - storageContainerName - Name of the storage container
//   - volumeID - ID of the volume in the storage container
//   - options - AvsStorageContainerVolumesClientBeginDeleteOptions contains the optional parameters for the AvsStorageContainerVolumesClient.BeginDelete
//     method.
func (client *AvsStorageContainerVolumesClient) BeginDelete(ctx context.Context, resourceGroupName string, storagePoolName string, storageContainerName string, volumeID string, options *AvsStorageContainerVolumesClientBeginDeleteOptions) (*runtime.Poller[AvsStorageContainerVolumesClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, storagePoolName, storageContainerName, volumeID, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[AvsStorageContainerVolumesClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[AvsStorageContainerVolumesClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Delete a volume in an AVS storage container
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-11-01
func (client *AvsStorageContainerVolumesClient) deleteOperation(ctx context.Context, resourceGroupName string, storagePoolName string, storageContainerName string, volumeID string, options *AvsStorageContainerVolumesClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "AvsStorageContainerVolumesClient.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, storagePoolName, storageContainerName, volumeID, 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 *AvsStorageContainerVolumesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, storagePoolName string, storageContainerName string, volumeID string, _ *AvsStorageContainerVolumesClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/PureStorage.Block/storagePools/{storagePoolName}/avsStorageContainers/{storageContainerName}/volumes/{volumeId}"
	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 storagePoolName == "" {
		return nil, errors.New("parameter storagePoolName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{storagePoolName}", url.PathEscape(storagePoolName))
	if storageContainerName == "" {
		return nil, errors.New("parameter storageContainerName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{storageContainerName}", url.PathEscape(storageContainerName))
	if volumeID == "" {
		return nil, errors.New("parameter volumeID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{volumeId}", url.PathEscape(volumeID))
	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", "2024-11-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	return req, nil
}

// Get - Get a volume in an AVS storage container
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-11-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - storagePoolName - Name of the storage pool
//   - storageContainerName - Name of the storage container
//   - volumeID - ID of the volume in the storage container
//   - options - AvsStorageContainerVolumesClientGetOptions contains the optional parameters for the AvsStorageContainerVolumesClient.Get
//     method.
func (client *AvsStorageContainerVolumesClient) Get(ctx context.Context, resourceGroupName string, storagePoolName string, storageContainerName string, volumeID string, options *AvsStorageContainerVolumesClientGetOptions) (AvsStorageContainerVolumesClientGetResponse, error) {
	var err error
	const operationName = "AvsStorageContainerVolumesClient.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, storagePoolName, storageContainerName, volumeID, options)
	if err != nil {
		return AvsStorageContainerVolumesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return AvsStorageContainerVolumesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return AvsStorageContainerVolumesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *AvsStorageContainerVolumesClient) getCreateRequest(ctx context.Context, resourceGroupName string, storagePoolName string, storageContainerName string, volumeID string, _ *AvsStorageContainerVolumesClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/PureStorage.Block/storagePools/{storagePoolName}/avsStorageContainers/{storageContainerName}/volumes/{volumeId}"
	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 storagePoolName == "" {
		return nil, errors.New("parameter storagePoolName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{storagePoolName}", url.PathEscape(storagePoolName))
	if storageContainerName == "" {
		return nil, errors.New("parameter storageContainerName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{storageContainerName}", url.PathEscape(storageContainerName))
	if volumeID == "" {
		return nil, errors.New("parameter volumeID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{volumeId}", url.PathEscape(volumeID))
	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", "2024-11-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByAvsStorageContainerPager - List volumes in an AVS storage container
//
// Generated from API version 2024-11-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - storagePoolName - Name of the storage pool
//   - storageContainerName - Name of the storage container
//   - options - AvsStorageContainerVolumesClientListByAvsStorageContainerOptions contains the optional parameters for the AvsStorageContainerVolumesClient.NewListByAvsStorageContainerPager
//     method.
func (client *AvsStorageContainerVolumesClient) NewListByAvsStorageContainerPager(resourceGroupName string, storagePoolName string, storageContainerName string, options *AvsStorageContainerVolumesClientListByAvsStorageContainerOptions) *runtime.Pager[AvsStorageContainerVolumesClientListByAvsStorageContainerResponse] {
	return runtime.NewPager(runtime.PagingHandler[AvsStorageContainerVolumesClientListByAvsStorageContainerResponse]{
		More: func(page AvsStorageContainerVolumesClientListByAvsStorageContainerResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *AvsStorageContainerVolumesClientListByAvsStorageContainerResponse) (AvsStorageContainerVolumesClientListByAvsStorageContainerResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "AvsStorageContainerVolumesClient.NewListByAvsStorageContainerPager")
			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.listByAvsStorageContainerCreateRequest(ctx, resourceGroupName, storagePoolName, storageContainerName, options)
			}, nil)
			if err != nil {
				return AvsStorageContainerVolumesClientListByAvsStorageContainerResponse{}, err
			}
			return client.listByAvsStorageContainerHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByAvsStorageContainerCreateRequest creates the ListByAvsStorageContainer request.
func (client *AvsStorageContainerVolumesClient) listByAvsStorageContainerCreateRequest(ctx context.Context, resourceGroupName string, storagePoolName string, storageContainerName string, _ *AvsStorageContainerVolumesClientListByAvsStorageContainerOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/PureStorage.Block/storagePools/{storagePoolName}/avsStorageContainers/{storageContainerName}/volumes"
	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 storagePoolName == "" {
		return nil, errors.New("parameter storagePoolName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{storagePoolName}", url.PathEscape(storagePoolName))
	if storageContainerName == "" {
		return nil, errors.New("parameter storageContainerName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{storageContainerName}", url.PathEscape(storageContainerName))
	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", "2024-11-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByAvsStorageContainerHandleResponse handles the ListByAvsStorageContainer response.
func (client *AvsStorageContainerVolumesClient) listByAvsStorageContainerHandleResponse(resp *http.Response) (AvsStorageContainerVolumesClientListByAvsStorageContainerResponse, error) {
	result := AvsStorageContainerVolumesClientListByAvsStorageContainerResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AvsStorageContainerVolumeListResult); err != nil {
		return AvsStorageContainerVolumesClientListByAvsStorageContainerResponse{}, err
	}
	return result, nil
}

// BeginUpdate - Update a volume in an AVS storage container
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-11-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - storagePoolName - Name of the storage pool
//   - storageContainerName - Name of the storage container
//   - volumeID - ID of the volume in the storage container
//   - properties - The resource properties to be updated.
//   - options - AvsStorageContainerVolumesClientBeginUpdateOptions contains the optional parameters for the AvsStorageContainerVolumesClient.BeginUpdate
//     method.
func (client *AvsStorageContainerVolumesClient) BeginUpdate(ctx context.Context, resourceGroupName string, storagePoolName string, storageContainerName string, volumeID string, properties AvsStorageContainerVolumeUpdate, options *AvsStorageContainerVolumesClientBeginUpdateOptions) (*runtime.Poller[AvsStorageContainerVolumesClientUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.update(ctx, resourceGroupName, storagePoolName, storageContainerName, volumeID, properties, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[AvsStorageContainerVolumesClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[AvsStorageContainerVolumesClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Update - Update a volume in an AVS storage container
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-11-01
func (client *AvsStorageContainerVolumesClient) update(ctx context.Context, resourceGroupName string, storagePoolName string, storageContainerName string, volumeID string, properties AvsStorageContainerVolumeUpdate, options *AvsStorageContainerVolumesClientBeginUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "AvsStorageContainerVolumesClient.BeginUpdate"
	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, storagePoolName, storageContainerName, volumeID, properties, 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.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// updateCreateRequest creates the Update request.
func (client *AvsStorageContainerVolumesClient) updateCreateRequest(ctx context.Context, resourceGroupName string, storagePoolName string, storageContainerName string, volumeID string, properties AvsStorageContainerVolumeUpdate, _ *AvsStorageContainerVolumesClientBeginUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/PureStorage.Block/storagePools/{storagePoolName}/avsStorageContainers/{storageContainerName}/volumes/{volumeId}"
	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 storagePoolName == "" {
		return nil, errors.New("parameter storagePoolName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{storagePoolName}", url.PathEscape(storagePoolName))
	if storageContainerName == "" {
		return nil, errors.New("parameter storageContainerName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{storageContainerName}", url.PathEscape(storageContainerName))
	if volumeID == "" {
		return nil, errors.New("parameter volumeID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{volumeId}", url.PathEscape(volumeID))
	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", "2024-11-01")
	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
}
