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

Add roles command to humioctl #156

Open
wants to merge 2 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
163 changes: 71 additions & 92 deletions api/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,170 +2,149 @@ package api

import (
"fmt"

graphql "github.com/cli/shurcooL-graphql"
)

type Roles struct {
client *Client
}

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

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 `json:"id"`
DisplayName string `json:"displayName"`
Color string `json:"color,omitempty"`
Description string `json:"description,omitempty"`
ViewPermissions []string `json:"viewPermissions"`
SystemPermissions []string `json:"systemPermissions,omitempty"`
OrgPermissions []string `graphql:"organizationPermissions" json:"organizationPermissions,omitempty"`
GroupsCount int `json:"groupsCount"`
UsersCount int `json:"usersCount"`
}

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

// List returns a list of roles in the Humio instance.
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)

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

return RolesList, nil
return query.Roles, nil
}

func (r *Roles) Create(role *Role) error {
// Create adds a new role to the Humio instance.
func (r *Roles) Create(role AddRoleInput) error {
var mutation struct {
Role `graphql:"createRole(input: {displayName: $displayName, viewPermissions: $permissions, color: $color, systemPermissions: $systemPermissions, organizationPermissions: $orgPermissions})"`
}

viewPermissions := make([]graphql.String, len(role.ViewPermissions))
for i, permission := range role.ViewPermissions {
viewPermissions[i] = graphql.String(permission)
}

systemPermissions := make([]graphql.String, len(role.SystemPermissions))
for i, permission := range role.SystemPermissions {
systemPermissions[i] = graphql.String(permission)
}

orgPermissions := make([]graphql.String, len(role.OrgPermissions))
for i, permission := range role.OrgPermissions {
orgPermissions[i] = graphql.String(permission)
Results struct {
Role struct {
ID string
}
} `graphql:"createRole(input:$input)"`
}

variables := map[string]interface{}{
"displayName": graphql.String(role.DisplayName),
"color": graphql.String(role.Color),
"description": graphql.String(role.Description),
"viewPermissions": viewPermissions,
"systemPermissions": systemPermissions,
"orgPermissions": orgPermissions,
"input": role,
}

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

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

if newRole == nil {
return fmt.Errorf("new role values must not be nil")
// Upddate updates a role in the Humio instance.
func (r *Roles) Update(update UpdateRoleInput, appendOp bool) error {
if appendOp {
role, err := r.Get(string(update.DisplayName))
if err != nil {
return err
}
for _, viewPerm := range role.ViewPermissions {
if vp, ok := ViewPermission(viewPerm).Get(viewPerm); ok {
update.ViewPermissions = append(update.ViewPermissions, vp)
}
}
for _, sysPerm := range role.SystemPermissions {
if sp, ok := SystemPermission(sysPerm).Get(sysPerm); ok {
*update.SystemPermissions = append(*update.SystemPermissions, sp)
}
}
for _, orgPerm := range role.OrgPermissions {
if op, ok := OrganizationPermission(orgPerm).Get(orgPerm); ok {
*update.OrganizationPermissions = append(*update.OrganizationPermissions, op)
}
}
}

var mutation struct {
Role `graphql:"updateRole(input: {roleId: $roleId, displayName: $displayName, color: $color, description: $description, viewPermissions: $viewPermissions, systemPermissions: $systemPermissions, organizationPermissions: $orgPermissions})"`
}

viewPermissions := make([]graphql.String, len(newRole.ViewPermissions))
for i, permission := range newRole.ViewPermissions {
viewPermissions[i] = graphql.String(permission)
}

systemPermissions := make([]graphql.String, len(newRole.SystemPermissions))
for i, permission := range newRole.SystemPermissions {
systemPermissions[i] = graphql.String(permission)
}

orgPermissions := make([]graphql.String, len(newRole.OrgPermissions))
for i, permission := range newRole.OrgPermissions {
orgPermissions[i] = graphql.String(permission)
Results struct {
Role struct {
ID string
}
} `graphql:"updateRole(input: $input)"`
}

variables := map[string]interface{}{
"roleId": graphql.String(roleId),
"displayName": graphql.String(newRole.DisplayName),
"color": graphql.String(newRole.Color),
"description": graphql.String(newRole.Description),
"viewPermissions": viewPermissions,
"systemPermissions": systemPermissions,
"organizationPermissions": orgPermissions,
"input": update,
}

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

func (r *Roles) RemoveRole(rolename string) error {
// Delete removes a role from the Humio instance.
func (r *Roles) Delete(name string) error {
var mutation struct {
RemoveRole struct {
// We have to make a selection, so just take __typename
Typename graphql.String `graphql:"__typename"`
} `graphql:"removeRole(input: {roleId: $roleId})"`
Result struct {
Result bool
} `graphql:"removeRole(roleId: $roleId)"`
}

role, err := r.client.Roles().Get(rolename)
role, err := r.client.Roles().Get(name)
if err != nil {
return err
}

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

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

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

var query struct {
Role `graphql:"role(roleId: $roleId)"`
Role Role `graphql:"role(roleId: $roleId)"`
}

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

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

return &query.Role, nil
}

func (r *Roles) GetRoleID(rolename string) (string, error) {
// GetRoleID returns the ID of a role given its name.
func (r *Roles) GetRoleID(name string) (string, error) {
roles, err := r.List()
if err != nil {
return "", fmt.Errorf("unable to list roles: %w", err)
}
var roleId string
for _, role := range roles {
if role.DisplayName == rolename {
roleId = role.ID
if role.DisplayName == name {
roleId = string(role.ID)
break
}
}

return roleId, nil
}
Loading