github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/ilm/table.go (about) 1 // Copyright (c) 2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package ilm 19 20 import ( 21 "github.com/jedib0t/go-pretty/v6/table" 22 "github.com/minio/minio-go/v7/pkg/lifecycle" 23 ) 24 25 // Table interface provides methods when implemented allows a []T to be rendered 26 // as a table. 27 type Table interface { 28 Len() int 29 Title() string 30 Rows() []table.Row 31 ColumnHeaders() table.Row 32 } 33 34 // LsFilter enumerates the 3 possible ilm-ls filter options. 35 type LsFilter uint8 36 37 const ( 38 // None - no filter 39 None LsFilter = iota 40 // ExpiryOnly - filter expiration actions across rules 41 ExpiryOnly 42 // TransitionOnly - filter transition actions across rules 43 TransitionOnly 44 ) 45 46 // Apply applies f on rules and filters lifecycle rules matching it 47 func (f LsFilter) Apply(rules []lifecycle.Rule) []lifecycle.Rule { 48 check := func(rule lifecycle.Rule) bool { 49 switch f { 50 case ExpiryOnly: 51 return !rule.Expiration.IsNull() || !rule.NoncurrentVersionExpiration.IsDaysNull() || 52 rule.NoncurrentVersionExpiration.NewerNoncurrentVersions > 0 53 case TransitionOnly: 54 return !rule.Transition.IsNull() || !rule.NoncurrentVersionTransition.IsStorageClassEmpty() 55 } 56 return true 57 } 58 59 var n int 60 for _, rule := range rules { 61 if check(rule) { 62 rules[n] = rule 63 n++ 64 } 65 } 66 rules = rules[:n] 67 return rules 68 } 69 70 type expirationCurrentRow struct { 71 ID string 72 Status string 73 Prefix string 74 Tags string 75 Days int 76 ExpireDelMarker bool 77 } 78 79 type expirationCurrentTable []expirationCurrentRow 80 81 func (e expirationCurrentTable) Len() int { 82 return len(e) 83 } 84 85 func (e expirationCurrentTable) Title() string { 86 return "Expiration for latest version (Expiration)" 87 } 88 89 func (e expirationCurrentTable) Rows() (rows []table.Row) { 90 for _, row := range e { 91 if row.Prefix == "" { 92 row.Prefix = "-" 93 } 94 if row.Tags == "" { 95 row.Tags = "-" 96 } 97 rows = append(rows, table.Row{row.ID, row.Status, row.Prefix, row.Tags, row.Days, row.ExpireDelMarker}) 98 } 99 return rows 100 } 101 102 func (e expirationCurrentTable) ColumnHeaders() (headers table.Row) { 103 return table.Row{"ID", "Status", "Prefix", "Tags", "Days to Expire", "Expire DeleteMarker"} 104 } 105 106 type expirationNoncurrentTable []expirationNoncurrentRow 107 108 type expirationNoncurrentRow struct { 109 ID string 110 Status string 111 Prefix string 112 Tags string 113 Days int 114 KeepVersions int 115 } 116 117 func (e expirationNoncurrentTable) Len() int { 118 return len(e) 119 } 120 121 func (e expirationNoncurrentTable) Title() string { 122 return "Expiration for older versions (NoncurrentVersionExpiration)" 123 } 124 125 func (e expirationNoncurrentTable) Rows() (rows []table.Row) { 126 for _, row := range e { 127 if row.Prefix == "" { 128 row.Prefix = "-" 129 } 130 if row.Tags == "" { 131 row.Tags = "-" 132 } 133 rows = append(rows, table.Row{row.ID, row.Status, row.Prefix, row.Tags, row.Days, row.KeepVersions}) 134 } 135 return rows 136 } 137 138 func (e expirationNoncurrentTable) ColumnHeaders() (headers table.Row) { 139 return table.Row{"ID", "Status", "Prefix", "Tags", "Days to Expire", "Keep Versions"} 140 } 141 142 type tierCurrentTable []tierCurrentRow 143 144 type tierCurrentRow struct { 145 ID string 146 Status string 147 Prefix string 148 Tags string 149 Days int 150 Tier string 151 } 152 153 func (t tierCurrentTable) Len() int { 154 return len(t) 155 } 156 157 func (t tierCurrentTable) Title() string { 158 return "Transition for latest version (Transition)" 159 } 160 161 func (t tierCurrentTable) ColumnHeaders() (headers table.Row) { 162 return table.Row{"ID", "Status", "Prefix", "Tags", "Days to Tier", "Tier"} 163 } 164 165 func (t tierCurrentTable) Rows() (rows []table.Row) { 166 for _, row := range t { 167 if row.Prefix == "" { 168 row.Prefix = "-" 169 } 170 if row.Tags == "" { 171 row.Tags = "-" 172 } 173 rows = append(rows, table.Row{row.ID, row.Status, row.Prefix, row.Tags, row.Days, row.Tier}) 174 } 175 return rows 176 } 177 178 type ( 179 tierNoncurrentTable []tierNoncurrentRow 180 tierNoncurrentRow tierCurrentRow 181 ) 182 183 func (t tierNoncurrentTable) Len() int { 184 return len(t) 185 } 186 187 func (t tierNoncurrentTable) Title() string { 188 return "Transition for older versions (NoncurrentVersionTransition)" 189 } 190 191 func (t tierNoncurrentTable) ColumnHeaders() table.Row { 192 return table.Row{"ID", "Status", "Prefix", "Tags", "Days to Tier", "Tier"} 193 } 194 195 func (t tierNoncurrentTable) Rows() (rows []table.Row) { 196 for _, row := range t { 197 if row.Prefix == "" { 198 row.Prefix = "-" 199 } 200 if row.Tags == "" { 201 row.Tags = "-" 202 } 203 rows = append(rows, table.Row{row.ID, row.Status, row.Prefix, row.Tags, row.Days, row.Tier}) 204 } 205 return rows 206 }