|
| 1 | +package quota |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "regexp" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/goharbor/go-client/pkg/sdk/v2.0/models" |
| 10 | + "github.com/goharbor/harbor-cli/pkg/api" |
| 11 | + "github.com/goharbor/harbor-cli/pkg/prompt" |
| 12 | + "github.com/goharbor/harbor-cli/pkg/views/quota/update" |
| 13 | + log "github.com/sirupsen/logrus" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +type QuotaUpdateReq struct { |
| 18 | + // The new hard limits for the quota |
| 19 | + Hard ResourceList `json:"hard,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +type ResourceList map[string]int64 |
| 23 | + |
| 24 | +// UpdateQuotaCommand updates the quota |
| 25 | +func UpdateQuotaCommand() *cobra.Command { |
| 26 | + var ( |
| 27 | + quotaID int64 |
| 28 | + storage string |
| 29 | + ) |
| 30 | + |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "update [QuotaID]", |
| 33 | + Short: "update project quotas for projects", |
| 34 | + Args: cobra.MaximumNArgs(1), |
| 35 | + Run: func(cmd *cobra.Command, args []string) { |
| 36 | + var err error |
| 37 | + var storageValue int64 |
| 38 | + |
| 39 | + if len(args) > 0 { |
| 40 | + quotaID, err = strconv.ParseInt(args[0], 10, 64) |
| 41 | + } else { |
| 42 | + quotaID = prompt.GetQuotaIDFromUser() |
| 43 | + } |
| 44 | + |
| 45 | + if err != nil { |
| 46 | + log.Errorf("failed to parse registry id: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + if storage != "" { |
| 50 | + if storage == "-1" { |
| 51 | + storageValue = -1 |
| 52 | + } else { |
| 53 | + storageValue, err = storageStringToBytes(storage) |
| 54 | + if err != nil { |
| 55 | + log.Errorf("failed to parse storage: %v", err) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + } |
| 59 | + } else { |
| 60 | + storage = update.UpdateQuotaView() |
| 61 | + storageValue, err = storageStringToBytes(storage) |
| 62 | + } |
| 63 | + |
| 64 | + hardlimit := &models.QuotaUpdateReq{ |
| 65 | + Hard: models.ResourceList{"storage": storageValue}, |
| 66 | + } |
| 67 | + |
| 68 | + err = api.UpdateQuota(quotaID, hardlimit) |
| 69 | + if err != nil { |
| 70 | + log.Errorf("failed to update quota: %v", err) |
| 71 | + os.Exit(1) |
| 72 | + } |
| 73 | + |
| 74 | + log.Infof("quota updated successfully!") |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + flags := cmd.Flags() |
| 79 | + flags.StringVarP(&storage, "storage", "", "", "Enter storage size (e.g., 50GiB, 200MiB, 4TiB)") |
| 80 | + |
| 81 | + return cmd |
| 82 | +} |
| 83 | + |
| 84 | +func storageStringToBytes(storage string) (int64, error) { |
| 85 | + // Define the conversion multipliers |
| 86 | + multipliers := map[string]int64{ |
| 87 | + "MiB": 1024 * 1024, |
| 88 | + "GiB": 1024 * 1024 * 1024, |
| 89 | + "TiB": 1024 * 1024 * 1024 * 1024, |
| 90 | + } |
| 91 | + |
| 92 | + // Define the regex to parse the input string |
| 93 | + re := regexp.MustCompile(`^(\d+)(MiB|GiB|TiB)$`) |
| 94 | + matches := re.FindStringSubmatch(storage) |
| 95 | + if matches == nil { |
| 96 | + return 0, errors.New("invalid storage format") |
| 97 | + } |
| 98 | + |
| 99 | + // Extract the value and unit from the matches |
| 100 | + valueStr, unit := matches[1], matches[2] |
| 101 | + value, err := strconv.ParseInt(valueStr, 10, 64) |
| 102 | + if err != nil { |
| 103 | + return 0, err |
| 104 | + } |
| 105 | + |
| 106 | + // Calculate the value in bytes |
| 107 | + bytes := value * multipliers[unit] |
| 108 | + |
| 109 | + // Check if the value exceeds 1024 TB |
| 110 | + maxBytes := 1024 * 1024 * 1024 * 1024 * 1024 |
| 111 | + if bytes > int64(maxBytes) { |
| 112 | + return 0, errors.New("value exceeds 1024 TB") |
| 113 | + } |
| 114 | + |
| 115 | + return bytes, nil |
| 116 | +} |
0 commit comments