Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Roles API and add cli to assign Role to a Group for a given View #124

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions api/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ func (g *Groups) List() ([]Group, error) {
return query.Page.Groups, nil
}

func (g *Groups) Get(name string) (*Group, error) {
var query struct {
Result Group `graphql:"groupByDisplayName(displayName: $displayName)"`
}

variables := map[string]interface{}{
"displayName": graphql.String(name),
}

err := g.client.Query(&query, variables)
if err != nil {
return nil, err
}

group := Group{
ID: query.Result.ID,
DisplayName: query.Result.DisplayName,
}

return &group, nil
}

func (g *Groups) AddUserToGroup(groupID string, userID string) error {
var mutation struct {
AddUsersToGroup struct {
Expand Down
39 changes: 20 additions & 19 deletions api/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,30 @@ type Roles struct {
}

type Role struct {
ID string `graphql:"id"`
DisplayName string `graphql:"displayName"`
Color string `graphql:"color"`
Description string `graphql:"description"`
ViewPermissions []string `graphql:"viewPermissions"`
SystemPermissions []string `graphql:"systemPermissions"`
OrgPermissions []string `graphql:"organizationPermissions"`
ID string `graphql:"id"`
DisplayName string `graphql:"displayName"`
Color string `graphql:"color"`
Description string `graphql:"description"`
ViewPermissions []string `graphql:"viewPermissions"`
SystemPermissions []string `graphql:"systemPermissions"`
OrganizationPermissions []string `graphql:"organizationPermissions"`
}

func (c *Client) Roles() *Roles { return &Roles{client: c} }

func (r *Roles) List() ([]Role, error) {
var query struct {
Roles struct {
Roles []Role
} `graphql:"roles()"`
Roles []Role `graphql:"roles"`
}

err := r.client.Query(&query, nil)
if err != nil {
return nil, err
}

var RolesList []Role
if err == nil {
RolesList = query.Roles.Roles
RolesList = query.Roles
}

return RolesList, nil
Expand All @@ -54,8 +55,8 @@ func (r *Roles) Create(role *Role) error {
systemPermissions[i] = graphql.String(permission)
}

orgPermissions := make([]graphql.String, len(role.OrgPermissions))
for i, permission := range role.OrgPermissions {
orgPermissions := make([]graphql.String, len(role.OrganizationPermissions))
for i, permission := range role.OrganizationPermissions {
orgPermissions[i] = graphql.String(permission)
}

Expand Down Expand Up @@ -95,8 +96,8 @@ func (r *Roles) Update(rolename string, newRole *Role) error {
systemPermissions[i] = graphql.String(permission)
}

orgPermissions := make([]graphql.String, len(newRole.OrgPermissions))
for i, permission := range newRole.OrgPermissions {
orgPermissions := make([]graphql.String, len(newRole.OrganizationPermissions))
for i, permission := range newRole.OrganizationPermissions {
orgPermissions[i] = graphql.String(permission)
}

Expand Down Expand Up @@ -134,8 +135,8 @@ func (r *Roles) RemoveRole(rolename string) error {
}

func (r *Roles) Get(rolename string) (*Role, error) {
roleId, err := r.GetRoleID(rolename)
if roleId == "" || err != nil {
roleID, err := r.GetRoleID(rolename)
if roleID == "" || err != nil {
return nil, fmt.Errorf("unable to get role id")
}

Expand All @@ -144,10 +145,10 @@ func (r *Roles) Get(rolename string) (*Role, error) {
}

variables := map[string]interface{}{
"roleId": graphql.String(roleId),
"roleId": graphql.String(roleID),
}

err = r.client.Query(query, variables)
err = r.client.Query(&query, variables)
if err != nil {
return nil, err
}
Expand Down
28 changes: 28 additions & 0 deletions api/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ViewConnection struct {

type ViewQueryData struct {
Name string
ID string
Description string
ViewInfo struct {
Connections []struct {
Expand All @@ -29,6 +30,7 @@ type ViewQueryData struct {

type View struct {
Name string
ID string
Description string
Connections []ViewConnection
}
Expand Down Expand Up @@ -59,6 +61,7 @@ func (c *Views) Get(name string) (*View, error) {

view := View{
Name: query.Result.Name,
ID: query.Result.ID,
Description: query.Result.Description,
Connections: connections,
}
Expand Down Expand Up @@ -171,3 +174,28 @@ func (c *Views) UpdateDescription(name string, description string) error {

return c.client.Mutate(&mutation, variables)
}

func (c *Views) AssignRoleToGroup(viewName, groupID, roleID string) error {
viewData, err := c.Get(viewName)

if err != nil {
return err
}

viewID := viewData.ID

var mutation struct {
AssignRoleToGroup struct {
// We have to make a selection, so just take __typename
Typename graphql.String `graphql:"__typename"`
} `graphql:"assignRoleToGroup(input:{viewId: $viewId, groupId: $groupId, roleId: $roleId})"`
}

variables := map[string]interface{}{
"viewId": graphql.String(viewID),
"groupId": graphql.String(groupID),
"roleId": graphql.String(roleID),
}

return c.client.Mutate(&mutation, variables)
}
1 change: 1 addition & 0 deletions cmd/humioctl/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func newViewsCmd() *cobra.Command {
cmd.AddCommand(newViewsCreateCmd())
cmd.AddCommand(newViewsUpdateCmd())
cmd.AddCommand(newViewsDeleteCmd())
cmd.AddCommand(newViewsAssignRoleGroupCmd())

return cmd
}
Expand Down
44 changes: 44 additions & 0 deletions cmd/humioctl/views_assign_role_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright © 2018 Humio Ltd.
//
// 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 main

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

func newViewsAssignRoleGroupCmd() *cobra.Command {
cmd := cobra.Command{
Use: "assign <role> <group> <view>",
Short: "Assign Role to a Group for a View",
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
roleName := args[0]
groupName := args[1]
viewName := args[2]
client := NewApiClient(cmd)

role, err := client.Roles().Get(roleName)
exitOnError(cmd, err, "Error fetching role")

group, err := client.Groups().Get(groupName)
exitOnError(cmd, err, "Error fetching group")

err = client.Views().AssignRoleToGroup(viewName, group.ID, role.ID)
exitOnError(cmd, err, "Error assigning permission")
},
}

return &cmd
}