-
Notifications
You must be signed in to change notification settings - Fork 9
feat(nautobotOP): Use UUID for Nautobot Operator Sync #1994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,9 +58,31 @@ func (s *LocationService) GetByName(ctx context.Context, name string) nb.Locatio | |
| return list.Results[0] | ||
| } | ||
|
|
||
| func (s *LocationService) GetByID(ctx context.Context, id string) nb.Location { | ||
| if id == "" { | ||
| return nb.Location{} | ||
| } | ||
| if location, ok := cache.FindByID(s.client.Cache, "locations", id, func(l nb.Location) *string { | ||
| return l.Id | ||
| }); ok { | ||
| return location | ||
| } | ||
|
|
||
| list, resp, err := s.client.APIClient.DcimAPI.DcimLocationsList(ctx).Depth(10).Id([]string{id}).Execute() | ||
| if err != nil { | ||
| bodyString := helpers.ReadResponseBody(resp) | ||
| s.client.AddReport("GetLocationByID", "failed to get", "id", id, "error", err.Error(), "response_body", bodyString) | ||
| return nb.Location{} | ||
| } | ||
| if list == nil || len(list.Results) == 0 || list.Results[0].Id == nil { | ||
| return nb.Location{} | ||
| } | ||
|
|
||
| return list.Results[0] | ||
| } | ||
|
|
||
| func (s *LocationService) ListAll(ctx context.Context) []nb.Location { | ||
| ids := s.client.GetChangeObjectIDS(ctx, "dcim.location") | ||
| list, resp, err := s.client.APIClient.DcimAPI.DcimLocationsList(ctx).Id(ids).Depth(10).Execute() | ||
| list, resp, err := s.client.APIClient.DcimAPI.DcimLocationsList(ctx).Limit(10000).Depth(10).Execute() | ||
| if err != nil { | ||
| bodyString := helpers.ReadResponseBody(resp) | ||
| s.client.AddReport("ListAllLocations", "failed to list", "error", err.Error(), "response_body", bodyString) | ||
|
|
@@ -77,16 +99,6 @@ func (s *LocationService) ListAll(ctx context.Context) []nb.Location { | |
| } | ||
|
|
||
| func (s *LocationService) Update(ctx context.Context, id string, req nb.LocationRequest) (*nb.Location, error) { | ||
| owned, err := s.client.IsCreatedByUser(ctx, id) | ||
| if err != nil { | ||
| s.client.AddReport("UpdateLocation", "failed to check ownership", "id", id, "error", err.Error()) | ||
| return nil, err | ||
| } | ||
| if !owned { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if !owned , this check is gone from Update but still exists in destroy . without this guard one can accidentally puts a UUID of a human-created or externally-managed Nautobot object in their YAML. the operator will overwrite it without any warnin I see . |
||
| log.Warn("skipping update, object not created by user", "id", id, "user", s.client.Username) | ||
| return nil, nil | ||
| } | ||
|
|
||
| location, resp, err := s.client.APIClient.DcimAPI.DcimLocationsUpdate(ctx, id).LocationRequest(req).Execute() | ||
| if err != nil { | ||
| bodyString := helpers.ReadResponseBody(resp) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ package dcim | |
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| "github.com/charmbracelet/log" | ||
| "github.com/rackerlabs/understack/go/nautobotop/internal/nautobot/cache" | ||
|
|
@@ -23,28 +22,20 @@ func NewManufacturerService(nautobotClient *client.NautobotClient) *Manufacturer | |
| } | ||
|
|
||
| func (s *ManufacturerService) ListAll(ctx context.Context) []nb.Manufacturer { | ||
| ids := s.client.GetChangeObjectIDS(ctx, "dcim.manufacturer") | ||
|
|
||
| // Define the API call function for this specific endpoint | ||
| apiCall := func(ctx context.Context, batchIds []string) ([]nb.Manufacturer, *http.Response, error) { | ||
| list, resp, err := s.client.APIClient.DcimAPI.DcimManufacturersList(ctx).Id(batchIds).Depth(10).Execute() | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
| if list == nil { | ||
| return []nb.Manufacturer{}, resp, nil | ||
| } | ||
| return list.Results, resp, nil | ||
| list, resp, err := s.client.APIClient.DcimAPI.DcimManufacturersList(ctx).Limit(10000).Depth(10).Execute() | ||
| if err != nil { | ||
| bodyString := helpers.ReadResponseBody(resp) | ||
| s.client.AddReport("ListAllManufacturers", "failed to list", "error", err.Error(), "response_body", bodyString) | ||
| return []nb.Manufacturer{} | ||
| } | ||
| if list == nil || len(list.Results) == 0 { | ||
| return []nb.Manufacturer{} | ||
| } | ||
| if list.Results[0].Id == nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In ListAll, if Results[0].Id == nil, the entire result set is discarded and an empty slice is returned. I m not sure if it is intentional . but let's say there are 10 items , it would check just 1st or the 0th position and would discard all . right ? |
||
| return []nb.Manufacturer{} | ||
| } | ||
|
|
||
| // Use the helper function for pagination | ||
| return helpers.PaginatedListWithIDs( | ||
| ctx, | ||
| ids, | ||
| apiCall, | ||
| s.client.AddReport, | ||
| "ListAllManufacturers", | ||
| ) | ||
| return list.Results | ||
| } | ||
|
|
||
| func (s *ManufacturerService) GetByName(ctx context.Context, name string) nb.Manufacturer { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so here if found in cache, no API call needed otherwise and it call API. bt where would it update cache ? how does it write ?