given an array of environment names, maps these all to actual objects by querying the server
(client *octopusApiClient.Client, environmentNamesOrIds []string)
| 433 | |
| 434 | // given an array of environment names, maps these all to actual objects by querying the server |
| 435 | func FindEnvironments(client *octopusApiClient.Client, environmentNamesOrIds []string) ([]*environments.Environment, error) { |
| 436 | if len(environmentNamesOrIds) == 0 { |
| 437 | return nil, nil |
| 438 | } |
| 439 | // there's no "bulk lookup" API, so we either need to do a foreach loop to find each environment individually, or load the entire server's worth of environments |
| 440 | // it's probably going to be cheaper to just list out all the environments and match them client side, so we'll do that for simplicity's sake |
| 441 | allEnvs, err := client.Environments.GetAll() |
| 442 | if err != nil { |
| 443 | return nil, err |
| 444 | } |
| 445 | |
| 446 | nameLookup := make(map[string]*environments.Environment, len(allEnvs)) |
| 447 | idLookup := make(map[string]*environments.Environment, len(allEnvs)) |
| 448 | |
| 449 | for _, env := range allEnvs { |
| 450 | nameLookup[strings.ToLower(env.GetName())] = env |
| 451 | idLookup[strings.ToLower(env.GetID())] = env |
| 452 | } |
| 453 | |
| 454 | var result []*environments.Environment |
| 455 | for _, n := range environmentNamesOrIds { |
| 456 | nameOrId := strings.ToLower(n) |
| 457 | env := nameLookup[nameOrId] |
| 458 | if env != nil { |
| 459 | result = append(result, env) |
| 460 | } else { |
| 461 | env = idLookup[nameOrId] |
| 462 | if env != nil { |
| 463 | result = append(result, env) |
| 464 | } else { |
| 465 | return nil, fmt.Errorf("cannot find environment %s", nameOrId) |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | return result, nil |
| 470 | } |
no test coverage detected