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

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

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

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

// CreateOrUpdate - This operation creates or updates a policy definition in the given subscription with the given name.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - policyDefinitionName - The name of the policy definition.
//   - policyDefinitionVersion - The policy definition version. The format is x.y.z where x is the major version number, y is
//     the minor version number, and z is the patch number
//   - parameters - The policy definition properties.
//   - options - DefinitionVersionsClientCreateOrUpdateOptions contains the optional parameters for the DefinitionVersionsClient.CreateOrUpdate
//     method.
func (client *DefinitionVersionsClient) CreateOrUpdate(ctx context.Context, policyDefinitionName string, policyDefinitionVersion string, parameters DefinitionVersion, options *DefinitionVersionsClientCreateOrUpdateOptions) (DefinitionVersionsClientCreateOrUpdateResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.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, policyDefinitionName, policyDefinitionVersion, parameters, options)
	if err != nil {
		return DefinitionVersionsClientCreateOrUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientCreateOrUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientCreateOrUpdateResponse{}, err
	}
	resp, err := client.createOrUpdateHandleResponse(httpResp)
	return resp, err
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *DefinitionVersionsClient) createOrUpdateCreateRequest(ctx context.Context, policyDefinitionName string, policyDefinitionVersion string, parameters DefinitionVersion, _ *DefinitionVersionsClientCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions/{policyDefinitionVersion}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	if policyDefinitionVersion == "" {
		return nil, errors.New("parameter policyDefinitionVersion cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionVersion}", url.PathEscape(policyDefinitionVersion))
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
		return nil, err
	}
	return req, nil
}

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

// CreateOrUpdateAtManagementGroup - This operation creates or updates a policy definition version in the given management
// group with the given name.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - managementGroupName - The name of the management group. The name is case insensitive.
//   - policyDefinitionName - The name of the policy definition.
//   - policyDefinitionVersion - The policy definition version. The format is x.y.z where x is the major version number, y is
//     the minor version number, and z is the patch number
//   - parameters - The policy definition properties.
//   - options - DefinitionVersionsClientCreateOrUpdateAtManagementGroupOptions contains the optional parameters for the DefinitionVersionsClient.CreateOrUpdateAtManagementGroup
//     method.
func (client *DefinitionVersionsClient) CreateOrUpdateAtManagementGroup(ctx context.Context, managementGroupName string, policyDefinitionName string, policyDefinitionVersion string, parameters DefinitionVersion, options *DefinitionVersionsClientCreateOrUpdateAtManagementGroupOptions) (DefinitionVersionsClientCreateOrUpdateAtManagementGroupResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.CreateOrUpdateAtManagementGroup"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateAtManagementGroupCreateRequest(ctx, managementGroupName, policyDefinitionName, policyDefinitionVersion, parameters, options)
	if err != nil {
		return DefinitionVersionsClientCreateOrUpdateAtManagementGroupResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientCreateOrUpdateAtManagementGroupResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientCreateOrUpdateAtManagementGroupResponse{}, err
	}
	resp, err := client.createOrUpdateAtManagementGroupHandleResponse(httpResp)
	return resp, err
}

// createOrUpdateAtManagementGroupCreateRequest creates the CreateOrUpdateAtManagementGroup request.
func (client *DefinitionVersionsClient) createOrUpdateAtManagementGroupCreateRequest(ctx context.Context, managementGroupName string, policyDefinitionName string, policyDefinitionVersion string, parameters DefinitionVersion, _ *DefinitionVersionsClientCreateOrUpdateAtManagementGroupOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Management/managementGroups/{managementGroupName}/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions/{policyDefinitionVersion}"
	if managementGroupName == "" {
		return nil, errors.New("parameter managementGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{managementGroupName}", url.PathEscape(managementGroupName))
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	if policyDefinitionVersion == "" {
		return nil, errors.New("parameter policyDefinitionVersion cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionVersion}", url.PathEscape(policyDefinitionVersion))
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
		return nil, err
	}
	return req, nil
}

// createOrUpdateAtManagementGroupHandleResponse handles the CreateOrUpdateAtManagementGroup response.
func (client *DefinitionVersionsClient) createOrUpdateAtManagementGroupHandleResponse(resp *http.Response) (DefinitionVersionsClientCreateOrUpdateAtManagementGroupResponse, error) {
	result := DefinitionVersionsClientCreateOrUpdateAtManagementGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DefinitionVersion); err != nil {
		return DefinitionVersionsClientCreateOrUpdateAtManagementGroupResponse{}, err
	}
	return result, nil
}

