From a2a23ec398b7600e1df0133d9cedb7b66e5b7e67 Mon Sep 17 00:00:00 2001 From: magodo Date: Mon, 25 Oct 2021 14:03:56 +0800 Subject: [PATCH 1/2] storage: file share add supports for `EnabledProtocol` --- storage/2020-08-04/file/shares/create.go | 7 +++ .../2020-08-04/file/shares/lifecycle_test.go | 43 +++++++++++++++++++ storage/2020-08-04/file/shares/models.go | 10 +++++ .../2020-08-04/file/shares/properties_get.go | 10 ++++- 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/storage/2020-08-04/file/shares/create.go b/storage/2020-08-04/file/shares/create.go index 0124e81..9ef2f13 100644 --- a/storage/2020-08-04/file/shares/create.go +++ b/storage/2020-08-04/file/shares/create.go @@ -18,6 +18,9 @@ type CreateInput struct { // Must be greater than 0, and less than or equal to 5TB (5120). QuotaInGB int + // Specifies the enabled protocols on the share. If not specified, the default is SMB. + EnabledProtocol ShareProtocol + MetaData map[string]string } @@ -76,6 +79,10 @@ func (client Client) CreatePreparer(ctx context.Context, accountName, shareName "x-ms-share-quota": input.QuotaInGB, } + if input.EnabledProtocol != "" { + headers["x-ms-enabled-protocols"] = input.EnabledProtocol + } + headers = metadata.SetIntoHeaders(headers, input.MetaData) preparer := autorest.CreatePreparer( diff --git a/storage/2020-08-04/file/shares/lifecycle_test.go b/storage/2020-08-04/file/shares/lifecycle_test.go index fb3531a..bb5f818 100644 --- a/storage/2020-08-04/file/shares/lifecycle_test.go +++ b/storage/2020-08-04/file/shares/lifecycle_test.go @@ -293,3 +293,46 @@ func TestSharesLifecycleLargeQuota(t *testing.T) { t.Fatalf("Error deleting Share: %s", err) } } + +func TestSharesLifecycleNFSProtocol(t *testing.T) { + client, err := testhelpers.Build(t) + if err != nil { + t.Fatal(err) + } + ctx := context.TODO() + resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt()) + accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString()) + shareName := fmt.Sprintf("share-%d", testhelpers.RandomInt()) + + testData, err := client.BuildTestResourcesWithSku(ctx, resourceGroup, accountName, storage.KindFileStorage, storage.SkuNamePremiumLRS) + if err != nil { + t.Fatal(err) + } + defer client.DestroyTestResources(ctx, resourceGroup, accountName) + + storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey) + sharesClient := NewWithEnvironment(client.Environment) + sharesClient.Client = client.PrepareWithAuthorizer(sharesClient.Client, storageAuth) + + input := CreateInput{ + QuotaInGB: 1000, + EnabledProtocol: NFS, + } + _, err = sharesClient.Create(ctx, accountName, shareName, input) + if err != nil { + t.Fatalf("Error creating fileshare: %s", err) + } + + share, err := sharesClient.GetProperties(ctx, accountName, shareName) + if err != nil { + t.Fatalf("Error retrieving share: %s", err) + } + if share.EnabledProtocol != NFS { + t.Fatalf(`Expected enabled protocol to be "NFS" but got: %q`, share.EnabledProtocol) + } + + _, err = sharesClient.Delete(ctx, accountName, shareName, false) + if err != nil { + t.Fatalf("Error deleting Share: %s", err) + } +} diff --git a/storage/2020-08-04/file/shares/models.go b/storage/2020-08-04/file/shares/models.go index 31ef7c2..3272716 100644 --- a/storage/2020-08-04/file/shares/models.go +++ b/storage/2020-08-04/file/shares/models.go @@ -10,3 +10,13 @@ type AccessPolicy struct { Expiry string `xml:"Expiry"` Permission string `xml:"Permission"` } + +type ShareProtocol string + +const ( + // SMB indicates the share can be accessed by SMBv3.0, SMBv2.1 and REST. + SMB ShareProtocol = "SMB" + + // NFS indicates the share can be accessed by NFSv4.1. A premium account is required for this option. + NFS ShareProtocol = "NFS" +) diff --git a/storage/2020-08-04/file/shares/properties_get.go b/storage/2020-08-04/file/shares/properties_get.go index 95fa58d..6ff05cb 100644 --- a/storage/2020-08-04/file/shares/properties_get.go +++ b/storage/2020-08-04/file/shares/properties_get.go @@ -17,8 +17,9 @@ import ( type GetPropertiesResult struct { autorest.Response - MetaData map[string]string - ShareQuota int + MetaData map[string]string + ShareQuota int + EnabledProtocol ShareProtocol } // GetProperties returns the properties about the specified Storage Share @@ -100,6 +101,11 @@ func (client Client) GetPropertiesResponder(resp *http.Response) (result GetProp } result.ShareQuota = quota } + + protocolRaw := resp.Header.Get("x-ms-enabled-protocols") + if protocolRaw != "" { + result.EnabledProtocol = ShareProtocol(protocolRaw) + } } err = autorest.Respond( From ce9f6d4226054967bac96fb38da890c7e04f7e14 Mon Sep 17 00:00:00 2001 From: magodo Date: Wed, 27 Oct 2021 10:04:32 +0800 Subject: [PATCH 2/2] explictly set the SMB protocol when user doesnt specify --- storage/2020-08-04/file/shares/create.go | 4 +++- storage/2020-08-04/file/shares/lifecycle_test.go | 3 +++ storage/2020-08-04/file/shares/properties_get.go | 7 ++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/storage/2020-08-04/file/shares/create.go b/storage/2020-08-04/file/shares/create.go index 9ef2f13..caefccf 100644 --- a/storage/2020-08-04/file/shares/create.go +++ b/storage/2020-08-04/file/shares/create.go @@ -79,9 +79,11 @@ func (client Client) CreatePreparer(ctx context.Context, accountName, shareName "x-ms-share-quota": input.QuotaInGB, } + protocol := SMB if input.EnabledProtocol != "" { - headers["x-ms-enabled-protocols"] = input.EnabledProtocol + protocol = input.EnabledProtocol } + headers["x-ms-enabled-protocols"] = protocol headers = metadata.SetIntoHeaders(headers, input.MetaData) diff --git a/storage/2020-08-04/file/shares/lifecycle_test.go b/storage/2020-08-04/file/shares/lifecycle_test.go index bb5f818..e2d2534 100644 --- a/storage/2020-08-04/file/shares/lifecycle_test.go +++ b/storage/2020-08-04/file/shares/lifecycle_test.go @@ -74,6 +74,9 @@ func TestSharesLifecycle(t *testing.T) { if share.ShareQuota != 1 { t.Fatalf("Expected Quota to be 1 but got: %d", share.ShareQuota) } + if share.EnabledProtocol != SMB { + t.Fatalf("Expected EnabledProtocol to SMB but got: %s", share.EnabledProtocol) + } _, err = sharesClient.SetProperties(ctx, accountName, shareName, 5) if err != nil { diff --git a/storage/2020-08-04/file/shares/properties_get.go b/storage/2020-08-04/file/shares/properties_get.go index 6ff05cb..04eb96f 100644 --- a/storage/2020-08-04/file/shares/properties_get.go +++ b/storage/2020-08-04/file/shares/properties_get.go @@ -102,10 +102,11 @@ func (client Client) GetPropertiesResponder(resp *http.Response) (result GetProp result.ShareQuota = quota } - protocolRaw := resp.Header.Get("x-ms-enabled-protocols") - if protocolRaw != "" { - result.EnabledProtocol = ShareProtocol(protocolRaw) + protocol := SMB + if protocolRaw := resp.Header.Get("x-ms-enabled-protocols"); protocolRaw != "" { + protocol = ShareProtocol(protocolRaw) } + result.EnabledProtocol = protocol } err = autorest.Respond(