Skip to content

Commit

Permalink
Users api functionality (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
wizedkyle authored Nov 21, 2020
1 parent 2215b9d commit 9e1c7bd
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 1 deletion.
4 changes: 4 additions & 0 deletions api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ type CreateUserRequest struct {
Roleids []string `json:"roleIds"`
}

type UpdateUserEmail struct {
Email string `json:"email"`
}

type UserResponse struct {
Firstname string `json:"firstName"`
Lastname string `json:"lastName"`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/wizedkyle/sumocli
go 1.15

require (
github.com/jarcoal/httpmock v1.0.6
github.com/manifoldco/promptui v0.8.0
github.com/rs/zerolog v1.20.0
github.com/spf13/cobra v1.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jarcoal/httpmock v1.0.6 h1:e81vOSexXU3mJuJ4l//geOmKIt+Vkxerk1feQBC8D0g=
github.com/jarcoal/httpmock v1.0.6/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand Down
71 changes: 71 additions & 0 deletions pkg/cmd/users/change/change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package change

import (
"encoding/json"
"fmt"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/wizedkyle/sumocli/api"
"github.com/wizedkyle/sumocli/pkg/cmd/factory"
"github.com/wizedkyle/sumocli/pkg/logging"
"io/ioutil"
"os"
)

func NewCmdUserChangeEmail() *cobra.Command {
var (
id string
email string
)

cmd := &cobra.Command{
Use: "change email",
Short: "Changes the email address of a Sumo Logic user.",
Run: func(cmd *cobra.Command, args []string) {
logger := logging.GetLoggerForCommand(cmd)
logger.Debug().Msg("User change email request started.")
userChangeEmail(id, email, logger)
logger.Debug().Msg("User change email request finished.")
},
}

cmd.Flags().StringVar(&id, "id", "", "Specify the id of the user that needs to have the email changed.")
cmd.Flags().StringVar(&email, "email", "", "Specify the users new email address.")

return cmd
}

func userChangeEmail(id string, email string, logger zerolog.Logger) {
if id == "" {
fmt.Println("--id field needs to be set.")
os.Exit(0)
}
if email == "" {
fmt.Println("--email field needs to be set.")
os.Exit(0)
}

requestBodySchema := &api.UpdateUserEmail{
Email: email,
}
requestBody, _ := json.Marshal(requestBodySchema)
requestUrl := "v1/users/" + id + "/email/requestChange"
client, request := factory.NewHttpRequestWithBody("POST", requestUrl, requestBody)
response, err := client.Do(request)
logging.LogError(err, logger)

defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
logging.LogError(err, logger)

if response.StatusCode != 204 {
var responseError api.ResponseError
jsonErr := json.Unmarshal(responseBody, &responseError)
logging.LogError(jsonErr, logger)
if responseError.Errors[0].Code == "um1:unverified_email" {
fmt.Println(responseError.Errors[0].Message)
}
} else {
fmt.Println("Users email address successfully updated to: " + email)
}
}
1 change: 0 additions & 1 deletion pkg/cmd/users/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func user(firstName string, lastName string, emailAddress string, roleIds []stri
Roleids: roleIds,
}
requestBody, _ := json.Marshal(requestBodySchema)
fmt.Println(string(requestBody))
client, request := factory.NewHttpRequestWithBody("POST", "v1/users", requestBody)
response, err := client.Do(request)
logging.LogError(err, logger)
Expand Down
60 changes: 60 additions & 0 deletions pkg/cmd/users/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package delete

import (
"encoding/json"
"fmt"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/wizedkyle/sumocli/api"
"github.com/wizedkyle/sumocli/pkg/cmd/factory"
"github.com/wizedkyle/sumocli/pkg/logging"
"io/ioutil"
"os"
)

func NewCmdUserDelete() *cobra.Command {
var (
id string
transferTo string
)

cmd := &cobra.Command{
Use: "delete",
Short: "Deletes a Sumo Logic user",
Run: func(cmd *cobra.Command, args []string) {
logger := logging.GetLoggerForCommand(cmd)
logger.Debug().Msg("User delete request started.")
deleteUser(id, transferTo, logger)
logger.Debug().Msg("User delete request finished.")
},
}

cmd.Flags().StringVar(&id, "id", "", "Specify the id of the user to delete.")
cmd.Flags().StringVar(&transferTo, "transferto", "", "Specify the id of the user to transfer data to.")

return cmd
}

func deleteUser(id string, transferTo string, logger zerolog.Logger) {
if id == "" {
fmt.Println("--id field needs to be set.")
os.Exit(0)
}

requestUrl := "v1/users/" + id
client, request := factory.NewHttpRequest("DELETE", requestUrl)
response, err := client.Do(request)
logging.LogError(err, logger)

defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
logging.LogError(err, logger)

if response.StatusCode != 204 {
var responseError api.ResponseError
jsonErr := json.Unmarshal(responseBody, &responseError)
logging.LogError(jsonErr, logger)
} else {
fmt.Println("User was deleted.")
}
}
32 changes: 32 additions & 0 deletions pkg/cmd/users/disable/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package disable

import (
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/wizedkyle/sumocli/pkg/logging"
)

func NewCmdUserDisableMFA() *cobra.Command {
var (
id string
email string
password string
)

cmd := &cobra.Command{
Use: "disable mfa",
Short: "Disables MFA for a Sumo Logic user.",
Run: func(cmd *cobra.Command, args []string) {
logger := logging.GetLoggerForCommand(cmd)
logger.Debug().Msg("User disable mfa request started.")
userDisableMFA(id, email, password, logger)
logger.Debug().Msg("User disable mfa request finished.")
},
}

return cmd
}

func userDisableMFA(id string, email string, password string, logger zerolog.Logger) {

}
4 changes: 4 additions & 0 deletions pkg/cmd/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package users

import (
"github.com/spf13/cobra"
cmdUserChange "github.com/wizedkyle/sumocli/pkg/cmd/users/change"
cmdUserCreate "github.com/wizedkyle/sumocli/pkg/cmd/users/create"
cmdUserDelete "github.com/wizedkyle/sumocli/pkg/cmd/users/delete"
cmdUserGet "github.com/wizedkyle/sumocli/pkg/cmd/users/get"
cmdUserList "github.com/wizedkyle/sumocli/pkg/cmd/users/list"
cmdUserUnlock "github.com/wizedkyle/sumocli/pkg/cmd/users/unlock"
Expand All @@ -15,7 +17,9 @@ func NewCmdUser() *cobra.Command {
Long: "Work with Sumo Logic users",
}

cmd.AddCommand(cmdUserChange.NewCmdUserChangeEmail())
cmd.AddCommand(cmdUserCreate.NewCmdUserCreate())
cmd.AddCommand(cmdUserDelete.NewCmdUserDelete())
cmd.AddCommand(cmdUserGet.NewCmdGetUser())
cmd.AddCommand(cmdUserList.NewCmdUserList())
cmd.AddCommand(cmdUserUnlock.NewCmdUnlockUser())
Expand Down

0 comments on commit 9e1c7bd

Please sign in to comment.