s3api_policy.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. string
  41. set bool
  42. }
  43. // MarshalXML encodes Prefix field into an XML form.
  44. func (p Prefix) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  45. if !p.set {
  46. return nil
  47. }
  48. return e.EncodeElement(p.string, startElement)
  49. }
  50. // MarshalXML encodes Filter field into an XML form.
  51. func (f Filter) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  52. if err := e.EncodeToken(start); err != nil {
  53. return err
  54. }
  55. if err := e.EncodeElement(f.Prefix, xml.StartElement{Name: xml.Name{Local: "Prefix"}}); err != nil {
  56. return err
  57. }
  58. return e.EncodeToken(xml.EndElement{Name: start.Name})
  59. }
  60. // And - a tag to combine a prefix and multiple tags for lifecycle configuration rule.
  61. type And struct {
  62. XMLName xml.Name `xml:"And"`
  63. Prefix Prefix `xml:"Prefix,omitempty"`
  64. Tags []Tag `xml:"Tag,omitempty"`
  65. }
  66. // Expiration - expiration actions for a rule in lifecycle configuration.
  67. type Expiration struct {
  68. XMLName xml.Name `xml:"Expiration"`
  69. Days int `xml:"Days,omitempty"`
  70. Date ExpirationDate `xml:"Date,omitempty"`
  71. DeleteMarker ExpireDeleteMarker `xml:"ExpiredObjectDeleteMarker"`
  72. set bool
  73. }
  74. // MarshalXML encodes expiration field into an XML form.
  75. func (e Expiration) MarshalXML(enc *xml.Encoder, startElement xml.StartElement) error {
  76. if !e.set {
  77. return nil
  78. }
  79. type expirationWrapper Expiration
  80. return enc.EncodeElement(expirationWrapper(e), startElement)
  81. }
  82. // ExpireDeleteMarker represents value of ExpiredObjectDeleteMarker field in Expiration XML element.
  83. type ExpireDeleteMarker struct {
  84. val bool
  85. set bool
  86. }
  87. // MarshalXML encodes delete marker boolean into an XML form.
  88. func (b ExpireDeleteMarker) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  89. if !b.set {
  90. return nil
  91. }
  92. return e.EncodeElement(b.val, startElement)
  93. }
  94. // ExpirationDate is a embedded type containing time.Time to unmarshal
  95. // Date in Expiration
  96. type ExpirationDate struct {
  97. time.Time
  98. }
  99. // MarshalXML encodes expiration date if it is non-zero and encodes
  100. // empty string otherwise
  101. func (eDate ExpirationDate) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  102. if eDate.Time.IsZero() {
  103. return nil
  104. }
  105. return e.EncodeElement(eDate.Format(time.RFC3339), startElement)
  106. }
  107. // Transition - transition actions for a rule in lifecycle configuration.
  108. type Transition struct {
  109. XMLName xml.Name `xml:"Transition"`
  110. Days int `xml:"Days,omitempty"`
  111. Date time.Time `xml:"Date,omitempty"`
  112. StorageClass string `xml:"StorageClass,omitempty"`
  113. set bool
  114. }
  115. // MarshalXML encodes transition field into an XML form.
  116. func (t Transition) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
  117. if !t.set {
  118. return nil
  119. }
  120. type transitionWrapper Transition
  121. return enc.EncodeElement(transitionWrapper(t), start)
  122. }
  123. // TransitionDays is a type alias to unmarshal Days in Transition
  124. type TransitionDays int