Skip to content

Commit

Permalink
Merge pull request #880 from GDLMadushanka/patch
Browse files Browse the repository at this point in the history
Add role support to MI CLI
  • Loading branch information
GDLMadushanka authored Feb 23, 2022
2 parents 9e4fd83 + 29bbcfa commit cf22bfa
Show file tree
Hide file tree
Showing 30 changed files with 1,112 additions and 18 deletions.
85 changes: 85 additions & 0 deletions import-export-cli/cmd/mi/add/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 add

import (
"bufio"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
impl "github.com/wso2/product-apim-tooling/import-export-cli/mi/impl"
miUtils "github.com/wso2/product-apim-tooling/import-export-cli/mi/utils"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)

var addRoleCmdEnvironment string

const addRoleCmdLiteral = "role [role-name]"
const addRoleCmdShortDesc = "Add new role to a Micro Integrator"

const addRoleCmdLongDesc = "Add a new role with the name specified by the command line argument [role-name] to a Micro Integrator in the environment specified by the flag --environment, -e"

var addRoleCmdExamples = "To add a new role\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + addCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(addRoleCmdLiteral) + " [role-name] -e dev\n" +
"NOTE: The flag (--environment (-e)) is mandatory"

var addRoleCmd = &cobra.Command{
Use: addRoleCmdLiteral,
Short: addRoleCmdShortDesc,
Long: addRoleCmdLongDesc,
Example: addRoleCmdExamples,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
handleAddRoleCmdArguments(args)
},
}

func init() {
AddCmd.AddCommand(addRoleCmd)
addRoleCmd.Flags().StringVarP(&addRoleCmdEnvironment, "environment", "e", "", "Environment of the micro integrator to which a new user should be added")
addRoleCmd.MarkFlagRequired("environment")
}

func handleAddRoleCmdArguments(args []string) {
printAddCmdVerboseLog(miUtils.GetTrimmedCmdLiteral(addRoleCmdLiteral))
credentials.HandleMissingCredentials(addRoleCmdEnvironment)
startConsoleToAddRole(args[0])
}

func startConsoleToAddRole(roleName string) {
reader := bufio.NewReader(os.Stdin)

fmt.Printf("Enter user store (domain) for " + roleName + " default (primary): ")
domain, _ := reader.ReadString('\n')
domain = strings.TrimSuffix(domain, "\n")

executeAddNewRole(roleName, domain)
}

func executeAddNewRole(roleName, domain string) {
resp, err := impl.AddMIRole(addRoleCmdEnvironment, roleName, domain)
if err != nil {
fmt.Println(utils.LogPrefixError+"Adding new role [ "+roleName+" ]", err)
} else {
fmt.Println("Adding new role [ "+roleName+" ] status:", resp)
}
}
11 changes: 8 additions & 3 deletions import-export-cli/cmd/mi/add/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"syscall"
"strings"

"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
Expand Down Expand Up @@ -82,15 +83,19 @@ func startConsoleToAddUser(userName string) {
userConfirmPassword := string(byteUserConfirmationPassword)
fmt.Println()

fmt.Printf("Enter user store for " + userName + " default (primary): ")
domain, _ := reader.ReadString('\n')
domain = strings.TrimSuffix(domain, "\n")

if userConfirmPassword == userPassword {
executeAddNewUser(userName, userPassword, isAdmin)
executeAddNewUser(userName, userPassword, isAdmin, domain)
} else {
fmt.Println("Passwords are not matching.")
}
}

