This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from magneticio/addnewuser
Add new user
- Loading branch information
Showing
5 changed files
with
214 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright © 2018 Developer [email protected] | ||
// | ||
// 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 cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/magneticio/vampkubistcli/client" | ||
"github.com/magneticio/vampkubistcli/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// addCmd represents the add command | ||
var addCmd = &cobra.Command{ | ||
Use: "add", | ||
Short: "Add a new user", | ||
Long: AddAppName(`Add a new user: | ||
$AppName add username | ||
This will print command to login for a new user. | ||
`), | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 2 { | ||
return errors.New("Not Enough Arguments") | ||
} | ||
Type = args[0] | ||
Name = args[1] | ||
|
||
if Type == "user" { | ||
Username := strings.ToLower(Name) | ||
// TODO: this is a temporary workaround it will be handled in the backend | ||
temporarayPassword := util.RandomString(50) | ||
SourceFileType := "json" | ||
Source := "{\"userName\":\"" + Username + "\",\"password\":\"" + temporarayPassword + "\"}" | ||
restClient := client.NewRestClient(Config.Url, Config.Token, Debug, Config.Cert) | ||
values := make(map[string]string) | ||
values["project"] = Config.Project | ||
values["cluster"] = Config.Cluster | ||
values["virtual_cluster"] = Config.VirtualCluster | ||
values["application"] = Application | ||
isCreated, createError := restClient.Create(Type, Name, Source, SourceFileType, values) | ||
if !isCreated { | ||
return createError | ||
} | ||
fmt.Printf("User created.\n") | ||
token, loginError := restClient.Login(Username, temporarayPassword) | ||
if loginError != nil { | ||
return loginError | ||
} | ||
fmt.Printf("User can login with:\n") | ||
fmt.Printf("vamp login --url %v --user %v --initial --token %v --cert <<EOF \"%v\"\nEOF\n", Config.Url, Username, token, Config.Cert) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(addCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// addCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright © 2018 Developer [email protected] | ||
// | ||
// 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 cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/magneticio/vampkubistcli/client" | ||
"github.com/magneticio/vampkubistcli/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// passwdCmd represents the passwd command | ||
var passwdCmd = &cobra.Command{ | ||
Use: "passwd", | ||
Short: "Update password", | ||
Long: AddAppName(`Update password | ||
For the current user: | ||
$AppName | ||
For a different user | ||
$AppName --user username`), | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
Type := "user" | ||
passwd, passwdError := util.GetParameterFromTerminalAsSecret( | ||
"Enter your password (password will not be visible):", | ||
"Enter your password again (password will not be visible):", | ||
"Passwords do not match.") | ||
if passwdError != nil { | ||
return passwdError | ||
} | ||
SourceFileType := "json" | ||
if Username == "" { | ||
Username = Config.Username | ||
} | ||
Source := "{\"userName\":\"" + Username + "\",\"password\":\"" + passwd + "\"}" | ||
restClient := client.NewRestClient(Config.Url, Config.Token, Debug, Config.Cert) | ||
values := make(map[string]string) | ||
values["project"] = Config.Project | ||
values["cluster"] = Config.Cluster | ||
values["virtual_cluster"] = Config.VirtualCluster | ||
values["application"] = Application | ||
_, updateError := restClient.Update(Type, Username, Source, SourceFileType, values) | ||
if updateError != nil { | ||
return updateError | ||
} | ||
fmt.Printf("User password updated.\nLogin with the new password.\n") | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(passwdCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// passwdCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// passwdCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
passwdCmd.Flags().StringVarP(&Username, "user", "", "", "Username of the user to change password") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters