-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package models | ||
|
||
import ( | ||
"path" | ||
|
||
internalpbv2 "github.com/milvus-io/birdwatcher/proto/v2.2/internalpb" | ||
) | ||
|
||
type UserInfo struct { | ||
Username string | ||
EncryptedPassword string | ||
Tenant string | ||
IsSuper bool | ||
Sha256Password string | ||
} | ||
|
||
func NewUserInfo(info *internalpbv2.CredentialInfo, key string) *UserInfo { | ||
return &UserInfo{ | ||
Username: path.Base(key), | ||
EncryptedPassword: info.GetEncryptedPassword(), | ||
Tenant: info.GetTenant(), | ||
IsSuper: info.GetIsSuper(), | ||
Sha256Password: info.GetSha256Password(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"path" | ||
|
||
"github.com/milvus-io/birdwatcher/models" | ||
internalpbv2 "github.com/milvus-io/birdwatcher/proto/v2.2/internalpb" | ||
"github.com/samber/lo" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
const ( | ||
userPrefix = `root-coord/credential/users` | ||
) | ||
|
||
func ListUsers(ctx context.Context, cli clientv3.KV, basePath string) ([]*models.UserInfo, error) { | ||
prefix := path.Join(basePath, userPrefix) | ||
|
||
infos, keys, err := ListJSONObjects[internalpbv2.CredentialInfo](ctx, cli, prefix) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return lo.Map(infos, func(info *internalpbv2.CredentialInfo, idx int) *models.UserInfo { | ||
return models.NewUserInfo(info, keys[idx]) | ||
}), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package show | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/milvus-io/birdwatcher/framework" | ||
"github.com/milvus-io/birdwatcher/models" | ||
"github.com/milvus-io/birdwatcher/states/etcd/common" | ||
) | ||
|
||
type UserParam struct { | ||
framework.ParamBase `use:"show user" desc:"display user info from rootcoord meta"` | ||
// DatabaseName string `name:"name" default:"" desc:"database name to filter with"` | ||
} | ||
|
||
// DatabaseCommand returns show database comand. | ||
func (c *ComponentShow) UserCommand(ctx context.Context, p *UserParam) (*Users, error) { | ||
users, err := common.ListUsers(ctx, c.client, c.basePath) | ||
if err != nil { | ||
fmt.Println("failed to list database info", err.Error()) | ||
return nil, errors.Wrap(err, "failed to list database info") | ||
} | ||
|
||
return framework.NewListResult[Users](users), nil | ||
} | ||
|
||
type Users struct { | ||
framework.ListResultSet[*models.UserInfo] | ||
} | ||
|
||
func (rs *Users) PrintAs(format framework.Format) string { | ||
switch format { | ||
case framework.FormatDefault, framework.FormatPlain: | ||
sb := &strings.Builder{} | ||
for _, user := range rs.Data { | ||
//rs.printDatabaseInfo(sb, database) | ||
sb.WriteString(fmt.Sprintf("Username: %s Tenant:%s\n", user.Username, user.Tenant)) | ||
} | ||
fmt.Fprintf(sb, "--- Total Users(s): %d\n", len(rs.Data)) | ||
return sb.String() | ||
default: | ||
} | ||
return "" | ||
} |