Skip to content

Commit

Permalink
switch azurefile snanshot to management api
Browse files Browse the repository at this point in the history
return FileShare in CreateSnapshot

use fileshare instead of snapshot

fix unittest

add ListFileShare function

 add comment

change ListFileShare function
  • Loading branch information
umagnus committed Oct 25, 2022
1 parent ccc0413 commit ec4057a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 31 deletions.
57 changes: 49 additions & 8 deletions pkg/azureclients/fileclient/azure_fileclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"

"k8s.io/klog/v2"

Expand Down Expand Up @@ -84,11 +85,13 @@ func (c *Client) WithSubscriptionID(subscriptionID string) Interface {
}

// CreateFileShare creates a file share
func (c *Client) CreateFileShare(ctx context.Context, resourceGroupName, accountName string, shareOptions *ShareOptions) error {
// expand - optional, used to expand the properties within share's properties. Valid values are: snapshots.
// Should be passed as a string with delimiter ','
func (c *Client) CreateFileShare(ctx context.Context, resourceGroupName, accountName string, shareOptions *ShareOptions, expand string) (storage.FileShare, error) {
mc := metrics.NewMetricContext("file_shares", "create", resourceGroupName, c.subscriptionID, "")

if shareOptions == nil {
return fmt.Errorf("share options is nil")
return storage.FileShare{}, fmt.Errorf("share options is nil")
}
quota := int32(shareOptions.RequestGiB)
fileShareProperties := &storage.FileShareProperties{
Expand All @@ -110,7 +113,7 @@ func (c *Client) CreateFileShare(ctx context.Context, resourceGroupName, account
Name: &shareOptions.Name,
FileShareProperties: fileShareProperties,
}
_, err := c.fileSharesClient.Create(ctx, resourceGroupName, accountName, shareOptions.Name, fileShare, "")
FileShare, err := c.fileSharesClient.Create(ctx, resourceGroupName, accountName, shareOptions.Name, fileShare, expand)
var rerr *retry.Error
if err != nil {
rerr = &retry.Error{
Expand All @@ -119,14 +122,16 @@ func (c *Client) CreateFileShare(ctx context.Context, resourceGroupName, account
}
mc.Observe(rerr)

return err
return FileShare, err
}

// DeleteFileShare deletes a file share
func (c *Client) DeleteFileShare(ctx context.Context, resourceGroupName, accountName, name string) error {
// xMsSnapshot - optional, used to delete a snapshot.
// It is a DateTime value that uniquely identifies the share snapshot. e.g. "2017-05-10T17:52:33.9551861Z"
func (c *Client) DeleteFileShare(ctx context.Context, resourceGroupName, accountName, name, xMsSnapshot string) error {
mc := metrics.NewMetricContext("file_shares", "delete", resourceGroupName, c.subscriptionID, "")

_, err := c.fileSharesClient.Delete(ctx, resourceGroupName, accountName, name, "", "")
_, err := c.fileSharesClient.Delete(ctx, resourceGroupName, accountName, name, xMsSnapshot, "")
var rerr *retry.Error
if err != nil {
rerr = &retry.Error{
Expand Down Expand Up @@ -176,10 +181,30 @@ func (c *Client) ResizeFileShare(ctx context.Context, resourceGroupName, account
}

// GetFileShare gets a file share
func (c *Client) GetFileShare(ctx context.Context, resourceGroupName, accountName, name string) (storage.FileShare, error) {
// xMsSnapshot - optional, used to retrieve properties of a snapshot.
// It is a DateTime value that uniquely identifies the share snapshot. e.g. "2017-05-10T17:52:33.9551861Z"
func (c *Client) GetFileShare(ctx context.Context, resourceGroupName, accountName, name, xMsSnapshot string) (storage.FileShare, error) {
mc := metrics.NewMetricContext("file_shares", "get", resourceGroupName, c.subscriptionID, "")

result, err := c.fileSharesClient.Get(ctx, resourceGroupName, accountName, name, "stats", "")
result, err := c.fileSharesClient.Get(ctx, resourceGroupName, accountName, name, "stats", xMsSnapshot)
var rerr *retry.Error
if err != nil {
rerr = &retry.Error{
RawError: err,
}
}
mc.Observe(rerr)

return result, err
}

// ListFileShare gets a file share list
// expand - optional, used to expand the properties within share's properties. Valid values are: deleted,
// snapshots. Should be passed as a string with delimiter ','
func (c *Client) ListFileShare(ctx context.Context, resourceGroupName, accountName, filter, expand string) ([]storage.FileShareItem, error) {
mc := metrics.NewMetricContext("file_shares", "list", resourceGroupName, c.subscriptionID, "")

page, err := c.fileSharesClient.List(ctx, resourceGroupName, accountName, "", filter, expand)
var rerr *retry.Error
if err != nil {
rerr = &retry.Error{
Expand All @@ -188,6 +213,22 @@ func (c *Client) GetFileShare(ctx context.Context, resourceGroupName, accountNam
}
mc.Observe(rerr)

result := make([]storage.FileShareItem, 0)

for {
result = append(result, page.Values()...)

// Abort the loop when there's no nextLink in the response.
if to.String(page.Response().NextLink) == "" {
break
}

if err = page.NextWithContext(ctx); err != nil {
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "snapshot.list.next", resourceGroupName, err)
mc.Observe(retry.GetError(page.Response().Response.Response, err))
}
}

return result, err
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/azureclients/fileclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import (
// Interface is the client interface for creating file shares, interface for test injection.
// Don't forget to run "hack/update-mock-clients.sh" command to generate the mock client.
type Interface interface {
CreateFileShare(ctx context.Context, resourceGroupName, accountName string, shareOptions *ShareOptions) error
DeleteFileShare(ctx context.Context, resourceGroupName, accountName, name string) error
CreateFileShare(ctx context.Context, resourceGroupName, accountName string, shareOptions *ShareOptions, expand string) (storage.FileShare, error)
DeleteFileShare(ctx context.Context, resourceGroupName, accountName, name, xMsSnapshot string) error
ResizeFileShare(ctx context.Context, resourceGroupName, accountName, name string, sizeGiB int) error
GetFileShare(ctx context.Context, resourceGroupName, accountName, name string) (storage.FileShare, error)
GetFileShare(ctx context.Context, resourceGroupName, accountName, name, xMsSnapshot string) (storage.FileShare, error)
ListFileShare(ctx context.Context, resourceGroupName, accountName, filter, expand string) ([]storage.FileShareItem, error)
GetServiceProperties(ctx context.Context, resourceGroupName, accountName string) (storage.FileServiceProperties, error)
SetServiceProperties(ctx context.Context, resourceGroupName, accountName string, parameters storage.FileServiceProperties) (storage.FileServiceProperties, error)
WithSubscriptionID(subscriptionID string) Interface
Expand Down
44 changes: 30 additions & 14 deletions pkg/azureclients/fileclient/mockfileclient/interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pkg/provider/azure_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ import (

// create file share
func (az *Cloud) createFileShare(ctx context.Context, subsID, resourceGroupName, accountName string, shareOptions *fileclient.ShareOptions) error {
return az.FileClient.WithSubscriptionID(subsID).CreateFileShare(ctx, resourceGroupName, accountName, shareOptions)
_, err := az.FileClient.WithSubscriptionID(subsID).CreateFileShare(ctx, resourceGroupName, accountName, shareOptions, "")
return err
}

func (az *Cloud) deleteFileShare(ctx context.Context, subsID, resourceGroupName, accountName, name string) error {
return az.FileClient.WithSubscriptionID(subsID).DeleteFileShare(ctx, resourceGroupName, accountName, name)
return az.FileClient.WithSubscriptionID(subsID).DeleteFileShare(ctx, resourceGroupName, accountName, name, "")
}

func (az *Cloud) resizeFileShare(ctx context.Context, subsID, resourceGroupName, accountName, name string, sizeGiB int) error {
return az.FileClient.WithSubscriptionID(subsID).ResizeFileShare(ctx, resourceGroupName, accountName, name, sizeGiB)
}

func (az *Cloud) getFileShare(ctx context.Context, subsID, resourceGroupName, accountName, name string) (storage.FileShare, error) {
return az.FileClient.WithSubscriptionID(subsID).GetFileShare(ctx, resourceGroupName, accountName, name)
return az.FileClient.WithSubscriptionID(subsID).GetFileShare(ctx, resourceGroupName, accountName, name, "")
}
6 changes: 3 additions & 3 deletions pkg/provider/azure_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestCreateFileShare(t *testing.T) {
mockFileClient := mockfileclient.NewMockInterface(ctrl)
cloud.FileClient = mockFileClient
mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes()
mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(test.err).AnyTimes()
mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.FileShare{}, test.err).AnyTimes()

mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl)
cloud.StorageAccountClient = mockStorageAccountsClient
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestDeleteFileShare(t *testing.T) {
mockFileClient := mockfileclient.NewMockInterface(ctrl)
cloud.FileClient = mockFileClient
mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes()
mockFileClient.EXPECT().DeleteFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(test.err).Times(1)
mockFileClient.EXPECT().DeleteFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(test.err).Times(1)

err := cloud.DeleteFileShare(ctx, "", test.rg, test.acct, test.name)
if test.expectErr && err == nil {
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestGetFileShare(t *testing.T) {
cloud := &Cloud{}
mockFileClient := mockfileclient.NewMockInterface(ctrl)
mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes()
mockFileClient.EXPECT().GetFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.FileShare{}, nil).AnyTimes()
mockFileClient.EXPECT().GetFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.FileShare{}, nil).AnyTimes()
cloud.FileClient = mockFileClient

tests := []struct {
Expand Down

0 comments on commit ec4057a

Please sign in to comment.