(oldWS, newWS workspacesByID)
| 198 | } |
| 199 | |
| 200 | func produceUpdate(oldWS, newWS workspacesByID) (out *proto.WorkspaceUpdate, updated bool) { |
| 201 | out = &proto.WorkspaceUpdate{ |
| 202 | UpsertedWorkspaces: []*proto.Workspace{}, |
| 203 | UpsertedAgents: []*proto.Agent{}, |
| 204 | DeletedWorkspaces: []*proto.Workspace{}, |
| 205 | DeletedAgents: []*proto.Agent{}, |
| 206 | } |
| 207 | |
| 208 | for wsID, newWorkspace := range newWS { |
| 209 | oldWorkspace, exists := oldWS[wsID] |
| 210 | // Upsert both workspace and agents if the workspace is new |
| 211 | if !exists { |
| 212 | out.UpsertedWorkspaces = append(out.UpsertedWorkspaces, &proto.Workspace{ |
| 213 | Id: tailnet.UUIDToByteSlice(wsID), |
| 214 | Name: newWorkspace.WorkspaceName, |
| 215 | Status: newWorkspace.Status, |
| 216 | }) |
| 217 | for _, agent := range newWorkspace.Agents { |
| 218 | out.UpsertedAgents = append(out.UpsertedAgents, &proto.Agent{ |
| 219 | Id: tailnet.UUIDToByteSlice(agent.ID), |
| 220 | Name: agent.Name, |
| 221 | WorkspaceId: tailnet.UUIDToByteSlice(wsID), |
| 222 | }) |
| 223 | } |
| 224 | updated = true |
| 225 | continue |
| 226 | } |
| 227 | // Upsert workspace if the workspace is updated |
| 228 | if !newWorkspace.Equal(oldWorkspace) { |
| 229 | out.UpsertedWorkspaces = append(out.UpsertedWorkspaces, &proto.Workspace{ |
| 230 | Id: tailnet.UUIDToByteSlice(wsID), |
| 231 | Name: newWorkspace.WorkspaceName, |
| 232 | Status: newWorkspace.Status, |
| 233 | }) |
| 234 | updated = true |
| 235 | } |
| 236 | |
| 237 | add, remove := slice.SymmetricDifference(oldWorkspace.Agents, newWorkspace.Agents) |
| 238 | for _, agent := range add { |
| 239 | out.UpsertedAgents = append(out.UpsertedAgents, &proto.Agent{ |
| 240 | Id: tailnet.UUIDToByteSlice(agent.ID), |
| 241 | Name: agent.Name, |
| 242 | WorkspaceId: tailnet.UUIDToByteSlice(wsID), |
| 243 | }) |
| 244 | updated = true |
| 245 | } |
| 246 | for _, agent := range remove { |
| 247 | out.DeletedAgents = append(out.DeletedAgents, &proto.Agent{ |
| 248 | Id: tailnet.UUIDToByteSlice(agent.ID), |
| 249 | Name: agent.Name, |
| 250 | WorkspaceId: tailnet.UUIDToByteSlice(wsID), |
| 251 | }) |
| 252 | updated = true |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Delete workspace and agents if the workspace is deleted |
| 257 | for wsID, oldWorkspace := range oldWS { |
no test coverage detected