lease

package
v1.3.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: 12 Imported by: 4

Documentation

Overview

Example (Lease_ContainerClient_AcquireLease)

This example shows how to perform various lease operations on a container. The same lease operations can be performed on individual blobs as well. A lease on a container prevents it from being deleted by others, while a lease on a blob 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"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/lease"
)

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

// This example shows how to perform various lease operations on a container.
// The same lease operations can be performed on individual blobs as well.
// A lease on a container prevents it from being deleted by others, while a lease on a blob
// protects it from both modifications and deletions.
func main() {
	// From the Azure portal, get your Storage account's name and account key.
	accountName, accountKey := os.Getenv("AZURE_STORAGE_ACCOUNT_NAME"), os.Getenv("AZURE_STORAGE_ACCOUNT_KEY")

	// Use your Storage account's name and key to create a credential object; this is used to access your account.
	credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	// Create an containerClient object that wraps the container's URL and a default pipeline.
	containerURL := fmt.Sprintf("https://%s.blob.core.windows.net/mycontainer", accountName)
	containerClient, err := container.NewClientWithSharedKeyCredential(containerURL, credential, 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"
	containerLeaseClient, err := lease.NewContainerClient(containerClient,
		&lease.ContainerClientOptions{LeaseID: to.Ptr(leaseID)})
	handleError(err)

	// Now acquire a lease on the container.
	// 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 := containerLeaseClient.AcquireLease(context.TODO(), duration, nil)
	handleError(err)
	fmt.Println("The container is leased for delete operations with lease ID", *acquireLeaseResponse.LeaseID)

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

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

	// _, err = containerClient.Delete(context.TODO(), &container.DeleteOptions{
	// 	AccessConditions: &container.AccessConditions{
	// 		LeaseAccessConditions: &container.LeaseAccessConditions{LeaseID: acquireLeaseResponse.LeaseID},
	// 	},
	// })

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

	// AcquireLease a lease again to perform other operations.
	// Duration is still 60
	acquireLeaseResponse, err = containerLeaseClient.AcquireLease(context.TODO(), duration, nil)
	handleError(err)
	fmt.Println("The container 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 := containerLeaseClient.ChangeLease(context.TODO(), newLeaseID, nil)
	handleError(err)
	fmt.Println("The lease ID was changed to", *changeLeaseResponse.LeaseID)

	// The lease can be renewed.
	renewLeaseResponse, err := containerLeaseClient.RenewLease(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 = containerLeaseClient.BreakLease(context.TODO(), &lease.ContainerBreakOptions{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

View Source
const BreakNaturally = -1

BreakNaturally tells ContainerClient's or BlobClient's BreakLease method to break the lease using service semantics.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessConditions

type AccessConditions = generated.LeaseAccessConditions

AccessConditions contains a group of parameters for specifying lease access conditions.

type BlobAcquireOptions

type BlobAcquireOptions struct {
	ModifiedAccessConditions *ModifiedAccessConditions
}

BlobAcquireOptions contains the optional parameters for the LeaseClient.AcquireLease method.

type BlobAcquireResponse

type BlobAcquireResponse = generated.BlobClientAcquireLeaseResponse

BlobAcquireResponse contains the response from method BlobClient.AcquireLease.

type BlobBreakOptions

type BlobBreakOptions struct {
	// For a break operation, 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
	ModifiedAccessConditions *ModifiedAccessConditions
}

BlobBreakOptions contains the optional parameters for the LeaseClient.BreakLease method.

type BlobBreakResponse

type BlobBreakResponse = generated.BlobClientBreakLeaseResponse

BlobBreakResponse contains the response from method BlobClient.BreakLease.

type BlobChangeOptions

type BlobChangeOptions struct {
	ModifiedAccessConditions *ModifiedAccessConditions
}

BlobChangeOptions contains the optional parameters for the LeaseClient.ChangeLease method.

type BlobChangeResponse

type BlobChangeResponse = generated.BlobClientChangeLeaseResponse

BlobChangeResponse contains the response from method BlobClient.ChangeLease.

type BlobClient

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

BlobClient provides lease functionality for the underlying blob client.

func NewBlobClient

func NewBlobClient[T appendblob.Client | blob.Client | blockblob.Client | pageblob.Client](client *T, options *BlobClientOptions) (*BlobClient, error)

NewBlobClient creates a blob lease client for the provided blob client.

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

func (*BlobClient) AcquireLease

func (c *BlobClient) AcquireLease(ctx context.Context, duration int32, o *BlobAcquireOptions) (BlobAcquireResponse, error)

AcquireLease acquires a lease on the blob for write and delete operations. The lease Duration must be between 15 and 60 seconds, or infinite (-1). For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

func (*BlobClient) BreakLease

BreakLease breaks the blob's previously-acquired lease (if it exists). Pass the LeaseBreakDefault (-1) constant to break a fixed-Duration lease when it expires or an infinite lease immediately. For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

func (*BlobClient) ChangeLease

func (c *BlobClient) ChangeLease(ctx context.Context, proposedLeaseID string, o *BlobChangeOptions) (BlobChangeResponse, error)

ChangeLease changes the blob's lease ID. For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

func (*BlobClient) LeaseID

func (c *BlobClient) LeaseID() *string

LeaseID returns leaseID of the client.

func (*BlobClient) ReleaseLease

ReleaseLease releases the blob's previously-acquired lease. For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

func (*BlobClient) RenewLease

RenewLease renews the blob's previously-acquired lease. For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

type BlobClientOptions

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

BlobClientOptions contains the optional values when creating a BlobClient.

type BlobReleaseOptions

type BlobReleaseOptions struct {
	ModifiedAccessConditions *ModifiedAccessConditions
}

BlobReleaseOptions contains the optional parameters for the LeaseClient.ReleaseLease method.

type BlobReleaseResponse

type BlobReleaseResponse = generated.BlobClientReleaseLeaseResponse

BlobReleaseResponse contains the response from method BlobClient.ReleaseLease.

type BlobRenewOptions

type BlobRenewOptions struct {
	ModifiedAccessConditions *ModifiedAccessConditions
}

BlobRenewOptions contains the optional parameters for the LeaseClient.RenewLease method.

type BlobRenewResponse

type BlobRenewResponse = generated.BlobClientRenewLeaseResponse

BlobRenewResponse contains the response from method BlobClient.RenewLease.

type ContainerAcquireOptions

type ContainerAcquireOptions struct {
	ModifiedAccessConditions *ModifiedAccessConditions
}

ContainerAcquireOptions contains the optional parameters for the LeaseClient.AcquireLease method.

type ContainerAcquireResponse

type ContainerAcquireResponse = generated.ContainerClientAcquireLeaseResponse

ContainerAcquireResponse contains the response from method BlobClient.AcquireLease.

type ContainerBreakOptions

type ContainerBreakOptions struct {
	// For a break operation, 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
	ModifiedAccessConditions *ModifiedAccessConditions
}

ContainerBreakOptions contains the optional parameters for the LeaseClient.BreakLease method.

type ContainerBreakResponse

type ContainerBreakResponse = generated.ContainerClientBreakLeaseResponse

ContainerBreakResponse contains the response from method BlobClient.BreakLease.

type ContainerChangeOptions

type ContainerChangeOptions struct {
	ModifiedAccessConditions *ModifiedAccessConditions
}

ContainerChangeOptions contains the optional parameters for the LeaseClient.ChangeLease method.

type ContainerChangeResponse

type ContainerChangeResponse = generated.ContainerClientChangeLeaseResponse

ContainerChangeResponse contains the response from method BlobClient.ChangeLease.

type ContainerClient

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

ContainerClient provides lease functionality for the underlying container client.

func NewContainerClient

func NewContainerClient(client *container.Client, options *ContainerClientOptions) (*ContainerClient, error)

NewContainerClient creates a container lease client for the provided container client.

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

func (*ContainerClient) AcquireLease

AcquireLease acquires a lease on the blob for write and delete operations. The lease Duration must be between 15 and 60 seconds, or infinite (-1). For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

func (*ContainerClient) BreakLease

BreakLease breaks the blob's previously-acquired lease (if it exists). Pass the LeaseBreakDefault (-1) constant to break a fixed-Duration lease when it expires or an infinite lease immediately. For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

func (*ContainerClient) ChangeLease

func (c *ContainerClient) ChangeLease(ctx context.Context, proposedLeaseID string, o *ContainerChangeOptions) (ContainerChangeResponse, error)

ChangeLease changes the blob's lease ID. For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

func (*ContainerClient) LeaseID

func (c *ContainerClient) LeaseID() *string

LeaseID returns leaseID of the client.

func (*ContainerClient) ReleaseLease

ReleaseLease releases the blob's previously-acquired lease. For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

func (*ContainerClient) RenewLease

RenewLease renews the blob's previously-acquired lease. For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-blob.

type ContainerClientOptions

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

ContainerClientOptions contains the optional values when creating a ContainerClient.

type ContainerReleaseOptions

type ContainerReleaseOptions struct {
	ModifiedAccessConditions *ModifiedAccessConditions
}

ContainerReleaseOptions contains the optional parameters for the LeaseClient.ReleaseLease method.

type ContainerReleaseResponse

type ContainerReleaseResponse = generated.ContainerClientReleaseLeaseResponse

ContainerReleaseResponse contains the response from method BlobClient.ReleaseLease.

type ContainerRenewOptions

type ContainerRenewOptions struct {
	ModifiedAccessConditions *ModifiedAccessConditions
}

ContainerRenewOptions contains the optional parameters for the LeaseClient.RenewLease method.

type ContainerRenewResponse

type ContainerRenewResponse = generated.ContainerClientRenewLeaseResponse

ContainerRenewResponse contains the response from method BlobClient.RenewLease.

type DurationType added in v0.6.0

type DurationType = generated.LeaseDurationType

DurationType defines values for DurationType

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

func PossibleDurationTypeValues added in v0.6.0

func PossibleDurationTypeValues() []DurationType

PossibleDurationTypeValues returns the possible values for the DurationType const type.

type ModifiedAccessConditions

type ModifiedAccessConditions = exported.ModifiedAccessConditions

ModifiedAccessConditions contains a group of parameters for specifying access conditions.

type StateType added in v0.6.0

type StateType = generated.LeaseStateType

StateType defines values for StateType

func PossibleStateTypeValues added in v0.6.0

func PossibleStateTypeValues() []StateType

PossibleStateTypeValues returns the possible values for the StateType const type.

type StatusType added in v0.6.0

type StatusType = generated.LeaseStatusType

StatusType defines values for StatusType

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

func PossibleStatusTypeValues added in v0.6.0

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