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

import (
	"context"
	"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"
	"strings"
)

// VMInstanceHybridIdentityMetadatasClient contains the methods for the VMInstanceHybridIdentityMetadatas group.
// Don't use this type directly, use NewVMInstanceHybridIdentityMetadatasClient() instead.
type VMInstanceHybridIdentityMetadatasClient struct {
	internal *arm.Client
}

// NewVMInstanceHybridIdentityMetadatasClient creates a new instance of VMInstanceHybridIdentityMetadatasClient with the specified values.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - pass nil to accept the default values.
func NewVMInstanceHybridIdentityMetadatasClient(credential azcore.TokenCredential, options *arm.ClientOptions) (*VMInstanceHybridIdentityMetadatasClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &VMInstanceHybridIdentityMetadatasClient{
		internal: cl,
	}
	return client, nil
}

// Get - Implements HybridIdentityMetadata GET method.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-10-07
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - options - VMInstanceHybridIdentityMetadatasClientGetOptions contains the optional parameters for the VMInstanceHybridIdentityMetadatasClient.Get
//     method.
func (client *VMInstanceHybridIdentityMetadatasClient) Get(ctx context.Context, resourceURI string, options *VMInstanceHybridIdentityMetadatasClientGetOptions) (VMInstanceHybridIdentityMetadatasClientGetResponse, error) {
	var err error
	const operationName = "VMInstanceHybridIdentityMetadatasClient.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, resourceURI, options)
	if err != nil {
		return VMInstanceHybridIdentityMetadatasClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return VMInstanceHybridIdentityMetadatasClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return VMInstanceHybridIdentityMetadatasClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *VMInstanceHybridIdentityMetadatasClient) getCreateRequest(ctx context.Context, resourceURI string, options *VMInstanceHybridIdentityMetadatasClientGetOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.ScVmm/virtualMachineInstances/default/hybridIdentityMetadata/default"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	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-10-07")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByVirtualMachineInstancePager - Returns the list of HybridIdentityMetadata of the given VM.
//
// Generated from API version 2023-10-07
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - options - VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceOptions contains the optional parameters for
//     the VMInstanceHybridIdentityMetadatasClient.NewListByVirtualMachineInstancePager method.
func (client *VMInstanceHybridIdentityMetadatasClient) NewListByVirtualMachineInstancePager(resourceURI string, options *VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceOptions) *runtime.Pager[VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse] {
	return runtime.NewPager(runtime.PagingHandler[VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse]{
		More: func(page VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse) (VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "VMInstanceHybridIdentityMetadatasClient.NewListByVirtualMachineInstancePager")
			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.listByVirtualMachineInstanceCreateRequest(ctx, resourceURI, options)
			}, nil)
			if err != nil {
				return VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse{}, err
			}
			return client.listByVirtualMachineInstanceHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByVirtualMachineInstanceCreateRequest creates the ListByVirtualMachineInstance request.
func (client *VMInstanceHybridIdentityMetadatasClient) listByVirtualMachineInstanceCreateRequest(ctx context.Context, resourceURI string, options *VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.ScVmm/virtualMachineInstances/default/hybridIdentityMetadata"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	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-10-07")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByVirtualMachineInstanceHandleResponse handles the ListByVirtualMachineInstance response.
func (client *VMInstanceHybridIdentityMetadatasClient) listByVirtualMachineInstanceHandleResponse(resp *http.Response) (VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse, error) {
	result := VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.VMInstanceHybridIdentityMetadataListResult); err != nil {
		return VMInstanceHybridIdentityMetadatasClientListByVirtualMachineInstanceResponse{}, err
	}
	return result, nil
}
