1 package storage
2
3
4
5
6 import (
7 "fmt"
8 "net/http"
9 "net/url"
10 "strconv"
11 )
12
13
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
22 type ShareProperties struct {
23 LastModified string `xml:"Last-Modified"`
24 Etag string `xml:"Etag"`
25 Quota int `xml:"Quota"`
26 }
27
28
29 func (s *Share) buildPath() string {
30 return fmt.Sprintf("/%s", s.Name)
31 }
32
33
34
35
36
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
54
55
56
57
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
81
82
83
84
85 func (s *Share) Delete(options *FileRequestOptions) error {
86 return s.fsc.deleteResource(s.buildPath(), resourceShare, options)
87 }
88
89
90
91
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
104
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
115
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
131 func (s *Share) GetRootDirectoryReference() *Directory {
132 return &Directory{
133 fsc: s.fsc,
134 share: s,
135 }
136 }
137
138
139 func (s *Share) ServiceClient() *FileServiceClient {
140 return s.fsc
141 }
142
143
144
145
146
147
148
149
150
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
162
163
164
165
166
167
168
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
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
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
202
203 func (s *Share) URL() string {
204 return s.fsc.client.getEndpoint(fileServiceName, s.buildPath(), url.Values{})
205 }
206
View as plain text