Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
ciao-cli: code refactory of getTenant function
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Leoswaldo Macias authored and leoswaldo committed Aug 11, 2016
1 parent 5a9a49f commit e4446d6
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions ciao-cli/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit e4446d6

Please sign in to comment.