lease

package
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Example (Lease_ShareClient_AcquireLease)

This example shows how to perform various lease operations on a share. The same lease operations can be performed on individual files as well. A lease on a share prevents it from being deleted by others, while a lease on a file protects it from both modifications and deletions.

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

package main

import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/lease"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/share"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

// This example shows how to perform various lease operations on a share.
// The same lease operations can be performed on individual files as well.
// A lease on a share prevents it from being deleted by others, while a lease on a file
// protects it from both modifications and deletions.
func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	shareName := "testshare"
	shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil)
	handleError(err)

	// Create a unique ID for the lease
	// A lease ID can be any valid GUID string format. To generate UUIDs, consider the github.com/google/uuid package
	leaseID := "36b1a876-cf98-4eb2-a5c3-6d68489658ff"
	shareLeaseClient, err := lease.NewShareClient(shareClient, &lease.ShareClientOptions{LeaseID: to.Ptr(leaseID)})
	handleError(err)

	// Now acquire a lease on the share.
	// You can choose to pass an empty string for proposed ID so that the service automatically assigns one for you.
	duration := int32(60)
	acquireLeaseResponse, err := shareLeaseClient.Acquire(context.TODO(), duration, nil)
	handleError(err)
	fmt.Println("The share is leased for delete operations with lease ID", *acquireLeaseResponse.LeaseID)

	// The share cannot be deleted without providing the lease ID.
	_, err = shareClient.Delete(context.TODO(), nil)
	if err == nil {
		log.Fatal("delete should have failed")
	}

	fmt.Println("The share cannot be deleted while there is an active lease")

	// share can be deleted by providing the lease id
	//_, err = shareClient.Delete(context.TODO(), &share.DeleteOptions{
	//	LeaseAccessConditions: &share.LeaseAccessConditions{LeaseID: acquireLeaseResponse.LeaseID},
	//})

	// We can release the lease now and the share can be deleted.
	_, err = shareLeaseClient.Release(context.TODO(), nil)
	handleError(err)
	fmt.Println("The lease on the share is now released")

	// AcquireLease a lease again to perform other operations.
	// Duration is still 60
	acquireLeaseResponse, err = shareLeaseClient.Acquire(context.TODO(), duration, nil)
	handleError(err)
	fmt.Println("The share is leased again with lease ID", *acquireLeaseResponse.LeaseID)

	// We can change the ID of an existing lease.
	newLeaseID := "6b3e65e5-e1bb-4a3f-8b72-13e9bc9cd3bf"
	changeLeaseResponse, err := shareLeaseClient.Change(context.TODO(), newLeaseID, nil)
	handleError(err)
	fmt.Println("The lease ID was changed to", *changeLeaseResponse.LeaseID)

	// The lease can be renewed.
	renewLeaseResponse, err := shareLeaseClient.Renew(context.TODO(), nil)
	handleError(err)
	fmt.Println("The lease was renewed with the same ID", *renewLeaseResponse.LeaseID)

	// Finally, the lease can be broken, and we could prevent others from acquiring a lease for a period of time
	_, err = shareLeaseClient.Break(context.TODO(), &lease.ShareBreakOptions{BreakPeriod: to.Ptr(int32(60))})
	handleError(err)
	fmt.Println("The lease was broken, and nobody can acquire a lease for 60 seconds")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessConditions

type AccessConditions = generated.LeaseAccessConditions

AccessConditions contains optional parameters to access leased entity.

type DurationType

type DurationType = generated.LeaseDurationType

DurationType - When a share is leased, specifies whether the lease is of infinite or fixed duration.

const (
	DurationTypeInfinite DurationType = generated.LeaseDurationTypeInfinite
	DurationTypeFixed    DurationType = generated.LeaseDurationTypeFixed
)

func PossibleDurationTypeValues

func PossibleDurationTypeValues() []DurationType

PossibleDurationTypeValues returns the possible values for the DurationType const type.

type FileAcquireOptions

type FileAcquireOptions struct {
}

FileAcquireOptions contains the optional parameters for the FileClient.Acquire method.

type FileAcquireResponse

type FileAcquireResponse = generated.FileClientAcquireLeaseResponse

FileAcquireResponse contains the response from method FileClient.Acquire.

type FileBreakOptions

type FileBreakOptions struct {
	// AccessConditions contains optional parameters to access leased entity.
	AccessConditions *AccessConditions
}

FileBreakOptions contains the optional parameters for the FileClient.Break method.

type FileBreakResponse

type FileBreakResponse = generated.FileClientBreakLeaseResponse

FileBreakResponse contains the response from method FileClient.Break.

type FileChangeOptions

type FileChangeOptions struct {
}

FileChangeOptions contains the optional parameters for the FileClient.Change method.

type FileChangeResponse

type FileChangeResponse = generated.FileClientChangeLeaseResponse

FileChangeResponse contains the response from method FileClient.Change.

type FileClient

type FileClient struct {
	// contains filtered or unexported fields
}

FileClient provides lease functionality for the underlying file client.

func NewFileClient

func NewFileClient(client *file.Client, options *FileClientOptions) (*FileClient, error)

NewFileClient creates a file lease client for the provided file client.

  • client - an instance of a file client
  • options - client options; pass nil to accept the default values

func (*FileClient) Acquire

Acquire operation can be used to request a new lease. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/lease-file.

func (*FileClient) Break

Break operation can be used to break the lease, if the file has an active lease. Once a lease is broken, it cannot be renewed. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/lease-file.

func (*FileClient) Change

func (f *FileClient) Change(ctx context.Context, proposedLeaseID string, options *FileChangeOptions) (FileChangeResponse, error)

Change operation can be used to change the lease ID of an active lease. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/lease-file.

func (*FileClient) LeaseID

func (f *FileClient) LeaseID() *string

LeaseID returns leaseID of the client.

func (*FileClient) Release

Release operation can be used to free the lease if it is no longer needed so that another client may immediately acquire a lease against the file. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/lease-file.

type FileClientOptions

type FileClientOptions struct {
	// LeaseID contains a caller-provided lease ID.
	LeaseID *string
}

FileClientOptions contains the optional values when creating a FileClient.

type FileReleaseOptions

type FileReleaseOptions struct {
}

FileReleaseOptions contains the optional parameters for the FileClient.Release method.

type FileReleaseResponse

type FileReleaseResponse = generated.FileClientReleaseLeaseResponse

FileReleaseResponse contains the response from method FileClient.Release.

type ShareAcquireOptions

type ShareAcquireOptions struct {
	// The snapshot parameter is an opaque DateTime value that, when present, specifies the share snapshot to query.
	ShareSnapshot *string
}

ShareAcquireOptions contains the optional parameters for the ShareClient.Acquire method.

type ShareAcquireResponse

type ShareAcquireResponse = generated.ShareClientAcquireLeaseResponse

ShareAcquireResponse contains the response from method ShareClient.Acquire.

type ShareBreakOptions

type ShareBreakOptions struct {
	// For a break operation, this is the proposed duration the lease should continue before it is broken, in seconds, between 0 and 60. This
	// break period is only used if it is shorter than the time remaining on the
	// lease. If longer, the time remaining on the lease is used. A new lease will not be available before the break period has
	// expired, but the lease may be held for longer than the break period. If this
	// header does not appear with a break operation, a fixed-duration lease breaks after the remaining lease period elapses,
	// and an infinite lease breaks immediately.
	BreakPeriod *int32
	// The snapshot parameter is an opaque DateTime value that, when present, specifies the share snapshot to query.
	ShareSnapshot *string
	// AccessConditions contains optional parameters to access leased entity.
	AccessConditions *AccessConditions
}

