entity.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package storage
  2. // Copyright 2017 Microsoft Corporation
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import (
  16. "bytes"
  17. "encoding/base64"
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "io/ioutil"
  22. "net/http"
  23. "net/url"
  24. "strconv"
  25. "strings"
  26. "time"
  27. "github.com/satori/go.uuid"
  28. )
  29. // Annotating as secure for gas scanning
  30. /* #nosec */
  31. const (
  32. partitionKeyNode = "PartitionKey"
  33. rowKeyNode = "RowKey"
  34. etagErrorTemplate = "Etag didn't match: %v"
  35. )
  36. var (
  37. errEmptyPayload = errors.New("Empty payload is not a valid metadata level for this operation")
  38. errNilPreviousResult = errors.New("The previous results page is nil")
  39. errNilNextLink = errors.New("There are no more pages in this query results")
  40. )
  41. // Entity represents an entity inside an Azure table.
  42. type Entity struct {
  43. Table *Table
  44. PartitionKey string
  45. RowKey string
  46. TimeStamp time.Time
  47. OdataMetadata string
  48. OdataType string
  49. OdataID string
  50. OdataEtag string
  51. OdataEditLink string
  52. Properties map[string]interface{}
  53. }
  54. // GetEntityReference returns an Entity object with the specified
  55. // partition key and row key.
  56. func (t *Table) GetEntityReference(partitionKey, rowKey string) *Entity {
  57. return &Entity{
  58. PartitionKey: partitionKey,
  59. RowKey: rowKey,
  60. Table: t,
  61. }
  62. }
  63. // EntityOptions includes options for entity operations.
  64. type EntityOptions struct {
  65. Timeout uint
  66. RequestID string `header:"x-ms-client-request-id"`
  67. }
  68. // GetEntityOptions includes options for a get entity operation
  69. type GetEntityOptions struct {
  70. Select []string
  71. RequestID string `header:"x-ms-client-request-id"`
  72. }
  73. // Get gets the referenced entity. Which properties to get can be
  74. // specified using the select option.
  75. // See:
  76. // https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/query-entities
  77. // https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/querying-tables-and-entities
  78. func (e *Entity) Get(timeout uint, ml MetadataLevel, options *GetEntityOptions) error {
  79. if ml == EmptyPayload {
  80. return errEmptyPayload
  81. }
  82. // RowKey and PartitionKey could be lost if not included in the query
  83. // As those are the entity identifiers, it is best if they are not lost
  84. rk := e.RowKey
  85. pk := e.PartitionKey
  86. query := url.Values{
  87. "timeout": {strconv.FormatUint(uint64(timeout), 10)},
  88. }
  89. headers := e.Table.tsc.client.getStandardHeaders()
  90. headers[headerAccept] = string(ml)
  91. if options != nil {
  92. if len(options.Select) > 0 {
  93. query.Add("$select", strings.Join(options.Select, ","))
  94. }
  95. headers = mergeHeaders(headers, headersFromStruct(*options))
  96. }
  97. uri := e.Table.tsc.client.getEndpoint(tableServiceName, e.buildPath(), query)
  98. resp, err := e.Table.tsc.client.exec(http.MethodGet, uri, headers, nil, e.Table.tsc.auth)
  99. if err != nil {
  100. return err
  101. }
  102. defer drainRespBody(resp)
  103. if err = checkRespCode(resp, []int{http.StatusOK}); err != nil {
  104. return err
  105. }
  106. respBody, err := ioutil.ReadAll(resp.Body)
  107. if err != nil {
  108. return err
  109. }
  110. err = json.Unmarshal(respBody, e)
  111. if err != nil {
  112. return err
  113. }
  114. e.PartitionKey = pk
  115. e.RowKey = rk
  116. return nil
  117. }
  118. // Insert inserts the referenced entity in its table.
  119. // The function fails if there is an entity with the same
  120. // PartitionKey and RowKey in the table.
  121. // ml determines the level of detail of metadata in the operation response,
  122. // or no data at all.
  123. // See: https://docs.microsoft.com/rest/api/storageservices/fileservices/insert-entity
  124. func (e *Entity) Insert(ml MetadataLevel, options *EntityOptions) error {
  125. query, headers := options.getParameters()
  126. headers = mergeHeaders(headers, e.Table.tsc.client.getStandardHeaders())
  127. body, err := json.Marshal(e)
  128. if err != nil {
  129. return err
  130. }
  131. headers = addBodyRelatedHeaders(headers, len(body))
  132. headers = addReturnContentHeaders(headers, ml)
  133. uri := e.Table.tsc.client.getEndpoint(tableServiceName, e.Table.buildPath(), query)
  134. resp, err := e.Table.tsc.client.exec(http.MethodPost, uri, headers, bytes.NewReader(body), e.Table.tsc.auth)
  135. if err != nil {
  136. return err
  137. }
  138. defer drainRespBody(resp)
  139. if ml != EmptyPayload {
  140. if err = checkRespCode(resp, []int{http.StatusCreated}); err != nil {
  141. return err
  142. }
  143. data, err := ioutil.ReadAll(resp.Body)
  144. if err != nil {
  145. return err
  146. }
  147. if err = e.UnmarshalJSON(data); err != nil {
  148. return err
  149. }
  150. } else {
  151. if err = checkRespCode(resp, []int{http.StatusNoContent}); err != nil {
  152. return err
  153. }
  154. }
  155. return nil
  156. }
  157. // Update updates the contents of an entity. The function fails if there is no entity
  158. // with the same PartitionKey and RowKey in the table or if the ETag is different
  159. // than the one in Azure.
  160. // See: https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/update-entity2
  161. func (e *Entity) Update(force bool, options *EntityOptions) error {
  162. return e.updateMerge(force, http.MethodPut, options)
  163. }
  164. // Merge merges the contents of entity specified with PartitionKey and RowKey
  165. // with the content specified in Properties.
  166. // The function fails if there is no entity with the same PartitionKey and
  167. // RowKey in the table or if the ETag is different than the one in Azure.
  168. // Read more: https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/merge-entity
  169. func (e *Entity) Merge(force bool, options *EntityOptions) error {
  170. return e.updateMerge(force, "MERGE", options)
  171. }
  172. // Delete deletes the entity.
  173. // The function fails if there is no entity with the same PartitionKey and
  174. // RowKey in the table or if the ETag is different than the one in Azure.
  175. // See: https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/delete-entity1
  176. func (e *Entity) Delete(force bool, options *EntityOptions) error {
  177. query, headers := options.getParameters()
  178. headers = mergeHeaders(headers, e.Table.tsc.client.getStandardHeaders())
  179. headers = addIfMatchHeader(headers, force, e.OdataEtag)
  180. headers = addReturnContentHeaders(headers, EmptyPayload)
  181. uri := e.Table.tsc.client.getEndpoint(tableServiceName, e.buildPath(), query)
  182. resp, err := e.Table.tsc.client.exec(http.MethodDelete, uri, headers, nil, e.Table.tsc.auth)
  183. if err != nil {
  184. if resp.StatusCode == http.StatusPreconditionFailed {
  185. return fmt.Errorf(etagErrorTemplate, err)
  186. }
  187. return err
  188. }
  189. defer drainRespBody(resp)
  190. if err = checkRespCode(resp, []int{http.StatusNoContent}); err != nil {
  191. return err
  192. }
  193. return e.updateTimestamp(resp.Header)
  194. }
  195. // InsertOrReplace inserts an entity or replaces the existing one.
  196. // Read more: https://docs.microsoft.com/rest/api/storageservices/fileservices/insert-or-replace-entity
  197. func (e *Entity) InsertOrReplace(options *EntityOptions) error {
  198. return e.insertOr(http.MethodPut, options)
  199. }
  200. // InsertOrMerge inserts an entity or merges the existing one.
  201. // Read more: https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/insert-or-merge-entity
  202. func (e *Entity) InsertOrMerge(options *EntityOptions) error {
  203. return e.insertOr("MERGE", options)
  204. }
  205. func (e *Entity) buildPath() string {
  206. return fmt.Sprintf("%s(PartitionKey='%s', RowKey='%s')", e.Table.buildPath(), e.PartitionKey, e.RowKey)
  207. }
  208. // MarshalJSON is a custom marshaller for entity
  209. func (e *Entity) MarshalJSON() ([]byte, error) {
  210. completeMap := map[string]interface{}{}
  211. completeMap[partitionKeyNode] = e.PartitionKey
  212. completeMap[rowKeyNode] = e.RowKey
  213. for k, v := range e.Properties {
  214. typeKey := strings.Join([]string{k, OdataTypeSuffix}, "")
  215. switch t := v.(type) {
  216. case []byte:
  217. completeMap[typeKey] = OdataBinary
  218. completeMap[k] = t
  219. case time.Time:
  220. completeMap[typeKey] = OdataDateTime
  221. completeMap[k] = t.Format(time.RFC3339Nano)
  222. case uuid.UUID:
  223. completeMap[typeKey] = OdataGUID
  224. completeMap[k] = t.String()
  225. case int64:
  226. completeMap[typeKey] = OdataInt64
  227. completeMap[k] = fmt.Sprintf("%v", v)
  228. default:
  229. completeMap[k] = v
  230. }
  231. if strings.HasSuffix(k, OdataTypeSuffix) {
  232. if !(completeMap[k] == OdataBinary ||
  233. completeMap[k] == OdataDateTime ||
  234. completeMap[k] == OdataGUID ||
  235. completeMap[k] == OdataInt64) {
  236. return nil, fmt.Errorf("Odata.type annotation %v value is not valid", k)
  237. }
  238. valueKey := strings.TrimSuffix(k, OdataTypeSuffix)
  239. if _, ok := completeMap[valueKey]; !ok {
  240. return nil, fmt.Errorf("Odata.type annotation %v defined without value defined", k)
  241. }
  242. }
  243. }
  244. return json.Marshal(completeMap)
  245. }
  246. // UnmarshalJSON is a custom unmarshaller for entities
  247. func (e *Entity) UnmarshalJSON(data []byte) error {
  248. errorTemplate := "Deserializing error: %v"
  249. props := map[string]interface{}{}
  250. err := json.Unmarshal(data, &props)
  251. if err != nil {
  252. return err
  253. }
  254. // deselialize metadata
  255. e.OdataMetadata = stringFromMap(props, "odata.metadata")
  256. e.OdataType = stringFromMap(props, "odata.type")
  257. e.OdataID = stringFromMap(props, "odata.id")
  258. e.OdataEtag = stringFromMap(props, "odata.etag")
  259. e.OdataEditLink = stringFromMap(props, "odata.editLink")
  260. e.PartitionKey = stringFromMap(props, partitionKeyNode)
  261. e.RowKey = stringFromMap(props, rowKeyNode)
  262. // deserialize timestamp
  263. timeStamp, ok := props["Timestamp"]
  264. if ok {
  265. str, ok := timeStamp.(string)
  266. if !ok {
  267. return fmt.Errorf(errorTemplate, "Timestamp casting error")
  268. }
  269. t, err := time.Parse(time.RFC3339Nano, str)
  270. if err != nil {
  271. return fmt.Errorf(errorTemplate, err)
  272. }
  273. e.TimeStamp = t
  274. }
  275. delete(props, "Timestamp")
  276. delete(props, "Timestamp@odata.type")
  277. // deserialize entity (user defined fields)
  278. for k, v := range props {
  279. if strings.HasSuffix(k, OdataTypeSuffix) {
  280. valueKey := strings.TrimSuffix(k, OdataTypeSuffix)
  281. str, ok := props[valueKey].(string)
  282. if !ok {
  283. return fmt.Errorf(errorTemplate, fmt.Sprintf("%v casting error", v))
  284. }
  285. switch v {
  286. case OdataBinary:
  287. props[valueKey], err = base64.StdEncoding.DecodeString(str)
  288. if err != nil {
  289. return fmt.Errorf(errorTemplate, err)
  290. }
  291. case OdataDateTime:
  292. t, err := time.Parse("2006-01-02T15:04:05Z", str)
  293. if err != nil {
  294. return fmt.Errorf(errorTemplate, err)
  295. }
  296. props[valueKey] = t
  297. case OdataGUID:
  298. props[valueKey] = uuid.FromStringOrNil(str)
  299. case OdataInt64:
  300. i, err := strconv.ParseInt(str, 10, 64)
  301. if err != nil {
  302. return fmt.Errorf(errorTemplate, err)
  303. }
  304. props[valueKey] = i
  305. default:
  306. return fmt.Errorf(errorTemplate, fmt.Sprintf("%v is not supported", v))
  307. }
  308. delete(props, k)
  309. }
  310. }
  311. e.Properties = props
  312. return nil
  313. }
  314. func getAndDelete(props map[string]interface{}, key string) interface{} {
  315. if value, ok := props[key]; ok {
  316. delete(props, key)
  317. return value
  318. }
  319. return nil
  320. }
  321. func addIfMatchHeader(h map[string]string, force bool, etag string) map[string]string {
  322. if force {
  323. h[headerIfMatch] = "*"
  324. } else {
  325. h[headerIfMatch] = etag
  326. }
  327. return h
  328. }
  329. // updates Etag and timestamp
  330. func (e *Entity) updateEtagAndTimestamp(headers http.Header) error {
  331. e.OdataEtag = headers.Get(headerEtag)
  332. return e.updateTimestamp(headers)
  333. }
  334. func (e *Entity) updateTimestamp(headers http.Header) error {
  335. str := headers.Get(headerDate)
  336. t, err := time.Parse(time.RFC1123, str)
  337. if err != nil {
  338. return fmt.Errorf("Update timestamp error: %v", err)
  339. }
  340. e.TimeStamp = t
  341. return nil
  342. }
  343. func (e *Entity) insertOr(verb string, options *EntityOptions) error {
  344. query, headers := options.getParameters()
  345. headers = mergeHeaders(headers, e.Table.tsc.client.getStandardHeaders())
  346. body, err := json.Marshal(e)
  347. if err != nil {
  348. return err
  349. }
  350. headers = addBodyRelatedHeaders(headers, len(body))
  351. headers = addReturnContentHeaders(headers, EmptyPayload)
  352. uri := e.Table.tsc.client.getEndpoint(tableServiceName, e.buildPath(), query)
  353. resp, err := e.Table.tsc.client.exec(verb, uri, headers, bytes.NewReader(body), e.Table.tsc.auth)
  354. if err != nil {
  355. return err
  356. }
  357. defer drainRespBody(resp)
  358. if err = checkRespCode(resp, []int{http.StatusNoContent}); err != nil {
  359. return err
  360. }
  361. return e.updateEtagAndTimestamp(resp.Header)
  362. }
  363. func (e *Entity) updateMerge(force bool, verb string, options *EntityOptions) error {
  364. query, headers := options.getParameters()
  365. headers = mergeHeaders(headers, e.Table.tsc.client.getStandardHeaders())
  366. body, err := json.Marshal(e)
  367. if err != nil {
  368. return err
  369. }
  370. headers = addBodyRelatedHeaders(headers, len(body))
  371. headers = addIfMatchHeader(headers, force, e.OdataEtag)
  372. headers = addReturnContentHeaders(headers, EmptyPayload)
  373. uri := e.Table.tsc.client.getEndpoint(tableServiceName, e.buildPath(), query)
  374. resp, err := e.Table.tsc.client.exec(verb, uri, headers, bytes.NewReader(body), e.Table.tsc.auth)
  375. if err != nil {
  376. if resp.StatusCode == http.StatusPreconditionFailed {
  377. return fmt.Errorf(etagErrorTemplate, err)
  378. }
  379. return err
  380. }
  381. defer drainRespBody(resp)
  382. if err = checkRespCode(resp, []int{http.StatusNoContent}); err != nil {
  383. return err
  384. }
  385. return e.updateEtagAndTimestamp(resp.Header)
  386. }
  387. func stringFromMap(props map[string]interface{}, key string) string {
  388. value := getAndDelete(props, key)
  389. if value != nil {
  390. return value.(string)
  391. }
  392. return ""
  393. }
  394. func (options *EntityOptions) getParameters() (url.Values, map[string]string) {
  395. query := url.Values{}
  396. headers := map[string]string{}
  397. if options != nil {
  398. query = addTimeout(query, options.Timeout)
  399. headers = headersFromStruct(*options)
  400. }
  401. return query, headers
  402. }