-
Notifications
You must be signed in to change notification settings - Fork 132
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 #880 from GDLMadushanka/patch
Add role support to MI CLI
- Loading branch information
Showing
30 changed files
with
1,112 additions
and
18 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,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) | ||
} | ||
} |
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,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) | ||
} | ||
} |
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,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) | ||
} | ||
} | ||
|
||
|
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
Oops, something went wrong.