|
| 1 | +// Copyright Project Harbor Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package quota |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "os" |
| 19 | + "strconv" |
| 20 | + |
| 21 | + "github.com/goharbor/go-client/pkg/sdk/v2.0/models" |
| 22 | + "github.com/goharbor/harbor-cli/pkg/api" |
| 23 | + "github.com/goharbor/harbor-cli/pkg/prompt" |
| 24 | + "github.com/goharbor/harbor-cli/pkg/utils" |
| 25 | + "github.com/goharbor/harbor-cli/pkg/views/quota/update" |
| 26 | + log "github.com/sirupsen/logrus" |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +type QuotaUpdateReq struct { |
| 31 | + // The new hard limits for the quota |
| 32 | + Hard ResourceList `json:"hard,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +type ResourceList map[string]int64 |
| 36 | + |
| 37 | +// UpdateQuotaCommand updates the quota |
| 38 | +func UpdateQuotaCommand() *cobra.Command { |
| 39 | + var ( |
| 40 | + storage string |
| 41 | + ) |
| 42 | + |
| 43 | + var opts api.ListQuotaFlags |
| 44 | + cmd := &cobra.Command{ |
| 45 | + Use: "update [QuotaID]", |
| 46 | + Short: "update quotas for projects", |
| 47 | + Args: cobra.MaximumNArgs(1), |
| 48 | + Run: func(cmd *cobra.Command, args []string) { |
| 49 | + var err error |
| 50 | + var storageValue int64 |
| 51 | + |
| 52 | + // get quota id with quota |
| 53 | + quota, err := GetQuotaFromUser(args, opts) |
| 54 | + if err != nil { |
| 55 | + log.Errorf("error: %v", err) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + if storage != "" { |
| 60 | + if storage == "-1" { |
| 61 | + storageValue = -1 |
| 62 | + } else { |
| 63 | + storageValue, err = utils.StorageStringToBytes(storage) |
| 64 | + if err != nil { |
| 65 | + log.Errorf("failed to parse storage: %v", err) |
| 66 | + os.Exit(1) |
| 67 | + } |
| 68 | + } |
| 69 | + } else { |
| 70 | + storage = update.UpdateQuotaView(quota) |
| 71 | + storageValue, err = utils.StorageStringToBytes(storage) |
| 72 | + if err != nil { |
| 73 | + log.Errorf("failed to parse storage: %v", err) |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + hardlimit := &models.QuotaUpdateReq{ |
| 79 | + Hard: models.ResourceList{"storage": storageValue}, |
| 80 | + } |
| 81 | + |
| 82 | + err = api.UpdateQuota(quota.ID, hardlimit) |
| 83 | + if err != nil { |
| 84 | + log.Errorf("failed to update quota: %v", err) |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + |
| 88 | + log.Infof("quota updated successfully!") |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + flags := cmd.Flags() |
| 93 | + flags.StringVarP(&storage, "storage", "", "", "Enter storage size (e.g., 50GiB, 20MiB, 4TiB)") |
| 94 | + flags.StringVarP(&opts.Reference, "project-name", "", "", "Get quota by project-name") |
| 95 | + flags.StringVarP(&opts.ReferenceID, "project-id", "", "", "Get quota by project ID") |
| 96 | + |
| 97 | + return cmd |
| 98 | +} |
| 99 | + |
| 100 | +func GetQuotaFromUser(args []string, opts api.ListQuotaFlags) (*models.Quota, error) { |
| 101 | + var err error |
| 102 | + var quota *models.Quota |
| 103 | + |
| 104 | + if len(args) > 0 { |
| 105 | + quotaID, err := strconv.ParseInt(args[0], 10, 64) |
| 106 | + if err != nil { |
| 107 | + err := fmt.Errorf("failed to parse quotaID: %v", err) |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + quota, err = api.GetQuota(int64(quotaID)) |
| 111 | + if err != nil { |
| 112 | + err := fmt.Errorf("failed to get Quota: %v", err) |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + } else if opts.Reference != "" { |
| 116 | + project, err := api.GetProject(opts.Reference, false) |
| 117 | + if err != nil { |
| 118 | + err := fmt.Errorf("failed to get project: %v", err) |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + projectID := project.Payload.ProjectID |
| 122 | + quota, err = api.GetQuotaByRef(int64(projectID)) |
| 123 | + if err != nil { |
| 124 | + err := fmt.Errorf("failed to get quota: %v", err) |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + } else if opts.ReferenceID != "" { |
| 128 | + projectID, err := strconv.ParseInt(opts.ReferenceID, 10, 64) |
| 129 | + if err != nil { |
| 130 | + err := fmt.Errorf("invalid projectID: %v", err) |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + quota, err = api.GetQuotaByRef(projectID) |
| 134 | + if err != nil { |
| 135 | + err := fmt.Errorf("failed to get quota: %v", err) |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + } else { |
| 139 | + quotaID := prompt.GetQuotaIDFromUser() |
| 140 | + if quotaID == 0 { |
| 141 | + err := fmt.Errorf("failed to get quotaID from user") |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + quota, err = api.GetQuota(quotaID) |
| 145 | + if err != nil { |
| 146 | + err := fmt.Errorf("failed to get quota: %v", err) |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return quota, nil |
| 152 | +} |
0 commit comments