diff --git a/client/api/group.go b/client/api/group.go index 70d588a..5243954 100644 --- a/client/api/group.go +++ b/client/api/group.go @@ -39,7 +39,7 @@ func (c *ClientAPI) GetGroupsByOrg(organizationId, pathPrefix, offset, limit, or } func (c *ClientAPI) CreateGroup(organizationId, groupName, path string) (string, error) { - body := map[string]string{ + body := map[string]interface{}{ "name": groupName, "path": path, } @@ -51,7 +51,7 @@ func (c *ClientAPI) CreateGroup(organizationId, groupName, path string) (string, } func (c *ClientAPI) UpdateGroup(organizationId, groupName, newName, newPath string) (string, error) { - body := map[string]string{ + body := map[string]interface{}{ "name": newName, "path": newPath, } @@ -72,9 +72,9 @@ func (c *ClientAPI) DeleteGroup(organizationId, groupName string) (string, error func (c *ClientAPI) GetGroupPolicies(organizationId, groupName, offset, limit, orderBy string) (string, error) { urlParams := map[string]string{ - "Offset": offset, - "Limit": limit, - "OrderBy": orderBy, + "Offset": offset, + "Limit": limit, + "OrderBy": orderBy, } req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName+"/policies", nil, urlParams) if err != nil { diff --git a/client/api/manager.go b/client/api/manager.go index 7b24e95..39d238d 100644 --- a/client/api/manager.go +++ b/client/api/manager.go @@ -11,6 +11,16 @@ type UserAPI interface { DeleteUser(externalId string) (string, error) } +type PolicyAPI interface { + GetPolicy(organizationId, policyName string) (string, error) + GetAllPolicies(pathPrefix, offset, limit, orderBy string) (string, error) + CreatePolicy(organizationId, policyName, path, statement string) (string, error) + UpdatePolicy(organizationId, policyName, path, statement string) (string, error) + DeletePolicy(organizationId, policyName string) (string, error) + GetGroupsAttached(organizationId, policyName, offset, limit, orderBy string) (string, error) + GetPoliciesOrganization(organizationId, pathPrefix, offset, limit, orderBy string) (string, error) +} + type GroupAPI interface { GetGroup(organizationId, groupName string) (string, error) GetAllGroups(pathPrefix, offset, limit, orderBy string) (string, error) diff --git a/client/api/policy.go b/client/api/policy.go index cb2be78..12cf82c 100644 --- a/client/api/policy.go +++ b/client/api/policy.go @@ -1,6 +1,11 @@ package api -import internalhttp "github.com/Tecsisa/foulkon/http" +import ( + "encoding/json" + + "github.com/Tecsisa/foulkon/api" + internalhttp "github.com/Tecsisa/foulkon/http" +) func (c *ClientAPI) GetPolicy(organizationId, policyName string) (string, error) { req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies/"+policyName, nil, nil) @@ -10,7 +15,7 @@ func (c *ClientAPI) GetPolicy(organizationId, policyName string) (string, error) return c.makeRequest(req) } -func (c *ClientAPI) GetAllPolicy(pathPrefix, offset, limit, orderBy string) (string, error) { +func (c *ClientAPI) GetAllPolicies(pathPrefix, offset, limit, orderBy string) (string, error) { urlParams := map[string]string{ "PathPrefix": pathPrefix, "Offset": offset, @@ -24,15 +29,18 @@ func (c *ClientAPI) GetAllPolicy(pathPrefix, offset, limit, orderBy string) (str return c.makeRequest(req) } -func (c *ClientAPI) CreatePolicy(organizationId, policyName, path, effect, actions, resources string) (string, error) { - body := map[string]string{ +func (c *ClientAPI) CreatePolicy(organizationId, policyName, path, statement string) (string, error) { + + statementApi := []api.Statement{} + if err := json.Unmarshal([]byte(statement), &statementApi); err != nil { + panic(err) + } + body := map[string]interface{}{ "name": policyName, "path": path, - "Statements": "\"Statements\" : [ { \"Effect\" : \"allow\", \"Actions\" : [\"iam:*\"], \"Resources\" : [\"urn:everything:*\"] } ]", - //"effect": effect, - //"actions": actions, - //"resources": resources, + "Statements": statementApi, } + req, err := c.prepareRequest("POST", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies", body, nil) if err != nil { return "", err @@ -40,13 +48,23 @@ func (c *ClientAPI) CreatePolicy(organizationId, policyName, path, effect, actio return c.makeRequest(req) } -//func (c *ClientAPI) UpdatePolicy(organizationId, policyName string) (string, error) { -// req, err := c.prepareRequest("PUT", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies/"+policyName, nil, nil) -// if err != nil { -// return "", err -// } -// return c.makeRequest(req) -//} +func (c *ClientAPI) UpdatePolicy(organizationId, policyName, path, statement string) (string, error) { + statementApi := []api.Statement{} + if err := json.Unmarshal([]byte(statement), &statementApi); err != nil { + panic(err) + } + body := map[string]interface{}{ + "name": policyName, + "path": path, + "Statements": statementApi, + } + + req, err := c.prepareRequest("PUT", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies/"+policyName, body, nil) + if err != nil { + return "", err + } + return c.makeRequest(req) +} func (c *ClientAPI) DeletePolicy(organizationId, policyName string) (string, error) { req, err := c.prepareRequest("DELETE", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies/"+policyName, nil, nil) @@ -56,7 +74,7 @@ func (c *ClientAPI) DeletePolicy(organizationId, policyName string) (string, err return c.makeRequest(req) } -func (c *ClientAPI) GetGroupsPolicy(organizationId, policyName, offset, limit, orderBy string) (string, error) { +func (c *ClientAPI) GetGroupsAttached(organizationId, policyName, offset, limit, orderBy string) (string, error) { urlParams := map[string]string{ "Offset": offset, "Limit": limit, diff --git a/client/api/user.go b/client/api/user.go index 142fabc..526fe78 100644 --- a/client/api/user.go +++ b/client/api/user.go @@ -38,7 +38,7 @@ func (c *ClientAPI) GetUserGroups(externalId, offset, limit, orderBy string) (st } func (c *ClientAPI) CreateUser(externalId, path string) (string, error) { - body := map[string]string{ + body := map[string]interface{}{ "externalId": externalId, "path": path, } @@ -51,7 +51,7 @@ func (c *ClientAPI) CreateUser(externalId, path string) (string, error) { } func (c *ClientAPI) UpdateUser(externalId, path string) (string, error) { - body := map[string]string{ + body := map[string]interface{}{ "path": path, } diff --git a/client/api/util.go b/client/api/util.go index 3adbf3d..b44cd34 100644 --- a/client/api/util.go +++ b/client/api/util.go @@ -9,7 +9,7 @@ import ( ) // Helper func for updating request params -func (c *ClientAPI) prepareRequest(method, url string, postContent, queryParams map[string]string) (*http.Request, error) { +func (c *ClientAPI) prepareRequest(method, url string, postContent map[string]interface{}, queryParams map[string]string) (*http.Request, error) { url = c.Address + url // insert post content to body var body *bytes.Buffer diff --git a/client/cli/main.go b/client/cli/main.go index 52c6a74..8237d56 100644 --- a/client/cli/main.go +++ b/client/cli/main.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "os" - "strings" "github.com/Tecsisa/foulkon/client/api" @@ -22,20 +21,61 @@ const ( FLAG_LIMIT = "limit" FLAG_ORDERBY = "orderBy" FLAG_PATHPREFIX = "pathPrefix" - FLAG_PATH = "path" FLAG_NEWPATH = "newPath" - FLAG_EFFECT = "effect" - FLAG_ACTIONS = "actions" - FLAG_RESOURCES = "resources" + FLAG_PATH = "path" + FLAG_STATEMENT = "statement" ) type Cli struct { - UserApi api.UserAPI - GroupApi api.GroupAPI + UserApi api.UserAPI + GroupApi api.GroupAPI + PolicyApi api.PolicyAPI +} + +// Helper func for updating request params +func parseFlags(availableFlags map[string]string, validFlags, cliArgs []string, requireFlags int) map[string]string { + params := make(map[string]string) + + flagSet := flag.NewFlagSet(cliArgs[0]+" "+cliArgs[1], flag.ExitOnError) + + for _, val := range validFlags { + flagSet.String(val, "", availableFlags[val]) + } + + if err := flagSet.Parse(cliArgs[2:]); err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + os.Exit(1) + } + + for i, v := range validFlags { + val := flagSet.Lookup(v).Value.String() + if i < requireFlags && val == "" { + return nil + } + params[v] = val + } + + return params } func main() { + availableFlags := map[string]string{ + FLAG_OFFSET: "Offset of returned items", + FLAG_EXTERNALID: "User's external identifier", + FLAG_ORGANIZATIONID: "Policy's organization", + FLAG_POLICYNAME: "Policy's name", + FLAG_LIMIT: "Maximum number of items in response", + FLAG_ORDERBY: "Sort the result by specified column", + FLAG_PATHPREFIX: "Search starts from this path", + FLAG_PATH: "--- location", + FLAG_STATEMENT: "policy's statement", + FLAG_GROUPNAME: "Group's name", + FLAG_NEWGROUPNAME: "New group name", + FLAG_USERNAME: "User's Name", + FLAG_NEWPATH: "New Path", + } + help := `Foulkon CLI usage: foulkon [-address=http://1.2.3.4:8080] [] Available commands: @@ -44,59 +84,56 @@ Available commands: policy authorize -To get more help, please execute this cli with a +To get more help, please execute this cli with a ` -` userHelp := `User actions: - get -extId=xxx retrieve user xxx - get-all retrieve users - groups -id=xxx retrieve user's groups - create -id=xxx -path=/path/ create user 'xxx' with path '/path/' - update -id=xxx -path=/new/ update user 'xxx' with path '/new/' - delete -id=xxx delete user 'xxx' -` + get -id=xxx retrieve user xxx + get-all retrieve users + groups -id=xxx retrieve user's groups + create -id=xxx -path=/path/ create user 'xxx' with path '/path/' + update -id=xxx -path=/new/ update user 'xxx' with path '/new/' + delete -id=xxx delete user 'xxx' + + optionalParams: + -pathPrefix, -offset, -limit, -orderBy Control de output in list actions + ` + groupHelp := `Group actions: get -groupName=xxx retrieve group xxx - get-all retrieve all groups - get-org-groups -orgId=xxx retrieve all groups within an organization xxx + get-all retrieve all groups + get-org-groups -orgId=xxx retrieve all groups within an organization xxx create -groupName=xxx -orgName=xxx -path=/path/ create group 'xxx' with path '/path/' update -orgId=xxx -groupName=xxx -newGroupName=xxx -newPath=xxx update group 'xxx' that belong to oranization 'yyy' delete -policyName=xxx -orgId=yyy delete group 'xxx' that belong to oranization 'yyy' - get-members -orgId=xxx -groupName=xxx retrieve group members + get-members -orgId=xxx -groupName=xxx retrieve group members add-member -orgId=xxx -groupName=xxx -userName=yyy add member 'yyy' to group 'xxx' remove-member -orgId=xxx -groupName=xxx -userName=yyy remove member 'yyy' from group 'xxx' - get-policies -orgId=xxx -groupName=xxx retrieve group policies + get-policies -orgId=xxx -groupName=xxx retrieve group policies attach-policy -orgId=xxx -groupName=xxx -policyName=yyy attach policy 'yyy' to group 'xxx' detach-policy -orgId=xxx -groupName=xxx -policyName=yyy detach policy 'yyy' to group 'xxx' -` + + Optional Parameters: + -pathPrefix, -offset, -limit, -orderBy Control de output in list actions + ` + policyHelp := `Policy actions: - get -policyName=xxx retrieve policy xxx - get-all retrieve all policies - groups-policy -id=xxx retrieve all group with policy 'xxx' attached to - policies-organization -orgId=yyy retrieve all policies that belong to oranization 'yyy' - create -id=xxx -path=/path/ create policy 'xxx' with path '/path/' - update -policyName=xxx -orgId=yyy update policy 'xxx' that belong to oranization 'yyy' - delete -policyName=xxx -orgId=yyy delete policy 'xxx' that belong to oranization 'yyy' -` + get -orgId=yyy -policyName=xxx retrieve policy xxx that belong to organizaation 'yyy' + get-all retrieve all policies + groups-attached -orgId=yyy -policyName=xxx retrieve all groups with policy 'xxx', that belong to organizaation 'yyy', attached to + policies-organization -orgId=yyy retrieve all policies that belong to oranization 'yyy' + create -orgId=yyy -policyName=xxx -path=/path/ -statement=zzz create policy 'xxx' with path '/path/' that belong to organizaation 'yyy' and with statements 'zzz'(JSON format) + update -orgId=yyy -policyName=xxx -statement=zzz update policy 'xxx' with path '/path/' that belong to organizaation 'yyy' and new statements 'zzz'(JSON format) + delete -orgId=yyy -policyName=xxx delete policy 'xxx' that belong to oranization 'yyy' + + Optional Parameters: + -pathPrefix, -offset, -limit, -orderBy Control de output in list actions + ` var cli Cli clientApi := &api.ClientAPI{} cli.UserApi = clientApi - cli.GroupApi = clientApi - availableFlags := map[string]string{ - FLAG_OFFSET: "The offset of the items returned", - FLAG_EXTERNALID: "User's external identifier", - FLAG_ORGANIZATIONID: "Policy organization", - FLAG_POLICYNAME: "Policy name", - FLAG_LIMIT: "The maximum number of items in the response", - FLAG_ORDERBY: "Order data by field", - FLAG_PATHPREFIX: "Search starts from this path", - FLAG_PATH: "--- location", - FLAG_EFFECT: "flag effect", - FLAG_ACTIONS: "flag actions", - FLAG_RESOURCES: "flag resources", - } + cli.PolicyApi = clientApi //remove program path name from args args := os.Args[1:] @@ -117,11 +154,20 @@ To get more help, please execute this cli with a } } + // remove help flag + for i, arg := range args { + if arg == "help" { + args[i] = "--help" + break + } + } + // force -h flag if len(args) < 2 { - args = append(args, "-h") + args = append(args, "--help") } + //statement_example := `[{"effect":"allow","actions":["iam:getUser","iam:*"],"resources":["urn:everything:*"]}]` var msg string var err error @@ -130,98 +176,175 @@ To get more help, please execute this cli with a case "user": switch args[1] { case "get": - params := parseFlags(availableFlags, []string{FLAG_EXTERNALID}, args) - msg, err = cli.UserApi.GetUser(params[FLAG_EXTERNALID]) + + if params := parseFlags(availableFlags, []string{FLAG_EXTERNALID}, args, 1); params == nil { + msg = userHelp + } else { + msg, err = cli.UserApi.GetUser(params[FLAG_EXTERNALID]) + } case "get-all": - params := parseFlags(availableFlags, []string{FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args) - msg, err = cli.UserApi.GetAllUsers(params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + if params := parseFlags(availableFlags, []string{FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args, 0); params == nil { + msg = userHelp + } else { + msg, err = cli.UserApi.GetAllUsers(params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + } case "groups": - params := parseFlags(availableFlags, []string{FLAG_EXTERNALID, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args) - msg, err = cli.UserApi.GetAllUsers(params[FLAG_EXTERNALID], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + if params := parseFlags(availableFlags, []string{FLAG_EXTERNALID, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args, 1); params == nil { + msg = userHelp + } else { + msg, err = cli.UserApi.GetAllUsers(params[FLAG_EXTERNALID], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + } case "create": - params := parseFlags(availableFlags, []string{FLAG_EXTERNALID, FLAG_PATH}, args) - msg, err = cli.UserApi.CreateUser(params[FLAG_EXTERNALID], params[FLAG_PATH]) + if params := parseFlags(availableFlags, []string{FLAG_EXTERNALID, FLAG_PATH}, args, 2); params == nil { + msg = userHelp + } else { + msg, err = cli.UserApi.CreateUser(params[FLAG_EXTERNALID], params[FLAG_PATH]) + } case "delete": - params := parseFlags(availableFlags, []string{FLAG_EXTERNALID}, args) - msg, err = cli.UserApi.DeleteUser(params[FLAG_EXTERNALID]) + if params := parseFlags(availableFlags, []string{FLAG_EXTERNALID}, args, 1); params == nil { + msg = userHelp + } else { + msg, err = cli.UserApi.DeleteUser(params[FLAG_EXTERNALID]) + } case "update": - params := parseFlags(availableFlags, []string{FLAG_EXTERNALID, FLAG_PATH}, args) - msg, err = cli.UserApi.UpdateUser(params[FLAG_EXTERNALID], params[FLAG_PATH]) - case "-h": + if params := parseFlags(availableFlags, []string{FLAG_EXTERNALID, FLAG_PATH}, args, 2); params == nil { + msg = userHelp + } else { + msg, err = cli.UserApi.UpdateUser(params[FLAG_EXTERNALID], params[FLAG_PATH]) + } + case "--help": fallthrough default: msg = userHelp } - case "group": + case "policy": switch args[1] { case "get": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME}, args) - msg, err = cli.GroupApi.GetGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME]) + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME}, args, 2); params == nil { + msg = policyHelp + } else { + msg, err = cli.PolicyApi.GetPolicy(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME]) + } case "get-all": - params := parseFlags(availableFlags, []string{FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args) - msg, err = cli.GroupApi.GetAllGroups(params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) - case "get-org-groups": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args) - msg, err = cli.GroupApi.GetGroupsByOrg(params[FLAG_ORGANIZATIONID], params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + if params := parseFlags(availableFlags, []string{FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args, 0); params == nil { + msg = policyHelp + } else { + msg, err = cli.PolicyApi.GetAllPolicies(params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + } + case "groups-attached": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args, 2); params == nil { + msg = policyHelp + } else { + msg, err = cli.PolicyApi.GetGroupsAttached(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + } + case "policies-organization": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args, 1); params == nil { + msg = policyHelp + } else { + msg, err = cli.PolicyApi.GetPoliciesOrganization(params[FLAG_ORGANIZATIONID], params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + } case "create": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_PATH}, args) - msg, err = cli.GroupApi.CreateGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_PATH]) + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME, FLAG_PATH, FLAG_STATEMENT}, args, 4); params == nil { + msg = policyHelp + } else { + msg, err = cli.PolicyApi.CreatePolicy(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME], params[FLAG_PATH], params[FLAG_STATEMENT]) + } case "update": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_NEWGROUPNAME, FLAG_NEWPATH}, args) - msg, err = cli.GroupApi.UpdateGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_NEWGROUPNAME], params[FLAG_NEWPATH]) + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME, FLAG_PATH, FLAG_STATEMENT}, args, 4); params == nil { + msg = policyHelp + } else { + msg, err = cli.PolicyApi.UpdatePolicy(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME], params[FLAG_PATH], params[FLAG_STATEMENT]) + } case "delete": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME}, args) - msg, err = cli.GroupApi.DeleteGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME]) - case "get-members": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args) - msg, err = cli.GroupApi.GetGroupMembers(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) - case "add-member": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_USERNAME}, args) - msg, err = cli.GroupApi.AddMemberToGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_USERNAME]) - case "remove-member": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_USERNAME}, args) - msg, err = cli.GroupApi.RemoveMemberFromGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_USERNAME]) - case "get-policies": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args) - msg, err = cli.GroupApi.GetGroupPolicies(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) - case "attach-policy": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_POLICYNAME}, args) - msg, err = cli.GroupApi.AttachPolicyToGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_POLICYNAME]) - case "detach-policy": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_POLICYNAME}, args) - msg, err = cli.GroupApi.DetachPolicyFromGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_POLICYNAME]) - case "-h": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME}, args, 2); params == nil { + msg = policyHelp + } else { + msg, err = cli.PolicyApi.DeletePolicy(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME]) + } + case "-help": fallthrough default: - msg = groupHelp + msg = policyHelp } - case "policy": + + case "group": switch args[1] { case "get": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME}, args) - msg, err = clientApi.GetPolicy(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME]) + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME}, args, 2); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.GetGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME]) + } case "get-all": - params := parseFlags(availableFlags, []string{FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args) - msg, err = clientApi.GetAllPolicy(params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + if params := parseFlags(availableFlags, []string{FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args, 0); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.GetAllGroups(params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + } + case "get-org-groups": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args, 1); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.GetGroupsByOrg(params[FLAG_ORGANIZATIONID], params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + } case "create": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME, FLAG_PATH, FLAG_EFFECT, FLAG_ACTIONS, FLAG_RESOURCES}, args) - msg, err = clientApi.CreatePolicy(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME], params[FLAG_PATH], params[FLAG_EFFECT], params[FLAG_ACTIONS], params[FLAG_RESOURCES]) + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_PATH}, args, 3); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.CreateGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_PATH]) + } case "update": - //params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME}, args) - //msg, err = clientApi.UpdatePolicy(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME]) + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_NEWGROUPNAME, FLAG_NEWPATH}, args, 4); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.UpdateGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_NEWGROUPNAME], params[FLAG_NEWPATH]) + } case "delete": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME}, args) - msg, err = clientApi.DeletePolicy(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME]) - case "groups-policy": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_POLICYNAME, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args) - msg, err = clientApi.GetGroupsPolicy(params[FLAG_ORGANIZATIONID], params[FLAG_POLICYNAME], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) - case "policies-organization": - params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args) - msg, err = clientApi.GetPoliciesOrganization(params[FLAG_ORGANIZATIONID], params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME}, args, 2); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.DeleteGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME]) + } + case "get-members": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args, 2); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.GetGroupMembers(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_PATHPREFIX], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + } + case "add-member": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_USERNAME}, args, 3); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.AddMemberToGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_USERNAME]) + } + case "remove-member": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_USERNAME}, args, 3); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.RemoveMemberFromGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_USERNAME]) + } + case "get-policies": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_PATHPREFIX, FLAG_OFFSET, FLAG_LIMIT, FLAG_ORDERBY}, args, 2); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.GetGroupPolicies(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_OFFSET], params[FLAG_LIMIT], params[FLAG_ORDERBY]) + } + case "attach-policy": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_POLICYNAME}, args, 3); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.AttachPolicyToGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_POLICYNAME]) + } + case "detach-policy": + if params := parseFlags(availableFlags, []string{FLAG_ORGANIZATIONID, FLAG_GROUPNAME, FLAG_POLICYNAME}, args, 3); params == nil { + msg = groupHelp + } else { + msg, err = cli.GroupApi.DetachPolicyFromGroup(params[FLAG_ORGANIZATIONID], params[FLAG_GROUPNAME], params[FLAG_POLICYNAME]) + } case "-h": fallthrough default: - msg = policyHelp + msg = groupHelp } default: msg = help @@ -233,27 +356,3 @@ To get more help, please execute this cli with a println(msg) os.Exit(0) } - -// Helper func for updating request params -func parseFlags(availableFlags map[string]string, validFlags []string, cliArgs []string) map[string]string { - params := make(map[string]string) - - flagSet := flag.NewFlagSet(cliArgs[0]+" "+cliArgs[1], flag.ExitOnError) - - for _, val := range validFlags { - flagSet.String(val, "", availableFlags[val]) - } - - if err := flagSet.Parse(cliArgs[2:]); err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - os.Exit(1) - } - - for _, v := range validFlags { - if val := flagSet.Lookup(v); val != nil { - params[v] = val.Value.String() - } - } - - return params -}