ShareBreakOptions contains the optional parameters for the ShareClient.Break method.

type ShareBreakResponse

type ShareBreakResponse = generated.ShareClientBreakLeaseResponse

ShareBreakResponse contains the response from method ShareClient.Break.

type ShareChangeOptions

type ShareChangeOptions struct {
	// The snapshot parameter is an opaque DateTime value that, when present, specifies the share snapshot to query.
	ShareSnapshot *string
}

ShareChangeOptions contains the optional parameters for the ShareClient.Change method.

type ShareChangeResponse

type ShareChangeResponse = generated.ShareClientChangeLeaseResponse

ShareChangeResponse contains the response from method ShareClient.Change.

type ShareClient

type ShareClient struct {
	// contains filtered or unexported fields
}

ShareClient provides lease functionality for the underlying share client.

func NewShareClient

func NewShareClient(client *share.Client, options *ShareClientOptions) (*ShareClient, error)

NewShareClient creates a share lease client for the provided share client.

  • client - an instance of a share client
  • options - client options; pass nil to accept the default values

func (*ShareClient) Acquire

func (s *ShareClient) Acquire(ctx context.Context, duration int32, options *ShareAcquireOptions) (ShareAcquireResponse, error)

Acquire operation can be used to request a new lease. The lease duration must be between 15 and 60 seconds, or infinite (-1). For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/lease-share.

func (*ShareClient) Break

Break operation can be used to break the lease, if the file share has an active lease. Once a lease is broken, it cannot be renewed. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/lease-share.

func (*ShareClient) Change

func (s *ShareClient) Change(ctx context.Context, proposedLeaseID string, options *ShareChangeOptions) (ShareChangeResponse, error)

Change operation can be used to change the lease ID of an active lease. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/lease-share.

func (*ShareClient) LeaseID

func (s *ShareClient) LeaseID() *string

LeaseID returns leaseID of the client.

func (*ShareClient) Release

Release operation can be used to free the lease if it is no longer needed so that another client may immediately acquire a lease against the file share. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/lease-share.

func (*ShareClient) Renew

Renew operation can be used to renew an existing lease. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/lease-share.

type ShareClientOptions

type ShareClientOptions struct {
	// LeaseID contains a caller-provided lease ID.
	LeaseID *string
}

ShareClientOptions contains the optional values when creating a ShareClient.

type ShareReleaseOptions

type ShareReleaseOptions struct {
	// The snapshot parameter is an opaque DateTime value that, when present, specifies the share snapshot to query.
	ShareSnapshot *string
}

ShareReleaseOptions contains the optional parameters for the ShareClient.Release method.

type ShareReleaseResponse

type ShareReleaseResponse = generated.ShareClientReleaseLeaseResponse

ShareReleaseResponse contains the response from method ShareClient.Release.

type ShareRenewOptions

type ShareRenewOptions struct {
	// The snapshot parameter is an opaque DateTime value that, when present, specifies the share snapshot to query.
	ShareSnapshot *string
}

ShareRenewOptions contains the optional parameters for the ShareClient.Renew method.

type ShareRenewResponse

type ShareRenewResponse = generated.ShareClientRenewLeaseResponse

ShareRenewResponse contains the response from method ShareClient.Renew.

type StateType

type StateType = generated.LeaseStateType

StateType - Lease state of the share.

func PossibleStateTypeValues

func PossibleStateTypeValues() []StateType

PossibleStateTypeValues returns the possible values for the StateType const type.

type StatusType

type StatusType = generated.LeaseStatusType

StatusType - The current lease status of the share.

const (
	StatusTypeLocked   StatusType = generated.LeaseStatusTypeLocked
	StatusTypeUnlocked StatusType = generated.LeaseStatusTypeUnlocked
)

func PossibleStatusTypeValues

func PossibleStatusTypeValues() []StatusType

PossibleStatusTypeValues returns the possible values for the StatusType const type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL