package keyvault

// 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.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

import (
	"context"
	"net/http"

	"github.com/Azure/go-autorest/autorest"
	"github.com/Azure/go-autorest/autorest/azure"
	"github.com/Azure/go-autorest/autorest/validation"
	"github.com/Azure/go-autorest/tracing"
)

// HSMSecurityDomainClient is the the key vault client performs cryptographic key operations and vault operations
// against the Key Vault service.
type HSMSecurityDomainClient struct {
	BaseClient
}

// NewHSMSecurityDomainClient creates an instance of the HSMSecurityDomainClient client.
func NewHSMSecurityDomainClient() HSMSecurityDomainClient {
	return HSMSecurityDomainClient{New()}
}

// Download retrieves the Security Domain from the managed HSM. Calling this endpoint can be used to activate a
// provisioned managed HSM resource.
// Parameters:
// vaultBaseURL - the vault name, for example https://myvault.vault.azure.net.
// certificateInfoObject - the Security Domain download operation requires customer to provide N certificates
// (minimum 3 and maximum 10) containing a public key in JWK format.
func (client HSMSecurityDomainClient) Download(ctx context.Context, vaultBaseURL string, certificateInfoObject CertificateInfoObject) (result HSMSecurityDomainDownloadFuture, err error) {
	if tracing.IsEnabled() {
		ctx = tracing.StartSpan(ctx, fqdn+"/HSMSecurityDomainClient.Download")
		defer func() {
			sc := -1
			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
				sc = result.FutureAPI.Response().StatusCode
			}
			tracing.EndSpan(ctx, sc, err)
		}()
	}
	if err := validation.Validate([]validation.Validation{
		{TargetValue: certificateInfoObject,
			Constraints: []validation.Constraint{{Target: "certificateInfoObject.Certificates", Name: validation.Null, Rule: true,
				Chain: []validation.Constraint{{Target: "certificateInfoObject.Certificates", Name: validation.MaxItems, Rule: 10, Chain: nil},
					{Target: "certificateInfoObject.Certificates", Name: validation.MinItems, Rule: 3, Chain: nil},
					{Target: "certificateInfoObject.Certificates", Name: validation.UniqueItems, Rule: true, Chain: nil},
				}},
				{Target: "certificateInfoObject.Required", Name: validation.Null, Rule: false,
					Chain: []validation.Constraint{{Target: "certificateInfoObject.Required", Name: validation.InclusiveMaximum, Rule: int64(10), Chain: nil},
						{Target: "certificateInfoObject.Required", Name: validation.InclusiveMinimum, Rule: int64(2), Chain: nil},
					}}}}}); err != nil {
		return result, validation.NewError("keyvault.HSMSecurityDomainClient", "Download", err.Error())
	}

	req, err := client.DownloadPreparer(ctx, vaultBaseURL, certificateInfoObject)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "Download", nil, "Failure preparing request")
		return
	}

	result, err = client.DownloadSender(req)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "Download", result.Response(), "Failure sending request")
		return
	}

	return
}

// DownloadPreparer prepares the Download request.
func (client HSMSecurityDomainClient) DownloadPreparer(ctx context.Context, vaultBaseURL string, certificateInfoObject CertificateInfoObject) (*http.Request, error) {
	urlParameters := map[string]interface{}{
		"vaultBaseUrl": vaultBaseURL,
	}

	const APIVersion = "7.4"
	queryParameters := map[string]interface{}{
		"api-version": APIVersion,
	}

	preparer := autorest.CreatePreparer(
		autorest.AsContentType("application/json; charset=utf-8"),
		autorest.AsPost(),
		autorest.WithCustomBaseURL("{vaultBaseUrl}", urlParameters),
		autorest.WithPath("/securitydomain/download"),
		autorest.WithJSON(certificateInfoObject),
		autorest.WithQueryParameters(queryParameters))
	return preparer.Prepare((&http.Request{}).WithContext(ctx))
}

// DownloadSender sends the Download request. The method will close the
// http.Response Body if it receives an error.
func (client HSMSecurityDomainClient) DownloadSender(req *http.Request) (future HSMSecurityDomainDownloadFuture, err error) {
	var resp *http.Response
	future.FutureAPI = &azure.Future{}
	resp, err = client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
	if err != nil {
		return
	}
	var azf azure.Future
	azf, err = azure.NewFutureFromResponse(resp)
	future.FutureAPI = &azf
	future.Result = future.result
	return
}

// DownloadResponder handles the response to the Download request. The method always
// closes the http.Response Body.
func (client HSMSecurityDomainClient) DownloadResponder(resp *http.Response) (result SecurityDomainObject, err error) {
	err = autorest.Respond(
		resp,
		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
		autorest.ByUnmarshallingJSON(&result),
		autorest.ByClosing())
	result.Response = autorest.Response{Response: resp}
	return
}

// DownloadPending retrieves the Security Domain download operation status
// Parameters:
// vaultBaseURL - the vault name, for example https://myvault.vault.azure.net.
func (client HSMSecurityDomainClient) DownloadPending(ctx context.Context, vaultBaseURL string) (result SecurityDomainOperationStatus, err error) {
	if tracing.IsEnabled() {
		ctx = tracing.StartSpan(ctx, fqdn+"/HSMSecurityDomainClient.DownloadPending")
		defer func() {
			sc := -1
			if result.Response.Response != nil {
				sc = result.Response.Response.StatusCode
			}
			tracing.EndSpan(ctx, sc, err)
		}()
	}
	req, err := client.DownloadPendingPreparer(ctx, vaultBaseURL)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "DownloadPending", nil, "Failure preparing request")
		return
	}

	resp, err := client.DownloadPendingSender(req)
	if err != nil {
		result.Response = autorest.Response{Response: resp}
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "DownloadPending", resp, "Failure sending request")
		return
	}

	result, err = client.DownloadPendingResponder(resp)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "DownloadPending", resp, "Failure responding to request")
		return
	}

	return
}

// DownloadPendingPreparer prepares the DownloadPending request.
func (client HSMSecurityDomainClient) DownloadPendingPreparer(ctx context.Context, vaultBaseURL string) (*http.Request, error) {
	urlParameters := map[string]interface{}{
		"vaultBaseUrl": vaultBaseURL,
	}

	preparer := autorest.CreatePreparer(
		autorest.AsGet(),
		autorest.WithCustomBaseURL("{vaultBaseUrl}", urlParameters),
		autorest.WithPath("/securitydomain/download/pending"))
	return preparer.Prepare((&http.Request{}).WithContext(ctx))
}

// DownloadPendingSender sends the DownloadPending request. The method will close the
// http.Response Body if it receives an error.
func (client HSMSecurityDomainClient) DownloadPendingSender(req *http.Request) (*http.Response, error) {
	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
}

// DownloadPendingResponder handles the response to the DownloadPending request. The method always
// closes the http.Response Body.
func (client HSMSecurityDomainClient) DownloadPendingResponder(resp *http.Response) (result SecurityDomainOperationStatus, err error) {
	err = autorest.Respond(
		resp,
		azure.WithErrorUnlessStatusCode(http.StatusOK),
		autorest.ByUnmarshallingJSON(&result),
		autorest.ByClosing())
	result.Response = autorest.Response{Response: resp}
	return
}

// TransferKeyMethod retrieve Security Domain transfer key
// Parameters:
// vaultBaseURL - the vault name, for example https://myvault.vault.azure.net.
func (client HSMSecurityDomainClient) TransferKeyMethod(ctx context.Context, vaultBaseURL string) (result TransferKey, err error) {
	if tracing.IsEnabled() {
		ctx = tracing.StartSpan(ctx, fqdn+"/HSMSecurityDomainClient.TransferKeyMethod")
		defer func() {
			sc := -1
			if result.Response.Response != nil {
				sc = result.Response.Response.StatusCode
			}
			tracing.EndSpan(ctx, sc, err)
		}()
	}
	req, err := client.TransferKeyMethodPreparer(ctx, vaultBaseURL)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "TransferKeyMethod", nil, "Failure preparing request")
		return
	}

	resp, err := client.TransferKeyMethodSender(req)
	if err != nil {
		result.Response = autorest.Response{Response: resp}
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "TransferKeyMethod", resp, "Failure sending request")
		return
	}

	result, err = client.TransferKeyMethodResponder(resp)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "TransferKeyMethod", resp, "Failure responding to request")
		return
	}

	return
}

// TransferKeyMethodPreparer prepares the TransferKeyMethod request.
func (client HSMSecurityDomainClient) TransferKeyMethodPreparer(ctx context.Context, vaultBaseURL string) (*http.Request, error) {
	urlParameters := map[string]interface{}{
		"vaultBaseUrl": vaultBaseURL,
	}

	const APIVersion = "7.4"
	queryParameters := map[string]interface{}{
		"api-version": APIVersion,
	}

	preparer := autorest.CreatePreparer(
		autorest.AsGet(),
		autorest.WithCustomBaseURL("{vaultBaseUrl}", urlParameters),
		autorest.WithPath("/securitydomain/upload"),
		autorest.WithQueryParameters(queryParameters))
	return preparer.Prepare((&http.Request{}).WithContext(ctx))
}

// TransferKeyMethodSender sends the TransferKeyMethod request. The method will close the
// http.Response Body if it receives an error.
func (client HSMSecurityDomainClient) TransferKeyMethodSender(req *http.Request) (*http.Response, error) {
	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
}

// TransferKeyMethodResponder handles the response to the TransferKeyMethod request. The method always
// closes the http.Response Body.
func (client HSMSecurityDomainClient) TransferKeyMethodResponder(resp *http.Response) (result TransferKey, err error) {
	err = autorest.Respond(
		resp,
		azure.WithErrorUnlessStatusCode(http.StatusOK),
		autorest.ByUnmarshallingJSON(&result),
		autorest.ByClosing())
	result.Response = autorest.Response{Response: resp}
	return
}

// Upload restore the provided Security Domain.
// Parameters:
// vaultBaseURL - the vault name, for example https://myvault.vault.azure.net.
// securityDomain - the Security Domain to be restored.
func (client HSMSecurityDomainClient) Upload(ctx context.Context, vaultBaseURL string, securityDomain SecurityDomainObject) (result HSMSecurityDomainUploadFuture, err error) {
	if tracing.IsEnabled() {
		ctx = tracing.StartSpan(ctx, fqdn+"/HSMSecurityDomainClient.Upload")
		defer func() {
			sc := -1
			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
				sc = result.FutureAPI.Response().StatusCode
			}
			tracing.EndSpan(ctx, sc, err)
		}()
	}
	if err := validation.Validate([]validation.Validation{
		{TargetValue: securityDomain,
			Constraints: []validation.Constraint{{Target: "securityDomain.Value", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
		return result, validation.NewError("keyvault.HSMSecurityDomainClient", "Upload", err.Error())
	}

	req, err := client.UploadPreparer(ctx, vaultBaseURL, securityDomain)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "Upload", nil, "Failure preparing request")
		return
	}

	result, err = client.UploadSender(req)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "Upload", result.Response(), "Failure sending request")
		return
	}

	return
}