// Delete - This operation deletes the policy definition version in the given subscription with the given name.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - policyDefinitionName - The name of the policy definition.
//   - policyDefinitionVersion - The policy definition version. The format is x.y.z where x is the major version number, y is
//     the minor version number, and z is the patch number
//   - options - DefinitionVersionsClientDeleteOptions contains the optional parameters for the DefinitionVersionsClient.Delete
//     method.
func (client *DefinitionVersionsClient) Delete(ctx context.Context, policyDefinitionName string, policyDefinitionVersion string, options *DefinitionVersionsClientDeleteOptions) (DefinitionVersionsClientDeleteResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.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, policyDefinitionName, policyDefinitionVersion, options)
	if err != nil {
		return DefinitionVersionsClientDeleteResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientDeleteResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientDeleteResponse{}, err
	}
	return DefinitionVersionsClientDeleteResponse{}, nil
}

// deleteCreateRequest creates the Delete request.
func (client *DefinitionVersionsClient) deleteCreateRequest(ctx context.Context, policyDefinitionName string, policyDefinitionVersion string, _ *DefinitionVersionsClientDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions/{policyDefinitionVersion}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	if policyDefinitionVersion == "" {
		return nil, errors.New("parameter policyDefinitionVersion cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionVersion}", url.PathEscape(policyDefinitionVersion))
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// DeleteAtManagementGroup - This operation deletes the policy definition in the given management group with the given name.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - managementGroupName - The name of the management group. The name is case insensitive.
//   - policyDefinitionName - The name of the policy definition.
//   - policyDefinitionVersion - The policy definition version. The format is x.y.z where x is the major version number, y is
//     the minor version number, and z is the patch number
//   - options - DefinitionVersionsClientDeleteAtManagementGroupOptions contains the optional parameters for the DefinitionVersionsClient.DeleteAtManagementGroup
//     method.
func (client *DefinitionVersionsClient) DeleteAtManagementGroup(ctx context.Context, managementGroupName string, policyDefinitionName string, policyDefinitionVersion string, options *DefinitionVersionsClientDeleteAtManagementGroupOptions) (DefinitionVersionsClientDeleteAtManagementGroupResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.DeleteAtManagementGroup"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteAtManagementGroupCreateRequest(ctx, managementGroupName, policyDefinitionName, policyDefinitionVersion, options)
	if err != nil {
		return DefinitionVersionsClientDeleteAtManagementGroupResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientDeleteAtManagementGroupResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientDeleteAtManagementGroupResponse{}, err
	}
	return DefinitionVersionsClientDeleteAtManagementGroupResponse{}, nil
}

// deleteAtManagementGroupCreateRequest creates the DeleteAtManagementGroup request.
func (client *DefinitionVersionsClient) deleteAtManagementGroupCreateRequest(ctx context.Context, managementGroupName string, policyDefinitionName string, policyDefinitionVersion string, _ *DefinitionVersionsClientDeleteAtManagementGroupOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Management/managementGroups/{managementGroupName}/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions/{policyDefinitionVersion}"
	if managementGroupName == "" {
		return nil, errors.New("parameter managementGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{managementGroupName}", url.PathEscape(managementGroupName))
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	if policyDefinitionVersion == "" {
		return nil, errors.New("parameter policyDefinitionVersion cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionVersion}", url.PathEscape(policyDefinitionVersion))
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// Get - This operation retrieves the policy definition version in the given subscription with the given name.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - policyDefinitionName - The name of the policy definition.
//   - policyDefinitionVersion - The policy definition version. The format is x.y.z where x is the major version number, y is
//     the minor version number, and z is the patch number
//   - options - DefinitionVersionsClientGetOptions contains the optional parameters for the DefinitionVersionsClient.Get method.
func (client *DefinitionVersionsClient) Get(ctx context.Context, policyDefinitionName string, policyDefinitionVersion string, options *DefinitionVersionsClientGetOptions) (DefinitionVersionsClientGetResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.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, policyDefinitionName, policyDefinitionVersion, options)
	if err != nil {
		return DefinitionVersionsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *DefinitionVersionsClient) getCreateRequest(ctx context.Context, policyDefinitionName string, policyDefinitionVersion string, _ *DefinitionVersionsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions/{policyDefinitionVersion}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	if policyDefinitionVersion == "" {
		return nil, errors.New("parameter policyDefinitionVersion cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionVersion}", url.PathEscape(policyDefinitionVersion))
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// GetAtManagementGroup - This operation retrieves the policy definition version in the given management group with the given
// name.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - managementGroupName - The name of the management group. The name is case insensitive.
//   - policyDefinitionName - The name of the policy definition.
//   - policyDefinitionVersion - The policy definition version. The format is x.y.z where x is the major version number, y is
//     the minor version number, and z is the patch number
//   - options - DefinitionVersionsClientGetAtManagementGroupOptions contains the optional parameters for the DefinitionVersionsClient.GetAtManagementGroup
//     method.
func (client *DefinitionVersionsClient) GetAtManagementGroup(ctx context.Context, managementGroupName string, policyDefinitionName string, policyDefinitionVersion string, options *DefinitionVersionsClientGetAtManagementGroupOptions) (DefinitionVersionsClientGetAtManagementGroupResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.GetAtManagementGroup"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getAtManagementGroupCreateRequest(ctx, managementGroupName, policyDefinitionName, policyDefinitionVersion, options)
	if err != nil {
		return DefinitionVersionsClientGetAtManagementGroupResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientGetAtManagementGroupResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientGetAtManagementGroupResponse{}, err
	}
	resp, err := client.getAtManagementGroupHandleResponse(httpResp)
	return resp, err
}

// getAtManagementGroupCreateRequest creates the GetAtManagementGroup request.
func (client *DefinitionVersionsClient) getAtManagementGroupCreateRequest(ctx context.Context, managementGroupName string, policyDefinitionName string, policyDefinitionVersion string, _ *DefinitionVersionsClientGetAtManagementGroupOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Management/managementGroups/{managementGroupName}/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions/{policyDefinitionVersion}"
	if managementGroupName == "" {
		return nil, errors.New("parameter managementGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{managementGroupName}", url.PathEscape(managementGroupName))
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	if policyDefinitionVersion == "" {
		return nil, errors.New("parameter policyDefinitionVersion cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionVersion}", url.PathEscape(policyDefinitionVersion))
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getAtManagementGroupHandleResponse handles the GetAtManagementGroup response.
func (client *DefinitionVersionsClient) getAtManagementGroupHandleResponse(resp *http.Response) (DefinitionVersionsClientGetAtManagementGroupResponse, error) {
	result := DefinitionVersionsClientGetAtManagementGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DefinitionVersion); err != nil {
		return DefinitionVersionsClientGetAtManagementGroupResponse{}, err
	}
	return result, nil
}

// GetBuiltIn - This operation retrieves the built-in policy definition version with the given name.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - policyDefinitionName - The name of the policy definition.
//   - policyDefinitionVersion - The policy definition version. The format is x.y.z where x is the major version number, y is
//     the minor version number, and z is the patch number
//   - options - DefinitionVersionsClientGetBuiltInOptions contains the optional parameters for the DefinitionVersionsClient.GetBuiltIn
//     method.
func (client *DefinitionVersionsClient) GetBuiltIn(ctx context.Context, policyDefinitionName string, policyDefinitionVersion string, options *DefinitionVersionsClientGetBuiltInOptions) (DefinitionVersionsClientGetBuiltInResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.GetBuiltIn"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getBuiltInCreateRequest(ctx, policyDefinitionName, policyDefinitionVersion, options)
	if err != nil {
		return DefinitionVersionsClientGetBuiltInResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientGetBuiltInResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientGetBuiltInResponse{}, err
	}
	resp, err := client.getBuiltInHandleResponse(httpResp)
	return resp, err
}

// getBuiltInCreateRequest creates the GetBuiltIn request.
func (client *DefinitionVersionsClient) getBuiltInCreateRequest(ctx context.Context, policyDefinitionName string, policyDefinitionVersion string, _ *DefinitionVersionsClientGetBuiltInOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions/{policyDefinitionVersion}"
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	if policyDefinitionVersion == "" {
		return nil, errors.New("parameter policyDefinitionVersion cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionVersion}", url.PathEscape(policyDefinitionVersion))
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getBuiltInHandleResponse handles the GetBuiltIn response.
func (client *DefinitionVersionsClient) getBuiltInHandleResponse(resp *http.Response) (DefinitionVersionsClientGetBuiltInResponse, error) {
	result := DefinitionVersionsClientGetBuiltInResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DefinitionVersion); err != nil {
		return DefinitionVersionsClientGetBuiltInResponse{}, err
	}
	return result, nil
}

// NewListPager - This operation retrieves a list of all the policy definition versions for the given policy definition.
//
// Generated from API version 2023-04-01
//   - policyDefinitionName - The name of the policy definition.
//   - options - DefinitionVersionsClientListOptions contains the optional parameters for the DefinitionVersionsClient.NewListPager
//     method.
func (client *DefinitionVersionsClient) NewListPager(policyDefinitionName string, options *DefinitionVersionsClientListOptions) *runtime.Pager[DefinitionVersionsClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[DefinitionVersionsClientListResponse]{
		More: func(page DefinitionVersionsClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *DefinitionVersionsClientListResponse) (DefinitionVersionsClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "DefinitionVersionsClient.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, policyDefinitionName, options)
			}, nil)
			if err != nil {
				return DefinitionVersionsClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *DefinitionVersionsClient) listCreateRequest(ctx context.Context, policyDefinitionName string, options *DefinitionVersionsClientListOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	if options != nil && options.Top != nil {
		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
	}
	reqQP.Set("api-version", "2023-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// ListAll - This operation lists all the policy definition versions for all policy definitions within a subscription.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - options - DefinitionVersionsClientListAllOptions contains the optional parameters for the DefinitionVersionsClient.ListAll
//     method.
func (client *DefinitionVersionsClient) ListAll(ctx context.Context, options *DefinitionVersionsClientListAllOptions) (DefinitionVersionsClientListAllResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.ListAll"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.listAllCreateRequest(ctx, options)
	if err != nil {
		return DefinitionVersionsClientListAllResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientListAllResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientListAllResponse{}, err
	}
	resp, err := client.listAllHandleResponse(httpResp)
	return resp, err
}

// listAllCreateRequest creates the ListAll request.
func (client *DefinitionVersionsClient) listAllCreateRequest(ctx context.Context, _ *DefinitionVersionsClientListAllOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/listPolicyDefinitionVersions"
	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.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2023-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listAllHandleResponse handles the ListAll response.
func (client *DefinitionVersionsClient) listAllHandleResponse(resp *http.Response) (DefinitionVersionsClientListAllResponse, error) {
	result := DefinitionVersionsClientListAllResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DefinitionVersionListResult); err != nil {
		return DefinitionVersionsClientListAllResponse{}, err
	}
	return result, nil
}

// ListAllAtManagementGroup - This operation lists all the policy definition versions for all policy definitions at the management
// group scope.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - managementGroupName - The name of the management group. The name is case insensitive.
//   - options - DefinitionVersionsClientListAllAtManagementGroupOptions contains the optional parameters for the DefinitionVersionsClient.ListAllAtManagementGroup
//     method.
func (client *DefinitionVersionsClient) ListAllAtManagementGroup(ctx context.Context, managementGroupName string, options *DefinitionVersionsClientListAllAtManagementGroupOptions) (DefinitionVersionsClientListAllAtManagementGroupResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.ListAllAtManagementGroup"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.listAllAtManagementGroupCreateRequest(ctx, managementGroupName, options)
	if err != nil {
		return DefinitionVersionsClientListAllAtManagementGroupResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientListAllAtManagementGroupResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientListAllAtManagementGroupResponse{}, err
	}
	resp, err := client.listAllAtManagementGroupHandleResponse(httpResp)
	return resp, err
}

// listAllAtManagementGroupCreateRequest creates the ListAllAtManagementGroup request.
func (client *DefinitionVersionsClient) listAllAtManagementGroupCreateRequest(ctx context.Context, managementGroupName string, _ *DefinitionVersionsClientListAllAtManagementGroupOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Management/managementGroups/{managementGroupName}/providers/Microsoft.Authorization/listPolicyDefinitionVersions"
	if managementGroupName == "" {
		return nil, errors.New("parameter managementGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{managementGroupName}", url.PathEscape(managementGroupName))
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listAllAtManagementGroupHandleResponse handles the ListAllAtManagementGroup response.
func (client *DefinitionVersionsClient) listAllAtManagementGroupHandleResponse(resp *http.Response) (DefinitionVersionsClientListAllAtManagementGroupResponse, error) {
	result := DefinitionVersionsClientListAllAtManagementGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DefinitionVersionListResult); err != nil {
		return DefinitionVersionsClientListAllAtManagementGroupResponse{}, err
	}
	return result, nil
}

// ListAllBuiltins - This operation lists all the built-in policy definition versions for all built-in policy definitions.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-04-01
//   - options - DefinitionVersionsClientListAllBuiltinsOptions contains the optional parameters for the DefinitionVersionsClient.ListAllBuiltins
//     method.
func (client *DefinitionVersionsClient) ListAllBuiltins(ctx context.Context, options *DefinitionVersionsClientListAllBuiltinsOptions) (DefinitionVersionsClientListAllBuiltinsResponse, error) {
	var err error
	const operationName = "DefinitionVersionsClient.ListAllBuiltins"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.listAllBuiltinsCreateRequest(ctx, options)
	if err != nil {
		return DefinitionVersionsClientListAllBuiltinsResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DefinitionVersionsClientListAllBuiltinsResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DefinitionVersionsClientListAllBuiltinsResponse{}, err
	}
	resp, err := client.listAllBuiltinsHandleResponse(httpResp)
	return resp, err
}

// listAllBuiltinsCreateRequest creates the ListAllBuiltins request.
func (client *DefinitionVersionsClient) listAllBuiltinsCreateRequest(ctx context.Context, _ *DefinitionVersionsClientListAllBuiltinsOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Authorization/listPolicyDefinitionVersions"
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listAllBuiltinsHandleResponse handles the ListAllBuiltins response.
func (client *DefinitionVersionsClient) listAllBuiltinsHandleResponse(resp *http.Response) (DefinitionVersionsClientListAllBuiltinsResponse, error) {
	result := DefinitionVersionsClientListAllBuiltinsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DefinitionVersionListResult); err != nil {
		return DefinitionVersionsClientListAllBuiltinsResponse{}, err
	}
	return result, nil
}

// NewListBuiltInPager - This operation retrieves a list of all the built-in policy definition versions for the given policy
// definition.
//
// Generated from API version 2023-04-01
//   - policyDefinitionName - The name of the policy definition.
//   - options - DefinitionVersionsClientListBuiltInOptions contains the optional parameters for the DefinitionVersionsClient.NewListBuiltInPager
//     method.
func (client *DefinitionVersionsClient) NewListBuiltInPager(policyDefinitionName string, options *DefinitionVersionsClientListBuiltInOptions) *runtime.Pager[DefinitionVersionsClientListBuiltInResponse] {
	return runtime.NewPager(runtime.PagingHandler[DefinitionVersionsClientListBuiltInResponse]{
		More: func(page DefinitionVersionsClientListBuiltInResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *DefinitionVersionsClientListBuiltInResponse) (DefinitionVersionsClientListBuiltInResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "DefinitionVersionsClient.NewListBuiltInPager")
			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.listBuiltInCreateRequest(ctx, policyDefinitionName, options)
			}, nil)
			if err != nil {
				return DefinitionVersionsClientListBuiltInResponse{}, err
			}
			return client.listBuiltInHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listBuiltInCreateRequest creates the ListBuiltIn request.
func (client *DefinitionVersionsClient) listBuiltInCreateRequest(ctx context.Context, policyDefinitionName string, options *DefinitionVersionsClientListBuiltInOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions"
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	if options != nil && options.Top != nil {
		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
	}
	reqQP.Set("api-version", "2023-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listBuiltInHandleResponse handles the ListBuiltIn response.
func (client *DefinitionVersionsClient) listBuiltInHandleResponse(resp *http.Response) (DefinitionVersionsClientListBuiltInResponse, error) {
	result := DefinitionVersionsClientListBuiltInResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DefinitionVersionListResult); err != nil {
		return DefinitionVersionsClientListBuiltInResponse{}, err
	}
	return result, nil
}

// NewListByManagementGroupPager - This operation retrieves a list of all the policy definition versions for the given policy
// definition in the given management group.
//
// Generated from API version 2023-04-01
//   - managementGroupName - The name of the management group. The name is case insensitive.
//   - policyDefinitionName - The name of the policy definition.
//   - options - DefinitionVersionsClientListByManagementGroupOptions contains the optional parameters for the DefinitionVersionsClient.NewListByManagementGroupPager
//     method.
func (client *DefinitionVersionsClient) NewListByManagementGroupPager(managementGroupName string, policyDefinitionName string, options *DefinitionVersionsClientListByManagementGroupOptions) *runtime.Pager[DefinitionVersionsClientListByManagementGroupResponse] {
	return runtime.NewPager(runtime.PagingHandler[DefinitionVersionsClientListByManagementGroupResponse]{
		More: func(page DefinitionVersionsClientListByManagementGroupResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *DefinitionVersionsClientListByManagementGroupResponse) (DefinitionVersionsClientListByManagementGroupResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "DefinitionVersionsClient.NewListByManagementGroupPager")
			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.listByManagementGroupCreateRequest(ctx, managementGroupName, policyDefinitionName, options)
			}, nil)
			if err != nil {
				return DefinitionVersionsClientListByManagementGroupResponse{}, err
			}
			return client.listByManagementGroupHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByManagementGroupCreateRequest creates the ListByManagementGroup request.
func (client *DefinitionVersionsClient) listByManagementGroupCreateRequest(ctx context.Context, managementGroupName string, policyDefinitionName string, options *DefinitionVersionsClientListByManagementGroupOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Management/managementGroups/{managementGroupName}/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionName}/versions"
	if managementGroupName == "" {
		return nil, errors.New("parameter managementGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{managementGroupName}", url.PathEscape(managementGroupName))
	if policyDefinitionName == "" {
		return nil, errors.New("parameter policyDefinitionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{policyDefinitionName}", url.PathEscape(policyDefinitionName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	if options != nil && options.Top != nil {
		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
	}
	reqQP.Set("api-version", "2023-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByManagementGroupHandleResponse handles the ListByManagementGroup response.
func (client *DefinitionVersionsClient) listByManagementGroupHandleResponse(resp *http.Response) (DefinitionVersionsClientListByManagementGroupResponse, error) {
	result := DefinitionVersionsClientListByManagementGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DefinitionVersionListResult); err != nil {
		return DefinitionVersionsClientListByManagementGroupResponse{}, err
	}
	return result, nil
}
