lease

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Example (Lease_FileSystemClient_AcquireLease)

This example shows how to perform various lease operations on a filesystem. The same lease operations can be performed on individual files as well. A lease on a filesystem 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/storage/azdatalake"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azdatalake/filesystem"
	"log"
	"os"

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

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

// This example shows how to perform various lease operations on a filesystem.
// The same lease operations can be performed on individual files as well.
// A lease on a filesystem prevents it from being deleted by others, while a lease on a file
// 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 := azdatalake.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	// Create an fsClient object that wraps the filesystem's URL and a default pipeline.
	filesystemURL := fmt.Sprintf("https://%s.dfs.core.windows.net/myfs", accountName)
	fsClient, err := filesystem.NewClientWithSharedKeyCredential(filesystemURL, 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"
	filesystemLeaseClient, err := lease.NewFileSystemClient(fsClient,
		&lease.FileSystemClientOptions{LeaseID: to.Ptr(leaseID)})
	handleError(err)

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

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

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

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

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

	// The lease can be renewed.
	renewLeaseResponse, err := filesystemLeaseClient.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 = filesystemLeaseClient.BreakLease(context.TODO(), &lease.FileSystemBreakOptions{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 = exported.AccessConditions

AccessConditions identifies blob-specific access conditions which you optionally set.

type FileSystemAcquireOptions

type FileSystemAcquireOptions struct {
	// ModifiedAccessConditions contains optional parameters to access filesystem.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type FileSystemAcquireResponse

type FileSystemAcquireResponse = lease.ContainerAcquireResponse

FileSystemAcquireResponse contains the response from method FileSystemClient.AcquireLease.

type FileSystemBreakOptions

type FileSystemBreakOptions struct {
	// BreakPeriod is the proposed duration of seconds that the lease should continue before it is broken.
	BreakPeriod *int32
	// ModifiedAccessConditions contains optional parameters to access filesystem.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type FileSystemBreakResponse

type FileSystemBreakResponse = lease.ContainerBreakResponse

FileSystemBreakResponse contains the response from method FileSystemClient.BreakLease.

type FileSystemChangeOptions

type FileSystemChangeOptions struct {
	// ModifiedAccessConditions contains optional parameters to access filesystem.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type FileSystemChangeResponse

type FileSystemChangeResponse = lease.ContainerChangeResponse

FileSystemChangeResponse contains the response from method FileSystemClient.ChangeLease.

type FileSystemClient

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

FileSystemClient provides lease functionality for the underlying filesystem client.

func NewFileSystemClient

func NewFileSystemClient(client *filesystem.Client, options *FileSystemClientOptions) (*FileSystemClient, error)

NewFileSystemClient creates a filesystem lease client for the provided filesystem client.

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

func (*FileSystemClient) AcquireLease

AcquireLease acquires a lease on the filesystem 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 (*FileSystemClient) BreakLease

BreakLease breaks the filesystem'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 (*FileSystemClient) ChangeLease

func (c *FileSystemClient) ChangeLease(ctx context.Context, proposedLeaseID string, o *FileSystemChangeOptions) (FileSystemChangeResponse, error)

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

func (*FileSystemClient) LeaseID

func (c *FileSystemClient) LeaseID() *string

LeaseID returns leaseID of the client.

func (*FileSystemClient) ReleaseLease

ReleaseLease releases the filesystem's previously-acquired lease.

func (*FileSystemClient) RenewLease

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

type FileSystemClientOptions

type FileSystemClientOptions = lease.ContainerClientOptions

FileSystemClientOptions contains the optional values when creating a FileSystemClient.

type FileSystemReleaseOptions

type FileSystemReleaseOptions struct {
	// ModifiedAccessConditions contains optional parameters to access filesystem.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type FileSystemReleaseResponse

type FileSystemReleaseResponse = lease.ContainerReleaseResponse

FileSystemReleaseResponse contains the response from method FileSystemClient.ReleaseLease.

type FileSystemRenewOptions

type FileSystemRenewOptions struct {
	// ModifiedAccessConditions contains optional parameters to access filesystem.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type FileSystemRenewResponse

type FileSystemRenewResponse = lease.ContainerRenewResponse

FileSystemRenewResponse contains the response from method FileSystemClient.RenewLease.

type LeaseAccessConditions

type LeaseAccessConditions = exported.LeaseAccessConditions

LeaseAccessConditions contains optional parameters to access leased entity.

type ModifiedAccessConditions

type ModifiedAccessConditions = exported.ModifiedAccessConditions

ModifiedAccessConditions contains a group of parameters for specifying access conditions.

type PathAcquireOptions

type PathAcquireOptions struct {
	// ModifiedAccessConditions contains optional parameters to access path.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type PathAcquireResponse

type PathAcquireResponse = lease.BlobAcquireResponse

PathAcquireResponse contains the response from method PathClient.AcquireLease.

type PathBreakOptions

type PathBreakOptions struct {
	// BreakPeriod is the proposed duration of seconds that the lease should continue before it is broken.
	BreakPeriod *int32
	// ModifiedAccessConditions contains optional parameters to access path.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type PathBreakResponse

type PathBreakResponse = lease.BlobBreakResponse

PathBreakResponse contains the response from method PathClient.BreakLease.

type PathChangeOptions

type PathChangeOptions struct {
	// ModifiedAccessConditions contains optional parameters to access path.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type PathChangeResponse

type PathChangeResponse = lease.BlobChangeResponse

PathChangeResponse contains the response from method PathClient.ChangeLease.

type PathClient

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

PathClient provides lease functionality for the underlying path client.

func NewPathClient

func NewPathClient[T directory.Client | file.Client](client *T, options *PathClientOptions) (*PathClient, error)

NewPathClient creates a path lease client for the provided path client.

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

func (*PathClient) AcquireLease

func (c *PathClient) AcquireLease(ctx context.Context, duration int32, o *PathAcquireOptions) (PathAcquireResponse, error)

AcquireLease acquires a lease on the path 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 (*PathClient) BreakLease

BreakLease breaks the path's previously-acquired lease.

func (*PathClient) ChangeLease

func (c *PathClient) ChangeLease(ctx context.Context, proposedID string, o *PathChangeOptions) (PathChangeResponse, error)

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

func (*PathClient) LeaseID

func (c *PathClient) LeaseID() *string

LeaseID returns leaseID of the client.

func (*PathClient) ReleaseLease

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

func (*PathClient) RenewLease

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

type PathClientOptions

type PathClientOptions = lease.BlobClientOptions

PathClientOptions contains the optional values when creating a PathClient.

type PathReleaseOptions

type PathReleaseOptions struct {
	// ModifiedAccessConditions contains optional parameters to access path.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type PathReleaseResponse

type PathReleaseResponse = lease.BlobReleaseResponse

PathReleaseResponse contains the response from method PathClient.ReleaseLease.

type PathRenewOptions

type PathRenewOptions struct {
	// ModifiedAccessConditions contains optional parameters to access path.
	ModifiedAccessConditions *ModifiedAccessConditions
}

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

type PathRenewResponse

type PathRenewResponse = lease.BlobRenewResponse

PathRenewResponse contains the response from method PathClient.RenewLease.

Jump to

Keyboard shortcuts

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