diff --git a/import-export-cli/cmd/mi/add/role.go b/import-export-cli/cmd/mi/add/role.go new file mode 100644 index 000000000..5f21e9f35 --- /dev/null +++ b/import-export-cli/cmd/mi/add/role.go @@ -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) + } +} diff --git a/import-export-cli/cmd/mi/add/user.go b/import-export-cli/cmd/mi/add/user.go index e3b25f401..c98a70c98 100644 --- a/import-export-cli/cmd/mi/add/user.go +++ b/import-export-cli/cmd/mi/add/user.go @@ -23,6 +23,7 @@ import ( "fmt" "os" "syscall" + "strings" "github.com/spf13/cobra" "github.com/wso2/product-apim-tooling/import-export-cli/credentials" @@ -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 { diff --git a/import-export-cli/cmd/mi/delete/role.go b/import-export-cli/cmd/mi/delete/role.go new file mode 100644 index 000000000..4e8587cec --- /dev/null +++ b/import-export-cli/cmd/mi/delete/role.go @@ -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) + } +} diff --git a/import-export-cli/cmd/mi/delete/user.go b/import-export-cli/cmd/mi/delete/user.go index bd7f74a1b..1811fbbec 100644 --- a/import-export-cli/cmd/mi/delete/user.go +++ b/import-export-cli/cmd/mi/delete/user.go @@ -29,6 +29,7 @@ import ( ) var deleteUserCmdEnvironment string +var deleteUserCmdDomain string const deleteUserCmdLiteral = "user [user-name]" const deleteUserCmdShortDesc = "Delete a user from the Micro Integrator" @@ -36,7 +37,9 @@ 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{ @@ -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") } @@ -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 { diff --git a/import-export-cli/cmd/mi/get/roles.go b/import-export-cli/cmd/mi/get/roles.go new file mode 100644 index 000000000..a2c4db782 --- /dev/null +++ b/import-export-cli/cmd/mi/get/roles.go @@ -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) + } +} + + diff --git a/import-export-cli/cmd/mi/get/users.go b/import-export-cli/cmd/mi/get/users.go index b295b469c..e24f44927 100644 --- a/import-export-cli/cmd/mi/get/users.go +++ b/import-export-cli/cmd/mi/get/users.go @@ -33,6 +33,7 @@ var getUserCmdEnvironment string var getUserCmdFormat string var getUserCmdPattern string var getUserCmdRole string +var getUserCmdDomain string const getUserCmdLiteral = "users [user-name]" @@ -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{ @@ -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) { @@ -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 { diff --git a/import-export-cli/cmd/mi/update/user.go b/import-export-cli/cmd/mi/update/user.go new file mode 100644 index 000000000..9c0d05f8b --- /dev/null +++ b/import-export-cli/cmd/mi/update/user.go @@ -0,0 +1,95 @@ +/* +* 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 update + +import ( + "fmt" + "bufio" + "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 updateUserCmdEnvironment string + +const updateUserCmdLiteral = "user [user-name]" +const updateUserCmdShortDesc = "Update roles of a user in a Micro Integrator" + +const updateUserCmdLongDesc = "Update the roles of a user named [user-name] specified by the command line arguments in a Micro Integrator in the environment specified by the flag --environment, -e" + +var updateUserCmdExamples = "To update the roles\n" + + " " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + updateCmdLiteral + " " + miUtils.GetTrimmedCmdLiteral(updateUserCmdLiteral) + " [user-name] -e dev\n" + + "NOTE: The flag (--environment (-e)) is mandatory" + +var updateUserCmd = &cobra.Command{ + Use: updateUserCmdLiteral, + Short: updateUserCmdShortDesc, + Long: updateUserCmdLongDesc, + Example: updateUserCmdExamples, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + handleupdateUserCmdArguments(args) + }, +} + +func init() { + UpdateCmd.AddCommand(updateUserCmd) + updateUserCmd.Flags().StringVarP(&updateUserCmdEnvironment, "environment", "e", "", "Environment of the Micro Integrator of which the user's roles should be updated") + updateUserCmd.MarkFlagRequired("environment") +} + +func handleupdateUserCmdArguments(args []string) { + printUpdateCmdVerboseLog(miUtils.GetTrimmedCmdLiteral(updateUserCmdLiteral)) + credentials.HandleMissingCredentials(updateUserCmdEnvironment) + startConsoleToUpdateUser(args[0]) +} + +func executeUpdateUser(userName, domain string, addedRoles, removedRoles []string) { + resp, err := impl.UpdateMIUser(updateUserCmdEnvironment, userName, domain, addedRoles, removedRoles) + if err != nil { + fmt.Println(utils.LogPrefixError+"updating roles of user [ "+userName+" ] ", err) + } else { + fmt.Println(resp) + } +} + +func startConsoleToUpdateUser(userName string) { + reader := bufio.NewReader(os.Stdin) + + fmt.Printf("Enter user store(domain) for " + userName + " default (primary): ") + domain, _ := reader.ReadString('\n') + domain = strings.TrimSuffix(domain, "\n") + + fmt.Printf("Enter list of new roles to be assigned to " + userName + ": ") + addedRolesStr, _ := reader.ReadString('\n') + addedRolesStr = strings.TrimSuffix(addedRolesStr, "\n") + addedRoles := strings.Fields(addedRolesStr) + + fmt.Printf("Enter list of existing roles to be revoked from " + userName + ": ") + removedRolesStr, _ := reader.ReadString('\n') + removedRolesStr = strings.TrimSuffix(removedRolesStr, "\n") + removedRoles := strings.Fields(removedRolesStr) + + executeUpdateUser(userName,domain,addedRoles,removedRoles) +} diff --git a/import-export-cli/docs/apictl_get_api-logging.md b/import-export-cli/docs/apictl_get_api-logging.md new file mode 100644 index 000000000..9ddf996ef --- /dev/null +++ b/import-export-cli/docs/apictl_get_api-logging.md @@ -0,0 +1,40 @@ +## apictl get api-logging + +Display a list of API loggers in an environment + +### Synopsis + +Display a list of API loggers available for the APIs in the environment specified + +``` +apictl get api-logging [flags] +``` + +### Examples + +``` +apictl get api-logging -e dev --tenant-domain carbon.super +apictl get api-logging --api-id bf36ca3a-0332-49ba-abce-e9992228ae06 -e dev --tenant-domain carbon.super +``` + +### Options + +``` + -i, --api-id string API ID + -e, --environment string Environment of the APIs which the API loggers should be displayed + --format string Pretty-print API loggers using Go Templates. Use "{{ jsonPretty . }}" to list all fields + -h, --help help for api-logging + --tenant-domain string Tenant Domain +``` + +### Options inherited from parent commands + +``` + -k, --insecure Allow connections to SSL endpoints without certs + --verbose Enable verbose mode +``` + +### SEE ALSO + +* [apictl get](apictl_get.md) - Get APIs/APIProducts/Applications or revisions of a specific API/APIProduct in an environment or Get the log level of each API in an environment or Get the environments + diff --git a/import-export-cli/docs/apictl_mi_add.md b/import-export-cli/docs/apictl_mi_add.md index e67e0d542..149a0e498 100644 --- a/import-export-cli/docs/apictl_mi_add.md +++ b/import-export-cli/docs/apictl_mi_add.md @@ -34,5 +34,6 @@ apictl mi add log-level synapse-api org.apache.synapse.rest.API DEBUG -e dev * [apictl mi](apictl_mi.md) - Micro Integrator related commands * [apictl mi add log-level](apictl_mi_add_log-level.md) - Add new Logger to a Micro Integrator +* [apictl mi add role](apictl_mi_add_role.md) - Add new role to a Micro Integrator * [apictl mi add user](apictl_mi_add_user.md) - Add new user to a Micro Integrator diff --git a/import-export-cli/docs/apictl_mi_add_role.md b/import-export-cli/docs/apictl_mi_add_role.md new file mode 100644 index 000000000..ebce3faf9 --- /dev/null +++ b/import-export-cli/docs/apictl_mi_add_role.md @@ -0,0 +1,38 @@ +## apictl mi add role + +Add new role to a Micro Integrator + +### Synopsis + +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 + +``` +apictl mi add role [role-name] [flags] +``` + +### Examples + +``` +To add a new role + apictl mi add role [role-name] -e dev +NOTE: The flag (--environment (-e)) is mandatory +``` + +### Options + +``` + -e, --environment string Environment of the micro integrator to which a new user should be added + -h, --help help for role +``` + +### Options inherited from parent commands + +``` + -k, --insecure Allow connections to SSL endpoints without certs + --verbose Enable verbose mode +``` + +### SEE ALSO + +* [apictl mi add](apictl_mi_add.md) - Add new users or loggers to a Micro Integrator instance + diff --git a/import-export-cli/docs/apictl_mi_delete.md b/import-export-cli/docs/apictl_mi_delete.md index 9c6f34d7c..fe4b88cb4 100644 --- a/import-export-cli/docs/apictl_mi_delete.md +++ b/import-export-cli/docs/apictl_mi_delete.md @@ -32,5 +32,6 @@ apictl mi delete user capp-tester -e dev ### SEE ALSO * [apictl mi](apictl_mi.md) - Micro Integrator related commands +* [apictl mi delete role](apictl_mi_delete_role.md) - Delete a role from the Micro Integrator * [apictl mi delete user](apictl_mi_delete_user.md) - Delete a user from the Micro Integrator diff --git a/import-export-cli/docs/apictl_mi_delete_role.md b/import-export-cli/docs/apictl_mi_delete_role.md new file mode 100644 index 000000000..3d48b166e --- /dev/null +++ b/import-export-cli/docs/apictl_mi_delete_role.md @@ -0,0 +1,41 @@ +## apictl mi delete role + +Delete a role from the Micro Integrator + +### Synopsis + +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 + +``` +apictl mi delete role [role-name] [flags] +``` + +### Examples + +``` +To delete a role + apictl mi delete role [role-name] -e dev +To delete a role in a secondary user store + apictl mi delete role [role-name] -d [domain] -e dev +NOTE: The flag (--environment (-e)) is mandatory +``` + +### Options + +``` + -d, --domain string Select the domain of the role + -e, --environment string Environment of the Micro Integrator from which a role should be deleted + -h, --help help for role +``` + +### Options inherited from parent commands + +``` + -k, --insecure Allow connections to SSL endpoints without certs + --verbose Enable verbose mode +``` + +### SEE ALSO + +* [apictl mi delete](apictl_mi_delete.md) - Delete users from a Micro Integrator instance + diff --git a/import-export-cli/docs/apictl_mi_delete_user.md b/import-export-cli/docs/apictl_mi_delete_user.md index 661bd30f9..6404460cf 100644 --- a/import-export-cli/docs/apictl_mi_delete_user.md +++ b/import-export-cli/docs/apictl_mi_delete_user.md @@ -14,13 +14,16 @@ apictl mi delete user [user-name] [flags] ``` To delete a user - apictl mi delete user capp-tester -e dev + apictl mi delete user [user-id] -e dev +To delete a user in a secondary user store + apictl mi delete user [user-id] -d [domain] -e dev NOTE: The flag (--environment (-e)) is mandatory ``` ### Options ``` + -d, --domain string select user's domain -e, --environment string Environment of the micro integrator from which a user should be deleted -h, --help help for user ``` diff --git a/import-export-cli/docs/apictl_mi_get.md b/import-export-cli/docs/apictl_mi_get.md index 8505401a6..c2f23b90d 100644 --- a/import-export-cli/docs/apictl_mi_get.md +++ b/import-export-cli/docs/apictl_mi_get.md @@ -45,6 +45,7 @@ apictl mi get endpoints -e dev * [apictl mi get message-processors](apictl_mi_get_message-processors.md) - Get information about message processors deployed in a Micro Integrator * [apictl mi get message-stores](apictl_mi_get_message-stores.md) - Get information about message stores deployed in a Micro Integrator * [apictl mi get proxy-services](apictl_mi_get_proxy-services.md) - Get information about proxy services deployed in a Micro Integrator +* [apictl mi get roles](apictl_mi_get_roles.md) - Get information about roles * [apictl mi get sequences](apictl_mi_get_sequences.md) - Get information about sequences deployed in a Micro Integrator * [apictl mi get tasks](apictl_mi_get_tasks.md) - Get information about tasks deployed in a Micro Integrator * [apictl mi get templates](apictl_mi_get_templates.md) - Get information about templates deployed in a Micro Integrator diff --git a/import-export-cli/docs/apictl_mi_get_roles.md b/import-export-cli/docs/apictl_mi_get_roles.md new file mode 100644 index 000000000..a10a4bc9c --- /dev/null +++ b/import-export-cli/docs/apictl_mi_get_roles.md @@ -0,0 +1,45 @@ +## apictl mi get roles + +Get information about roles + +### Synopsis + +Get information about the roles in primary and secondary user stores. +List all roles of the Micro Integrator in the environment specified by the flag --environment, -e + +``` +apictl mi get roles [role-name] [flags] +``` + +### Examples + +``` +To list all the roles + apictl mi get roles -e dev +To get details about a role by providing the role name + apictl mi get roles [role-name] -e dev +To get details about a role in a secondary user store + apictl mi get roles [role-name] -d [domain] -e dev +NOTE: The flag (--environment (-e)) is mandatory +``` + +### Options + +``` + -d, --domain string Filter roles by domain + -e, --environment string Environment to be searched + --format string Pretty-print using Go Templates. Use "{{ jsonPretty . }}" to list all fields + -h, --help help for roles +``` + +### Options inherited from parent commands + +``` + -k, --insecure Allow connections to SSL endpoints without certs + --verbose Enable verbose mode +``` + +### SEE ALSO + +* [apictl mi get](apictl_mi_get.md) - Get information about artifacts deployed in a Micro Integrator instance + diff --git a/import-export-cli/docs/apictl_mi_get_users.md b/import-export-cli/docs/apictl_mi_get_users.md index e1394ca84..628913683 100644 --- a/import-export-cli/docs/apictl_mi_get_users.md +++ b/import-export-cli/docs/apictl_mi_get_users.md @@ -23,12 +23,15 @@ To get the list of users with a username matching with the wild card Ex: "*mi*" apictl mi get users -p [pattern] -e dev To get details about a user by providing the user-id apictl mi get users [user-id] -e dev +To get details about a user in a secondary user store + apictl mi get users [user-id] -d [domain] -e dev NOTE: The flag (--environment (-e)) is mandatory ``` ### Options ``` + -d, --domain string Filter users by domain -e, --environment string Environment to be searched --format string Pretty-print using Go Templates. Use "{{ jsonPretty . }}" to list all fields -h, --help help for users diff --git a/import-export-cli/docs/apictl_mi_update.md b/import-export-cli/docs/apictl_mi_update.md index 0eea8a506..edc2cd256 100644 --- a/import-export-cli/docs/apictl_mi_update.md +++ b/import-export-cli/docs/apictl_mi_update.md @@ -34,4 +34,5 @@ apictl mi update log-level org-apache-coyote DEBUG -e dev * [apictl mi](apictl_mi.md) - Micro Integrator related commands * [apictl mi update hashicorp-secret](apictl_mi_update_hashicorp-secret.md) - Update the secret ID of HashiCorp configuration in a Micro Integrator * [apictl mi update log-level](apictl_mi_update_log-level.md) - Update log level of a Logger in a Micro Integrator +* [apictl mi update user](apictl_mi_update_user.md) - Update roles of a user in a Micro Integrator diff --git a/import-export-cli/docs/apictl_mi_update_user.md b/import-export-cli/docs/apictl_mi_update_user.md new file mode 100644 index 000000000..14001e440 --- /dev/null +++ b/import-export-cli/docs/apictl_mi_update_user.md @@ -0,0 +1,38 @@ +## apictl mi update user + +Update roles of a user in a Micro Integrator + +### Synopsis + +Update the roles of a user named [user-name] specified by the command line arguments in a Micro Integrator in the environment specified by the flag --environment, -e + +``` +apictl mi update user [user-name] [flags] +``` + +### Examples + +``` +To update the roles + apictl mi update user [user-name] -e dev +NOTE: The flag (--environment (-e)) is mandatory +``` + +### Options + +``` + -e, --environment string Environment of the Micro Integrator of which the user's roles should be updated + -h, --help help for user +``` + +### Options inherited from parent commands + +``` + -k, --insecure Allow connections to SSL endpoints without certs + --verbose Enable verbose mode +``` + +### SEE ALSO + +* [apictl mi update](apictl_mi_update.md) - Update log level of Loggers in a Micro Integrator instance + diff --git a/import-export-cli/docs/apictl_set_api-logging.md b/import-export-cli/docs/apictl_set_api-logging.md new file mode 100644 index 000000000..3c28919db --- /dev/null +++ b/import-export-cli/docs/apictl_set_api-logging.md @@ -0,0 +1,40 @@ +## apictl set api-logging + +Set the log level for an API in an environment + +### Synopsis + +Set the log level for an API in the environment specified + +``` +apictl set api-logging [flags] +``` + +### Examples + +``` +apictl set api-logging --api-id bf36ca3a-0332-49ba-abce-e9992228ae06 --log-level full -e dev --tenant-domain carbon.super +apictl set api-logging --api-id bf36ca3a-0332-49ba-abce-e9992228ae06 --log-level off -e dev --tenant-domain carbon.super +``` + +### Options + +``` + -i, --api-id string API ID + -e, --environment string Environment of the API which the log level should be set + -h, --help help for api-logging + --log-level string Log Level + --tenant-domain string Tenant Domain +``` + +### Options inherited from parent commands + +``` + -k, --insecure Allow connections to SSL endpoints without certs + --verbose Enable verbose mode +``` + +### SEE ALSO + +* [apictl set](apictl_set.md) - Set configuration parameters or per API log levels + diff --git a/import-export-cli/mi/impl/common.go b/import-export-cli/mi/impl/common.go index e77efde93..484ff8ffc 100644 --- a/import-export-cli/mi/impl/common.go +++ b/import-export-cli/mi/impl/common.go @@ -165,6 +165,23 @@ func invokeDELETERequestWithRetry(url string, env string) (*resty.Response, erro }) } +func invokeDELETERequestWithRetryAndParams(url, env string, params map[string]string) (*resty.Response, error) { + return retryHTTPCall(miHTTPRetryCount, env, func(accessToken string) (*resty.Response, error) { + headers := make(map[string]string) + headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix + " " + accessToken + return utils.InvokeDELETERequestWithParams(url, params, headers) + }) +} + +func invokePUTRequestWithRetry(env, url string, body interface{}) (*resty.Response, error) { + return retryHTTPCall(miHTTPRetryCount, env, func(accessToken string) (*resty.Response, error) { + headers := make(map[string]string) + headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix + " " + accessToken + headers[utils.HeaderContentType] = utils.HeaderValueApplicationJSON + return utils.InvokePUTRequestWithoutQueryParams(url, headers, body) + }) +} + func unmarshalJSONToStringMap(body []byte) map[string]string { var data map[string]string unmarshalError := json.Unmarshal(body, &data) diff --git a/import-export-cli/mi/impl/constants.go b/import-export-cli/mi/impl/constants.go index f34192ba1..d2a452c29 100644 --- a/import-export-cli/mi/impl/constants.go +++ b/import-export-cli/mi/impl/constants.go @@ -41,3 +41,4 @@ const monthHeader = "MONTH" const yearHeader = "YEAR" const transactionCountHeader = "TRANSACTION COUNT" const userIDHeader = "USER ID" +const roleHeader = "ROLE" diff --git a/import-export-cli/mi/impl/getRoles.go b/import-export-cli/mi/impl/getRoles.go new file mode 100644 index 000000000..e0d9f1c00 --- /dev/null +++ b/import-export-cli/mi/impl/getRoles.go @@ -0,0 +1,102 @@ +/* +* 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 impl + +import ( + "fmt" + "io" + "os" + "strings" + "text/template" + + "github.com/wso2/product-apim-tooling/import-export-cli/formatter" + "github.com/wso2/product-apim-tooling/import-export-cli/mi/utils/artifactutils" + "github.com/wso2/product-apim-tooling/import-export-cli/utils" +) + +const ( + defaultRoleListTableFormat = "table {{.Role}}" + defaultRoleDetailedFormat = "Role Name - {{.Role}}\n" + + "Users - " + + "{{range $index, $user := .Users}}" + + "{{if $index}}, {{end}}" + + "{{$user}}" + + "{{end}}" +) + +// GetRoleList returns a list of roles in the micro integrator in a given environment +func GetRoleList(env string) (*artifactutils.RoleList, error) { + resp, err := callMIManagementEndpointOfResource(utils.MiManagementRoleResource, nil, env, &artifactutils.RoleList{}) + if err != nil { + return nil, err + } + return resp.(*artifactutils.RoleList), nil +} + +// PrintRoleList print a list of mi roles according to the given format +func PrintRoleList(roleList *artifactutils.RoleList, format string) { + if roleList.Count > 0 { + roles := roleList.Roles + roleListContext := getContextWithFormat(format, defaultRoleListTableFormat) + + renderer := func(w io.Writer, t *template.Template) error { + for _, role := range roles { + if err := t.Execute(w, role); err != nil { + return err + } + _, _ = w.Write([]byte{'\n'}) + } + return nil + } + roleListTableHeaders := map[string]string{ + "Role": roleHeader, + } + if err := roleListContext.Write(renderer, roleListTableHeaders); err != nil { + fmt.Println("Error executing template:", err.Error()) + } + } else { + fmt.Println("No roles found") + } +} + +// GetRoleInfo returns a information about a specific role in the micro integrator in a given environment +func GetRoleInfo(env, role, domain string) (*artifactutils.RoleSummary, error) { + var roleInfoResource = utils.MiManagementRoleResource + "/" + role + params := make(map[string]string) + putNonEmptyValueToMap(params, "domain", domain) + resp, err := callMIManagementEndpointOfResource(roleInfoResource, params, env, &artifactutils.RoleSummary{}) + if err != nil { + return nil, err + } + return resp.(*artifactutils.RoleSummary), nil +} + +// PrintRoleDetails prints details about a role according to the given format +func PrintRoleDetails(roleInfo *artifactutils.RoleSummary, format string) { + if format == "" || strings.HasPrefix(format, formatter.TableFormatKey) { + format = defaultRoleDetailedFormat + } + + roleInfoContext := formatter.NewContext(os.Stdout, format) + renderer := getItemRendererEndsWithNewLine(roleInfo) + + if err := roleInfoContext.Write(renderer, nil); err != nil { + fmt.Println("Error executing template:", err.Error()) + } +} diff --git a/import-export-cli/mi/impl/getUsers.go b/import-export-cli/mi/impl/getUsers.go index 03c4b99f7..972c6595f 100644 --- a/import-export-cli/mi/impl/getUsers.go +++ b/import-export-cli/mi/impl/getUsers.go @@ -81,10 +81,11 @@ func PrintUserList(userList *artifactutils.UserList, format string) { } // GetUserInfo returns a information about a specific user in the micro integrator in a given environment -func GetUserInfo(env, userID string) (*artifactutils.UserSummary, error) { +func GetUserInfo(env, userID, domain string) (*artifactutils.UserSummary, error) { var userInfoResource = utils.MiManagementUserResource + "/" + userID - - resp, err := callMIManagementEndpointOfResource(userInfoResource, nil, env, &artifactutils.UserSummary{}) + params := make(map[string]string) + putNonEmptyValueToMap(params, "domain", domain) + resp, err := callMIManagementEndpointOfResource(userInfoResource, params, env, &artifactutils.UserSummary{}) if err != nil { return nil, err } diff --git a/import-export-cli/mi/impl/role.go b/import-export-cli/mi/impl/role.go new file mode 100644 index 000000000..7318129a1 --- /dev/null +++ b/import-export-cli/mi/impl/role.go @@ -0,0 +1,56 @@ +/* +* 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 impl + +import ( + "github.com/wso2/product-apim-tooling/import-export-cli/utils" +) + +type newRoleRequestBody struct { + Role string `json:"role"` + Domain string `json:"domain"` +} + +// AddMIRole adds a new role to the micro integrator in a given environment +func AddMIRole(env, role, domain string) (interface{}, error) { + body := newRoleRequestBody{ + Role: role, + Domain: domain, + } + url := utils.GetMIManagementEndpointOfResource(utils.MiManagementRoleResource, env, utils.MainConfigFilePath) + return addNewMIRole(env, url, body) +} + +// DeleteMIRole deletes a role from a micro integrator in a given environment +func DeleteMIRole(env, roleName, domain string) (interface{}, error) { + url := utils.GetMIManagementEndpointOfResource(utils.MiManagementRoleResource, env, utils.MainConfigFilePath) + "/" + roleName + params := make(map[string]string) + putNonEmptyValueToMap(params, "domain", domain) + return deleteMIRole(url, env, params) +} + +func addNewMIRole(env, url string, body interface{}) (string, error) { + resp, err := invokePOSTRequestWithRetry(env, url, body) + return handleResponse(resp, err, url, "status", "Error") +} + +func deleteMIRole(url, env string, params map[string]string) (string, error) { + resp, err := invokeDELETERequestWithRetryAndParams(url, env, params) + return handleResponse(resp, err, url, "status", "Error") +} diff --git a/import-export-cli/mi/impl/user.go b/import-export-cli/mi/impl/user.go index a376bb308..c768c56ef 100644 --- a/import-export-cli/mi/impl/user.go +++ b/import-export-cli/mi/impl/user.go @@ -28,24 +28,35 @@ type newUserRequestBody struct { UserID string `json:"userId"` Password string `json:"password"` IsAdmin string `json:"isAdmin"` + Domain string `json:"domain"` +} + +type updateUserRolesRequestBody struct { + UserID string `json:"userId"` + Domain string `json:"domain"` + AddedRoles []string `json:"addedRoles"` + RemovedRoles []string `json:"removedRoles"` } // AddMIUser adds a new user to the micro integrator in a given environment -func AddMIUser(env, userName, password, isAdmin string) (interface{}, error) { +func AddMIUser(env, userName, password, isAdmin, domain string) (interface{}, error) { isAdmin = resolveIsAdmin(isAdmin) body := newUserRequestBody{ UserID: userName, Password: password, IsAdmin: isAdmin, + Domain: domain, } url := utils.GetMIManagementEndpointOfResource(utils.MiManagementUserResource, env, utils.MainConfigFilePath) return addNewMIUser(env, url, body) } // DeleteMIUser deletes a user from a micro integrator in a given environment -func DeleteMIUser(env, userName string) (interface{}, error) { +func DeleteMIUser(env, userName, domain string) (interface{}, error) { + params := make(map[string]string) + putNonEmptyValueToMap(params, "domain", domain) url := utils.GetMIManagementEndpointOfResource(utils.MiManagementUserResource, env, utils.MainConfigFilePath) + "/" + userName - return deleteMIUser(url, env) + return deleteMIUser(url, env, params) } func addNewMIUser(env, url string, body interface{}) (string, error) { @@ -53,8 +64,24 @@ func addNewMIUser(env, url string, body interface{}) (string, error) { return handleResponse(resp, err, url, "status", "Error") } -func deleteMIUser(url, env string) (string, error) { - resp, err := invokeDELETERequestWithRetry(url, env) +func deleteMIUser(url, env string, params map[string]string) (string, error) { + resp, err := invokeDELETERequestWithRetryAndParams(url, env, params) + return handleResponse(resp, err, url, "status", "Error") +} + +func UpdateMIUser(env, userName, domain string, addedRoles, removedRoles []string) (interface{}, error) { + body := updateUserRolesRequestBody{ + UserID: userName, + Domain: domain, + AddedRoles: addedRoles, + RemovedRoles: removedRoles, + } + url := utils.GetMIManagementEndpointOfResource(utils.MiManagementRoleResource, env, utils.MainConfigFilePath) + return updateMIUser(env, url, body) +} + +func updateMIUser(env, url string, body interface{}) (string, error) { + resp, err := invokePUTRequestWithRetry(env, url, body) return handleResponse(resp, err, url, "status", "Error") } diff --git a/import-export-cli/mi/utils/artifactutils/roleUtils.go b/import-export-cli/mi/utils/artifactutils/roleUtils.go new file mode 100644 index 000000000..2ab7eaff9 --- /dev/null +++ b/import-export-cli/mi/utils/artifactutils/roleUtils.go @@ -0,0 +1,33 @@ +/* +* 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 artifactutils + +type RoleList struct { + Count int32 `json:"count"` + Roles []Role `json:"list"` +} + +type RoleSummary struct { + Users []string `json:"users"` + Role string `json:"role"` +} + +type Role struct { + Role string `json:"role"` +} diff --git a/import-export-cli/resources/README.html b/import-export-cli/resources/README.html index 3f499610b..a5cd24d4a 100644 --- a/import-export-cli/resources/README.html +++ b/import-export-cli/resources/README.html @@ -791,10 +791,25 @@
Flags:
+ Required:
+ --environment, -e
+ Optional:
+ --domain, -d
+ Examples:
+ apictl mi get roles -e dev
+ apictl mi get roles tester -e dev
+ apictl mi get roles tester -d wso2.com -e dev
Flags:
+ Required:
+ --environment, -e
+ Examples:
+ apictl mi add role tester -e dev
+
+ Flags:
@@ -833,6 +857,15 @@ mi update hashicorp-secret [secret-id]
--environment, -e
Examples:
apictl mi update hashicorp-secret secret_id -e dev
+
+ Flags:
+ Required:
+ --environment, -e
+ Examples:
+ apictl mi update user capp-tester -e dev
Flags:
+ Required:
+ --environment, -e
+ Optional:
+ --domain, -d
+ Examples:
+ apictl mi delete role capp-tester -e dev
+ apictl mi delete role capp-tester -d wso2.com -e dev
+
+