...

Source file src/github.com/Azure/azure-sdk-for-go/storage/share.go

Documentation: github.com/Azure/azure-sdk-for-go/storage

     1  package storage
     2  
     3  // Copyright (c) Microsoft Corporation. All rights reserved.
     4  // Licensed under the MIT License. See License.txt in the project root for license information.
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"net/url"
    10  	"strconv"
    11  )
    12  
    13  // Share represents an Azure file share.
    14  type Share struct {
    15  	fsc        *FileServiceClient
    16  	Name       string          `xml:"Name"`
    17  	Properties ShareProperties `xml:"Properties"`
    18  	Metadata   map[string]string
    19  }
    20  
    21  // ShareProperties contains various properties of a share.
    22  type ShareProperties struct {
    23  	LastModified string `xml:"Last-Modified"`
    24  	Etag         string `xml:"Etag"`
    25  	Quota        int    `xml:"Quota"`
    26  }
    27  
    28  // builds the complete path for this share object.
    29  func (s *Share) buildPath() string {
    30  	return fmt.Sprintf("/%s", s.Name)
    31  }
    32  
    33  // Create this share under the associated account.
    34  // If a share with the same name already exists, the operation fails.
    35  //
    36  // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Create-Share
    37  func (s *Share) Create(options *FileRequestOptions) error {
    38  	extraheaders := map[string]string{}
    39  	if s.Properties.Quota > 0 {
    40  		extraheaders["x-ms-share-quota"] = strconv.Itoa(s.Properties.Quota)
    41  	}
    42  
    43  	params := prepareOptions(options)
    44  	headers, err := s.fsc.createResource(s.buildPath(), resourceShare, params, mergeMDIntoExtraHeaders(s.Metadata, extraheaders), []int{http.StatusCreated})
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	s.updateEtagAndLastModified(headers)
    50  	return nil
    51  }
    52  
    53  // CreateIfNotExists creates this share under the associated account if
    54  // it does not exist. Returns true if the share is newly created or false if
    55  // the share already exists.
    56  //
    57  // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Create-Share
    58  func (s *Share) CreateIfNotExists(options *FileRequestOptions) (bool, error) {
    59  	extraheaders := map[string]string{}
    60  	if s.Properties.Quota > 0 {
    61  		extraheaders["x-ms-share-quota"] = strconv.Itoa(s.Properties.Quota)
    62  	}
    63  
    64  	params := prepareOptions(options)
    65  	resp, err := s.fsc.createResourceNoClose(s.buildPath(), resourceShare, params, extraheaders)
    66  	if resp != nil {
    67  		defer drainRespBody(resp)
    68  		if resp.StatusCode == http.StatusCreated || resp.StatusCode == http.StatusConflict {
    69  			if resp.StatusCode == http.StatusCreated {
    70  				s.updateEtagAndLastModified(resp.Header)
    71  				return true, nil
    72  			}
    73  			return false, s.FetchAttributes(nil)
    74  		}
    75  	}
    76  
    77  	return false, err
    78  }
    79  
    80  // Delete marks this share for deletion. The share along with any files
    81  // and directories contained within it are later deleted during garbage
    82  // collection.  If the share does not exist the operation fails
    83  //
    84  // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Delete-Share
    85  func (s *Share) Delete(options *FileRequestOptions) error {
    86  	return s.fsc.deleteResource(s.buildPath(), resourceShare, options)
    87  }
    88  
    89  // DeleteIfExists operation marks this share for deletion if it exists.
    90  //
    91  // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Delete-Share
    92  func (s *Share) DeleteIfExists(options *FileRequestOptions) (bool, error) {
    93  	resp, err := s.fsc.deleteResourceNoClose(s.buildPath(), resourceShare, options)
    94  	if resp != nil {
    95  		defer drainRespBody(resp)
    96  		if resp.StatusCode == http.StatusAccepted || resp.StatusCode == http.StatusNotFound {
    97  			return resp.StatusCode == http.StatusAccepted, nil
    98  		}
    99  	}
   100  	return false, err
   101  }
   102  
   103  // Exists returns true if this share already exists
   104  // on the storage account, otherwise returns false.
   105  func (s *Share) Exists() (bool, error) {
   106  	exists, headers, err := s.fsc.resourceExists(s.buildPath(), resourceShare)
   107  	if exists {
   108  		s.updateEtagAndLastModified(headers)
   109  		s.updateQuota(headers)
   110  	}
   111  	return exists, err
   112  }
   113  
   114  // FetchAttributes retrieves metadata and properties for this share.
   115  // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-share-properties
   116  func (s *Share) FetchAttributes(options *FileRequestOptions) error {
   117  	params := prepareOptions(options)
   118  	headers, err := s.fsc.getResourceHeaders(s.buildPath(), compNone, resourceShare, params, http.MethodHead)
   119  	if err != nil {
   120  		return err
   121  	}
   122  
   123  	s.updateEtagAndLastModified(headers)
   124  	s.updateQuota(headers)
   125  	s.Metadata = getMetadataFromHeaders(headers)
   126  
   127  	return nil
   128  }
   129  
   130  // GetRootDirectoryReference returns a Directory object at the root of this share.
   131  func (s *Share) GetRootDirectoryReference() *Directory {
   132  	return &Directory{
   133  		fsc:   s.fsc,
   134  		share: s,
   135  	}
   136  }
   137  
   138  // ServiceClient returns the FileServiceClient associated with this share.
   139  func (s *Share) ServiceClient() *FileServiceClient {
   140  	return s.fsc
   141  }
   142  
   143  // SetMetadata replaces the metadata for this share.
   144  //
   145  // Some keys may be converted to Camel-Case before sending. All keys
   146  // are returned in lower case by GetShareMetadata. HTTP header names
   147  // are case-insensitive so case munging should not matter to other
   148  // applications either.
   149  //
   150  // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/set-share-metadata
   151  func (s *Share) SetMetadata(options *FileRequestOptions) error {
   152  	headers, err := s.fsc.setResourceHeaders(s.buildPath(), compMetadata, resourceShare, mergeMDIntoExtraHeaders(s.Metadata, nil), options)
   153  	if err != nil {
   154  		return err
   155  	}
   156  
   157  	s.updateEtagAndLastModified(headers)
   158  	return nil
   159  }
   160  
   161  // SetProperties sets system properties for this share.
   162  //
   163  // Some keys may be converted to Camel-Case before sending. All keys
   164  // are returned in lower case by SetShareProperties. HTTP header names
   165  // are case-insensitive so case munging should not matter to other
   166  // applications either.
   167  //
   168  // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Set-Share-Properties
   169  func (s *Share) SetProperties(options *FileRequestOptions) error {
   170  	extraheaders := map[string]string{}
   171  	if s.Properties.Quota > 0 {
   172  		if s.Properties.Quota > 5120 {
   173  			return fmt.Errorf("invalid value %v for quota, valid values are [1, 5120]", s.Properties.Quota)
   174  		}
   175  		extraheaders["x-ms-share-quota"] = strconv.Itoa(s.Properties.Quota)
   176  	}
   177  
   178  	headers, err := s.fsc.setResourceHeaders(s.buildPath(), compProperties, resourceShare, extraheaders, options)
   179  	if err != nil {
   180  		return err
   181  	}
   182  
   183  	s.updateEtagAndLastModified(headers)
   184  	return nil
   185  }
   186  
   187  // updates Etag and last modified date
   188  func (s *Share) updateEtagAndLastModified(headers http.Header) {
   189  	s.Properties.Etag = headers.Get("Etag")
   190  	s.Properties.LastModified = headers.Get("Last-Modified")
   191  }
   192  
   193  // updates quota value
   194  func (s *Share) updateQuota(headers http.Header) {
   195  	quota, err := strconv.Atoi(headers.Get("x-ms-share-quota"))
   196  	if err == nil {
   197  		s.Properties.Quota = quota
   198  	}
   199  }
   200  
   201  // URL gets the canonical URL to this share. This method does not create a publicly accessible
   202  // URL if the share is private and this method does not check if the share exists.
   203  func (s *Share) URL() string {
   204  	return s.fsc.client.getEndpoint(fileServiceName, s.buildPath(), url.Values{})
   205  }
   206  

View as plain text