Skip to content

Commit

Permalink
List groups command (#215)
Browse files Browse the repository at this point in the history
* List groups command

* Update groups on destination create
  • Loading branch information
BruceMacD committed Sep 1, 2021
1 parent f011e37 commit 8fa7e2b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 3 deletions.
31 changes: 30 additions & 1 deletion internal/api/model_group.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,39 @@ var usersCmd = &cobra.Command{
},
}

var groupsCmd = &cobra.Command{
Use: "groups",
Short: "List groups",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := apiClientFromConfig()
if err != nil {
return err
}

ctx, err := apiContextFromConfig()
if err != nil {
return err
}

groups, _, err := client.GroupsApi.ListGroups(ctx).Execute()
if err != nil {
return err
}

sort.Slice(groups, func(i, j int) bool {
return groups[i].Created > groups[j].Created
})

rows := [][]string{}
for _, g := range groups {
rows = append(rows, []string{g.Name, units.HumanDuration(time.Now().UTC().Sub(time.Unix(g.Created, 0))) + " ago", g.Source})
}

printTable([]string{"NAME", "CREATED", "SOURCE"}, rows)
return nil
},
}

func newRegistryCmd() (*cobra.Command, error) {
var options registry.Options

Expand Down Expand Up @@ -964,6 +997,7 @@ func NewRootCmd() (*cobra.Command, error) {

rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(usersCmd)
rootCmd.AddCommand(groupsCmd)
rootCmd.AddCommand(loginCmd)
rootCmd.AddCommand(logoutCmd)

Expand Down
5 changes: 3 additions & 2 deletions internal/registry/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ func (a *Api) ListUsers(w http.ResponseWriter, r *http.Request) {

func (a *Api) ListGroups(w http.ResponseWriter, r *http.Request) {
var groups []Group
if err := a.db.Find(&groups).Error; err != nil {
if err := a.db.Preload("Source").Find(&groups).Error; err != nil {
logging.L.Error(err.Error())
sendApiError(w, http.StatusInternalServerError, "could not list users")
sendApiError(w, http.StatusInternalServerError, "could not list groups")
return
}

Expand Down Expand Up @@ -624,6 +624,7 @@ func dbToApiGroup(g *Group) api.Group {
Created: g.Created,
Updated: g.Updated,
Name: g.Name,
Source: g.Source.Type,
}
}

Expand Down
26 changes: 26 additions & 0 deletions internal/registry/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,32 @@ func TestListRolesForUnknownCluster(t *testing.T) {
assert.Equal(t, 0, len(roles))
}

func TestListGroups(t *testing.T) {
a := &Api{db: db}

r := httptest.NewRequest(http.MethodGet, "/v1/groups", nil)

w := httptest.NewRecorder()
http.HandlerFunc(a.ListGroups).ServeHTTP(w, r)
assert.Equal(t, http.StatusOK, w.Code)

var groups []api.Group
if err := json.NewDecoder(w.Body).Decode(&groups); err != nil {
t.Fatal(err)
}

assert.Equal(t, 4, len(groups))

groupSources := make(map[string]string)
for _, g := range groups {
groupSources[g.Name] = g.Source
}
assert.Equal(t, "infra", groupSources["ios-developers"])
assert.Equal(t, "infra", groupSources["mac-admins"])
assert.Equal(t, "okta", groupSources["heroes"])
assert.Equal(t, "okta", groupSources["villains"])
}

func containsUser(users []api.User, email string) bool {
for _, u := range users {
if u.Email == email {
Expand Down
3 changes: 3 additions & 0 deletions internal/registry/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ func (r *Destination) BeforeCreate(tx *gorm.DB) (err error) {
}

func (d *Destination) AfterCreate(tx *gorm.DB) error {
if _, _, err := ApplyGroupMappings(tx, initialConfig.Groups); err != nil {
return err
}
_, err := ApplyUserMapping(tx, initialConfig.Users)
return err
}
Expand Down
8 changes: 8 additions & 0 deletions internal/registry/ui/api/models/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface Group {
* @memberof Group
*/
updated: number;
/**
*
* @type {string}
* @memberof Group
*/
source: string;
}

export function GroupFromJSON(json: any): Group {
Expand All @@ -59,6 +65,7 @@ export function GroupFromJSONTyped(json: any, ignoreDiscriminator: boolean): Gro
'name': json['name'],
'created': json['created'],
'updated': json['updated'],
'source': json['source'],
};
}

Expand All @@ -75,6 +82,7 @@ export function GroupToJSON(value?: Group | null): any {
'name': value.name,
'created': value.created,
'updated': value.updated,
'source': value.source,
};
}

Expand Down
3 changes: 3 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ components:
- name
- created
- updated
- source
properties:
id:
type: string
Expand All @@ -69,6 +70,8 @@ components:
updated:
type: integer
format: int64
source:
type: string
Source:
type: object
required:
Expand Down

0 comments on commit 8fa7e2b

Please sign in to comment.