From e4446d6567fa78074ee4b603e43b0bbd96cc28da Mon Sep 17 00:00:00 2001 From: Leoswaldo Macias Date: Tue, 9 Aug 2016 08:11:05 -0500 Subject: [PATCH] ciao-cli: code refactory of getTenant function The getTenant function was evaluating single cases while it could have a base to validate with a more structure scenarios as proposed below. First validation: ensure the user has at least one project. Second validation: if not projectID defined and the user has only one project, return it as default project, if the user has two or more project then print the projects the user has. Third validation: iterate through all projects and see if the given tentantID matches any project of the user. If no condition matches, then it would return with the last instruction in the function. Signed-off-by: Leoswaldo Macias --- ciao-cli/identity.go | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/ciao-cli/identity.go b/ciao-cli/identity.go index a25299975..b7f70456f 100644 --- a/ciao-cli/identity.go +++ b/ciao-cli/identity.go @@ -252,36 +252,27 @@ func getTenant(username string, password string, tenantID string) (string, strin return "", "", err } - if len(projects) == 0 { + if len(projects) <= 0 { return "", tenantID, fmt.Errorf("No tenant name for %s", username) } - if len(projects) > 1 { - if tenantID == "" { - fmt.Printf("Available projects for %s:\n", *identityUser) - for i, p := range projects { - fmt.Printf("\t Project[%d]: %s (%s)\n", i+1, p.Name, p.ID) - } - return "", "", fmt.Errorf("Please specify a project to use with -tenant-name or -tenant-id") + if tenantID == "" { + if len(projects) == 1 { + tenantName := projects[0].Name + tenantID = projects[0].ID + return tenantName, tenantID, nil } - - for _, p := range projects { - if p.ID != tenantID { - continue - } - return p.Name, tenantID, nil + fmt.Printf("Available projects for %s:\n", *identityUser) + for i, p := range projects { + fmt.Printf("\t Project[%d]: %s (%s)\n", i+1, p.Name, p.ID) } - return "", tenantID, fmt.Errorf("No tenant name for %s", tenantID) - } - - if tenantID != "" && projects[0].ID != tenantID { - return "", tenantID, fmt.Errorf("No tenant name for %s", tenantID) + return "", "", fmt.Errorf("Please specify a project to use with -tenant-name or -tenant-id") } - tenantName := projects[0].Name - if tenantID == "" { - tenantID = projects[0].ID + for _, p := range projects { + if p.ID == tenantID { + return p.Name, tenantID, nil + } } - - return tenantName, tenantID, nil + return "", tenantID, fmt.Errorf("No tenant name for %s", tenantID) }