func executeAddNewUser(userName, userPassword, isAdmin string) {
resp, err := impl.AddMIUser(addUserCmdEnvironment, userName, userPassword, isAdmin)
func executeAddNewUser(userName, userPassword, isAdmin, domain string) {
resp, err := impl.AddMIUser(addUserCmdEnvironment, userName, userPassword, isAdmin, domain)
if err != nil {
fmt.Println(utils.LogPrefixError+"Adding new user [ "+userName+" ]", err)
} else {
Expand Down
76 changes: 76 additions & 0 deletions import-export-cli/cmd/mi/delete/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 delete

import (
"fmt"

"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
"github.com/wso2/product-apim-tooling/import-export-cli/mi/impl"
miUtils "github.com/wso2/product-apim-tooling/import-export-cli/mi/utils"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)

var deleteRoleCmdEnvironment string
var deleteRoleCmdDomain string

const deleteRoleCmdLiteral = "role [role-name]"
const deleteRoleCmdShortDesc = "Delete a role from the Micro Integrator"

const deleteRoleCmdLongDesc = "Delete a role with the name specified by the command line argument [role-name] from a Micro Integrator in the environment specified by the flag --environment, -e"

var deleteRoleCmdExamples = "To delete a role\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + deleteCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(deleteRoleCmdLiteral) + " [role-name] -e dev\n" +
"To delete a role in a secondary user store\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + deleteCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(deleteRoleCmdLiteral) + " [role-name] -d [domain] -e dev\n" +
"NOTE: The flag (--environment (-e)) is mandatory"

var deleteRoleCmd = &cobra.Command{
Use: deleteRoleCmdLiteral,
Short: deleteRoleCmdShortDesc,
Long: deleteRoleCmdLongDesc,
Example: deleteRoleCmdExamples,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
handledeleteRoleCmdArguments(args)
},
}

func init() {
DeleteCmd.AddCommand(deleteRoleCmd)
deleteRoleCmd.Flags().StringVarP(&deleteRoleCmdDomain, "domain", "d", "", "Select the domain of the role")
deleteRoleCmd.Flags().StringVarP(&deleteRoleCmdEnvironment, "environment", "e", "", "Environment of the Micro Integrator from which a role should be deleted")
deleteRoleCmd.MarkFlagRequired("environment")
}

func handledeleteRoleCmdArguments(args []string) {
printDeleteCmdVerboseLog(miUtils.GetTrimmedCmdLiteral(deleteRoleCmdLiteral))
credentials.HandleMissingCredentials(deleteRoleCmdEnvironment)
executeDeleteRole(args[0])
}

func executeDeleteRole(roleName string) {
resp, err := impl.DeleteMIRole(deleteRoleCmdEnvironment, roleName, deleteRoleCmdDomain)
if err != nil {
fmt.Println(utils.LogPrefixError + "deleting role [ "+roleName+" ]", err)
} else {
fmt.Println("Deleting role [ "+roleName+" ] status:", resp)
}
}
8 changes: 6 additions & 2 deletions import-export-cli/cmd/mi/delete/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ import (
)

var deleteUserCmdEnvironment string
var deleteUserCmdDomain string

const deleteUserCmdLiteral = "user [user-name]"
const deleteUserCmdShortDesc = "Delete a user from the Micro Integrator"

const deleteUserCmdLongDesc = "Delete a user with the name specified by the command line argument [user-name] from a Micro Integrator in the environment specified by the flag --environment, -e"

var deleteUserCmdExamples = "To delete a user\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + deleteCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(deleteUserCmdLiteral) + " capp-tester -e dev\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + deleteCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(deleteUserCmdLiteral) + " [user-id] -e dev\n" +
"To delete a user in a secondary user store\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + deleteCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(deleteUserCmdLiteral) + " [user-id] -d [domain] -e dev\n" +
"NOTE: The flag (--environment (-e)) is mandatory"

var deleteUserCmd = &cobra.Command{
Expand All @@ -52,6 +55,7 @@ var deleteUserCmd = &cobra.Command{

func init() {
DeleteCmd.AddCommand(deleteUserCmd)
deleteUserCmd.Flags().StringVarP(&deleteUserCmdDomain, "domain", "d", "", "select user's domain")
deleteUserCmd.Flags().StringVarP(&deleteUserCmdEnvironment, "environment", "e", "", "Environment of the micro integrator from which a user should be deleted")
deleteUserCmd.MarkFlagRequired("environment")
}
Expand All @@ -63,7 +67,7 @@ func handledeleteUserCmdArguments(args []string) {
}

func executeDeleteUser(userName string) {
resp, err := impl.DeleteMIUser(deleteUserCmdEnvironment, userName)
resp, err := impl.DeleteMIUser(deleteUserCmdEnvironment, userName, deleteUserCmdDomain)
if err != nil {
fmt.Println(utils.LogPrefixError+"deleting user [ "+userName+" ]", err)
} else {
Expand Down
103 changes: 103 additions & 0 deletions import-export-cli/cmd/mi/get/roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 get

import (
"errors"
"fmt"

"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
impl "github.com/wso2/product-apim-tooling/import-export-cli/mi/impl"
miUtils "github.com/wso2/product-apim-tooling/import-export-cli/mi/utils"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)

var getRoleCmdEnvironment string
var getRoleCmdFormat string
var getRoleCmdDomain string

const getRoleCmdLiteral = "roles [role-name]"

const getRoleCmdShortDesc = "Get information about roles"
const getRoleCmdLongDesc = "Get information about the roles in primary and secondary user stores.\n" +
"List all roles of the Micro Integrator in the environment specified by the flag --environment, -e"

var getRoleCmdExamples = "To list all the roles\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + GetCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(getRoleCmdLiteral) + " -e dev\n" +
"To get details about a role by providing the role name\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + GetCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(getRoleCmdLiteral) + " [role-name] -e dev\n" +
"To get details about a role in a secondary user store\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + GetCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(getRoleCmdLiteral) + " [role-name] -d [domain] -e dev\n" +
"NOTE: The flag (--environment (-e)) is mandatory"

var getRoleCmd = &cobra.Command{
Use: getRoleCmdLiteral,
Short: getRoleCmdShortDesc,
Long: getRoleCmdLongDesc,
Example: getRoleCmdExamples,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
var errMessage = "accepts at most 1 arg(s), received " + fmt.Sprint(len(args))
return errors.New(errMessage)
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
handleGetRoleCmdArguments(args)
},
}

func init() {
GetCmd.AddCommand(getRoleCmd)
setEnvFlag(getRoleCmd, &getRoleCmdEnvironment)
setFormatFlag(getRoleCmd, &getRoleCmdFormat)
getRoleCmd.Flags().StringVarP(&getRoleCmdDomain, "domain", "d", "", "Filter roles by domain")
}

func handleGetRoleCmdArguments(args []string) {
printGetCmdVerboseLogForArtifact(miUtils.GetTrimmedCmdLiteral(getRoleCmdLiteral))
credentials.HandleMissingCredentials(getRoleCmdEnvironment)
if len(args) == 1 {
var role = args[0]
executeShowRole(role)
} else {
executeListRoles()
}
}

func executeShowRole(role string) {
roleInfo, err := impl.GetRoleInfo(getRoleCmdEnvironment, role, getRoleCmdDomain)
if err == nil {
impl.PrintRoleDetails(roleInfo, getRoleCmdFormat)
} else {
printErrorForArtifact("roles", role, err)
}
}

func executeListRoles() {
roleList, err := impl.GetRoleList(getRoleCmdEnvironment)
if err == nil {
impl.PrintRoleList(roleList, getRoleCmdFormat)
} else {
printErrorForArtifactList("roles", err)
}
}


6 changes: 5 additions & 1 deletion import-export-cli/cmd/mi/get/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var getUserCmdEnvironment string
var getUserCmdFormat string
var getUserCmdPattern string
var getUserCmdRole string
var getUserCmdDomain string

const getUserCmdLiteral = "users [user-name]"

Expand All @@ -49,6 +50,8 @@ var getUserCmdExamples = "Example:\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + GetCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(getUserCmdLiteral) + " -p [pattern] -e dev\n" +
"To get details about a user by providing the user-id\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + GetCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(getUserCmdLiteral) + " [user-id] -e dev\n" +
"To get details about a user in a secondary user store\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + GetCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(getUserCmdLiteral) + " [user-id] -d [domain] -e dev\n" +
"NOTE: The flag (--environment (-e)) is mandatory"

var getUserCmd = &cobra.Command{
Expand Down Expand Up @@ -79,6 +82,7 @@ func init() {
setFormatFlag(getUserCmd, &getUserCmdFormat)
getUserCmd.Flags().StringVarP(&getUserCmdRole, "role", "r", "", "Filter users by role")
getUserCmd.Flags().StringVarP(&getUserCmdPattern, "pattern", "p", "", "Filter users by regex")
getUserCmd.Flags().StringVarP(&getUserCmdDomain, "domain", "d", "", "Filter users by domain")
}

func handleGetUserCmdArguments(args []string) {
Expand All @@ -93,7 +97,7 @@ func handleGetUserCmdArguments(args []string) {
}

func executeShowUser(userID string) {
userInfo, err := impl.GetUserInfo(getUserCmdEnvironment, userID)
userInfo, err := impl.GetUserInfo(getUserCmdEnvironment, userID, getUserCmdDomain)
if err == nil {
impl.PrintUserDetails(userInfo, getUserCmdFormat)
} else {
Expand Down
Loading

0 comments on commit cf22bfa

Please sign in to comment.