(cfgKey, newEntry string)
| 370 | } |
| 371 | |
| 372 | func (profile *Profile) addEndpointEntry(cfgKey, newEntry string) { |
| 373 | changed := false |
| 374 | |
| 375 | // When finished, save the profile. |
| 376 | defer func() { |
| 377 | if !changed { |
| 378 | return |
| 379 | } |
| 380 | |
| 381 | err := profile.Save() |
| 382 | if err != nil { |
| 383 | log.Warningf("profile: failed to save profile %s after add an endpoint rule: %s", profile.ScopedID(), err) |
| 384 | } |
| 385 | }() |
| 386 | |
| 387 | // Lock the profile for editing. |
| 388 | profile.Lock() |
| 389 | defer profile.Unlock() |
| 390 | |
| 391 | // Get the endpoint list configuration value and add the new entry. |
| 392 | endpointList, ok := profile.configPerspective.GetAsStringArray(cfgKey) |
| 393 | if ok { |
| 394 | // A list already exists, check for duplicates within the same prefix. |
| 395 | newEntryPrefix := strings.Split(newEntry, " ")[0] + " " |
| 396 | for _, entry := range endpointList { |
| 397 | if !strings.HasPrefix(entry, newEntryPrefix) { |
| 398 | // We found an entry with a different prefix than the new entry. |
| 399 | // Beyond this entry we cannot possibly know if identical entries will |
| 400 | // match, so we will have to add the new entry no matter what the rest |
| 401 | // of the list has. |
| 402 | break |
| 403 | } |
| 404 | |
| 405 | if entry == newEntry { |
| 406 | // An identical entry is already in the list, abort. |
| 407 | log.Debugf("profile: ignoring new endpoint rule for %s, as identical is already present: %s", profile, newEntry) |
| 408 | return |
| 409 | } |
| 410 | } |
| 411 | endpointList = append([]string{newEntry}, endpointList...) |
| 412 | } else { |
| 413 | endpointList = []string{newEntry} |
| 414 | } |
| 415 | |
| 416 | // Save new value back to profile. |
| 417 | config.PutValueIntoHierarchicalConfig(profile.Config, cfgKey, endpointList) |
| 418 | changed = true |
| 419 | |
| 420 | // Reload the profile manually in order to parse the newly added entry. |
| 421 | profile.dataParsed = false |
| 422 | err := profile.parseConfig() |
| 423 | if err != nil { |
| 424 | log.Errorf("profile: failed to parse %s config after adding endpoint: %s", profile, err) |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // LayeredProfile returns the layered profile associated with this profile. |
| 429 | func (profile *Profile) LayeredProfile() *LayeredProfile { |
no test coverage detected