lifecycle.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2020 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. // Package lifecycle contains all the lifecycle related data types and marshallers.
  18. package lifecycle
  19. import (
  20. "encoding/json"
  21. "encoding/xml"
  22. "time"
  23. )
  24. // AbortIncompleteMultipartUpload structure, not supported yet on MinIO
  25. type AbortIncompleteMultipartUpload struct {
  26. XMLName xml.Name `xml:"AbortIncompleteMultipartUpload,omitempty" json:"-"`
  27. DaysAfterInitiation ExpirationDays `xml:"DaysAfterInitiation,omitempty" json:"DaysAfterInitiation,omitempty"`
  28. }
  29. // IsDaysNull returns true if days field is null
  30. func (n AbortIncompleteMultipartUpload) IsDaysNull() bool {
  31. return n.DaysAfterInitiation == ExpirationDays(0)
  32. }
  33. // MarshalXML if days after initiation is set to non-zero value
  34. func (n AbortIncompleteMultipartUpload) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  35. if n.IsDaysNull() {
  36. return nil
  37. }
  38. type abortIncompleteMultipartUploadWrapper AbortIncompleteMultipartUpload
  39. return e.EncodeElement(abortIncompleteMultipartUploadWrapper(n), start)
  40. }
  41. // NoncurrentVersionExpiration - Specifies when noncurrent object versions expire.
  42. // Upon expiration, server permanently deletes the noncurrent object versions.
  43. // Set this lifecycle configuration action on a bucket that has versioning enabled
  44. // (or suspended) to request server delete noncurrent object versions at a
  45. // specific period in the object's lifetime.
  46. type NoncurrentVersionExpiration struct {
  47. XMLName xml.Name `xml:"NoncurrentVersionExpiration" json:"-"`
  48. NoncurrentDays ExpirationDays `xml:"NoncurrentDays,omitempty"`
  49. }
  50. // MarshalXML if non-current days not set to non zero value
  51. func (n NoncurrentVersionExpiration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  52. if n.IsDaysNull() {
  53. return nil
  54. }
  55. type noncurrentVersionExpirationWrapper NoncurrentVersionExpiration
  56. return e.EncodeElement(noncurrentVersionExpirationWrapper(n), start)
  57. }
  58. // IsDaysNull returns true if days field is null
  59. func (n NoncurrentVersionExpiration) IsDaysNull() bool {
  60. return n.NoncurrentDays == ExpirationDays(0)
  61. }
  62. // NoncurrentVersionTransition structure, set this action to request server to
  63. // transition noncurrent object versions to different set storage classes
  64. // at a specific period in the object's lifetime.
  65. type NoncurrentVersionTransition struct {
  66. XMLName xml.Name `xml:"NoncurrentVersionTransition,omitempty" json:"-"`
  67. StorageClass string `xml:"StorageClass,omitempty" json:"StorageClass,omitempty"`
  68. NoncurrentDays ExpirationDays `xml:"NoncurrentDays,omitempty" json:"NoncurrentDays,omitempty"`
  69. }
  70. // IsDaysNull returns true if days field is null
  71. func (n NoncurrentVersionTransition) IsDaysNull() bool {
  72. return n.NoncurrentDays == ExpirationDays(0)
  73. }
  74. // IsStorageClassEmpty returns true if storage class field is empty
  75. func (n NoncurrentVersionTransition) IsStorageClassEmpty() bool {
  76. return n.StorageClass == ""
  77. }
  78. // MarshalXML is extended to leave out
  79. // <NoncurrentVersionTransition></NoncurrentVersionTransition> tags
  80. func (n NoncurrentVersionTransition) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  81. if n.IsDaysNull() || n.IsStorageClassEmpty() {
  82. return nil
  83. }
  84. type noncurrentVersionTransitionWrapper NoncurrentVersionTransition
  85. return e.EncodeElement(noncurrentVersionTransitionWrapper(n), start)
  86. }
  87. // Tag structure key/value pair representing an object tag to apply lifecycle configuration
  88. type Tag struct {
  89. XMLName xml.Name `xml:"Tag,omitempty" json:"-"`
  90. Key string `xml:"Key,omitempty" json:"Key,omitempty"`
  91. Value string `xml:"Value,omitempty" json:"Value,omitempty"`
  92. }
  93. // IsEmpty returns whether this tag is empty or not.
  94. func (tag Tag) IsEmpty() bool {
  95. return tag.Key == ""
  96. }
  97. // Transition structure - transition details of lifecycle configuration
  98. type Transition struct {
  99. XMLName xml.Name `xml:"Transition" json:"-"`
  100. Date ExpirationDate `xml:"Date,omitempty" json:"Date,omitempty"`
  101. StorageClass string `xml:"StorageClass,omitempty" json:"StorageClass,omitempty"`
  102. Days ExpirationDays `xml:"Days,omitempty" json:"Days,omitempty"`
  103. }
  104. // MarshalJSON customizes json encoding by omitting empty values
  105. func (t Transition) MarshalJSON() ([]byte, error) {
  106. type transition struct {
  107. Date *ExpirationDate `json:"Date,omitempty"`
  108. StorageClass string `json:"StorageClass,omitempty"`
  109. Days *ExpirationDays `json:"Days,omitempty"`
  110. }
  111. newt := transition{
  112. StorageClass: t.StorageClass,
  113. }
  114. if !t.IsDaysNull() {
  115. newt.Days = &t.Days
  116. }
  117. if !t.IsDateNull() {
  118. newt.Date = &t.Date
  119. }
  120. return json.Marshal(newt)
  121. }
  122. // IsDaysNull returns true if days field is null
  123. func (t Transition) IsDaysNull() bool {
  124. return t.Days == ExpirationDays(0)
  125. }
  126. // IsDateNull returns true if date field is null
  127. func (t Transition) IsDateNull() bool {
  128. return t.Date.Time.IsZero()
  129. }
  130. // IsNull returns true if both date and days fields are null
  131. func (t Transition) IsNull() bool {
  132. return t.IsDaysNull() && t.IsDateNull()
  133. }
  134. // MarshalXML is transition is non null
  135. func (t Transition) MarshalXML(en *xml.Encoder, startElement xml.StartElement) error {
  136. if t.IsNull() {
  137. return nil
  138. }
  139. type transitionWrapper Transition
  140. return en.EncodeElement(transitionWrapper(t), startElement)
  141. }
  142. // And And Rule for LifecycleTag, to be used in LifecycleRuleFilter
  143. type And struct {
  144. XMLName xml.Name `xml:"And" json:"-"`
  145. Prefix string `xml:"Prefix" json:"Prefix,omitempty"`
  146. Tags []Tag `xml:"Tag" json:"Tags,omitempty"`
  147. }
  148. // IsEmpty returns true if Tags field is null
  149. func (a And) IsEmpty() bool {
  150. return len(a.Tags) == 0 && a.Prefix == ""
  151. }
  152. // Filter will be used in selecting rule(s) for lifecycle configuration
  153. type Filter struct {
  154. XMLName xml.Name `xml:"Filter" json:"-"`
  155. And And `xml:"And,omitempty" json:"And,omitempty"`
  156. Prefix string `xml:"Prefix,omitempty" json:"Prefix,omitempty"`
  157. Tag Tag `xml:"Tag,omitempty" json:"Tag,omitempty"`
  158. }
  159. // IsNull returns true if all Filter fields are empty.
  160. func (f Filter) IsNull() bool {
  161. return f.Tag.IsEmpty() && f.And.IsEmpty() && f.Prefix == ""
  162. }
  163. // MarshalJSON customizes json encoding by removing empty values.
  164. func (f Filter) MarshalJSON() ([]byte, error) {
  165. type filter struct {
  166. And *And `json:"And,omitempty"`
  167. Prefix string `json:"Prefix,omitempty"`
  168. Tag *Tag `json:"Tag,omitempty"`
  169. }
  170. newf := filter{
  171. Prefix: f.Prefix,
  172. }
  173. if !f.Tag.IsEmpty() {
  174. newf.Tag = &f.Tag
  175. }
  176. if !f.And.IsEmpty() {
  177. newf.And = &f.And
  178. }
  179. return json.Marshal(newf)
  180. }
  181. // MarshalXML - produces the xml representation of the Filter struct
  182. // only one of Prefix, And and Tag should be present in the output.
  183. func (f Filter) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  184. if err := e.EncodeToken(start); err != nil {
  185. return err
  186. }
  187. switch {
  188. case !f.And.IsEmpty():
  189. if err := e.EncodeElement(f.And, xml.StartElement{Name: xml.Name{Local: "And"}}); err != nil {
  190. return err
  191. }
  192. case !f.Tag.IsEmpty():
  193. if err := e.EncodeElement(f.Tag, xml.StartElement{Name: xml.Name{Local: "Tag"}}); err != nil {
  194. return err
  195. }
  196. default:
  197. // Always print Prefix field when both And & Tag are empty
  198. if err := e.EncodeElement(f.Prefix, xml.StartElement{Name: xml.Name{Local: "Prefix"}}); err != nil {
  199. return err
  200. }
  201. }
  202. return e.EncodeToken(xml.EndElement{Name: start.Name})
  203. }
  204. // ExpirationDays is a type alias to unmarshal Days in Expiration
  205. type ExpirationDays int
  206. // MarshalXML encodes number of days to expire if it is non-zero and
  207. // encodes empty string otherwise
  208. func (eDays ExpirationDays) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  209. if eDays == 0 {
  210. return nil
  211. }
  212. return e.EncodeElement(int(eDays), startElement)
  213. }
  214. // ExpirationDate is a embedded type containing time.Time to unmarshal
  215. // Date in Expiration
  216. type ExpirationDate struct {
  217. time.Time
  218. }
  219. // MarshalXML encodes expiration date if it is non-zero and encodes
  220. // empty string otherwise
  221. func (eDate ExpirationDate) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  222. if eDate.Time.IsZero() {
  223. return nil
  224. }
  225. return e.EncodeElement(eDate.Format(time.RFC3339), startElement)
  226. }
  227. // ExpireDeleteMarker represents value of ExpiredObjectDeleteMarker field in Expiration XML element.
  228. type ExpireDeleteMarker bool
  229. // MarshalXML encodes delete marker boolean into an XML form.
  230. func (b ExpireDeleteMarker) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  231. if !b {
  232. return nil
  233. }
  234. type expireDeleteMarkerWrapper ExpireDeleteMarker
  235. return e.EncodeElement(expireDeleteMarkerWrapper(b), startElement)
  236. }
  237. // IsEnabled returns true if the auto delete-marker expiration is enabled
  238. func (b ExpireDeleteMarker) IsEnabled() bool {
  239. return bool(b)
  240. }
  241. // Expiration structure - expiration details of lifecycle configuration
  242. type Expiration struct {
  243. XMLName xml.Name `xml:"Expiration,omitempty" json:"-"`
  244. Date ExpirationDate `xml:"Date,omitempty" json:"Date,omitempty"`
  245. Days ExpirationDays `xml:"Days,omitempty" json:"Days,omitempty"`
  246. DeleteMarker ExpireDeleteMarker `xml:"ExpiredObjectDeleteMarker,omitempty"`
  247. }
  248. // MarshalJSON customizes json encoding by removing empty day/date specification.
  249. func (e Expiration) MarshalJSON() ([]byte, error) {
  250. type expiration struct {
  251. Date *ExpirationDate `json:"Date,omitempty"`
  252. Days *ExpirationDays `json:"Days,omitempty"`
  253. DeleteMarker ExpireDeleteMarker
  254. }
  255. newexp := expiration{
  256. DeleteMarker: e.DeleteMarker,
  257. }
  258. if !e.IsDaysNull() {
  259. newexp.Days = &e.Days
  260. }
  261. if !e.IsDateNull() {
  262. newexp.Date = &e.Date
  263. }
  264. return json.Marshal(newexp)
  265. }
  266. // IsDaysNull returns true if days field is null
  267. func (e Expiration) IsDaysNull() bool {
  268. return e.Days == ExpirationDays(0)
  269. }
  270. // IsDateNull returns true if date field is null
  271. func (e Expiration) IsDateNull() bool {
  272. return e.Date.Time.IsZero()
  273. }
  274. // IsDeleteMarkerExpirationEnabled returns true if the auto-expiration of delete marker is enabled
  275. func (e Expiration) IsDeleteMarkerExpirationEnabled() bool {
  276. return e.DeleteMarker.IsEnabled()
  277. }
  278. // IsNull returns true if both date and days fields are null
  279. func (e Expiration) IsNull() bool {
  280. return e.IsDaysNull() && e.IsDateNull() && !e.IsDeleteMarkerExpirationEnabled()
  281. }
  282. // MarshalXML is expiration is non null
  283. func (e Expiration) MarshalXML(en *xml.Encoder, startElement xml.StartElement) error {
  284. if e.IsNull() {
  285. return nil
  286. }
  287. type expirationWrapper Expiration
  288. return en.EncodeElement(expirationWrapper(e), startElement)
  289. }
  290. // MarshalJSON customizes json encoding by omitting empty values
  291. func (r Rule) MarshalJSON() ([]byte, error) {
  292. type rule struct {
  293. AbortIncompleteMultipartUpload *AbortIncompleteMultipartUpload `json:"AbortIncompleteMultipartUpload,omitempty"`
  294. Expiration *Expiration `json:"Expiration,omitempty"`
  295. ID string `json:"ID"`
  296. RuleFilter *Filter `json:"Filter,omitempty"`
  297. NoncurrentVersionExpiration *NoncurrentVersionExpiration `json:"NoncurrentVersionExpiration,omitempty"`
  298. NoncurrentVersionTransition *NoncurrentVersionTransition `json:"NoncurrentVersionTransition,omitempty"`
  299. Prefix string `json:"Prefix,omitempty"`
  300. Status string `json:"Status"`
  301. Transition *Transition `json:"Transition,omitempty"`
  302. }
  303. newr := rule{
  304. Prefix: r.Prefix,
  305. Status: r.Status,
  306. ID: r.ID,
  307. }
  308. if !r.RuleFilter.IsNull() {
  309. newr.RuleFilter = &r.RuleFilter
  310. }
  311. if !r.AbortIncompleteMultipartUpload.IsDaysNull() {
  312. newr.AbortIncompleteMultipartUpload = &r.AbortIncompleteMultipartUpload
  313. }
  314. if !r.Expiration.IsNull() {
  315. newr.Expiration = &r.Expiration
  316. }
  317. if !r.Transition.IsNull() {
  318. newr.Transition = &r.Transition
  319. }
  320. if !r.NoncurrentVersionExpiration.IsDaysNull() {
  321. newr.NoncurrentVersionExpiration = &r.NoncurrentVersionExpiration
  322. }
  323. if !r.NoncurrentVersionTransition.IsDaysNull() {
  324. newr.NoncurrentVersionTransition = &r.NoncurrentVersionTransition
  325. }
  326. return json.Marshal(newr)
  327. }
  328. // Rule represents a single rule in lifecycle configuration
  329. type Rule struct {
  330. XMLName xml.Name `xml:"Rule,omitempty" json:"-"`
  331. AbortIncompleteMultipartUpload AbortIncompleteMultipartUpload `xml:"AbortIncompleteMultipartUpload,omitempty" json:"AbortIncompleteMultipartUpload,omitempty"`
  332. Expiration Expiration `xml:"Expiration,omitempty" json:"Expiration,omitempty"`
  333. ID string `xml:"ID" json:"ID"`
  334. RuleFilter Filter `xml:"Filter,omitempty" json:"Filter,omitempty"`
  335. NoncurrentVersionExpiration NoncurrentVersionExpiration `xml:"NoncurrentVersionExpiration,omitempty" json:"NoncurrentVersionExpiration,omitempty"`
  336. NoncurrentVersionTransition NoncurrentVersionTransition `xml:"NoncurrentVersionTransition,omitempty" json:"NoncurrentVersionTransition,omitempty"`
  337. Prefix string `xml:"Prefix,omitempty" json:"Prefix,omitempty"`
  338. Status string `xml:"Status" json:"Status"`
  339. Transition Transition `xml:"Transition,omitempty" json:"Transition,omitempty"`
  340. }
  341. // Configuration is a collection of Rule objects.
  342. type Configuration struct {
  343. XMLName xml.Name `xml:"LifecycleConfiguration,omitempty" json:"-"`
  344. Rules []Rule `xml:"Rule"`
  345. }
  346. // Empty check if lifecycle configuration is empty
  347. func (c *Configuration) Empty() bool {
  348. if c == nil {
  349. return true
  350. }
  351. return len(c.Rules) == 0
  352. }
  353. // NewConfiguration initializes a fresh lifecycle configuration
  354. // for manipulation, such as setting and removing lifecycle rules
  355. // and filters.
  356. func NewConfiguration() *Configuration {
  357. return &Configuration{}
  358. }