| package config_parser |
| |
| import ( |
| "fmt" |
| "strconv" |
| "strings" |
| ) |
| |
| // parseCustomerAndProject parses a customer ID and project ID from a project string. |
| func parseCustomerAndProject(project string) (uint32, uint32, error) { |
| projectStrings := strings.Split(project, ":") |
| if len(projectStrings) > 2 { |
| return 0, 0, fmt.Errorf("project string must be in the form 'customer_id:project_id': %v", project) |
| } |
| if len(projectStrings) < 2 { |
| return 0, 0, fmt.Errorf("project string must be in the form 'customer_id:project_id': %v", project) |
| } |
| customerId, err := strconv.ParseUint(projectStrings[0], 10, 32) |
| if err != nil { |
| return 0, 0, err |
| } |
| projectId, err := strconv.ParseUint(projectStrings[1], 10, 32) |
| if err != nil { |
| return 0, 0, err |
| } |
| return uint32(customerId), uint32(projectId), nil |
| } |
| |
| // RemoveExcludedProjects removes filtered projects from the configs. |
| func RemoveExcludedProjects(excludeProjects []string, configs []ProjectConfigData) ([]ProjectConfigData, error) { |
| // Build a map of customer IDs to a set of the project IDs to filter. |
| customersToFilter := make(map[uint32]map[uint32]bool) |
| for _, excludeProject := range excludeProjects { |
| customerId, projectId, err := parseCustomerAndProject(excludeProject) |
| if err != nil { |
| return nil, err |
| } |
| customer, ok := customersToFilter[customerId] |
| if !ok { |
| customersToFilter[customerId] = make(map[uint32]bool) |
| customer = customersToFilter[customerId] |
| } |
| customer[projectId] = true |
| } |
| filteredConfigs := configs[:0] |
| for _, config := range configs { |
| if customer, ok := customersToFilter[config.CustomerId]; ok { |
| if _, ok = customer[config.ProjectId]; ok { |
| continue |
| } |
| } |
| filteredConfigs = append(filteredConfigs, config) |
| } |
| return filteredConfigs, nil |
| } |
| |
| // RemoveProjectsNotIncluded filters customers/projects in the configs. |
| func RemoveProjectsNotIncluded(includeCustomers []string, includeProjects []string, configs []ProjectConfigData) ([]ProjectConfigData, error) { |
| // Build a map of customer IDs to include. |
| customersToInclude := make(map[uint32]bool) |
| for _, includeCustomer := range includeCustomers { |
| customerId, err := strconv.ParseUint(includeCustomer, 10, 32) |
| if err != nil { |
| return nil, err |
| } |
| customersToInclude[uint32(customerId)] = true |
| } |
| // Build a map of customer IDs to a set of the project IDs to include. |
| customersWithProjectsToInclude := make(map[uint32]map[uint32]bool) |
| for _, includeProject := range includeProjects { |
| customerId, projectId, err := parseCustomerAndProject(includeProject) |
| if err != nil { |
| return nil, err |
| } |
| customer, ok := customersWithProjectsToInclude[customerId] |
| if !ok { |
| customersWithProjectsToInclude[customerId] = make(map[uint32]bool) |
| customer = customersWithProjectsToInclude[customerId] |
| } |
| customer[projectId] = true |
| } |
| includedConfigs := configs[:0] |
| for _, config := range configs { |
| if _, ok := customersToInclude[config.CustomerId]; ok { |
| includedConfigs = append(includedConfigs, config) |
| } else if customer, ok := customersWithProjectsToInclude[config.CustomerId]; ok { |
| if _, ok = customer[config.ProjectId]; ok { |
| includedConfigs = append(includedConfigs, config) |
| } |
| } |
| } |
| return includedConfigs, nil |
| } |