//go:build go1.18
// +build go1.18

// 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) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package armcontainerinstance

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

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

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

// CreateOrUpdate - Create or update container group profiles with specified configurations.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerGroupProfileName - The name of the container group profile.
//   - containerGroupProfile - The properties of the container group profile to be created or updated.
//   - options - ContainerGroupProfilesClientCreateOrUpdateOptions contains the optional parameters for the ContainerGroupProfilesClient.CreateOrUpdate
//     method.
func (client *ContainerGroupProfilesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, containerGroupProfileName string, containerGroupProfile ContainerGroupProfile, options *ContainerGroupProfilesClientCreateOrUpdateOptions) (ContainerGroupProfilesClientCreateOrUpdateResponse, error) {
	var err error
	const operationName = "ContainerGroupProfilesClient.CreateOrUpdate"
	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, containerGroupProfileName, containerGroupProfile, options)
	if err != nil {
		return ContainerGroupProfilesClientCreateOrUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ContainerGroupProfilesClientCreateOrUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return ContainerGroupProfilesClientCreateOrUpdateResponse{}, err
	}
	resp, err := client.createOrUpdateHandleResponse(httpResp)
	return resp, err
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *ContainerGroupProfilesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, containerGroupProfileName string, containerGroupProfile ContainerGroupProfile, options *ContainerGroupProfilesClientCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerInstance/containerGroupProfiles/{containerGroupProfileName}"
	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 containerGroupProfileName == "" {
		return nil, errors.New("parameter containerGroupProfileName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{containerGroupProfileName}", url.PathEscape(containerGroupProfileName))
	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", "2024-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, containerGroupProfile); err != nil {
		return nil, err
	}
	return req, nil
}

// createOrUpdateHandleResponse handles the CreateOrUpdate response.
func (client *ContainerGroupProfilesClient) createOrUpdateHandleResponse(resp *http.Response) (ContainerGroupProfilesClientCreateOrUpdateResponse, error) {
	result := ContainerGroupProfilesClientCreateOrUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ContainerGroupProfile); err != nil {
		return ContainerGroupProfilesClientCreateOrUpdateResponse{}, err
	}
	return result, nil
}

// Delete - Delete the specified container group profile in the specified subscription and resource group. The operation does
// not delete other resources provided by the user, such as volumes.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerGroupProfileName - The name of the container group profile.
//   - options - ContainerGroupProfilesClientDeleteOptions contains the optional parameters for the ContainerGroupProfilesClient.Delete
//     method.
func (client *ContainerGroupProfilesClient) Delete(ctx context.Context, resourceGroupName string, containerGroupProfileName string, options *ContainerGroupProfilesClientDeleteOptions) (ContainerGroupProfilesClientDeleteResponse, error) {
	var err error
	const operationName = "ContainerGroupProfilesClient.Delete"
	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, containerGroupProfileName, options)
	if err != nil {
		return ContainerGroupProfilesClientDeleteResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ContainerGroupProfilesClientDeleteResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return ContainerGroupProfilesClientDeleteResponse{}, err
	}
	return ContainerGroupProfilesClientDeleteResponse{}, nil
}

// deleteCreateRequest creates the Delete request.
func (client *ContainerGroupProfilesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, containerGroupProfileName string, options *ContainerGroupProfilesClientDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerInstance/containerGroupProfiles/{containerGroupProfileName}"
	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 containerGroupProfileName == "" {
		return nil, errors.New("parameter containerGroupProfileName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{containerGroupProfileName}", url.PathEscape(containerGroupProfileName))
	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-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// Get - Gets the properties of the specified container group profile in the specified subscription and resource group. The
// operation returns the properties of container group profile including containers,
// image registry credentials, restart policy, IP address type, OS type, volumes, current revision number, etc.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerGroupProfileName - The name of the container group profile.
//   - options - ContainerGroupProfilesClientGetOptions contains the optional parameters for the ContainerGroupProfilesClient.Get
//     method.
func (client *ContainerGroupProfilesClient) Get(ctx context.Context, resourceGroupName string, containerGroupProfileName string, options *ContainerGroupProfilesClientGetOptions) (ContainerGroupProfilesClientGetResponse, error) {
	var err error
	const operationName = "ContainerGroupProfilesClient.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, containerGroupProfileName, options)
	if err != nil {
		return ContainerGroupProfilesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ContainerGroupProfilesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ContainerGroupProfilesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *ContainerGroupProfilesClient) getCreateRequest(ctx context.Context, resourceGroupName string, containerGroupProfileName string, options *ContainerGroupProfilesClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerInstance/containerGroupProfiles/{containerGroupProfileName}"
	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 containerGroupProfileName == "" {
		return nil, errors.New("parameter containerGroupProfileName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{containerGroupProfileName}", url.PathEscape(containerGroupProfileName))
	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-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 *ContainerGroupProfilesClient) getHandleResponse(resp *http.Response) (ContainerGroupProfilesClientGetResponse, error) {
	result := ContainerGroupProfilesClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ContainerGroupProfile); err != nil {
		return ContainerGroupProfilesClientGetResponse{}, err
	}
	return result, nil
}

// NewListPager - Get a list of container group profiles in the specified subscription. This operation returns properties
// of each container group profile including containers, image registry credentials, restart
// policy, IP address type, OS type,volumes,current revision number, etc.
//
// Generated from API version 2024-05-01-preview
//   - options - ContainerGroupProfilesClientListOptions contains the optional parameters for the ContainerGroupProfilesClient.NewListPager
//     method.
func (client *ContainerGroupProfilesClient) NewListPager(options *ContainerGroupProfilesClientListOptions) *runtime.Pager[ContainerGroupProfilesClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[ContainerGroupProfilesClientListResponse]{
		More: func(page ContainerGroupProfilesClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ContainerGroupProfilesClientListResponse) (ContainerGroupProfilesClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ContainerGroupProfilesClient.NewListPager")
			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.listCreateRequest(ctx, options)
			}, nil)
			if err != nil {
				return ContainerGroupProfilesClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *ContainerGroupProfilesClient) listCreateRequest(ctx context.Context, options *ContainerGroupProfilesClientListOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ContainerInstance/containerGroupProfiles"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	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-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listHandleResponse handles the List response.
func (client *ContainerGroupProfilesClient) listHandleResponse(resp *http.Response) (ContainerGroupProfilesClientListResponse, error) {
	result := ContainerGroupProfilesClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ContainerGroupProfileListResult); err != nil {
		return ContainerGroupProfilesClientListResponse{}, err
	}
	return result, nil
}

// NewListByResourceGroupPager - Get a list of container group profiles in a specified subscription and resource group. This
// operation returns properties of each container group profile including containers, image registry
// credentials, restart policy, IP address type, OS type volumes, current revision number, etc.
//
// Generated from API version 2024-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - options - ContainerGroupProfilesClientListByResourceGroupOptions contains the optional parameters for the ContainerGroupProfilesClient.NewListByResourceGroupPager
//     method.
func (client *ContainerGroupProfilesClient) NewListByResourceGroupPager(resourceGroupName string, options *ContainerGroupProfilesClientListByResourceGroupOptions) *runtime.Pager[ContainerGroupProfilesClientListByResourceGroupResponse] {
	return runtime.NewPager(runtime.PagingHandler[ContainerGroupProfilesClientListByResourceGroupResponse]{
		More: func(page ContainerGroupProfilesClientListByResourceGroupResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ContainerGroupProfilesClientListByResourceGroupResponse) (ContainerGroupProfilesClientListByResourceGroupResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ContainerGroupProfilesClient.NewListByResourceGroupPager")
			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.listByResourceGroupCreateRequest(ctx, resourceGroupName, options)
			}, nil)
			if err != nil {
				return ContainerGroupProfilesClientListByResourceGroupResponse{}, err
			}
			return client.listByResourceGroupHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByResourceGroupCreateRequest creates the ListByResourceGroup request.
func (client *ContainerGroupProfilesClient) listByResourceGroupCreateRequest(ctx context.Context, resourceGroupName string, options *ContainerGroupProfilesClientListByResourceGroupOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerInstance/containerGroupProfiles"
	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))
	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-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByResourceGroupHandleResponse handles the ListByResourceGroup response.
func (client *ContainerGroupProfilesClient) listByResourceGroupHandleResponse(resp *http.Response) (ContainerGroupProfilesClientListByResourceGroupResponse, error) {
	result := ContainerGroupProfilesClientListByResourceGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ContainerGroupProfileListResult); err != nil {
		return ContainerGroupProfilesClientListByResourceGroupResponse{}, err
	}
	return result, nil
}

// Patch - Patches container group profile with specified properties.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-05-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - containerGroupProfileName - The name of the container group profile.
//   - properties - The container group profile properties that need to be updated.
//   - options - ContainerGroupProfilesClientPatchOptions contains the optional parameters for the ContainerGroupProfilesClient.Patch
//     method.
func (client *ContainerGroupProfilesClient) Patch(ctx context.Context, resourceGroupName string, containerGroupProfileName string, properties ContainerGroupProfilePatch, options *ContainerGroupProfilesClientPatchOptions) (ContainerGroupProfilesClientPatchResponse, error) {
	var err error
	const operationName = "ContainerGroupProfilesClient.Patch"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.patchCreateRequest(ctx, resourceGroupName, containerGroupProfileName, properties, options)
	if err != nil {
		return ContainerGroupProfilesClientPatchResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ContainerGroupProfilesClientPatchResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ContainerGroupProfilesClientPatchResponse{}, err
	}
	resp, err := client.patchHandleResponse(httpResp)
	return resp, err
}

// patchCreateRequest creates the Patch request.
func (client *ContainerGroupProfilesClient) patchCreateRequest(ctx context.Context, resourceGroupName string, containerGroupProfileName string, properties ContainerGroupProfilePatch, options *ContainerGroupProfilesClientPatchOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerInstance/containerGroupProfiles/{containerGroupProfileName}"
	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 containerGroupProfileName == "" {
		return nil, errors.New("parameter containerGroupProfileName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{containerGroupProfileName}", url.PathEscape(containerGroupProfileName))
	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-05-01-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, properties); err != nil {
		return nil, err
	}
	return req, nil
}

// patchHandleResponse handles the Patch response.
func (client *ContainerGroupProfilesClient) patchHandleResponse(resp *http.Response) (ContainerGroupProfilesClientPatchResponse, error) {
	result := ContainerGroupProfilesClientPatchResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ContainerGroupProfile); err != nil {
		return ContainerGroupProfilesClientPatchResponse{}, err
	}
	return result, nil
}
