improve additional teams lookup (#2445)
* improve additional teams lookup --------- Co-authored-by: Jan Mussler <janm81@gmail.com>
This commit is contained in:
		
							parent
							
								
									8ae769a567
								
							
						
					
					
						commit
						f41c14f468
					
				|  | @ -66,13 +66,20 @@ func (ptm *PostgresTeamMap) fetchAdditionalTeams(team string, superuserTeams boo | |||
| 		teams = (*ptm)[team].AdditionalTeams | ||||
| 	} | ||||
| 	if transitive { | ||||
| 		exclude = append(exclude, team) | ||||
| 		for _, additionalTeam := range teams { | ||||
| 			if !(util.SliceContains(exclude, additionalTeam)) { | ||||
| 				// remember to not check team and additionalTeam again
 | ||||
| 				exclude = append(exclude, additionalTeam) | ||||
| 				transitiveTeams := (*ptm).fetchAdditionalTeams(additionalTeam, superuserTeams, transitive, exclude) | ||||
| 				for _, transitiveTeam := range transitiveTeams { | ||||
| 					if !(util.SliceContains(exclude, transitiveTeam)) && !(util.SliceContains(teams, transitiveTeam)) { | ||||
| 						teams = append(teams, transitiveTeam) | ||||
| 					if !(util.SliceContains(exclude, transitiveTeam)) { | ||||
| 						// remember to not check transitive team again in case
 | ||||
| 						// it is one of the next additional teams of the outer loop
 | ||||
| 						exclude = append(exclude, transitiveTeam) | ||||
| 						if !(util.SliceContains(teams, transitiveTeam)) { | ||||
| 							// found a new transitive additional team
 | ||||
| 							teams = append(teams, transitiveTeam) | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
|  | @ -84,12 +91,12 @@ func (ptm *PostgresTeamMap) fetchAdditionalTeams(team string, superuserTeams boo | |||
| 
 | ||||
| // GetAdditionalTeams function to retrieve list of additional teams
 | ||||
| func (ptm *PostgresTeamMap) GetAdditionalTeams(team string, transitive bool) []string { | ||||
| 	return ptm.fetchAdditionalTeams(team, false, transitive, []string{}) | ||||
| 	return ptm.fetchAdditionalTeams(team, false, transitive, []string{team}) | ||||
| } | ||||
| 
 | ||||
| // GetAdditionalSuperuserTeams function to retrieve list of additional superuser teams
 | ||||
| func (ptm *PostgresTeamMap) GetAdditionalSuperuserTeams(team string, transitive bool) []string { | ||||
| 	return ptm.fetchAdditionalTeams(team, true, transitive, []string{}) | ||||
| 	return ptm.fetchAdditionalTeams(team, true, transitive, []string{team}) | ||||
| } | ||||
| 
 | ||||
| // Load function to import data from PostgresTeam CRD
 | ||||
|  |  | |||
|  | @ -42,12 +42,26 @@ var ( | |||
| 					AdditionalMembers:        map[string][]string{"acid": []string{"batman"}}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			{ | ||||
| 				TypeMeta: metav1.TypeMeta{ | ||||
| 					Kind:       "PostgresTeam", | ||||
| 					APIVersion: "acid.zalan.do/v1", | ||||
| 				}, | ||||
| 				ObjectMeta: metav1.ObjectMeta{ | ||||
| 					Name: "teamD", | ||||
| 				}, | ||||
| 				Spec: acidv1.PostgresTeamSpec{ | ||||
| 					AdditionalSuperuserTeams: map[string][]string{}, | ||||
| 					AdditionalTeams:          map[string][]string{"teamA": []string{"teamD"}, "teamC": []string{"teamD"}, "teamD": []string{"teamA", "teamB", "teamC"}}, | ||||
| 					AdditionalMembers:        map[string][]string{"acid": []string{"batman"}}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	pgTeamMap = PostgresTeamMap{ | ||||
| 		"teamA": { | ||||
| 			AdditionalSuperuserTeams: []string{"teamB", "team24x7"}, | ||||
| 			AdditionalTeams:          []string{"teamC"}, | ||||
| 			AdditionalTeams:          []string{"teamC", "teamD"}, | ||||
| 			AdditionalMembers:        []string{}, | ||||
| 		}, | ||||
| 		"teamB": { | ||||
|  | @ -57,7 +71,12 @@ var ( | |||
| 		}, | ||||
| 		"teamC": { | ||||
| 			AdditionalSuperuserTeams: []string{"team24x7"}, | ||||
| 			AdditionalTeams:          []string{"teamA", "teamB", "acid"}, | ||||
| 			AdditionalTeams:          []string{"teamA", "teamB", "teamD", "acid"}, | ||||
| 			AdditionalMembers:        []string{}, | ||||
| 		}, | ||||
| 		"teamD": { | ||||
| 			AdditionalSuperuserTeams: []string{}, | ||||
| 			AdditionalTeams:          []string{"teamA", "teamB", "teamC"}, | ||||
| 			AdditionalMembers:        []string{}, | ||||
| 		}, | ||||
| 		"team24x7": { | ||||
|  | @ -119,14 +138,14 @@ func TestGetAdditionalTeams(t *testing.T) { | |||
| 			"Check that additional teams are returned", | ||||
| 			"teamA", | ||||
| 			false, | ||||
| 			[]string{"teamC"}, | ||||
| 			[]string{"teamC", "teamD"}, | ||||
| 			"GetAdditionalTeams returns wrong list", | ||||
| 		}, | ||||
| 		{ | ||||
| 			"Check that additional teams are returned incl. transitive teams", | ||||
| 			"teamA", | ||||
| 			true, | ||||
| 			[]string{"teamC", "teamB", "acid"}, | ||||
| 			[]string{"teamC", "teamD", "teamB", "acid"}, | ||||
| 			"GetAdditionalTeams returns wrong list", | ||||
| 		}, | ||||
| 		{ | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue