diff --git a/api/users.go b/api/users.go index 9d69fbd6..15ff924b 100644 --- a/api/users.go +++ b/api/users.go @@ -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"` diff --git a/go.mod b/go.mod index b1d9ea3f..f02f159e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ef02dc7d..6a3c9a43 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/cmd/users/change/change.go b/pkg/cmd/users/change/change.go new file mode 100644 index 00000000..b79dce84 --- /dev/null +++ b/pkg/cmd/users/change/change.go @@ -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) + } +} diff --git a/pkg/cmd/users/create/create.go b/pkg/cmd/users/create/create.go index 59f38f98..d47fe217 100644 --- a/pkg/cmd/users/create/create.go +++ b/pkg/cmd/users/create/create.go @@ -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) diff --git a/pkg/cmd/users/delete/delete.go b/pkg/cmd/users/delete/delete.go new file mode 100644 index 00000000..663cefc3 --- /dev/null +++ b/pkg/cmd/users/delete/delete.go @@ -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.") + } +} diff --git a/pkg/cmd/users/disable/disable.go b/pkg/cmd/users/disable/disable.go new file mode 100644 index 00000000..dcf7f345 --- /dev/null +++ b/pkg/cmd/users/disable/disable.go @@ -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) { + +} diff --git a/pkg/cmd/users/users.go b/pkg/cmd/users/users.go index 13d1e9a3..346c2225 100644 --- a/pkg/cmd/users/users.go +++ b/pkg/cmd/users/users.go @@ -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" @@ -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())