Skip to content
Open
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
5 changes: 5 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/schedule"
"github.com/goharbor/harbor-cli/cmd/harbor/root/tag"
"github.com/goharbor/harbor-cli/cmd/harbor/root/user"
"github.com/goharbor/harbor-cli/cmd/harbor/root/usergroup"
"github.com/goharbor/harbor-cli/cmd/harbor/root/webhook"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -161,6 +162,10 @@ harbor help
cmd.GroupID = "access"
root.AddCommand(cmd)

cmd = usergroup.Usergroup()
cmd.GroupID = "access"
root.AddCommand(cmd)

// System
cmd = context.Context()
cmd.GroupID = "system"
Expand Down
36 changes: 36 additions & 0 deletions cmd/harbor/root/usergroup/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 usergroup

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

func Usergroup() *cobra.Command {
cmd := &cobra.Command{
Use: "usergroup",
Short: "Manage usergroup",
Long: `Manage usergroup in Harbor`,
Example: ` harbor usergroup list`,
}

cmd.AddCommand(
UserGroupCreateCommand(),
UserGroupsListCommand(),
UserGroupDeleteCommand(),
UserGroupUpdateCommand(),
)

return cmd
}
78 changes: 78 additions & 0 deletions cmd/harbor/root/usergroup/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 usergroup

import (
"encoding/json"
"fmt"
"strings"

"github.com/goharbor/harbor-cli/pkg/api"
create "github.com/goharbor/harbor-cli/pkg/views/usergroup/create"
"github.com/spf13/cobra"
)

type ErrorResponse struct {
Errors []struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"errors"`
}

func UserGroupCreateCommand() *cobra.Command {
var opts create.CreateUserGroupInput

cmd := &cobra.Command{
Use: "create",
Short: "create user group",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
// Getting Vals
err := create.CreateUserGroupView(&opts)
if err != nil {
return err
}

fmt.Printf("Creating user group with name: %s, type: %d%s\n", opts.GroupName, opts.GroupType, opts.LDAPGroupDN)
err = api.CreateUserGroup(opts.GroupName, opts.GroupType, opts.LDAPGroupDN)
if err != nil {
return formatError(err)
}

fmt.Printf("User group '%s' created successfully\n", opts.GroupName)
return nil
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.GroupName, "name", "n", "", "Group name")
flags.Int64VarP(&opts.GroupType, "type", "t", 0, "Group type")
flags.StringVarP(&opts.LDAPGroupDN, "ldap-dn", "l", "", "The DN of the LDAP group if group type is 1 (LDAP group)")

return cmd
}

func formatError(err error) error {
errStr := err.Error()
if strings.Contains(errStr, "conflict:") {
var errResp ErrorResponse
jsonStr := strings.TrimPrefix(errStr, "conflict: ")
if err := json.Unmarshal([]byte(jsonStr), &errResp); err == nil {
if len(errResp.Errors) > 0 {
return fmt.Errorf("%s", errResp.Errors[0].Message)
}
}
}
return fmt.Errorf("failed to create user group: %v", err)
}
63 changes: 63 additions & 0 deletions cmd/harbor/root/usergroup/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 usergroup

import (
"fmt"
"strconv"

"github.com/goharbor/harbor-cli/pkg/api"
delete "github.com/goharbor/harbor-cli/pkg/views/usergroup/delete"
"github.com/spf13/cobra"
)

func UserGroupDeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [groupID]",
Short: "delete user group",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var groupID int64
var err error

if len(args) > 0 && args[0] != "" {
groupID, err = strconv.ParseInt(args[0], 10, 64)
if err != nil {
return fmt.Errorf("failed to convert argument to ID: %v", err)
}
} else {
response, err := api.ListUserGroups()
if err != nil {
return fmt.Errorf("failed to list user groups: %v", err)
}

opts, err := delete.DeleteUserGroupView(response)
if err != nil {
return fmt.Errorf("failed to obtain user selection: %v", err)
}

groupID = opts.ID
}

err = api.DeleteUserGroup(groupID)
if err != nil {
return fmt.Errorf("failed to delete user group: %v", err)
}

return nil
},
}

return cmd
}
87 changes: 87 additions & 0 deletions cmd/harbor/root/usergroup/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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 usergroup

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/spf13/cobra"

list "github.com/goharbor/harbor-cli/pkg/views/usergroup/list"
)

func UserGroupsListCommand() *cobra.Command {
var (
searchq string
searchID int64
)

cmd := &cobra.Command{
Use: "list",
Short: "list user groups",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var userGroups []*models.UserGroup

if searchID != -1 {
resp, err := api.GetUserGroup(searchID)
if err != nil {
return err
}

userGroups = []*models.UserGroup{resp.Payload}
} else if searchq != "" {
resp, err := api.SearchUserGroups(searchq)
if err != nil {
return err
}

userGroups = SearchItemToModel(resp.Payload)
} else {
resp, err := api.ListUserGroups()
if err != nil {
return err
}

userGroups = resp.Payload
}

list.ListUserGroups(userGroups)
return nil
},
}

flags := cmd.Flags()
flags.StringVarP(&searchq, "search", "s", "", "use to search for a specific groupname")
flags.Int64VarP(&searchID, "id", "i", -1, "use to search for a specific groupid")

return cmd
}

func SearchItemToModel(items []*models.UserGroupSearchItem) []*models.UserGroup {
grps := make([]*models.UserGroup, len(items))

for k, item := range items {
grp := &models.UserGroup{
LdapGroupDn: "N/A",
GroupType: item.GroupType,
GroupName: item.GroupName,
ID: item.ID,
}

grps[k] = grp
}

return grps
}
49 changes: 49 additions & 0 deletions cmd/harbor/root/usergroup/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 usergroup

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/views/usergroup/update"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func UserGroupUpdateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "update user group",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
userGroupsResp, err := api.ListUserGroups()
if err != nil {
log.Errorf("failed to list user groups: %v", err)
return
}
input, err := update.UpdateUserGroupView(userGroupsResp)
if err != nil {
log.Errorf("failed to get user input: %v", err)
return
}
err = api.UpdateUserGroup(input.GroupID, input.GroupName, input.GroupType)
if err != nil {
log.Errorf("failed to update user group: %v", err)
} else {
log.Infof("User group `%s` updated successfully", input.GroupName)
}
},
}

return cmd
}
35 changes: 35 additions & 0 deletions doc/cli-docs/harbor-usergroup-create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: harbor usergroup create
weight: 30
---
## harbor usergroup create

### Description

##### create user group

```sh
harbor usergroup create [flags]
```

### Options

```sh
-h, --help help for create
-l, --ldap-dn string The DN of the LDAP group if group type is 1 (LDAP group)
-n, --name string Group name
-t, --type int Group type
```

### 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 usergroup](harbor-usergroup.md) - Manage usergroup

Loading