s3api_policy.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package s3api
  2. import (
  3. "encoding/xml"
  4. "time"
  5. )
  6. // Status represents lifecycle configuration status
  7. type ruleStatus string
  8. // Supported status types
  9. const (
  10. Enabled ruleStatus = "Enabled"
  11. Disabled ruleStatus = "Disabled"
  12. )
  13. // Lifecycle - Configuration for bucket lifecycle.
  14. type Lifecycle struct {
  15. XMLName xml.Name `xml:"LifecycleConfiguration"`
  16. Rules []Rule `xml:"Rule"`
  17. }
  18. // Rule - a rule for lifecycle configuration.
  19. type Rule struct {
  20. XMLName xml.Name `xml:"Rule"`
  21. ID string `xml:"ID,omitempty"`
  22. Status ruleStatus `xml:"Status"`
  23. Filter Filter `xml:"Filter,omitempty"`
  24. Prefix Prefix `xml:"Prefix,omitempty"`
  25. Expiration Expiration `xml:"Expiration,omitempty"`
  26. Transition Transition `xml:"Transition,omitempty"`
  27. }
  28. // Filter - a filter for a lifecycle configuration Rule.
  29. type Filter struct {
  30. XMLName xml.Name `xml:"Filter"`
  31. set bool
  32. Prefix Prefix
  33. And And
  34. andSet bool
  35. Tag Tag
  36. tagSet bool
  37. }
  38. // Prefix holds the prefix xml tag in <Rule> and <Filter>
  39. type Prefix struct {
  40. XMLName xml.Name `xml:"Prefix"`
  41. set bool
  42. val string
  43. }
  44. func (p Prefix) String() string {
  45. return p.val
  46. }
  47. // MarshalXML encodes Prefix field into an XML form.
  48. func (p Prefix) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  49. if !p.set {
  50. return nil
  51. }
  52. return e.EncodeElement(p.val, startElement)
  53. }
  54. func (p *Prefix) UnmarshalXML(d *xml.Decoder, startElement xml.StartElement) error {
  55. prefix := ""
  56. _ = d.DecodeElement(&prefix, &startElement)
  57. *p = Prefix{set: true, val: prefix}
  58. return nil
  59. }
  60. // MarshalXML encodes Filter field into an XML form.
  61. func (f Filter) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  62. if !f.set {
  63. return nil
  64. }
  65. if err := e.EncodeToken(start); err != nil {
  66. return err
  67. }
  68. if err := e.EncodeElement(f.Prefix, xml.StartElement{Name: xml.Name{Local: "Prefix"}}); err != nil {
  69. return err
  70. }
  71. return e.EncodeToken(xml.EndElement{Name: start.Name})
  72. }
  73. // And - a tag to combine a prefix and multiple tags for lifecycle configuration rule.
  74. type And struct {
  75. XMLName xml.Name `xml:"And"`
  76. Prefix Prefix `xml:"Prefix,omitempty"`
  77. Tags []Tag `xml:"Tag,omitempty"`
  78. }
  79. // Expiration - expiration actions for a rule in lifecycle configuration.
  80. type Expiration struct {
  81. XMLName xml.Name `xml:"Expiration"`
  82. Days int `xml:"Days,omitempty"`
  83. Date ExpirationDate `xml:"Date,omitempty"`
  84. DeleteMarker ExpireDeleteMarker `xml:"ExpiredObjectDeleteMarker"`
  85. set bool
  86. }
  87. // MarshalXML encodes expiration field into an XML form.
  88. func (e Expiration) MarshalXML(enc *xml.Encoder, startElement xml.StartElement) error {
  89. if !e.set {
  90. return nil
  91. }
  92. type expirationWrapper Expiration
  93. return enc.EncodeElement(expirationWrapper(e), startElement)
  94. }
  95. // ExpireDeleteMarker represents value of ExpiredObjectDeleteMarker field in Expiration XML element.
  96. type ExpireDeleteMarker struct {
  97. val bool
  98. set bool
  99. }
  100. // MarshalXML encodes delete marker boolean into an XML form.
  101. func (b ExpireDeleteMarker) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  102. if !b.set {
  103. return nil
  104. }
  105. return e.EncodeElement(b.val, startElement)
  106. }
  107. // ExpirationDate is a embedded type containing time.Time to unmarshal
  108. // Date in Expiration
  109. type ExpirationDate struct {
  110. time.Time
  111. }
  112. // MarshalXML encodes expiration date if it is non-zero and encodes
  113. // empty string otherwise
  114. func (eDate ExpirationDate) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  115. if eDate.Time.IsZero() {
  116. return nil
  117. }
  118. return e.EncodeElement(eDate.Format(time.RFC3339), startElement)
  119. }
  120. // Transition - transition actions for a rule in lifecycle configuration.
  121. type Transition struct {
  122. XMLName xml.Name `xml:"Transition"`
  123. Days int `xml:"Days,omitempty"`
  124. Date time.Time `xml:"Date,omitempty"`
  125. StorageClass string `xml:"StorageClass,omitempty"`
  126. set bool
  127. }
  128. // MarshalXML encodes transition field into an XML form.
  129. func (t Transition) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
  130. if !t.set {
  131. return nil
  132. }
  133. type transitionWrapper Transition
  134. return enc.EncodeElement(transitionWrapper(t), start)
  135. }
  136. // TransitionDays is a type alias to unmarshal Days in Transition
  137. type TransitionDays int