// UploadPreparer prepares the Upload request.
func (client HSMSecurityDomainClient) UploadPreparer(ctx context.Context, vaultBaseURL string, securityDomain SecurityDomainObject) (*http.Request, error) {
	urlParameters := map[string]interface{}{
		"vaultBaseUrl": vaultBaseURL,
	}

	preparer := autorest.CreatePreparer(
		autorest.AsContentType("application/json; charset=utf-8"),
		autorest.AsPost(),
		autorest.WithCustomBaseURL("{vaultBaseUrl}", urlParameters),
		autorest.WithPath("/securitydomain/upload"),
		autorest.WithJSON(securityDomain))
	return preparer.Prepare((&http.Request{}).WithContext(ctx))
}

// UploadSender sends the Upload request. The method will close the
// http.Response Body if it receives an error.
func (client HSMSecurityDomainClient) UploadSender(req *http.Request) (future HSMSecurityDomainUploadFuture, err error) {
	var resp *http.Response
	future.FutureAPI = &azure.Future{}
	resp, err = client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
	if err != nil {
		return
	}
	var azf azure.Future
	azf, err = azure.NewFutureFromResponse(resp)
	future.FutureAPI = &azf
	future.Result = future.result
	return
}

// UploadResponder handles the response to the Upload request. The method always
// closes the http.Response Body.
func (client HSMSecurityDomainClient) UploadResponder(resp *http.Response) (result SecurityDomainOperationStatus, err error) {
	err = autorest.Respond(
		resp,
		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
		autorest.ByUnmarshallingJSON(&result),
		autorest.ByClosing())
	result.Response = autorest.Response{Response: resp}
	return
}

// UploadPending get Security Domain upload operation status
// Parameters:
// vaultBaseURL - the vault name, for example https://myvault.vault.azure.net.
func (client HSMSecurityDomainClient) UploadPending(ctx context.Context, vaultBaseURL string) (result SecurityDomainOperationStatus, err error) {
	if tracing.IsEnabled() {
		ctx = tracing.StartSpan(ctx, fqdn+"/HSMSecurityDomainClient.UploadPending")
		defer func() {
			sc := -1
			if result.Response.Response != nil {
				sc = result.Response.Response.StatusCode
			}
			tracing.EndSpan(ctx, sc, err)
		}()
	}
	req, err := client.UploadPendingPreparer(ctx, vaultBaseURL)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "UploadPending", nil, "Failure preparing request")
		return
	}

	resp, err := client.UploadPendingSender(req)
	if err != nil {
		result.Response = autorest.Response{Response: resp}
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "UploadPending", resp, "Failure sending request")
		return
	}

	result, err = client.UploadPendingResponder(resp)
	if err != nil {
		err = autorest.NewErrorWithError(err, "keyvault.HSMSecurityDomainClient", "UploadPending", resp, "Failure responding to request")
		return
	}

	return
}

// UploadPendingPreparer prepares the UploadPending request.
func (client HSMSecurityDomainClient) UploadPendingPreparer(ctx context.Context, vaultBaseURL string) (*http.Request, error) {
	urlParameters := map[string]interface{}{
		"vaultBaseUrl": vaultBaseURL,
	}

	preparer := autorest.CreatePreparer(
		autorest.AsGet(),
		autorest.WithCustomBaseURL("{vaultBaseUrl}", urlParameters),
		autorest.WithPath("/securitydomain/upload/pending"))
	return preparer.Prepare((&http.Request{}).WithContext(ctx))
}

// UploadPendingSender sends the UploadPending request. The method will close the
// http.Response Body if it receives an error.
func (client HSMSecurityDomainClient) UploadPendingSender(req *http.Request) (*http.Response, error) {
	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
}

// UploadPendingResponder handles the response to the UploadPending request. The method always
// closes the http.Response Body.
func (client HSMSecurityDomainClient) UploadPendingResponder(resp *http.Response) (result SecurityDomainOperationStatus, err error) {
	err = autorest.Respond(
		resp,
		azure.WithErrorUnlessStatusCode(http.StatusOK),
		autorest.ByUnmarshallingJSON(&result),
		autorest.ByClosing())
	result.Response = autorest.Response{Response: resp}
	return
}
