Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/instance"
"github.com/goharbor/harbor-cli/cmd/harbor/root/labels"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/quota"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
"github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
"github.com/goharbor/harbor-cli/cmd/harbor/root/schedule"
Expand Down Expand Up @@ -95,19 +96,20 @@ harbor help
versionCommand(),
LoginCommand(),
config.Config(),
HealthCommand(),
project.Project(),
registry.Registry(),
repository.Repository(),
user.User(),
artifact.Artifact(),
tag.TagCommand(),
HealthCommand(),
cve.CVEAllowlist(),
schedule.Schedule(),
labels.Labels(),
InfoCommand(),
webhook.Webhook(),
instance.Instance(),
quota.Quota(),
)

return root
Expand Down
34 changes: 34 additions & 0 deletions cmd/harbor/root/quota/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package quota

import (
"github.com/spf13/cobra"
)

func Quota() *cobra.Command {
cmd := &cobra.Command{
Use: "quota",
Short: "Manage quotas",
Long: `Manage quotas of projects`,
Example: ` harbor quota list`,
}
cmd.AddCommand(
ListQuotaCommand(),
ViewQuotaCommand(),
UpdateQuotaCommand(),
)

return cmd
}
72 changes: 72 additions & 0 deletions cmd/harbor/root/quota/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package quota

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/quota/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// Lists the Quotas specified for each project
func ListQuotaCommand() *cobra.Command {
var opts api.ListQuotaFlags

cmd := &cobra.Command{
Use: "list",
Short: "list quotas",
Long: "list quotas specified for each project",
Run: func(cmd *cobra.Command, args []string) {
if opts.PageSize > 100 {
log.Errorf("page size should be less than or equal to 100")
return
}

quota, err := api.ListQuota(opts)
if err != nil {
log.Errorf("failed to get quota list: %v", err)
return
}

FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
err = utils.PrintFormat(quota, FormatFlag)
if err != nil {
log.Errorf("failed to get quota list: %v", err)
return
}
} else {
list.ListQuotas(quota.Payload)
}
},
}

flags := cmd.Flags()
flags.Int64VarP(&opts.Page, "page", "", 1, "Page number")
flags.Int64VarP(&opts.PageSize, "page-size", "", 0, "Size of per page (use 0 to fetch all)")
flags.StringVarP(&opts.Reference, "ref", "", "", "Reference type of quota")
flags.StringVarP(&opts.ReferenceID, "refid", "", "", "Reference ID of quota")
flags.StringVarP(
&opts.Sort,
"sort",
"",
"",
"Sort the resource list in ascending or descending order",
)

return cmd
}
152 changes: 152 additions & 0 deletions cmd/harbor/root/quota/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package quota

import (
"fmt"
"os"
"strconv"

"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/quota/update"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

type QuotaUpdateReq struct {
// The new hard limits for the quota
Hard ResourceList `json:"hard,omitempty"`
}

type ResourceList map[string]int64

// UpdateQuotaCommand updates the quota
func UpdateQuotaCommand() *cobra.Command {
var (
storage string
)

var opts api.ListQuotaFlags
cmd := &cobra.Command{
Use: "update [QuotaID]",
Short: "update quotas for projects",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var storageValue int64

// get quota id with quota
quota, err := GetQuotaFromUser(args, opts)
if err != nil {
log.Errorf("error: %v", err)
return
}

if storage != "" {
if storage == "-1" {
storageValue = -1
} else {
storageValue, err = utils.StorageStringToBytes(storage)
if err != nil {
log.Errorf("failed to parse storage: %v", err)
os.Exit(1)
}
}
} else {
storage = update.UpdateQuotaView(quota)
storageValue, err = utils.StorageStringToBytes(storage)
if err != nil {
log.Errorf("failed to parse storage: %v", err)
os.Exit(1)
}
}

hardlimit := &models.QuotaUpdateReq{
Hard: models.ResourceList{"storage": storageValue},
}

err = api.UpdateQuota(quota.ID, hardlimit)
if err != nil {
log.Errorf("failed to update quota: %v", err)
os.Exit(1)
}

log.Infof("quota updated successfully!")
},
}

flags := cmd.Flags()
flags.StringVarP(&storage, "storage", "", "", "Enter storage size (e.g., 50GiB, 20MiB, 4TiB)")
flags.StringVarP(&opts.Reference, "project-name", "", "", "Get quota by project-name")
flags.StringVarP(&opts.ReferenceID, "project-id", "", "", "Get quota by project ID")

return cmd
}

func GetQuotaFromUser(args []string, opts api.ListQuotaFlags) (*models.Quota, error) {
var err error
var quota *models.Quota

if len(args) > 0 {
quotaID, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
err := fmt.Errorf("failed to parse quotaID: %v", err)
return nil, err
}
quota, err = api.GetQuota(int64(quotaID))
if err != nil {
err := fmt.Errorf("failed to get Quota: %v", err)
return nil, err
}
} else if opts.Reference != "" {
project, err := api.GetProject(opts.Reference, false)
if err != nil {
err := fmt.Errorf("failed to get project: %v", err)
return nil, err
}
projectID := project.Payload.ProjectID
quota, err = api.GetQuotaByRef(int64(projectID))
if err != nil {
err := fmt.Errorf("failed to get quota: %v", err)
return nil, err
}
} else if opts.ReferenceID != "" {
projectID, err := strconv.ParseInt(opts.ReferenceID, 10, 64)
if err != nil {
err := fmt.Errorf("invalid projectID: %v", err)
return nil, err
}
quota, err = api.GetQuotaByRef(projectID)
if err != nil {
err := fmt.Errorf("failed to get quota: %v", err)
return nil, err
}
} else {
quotaID := prompt.GetQuotaIDFromUser()
if quotaID == 0 {
err := fmt.Errorf("failed to get quotaID from user")
return nil, err
}
quota, err = api.GetQuota(quotaID)
if err != nil {
err := fmt.Errorf("failed to get quota: %v", err)
return nil, err
}
}

return quota, nil
}
61 changes: 61 additions & 0 deletions cmd/harbor/root/quota/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package quota

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/quota/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// View a specified quota
func ViewQuotaCommand() *cobra.Command {
var opts api.ListQuotaFlags
cmd := &cobra.Command{
Use: "view [quotaID]",
Short: "get quota by quota ID",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var quota *models.Quota

// get quota id with quota
quota, err = GetQuotaFromUser(args, opts)
if err != nil {
log.Errorf("error: %v", err)
return
}
quotas := []*models.Quota{quota}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
err = utils.PrintFormat(quota, FormatFlag)
if err != nil {
log.Errorf("failed to get quota list: %v", err)
return
}
} else {
list.ListQuotas(quotas)
}
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.Reference, "project-name", "", "", "Get quota by project-name")
flags.StringVarP(&opts.ReferenceID, "project-id", "", "", "Get quota by project ID")

return cmd
}
41 changes: 41 additions & 0 deletions doc/cli-docs/harbor-quota-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: harbor quota list
weight: 85
---
## harbor quota list

### Description

##### list quotas

### Synopsis

list quotas specified for each project

```sh
harbor quota list [flags]
```

### Options

```sh
-h, --help help for list
--page int Page number (default 1)
--page-size int Size of per page (use 0 to fetch all)
--ref string Reference type of quota
--refid string Reference ID of quota
--sort string Sort the resource list in ascending or descending order
```

### Options inherited from parent commands

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```

### SEE ALSO

* [harbor quota](harbor-quota.md) - Manage quotas

Loading