(update *proto.WorkspaceUpdate, updateKind UpdateKind)
| 1261 | } |
| 1262 | |
| 1263 | func (t *tunnelUpdater) handleUpdate(update *proto.WorkspaceUpdate, updateKind UpdateKind) error { |
| 1264 | t.Lock() |
| 1265 | defer t.Unlock() |
| 1266 | |
| 1267 | // Snapshots represent the complete state, not a diff. Clear any |
| 1268 | // inherited or previously accumulated workspace data so that |
| 1269 | // workspaces absent from the snapshot (e.g. stopped while we |
| 1270 | // were disconnected) do not linger. |
| 1271 | if updateKind == Snapshot { |
| 1272 | t.workspaces = make(map[uuid.UUID]*Workspace) |
| 1273 | } |
| 1274 | |
| 1275 | currentUpdate := WorkspaceUpdate{ |
| 1276 | UpsertedWorkspaces: []*Workspace{}, |
| 1277 | UpsertedAgents: []*Agent{}, |
| 1278 | DeletedWorkspaces: []*Workspace{}, |
| 1279 | DeletedAgents: []*Agent{}, |
| 1280 | Kind: updateKind, |
| 1281 | } |
| 1282 | |
| 1283 | for _, uw := range update.UpsertedWorkspaces { |
| 1284 | workspaceID, err := uuid.FromBytes(uw.Id) |
| 1285 | if err != nil { |
| 1286 | return xerrors.Errorf("failed to parse workspace ID: %w", err) |
| 1287 | } |
| 1288 | w := &Workspace{ |
| 1289 | ID: workspaceID, |
| 1290 | Name: uw.Name, |
| 1291 | Status: uw.Status, |
| 1292 | ownerUsername: t.ownerUsername, |
| 1293 | agents: make(map[uuid.UUID]*Agent), |
| 1294 | } |
| 1295 | t.upsertWorkspaceLocked(w) |
| 1296 | currentUpdate.UpsertedWorkspaces = append(currentUpdate.UpsertedWorkspaces, w) |
| 1297 | } |
| 1298 | |
| 1299 | // delete agents before deleting workspaces, since the agents have workspace ID references |
| 1300 | for _, da := range update.DeletedAgents { |
| 1301 | agentID, err := uuid.FromBytes(da.Id) |
| 1302 | if err != nil { |
| 1303 | return xerrors.Errorf("failed to parse agent ID: %w", err) |
| 1304 | } |
| 1305 | workspaceID, err := uuid.FromBytes(da.WorkspaceId) |
| 1306 | if err != nil { |
| 1307 | return xerrors.Errorf("failed to parse workspace ID: %w", err) |
| 1308 | } |
| 1309 | deletedAgent, err := t.deleteAgentLocked(workspaceID, agentID) |
| 1310 | if err != nil { |
| 1311 | return xerrors.Errorf("failed to delete agent: %w", err) |
| 1312 | } |
| 1313 | currentUpdate.DeletedAgents = append(currentUpdate.DeletedAgents, deletedAgent) |
| 1314 | } |
| 1315 | for _, dw := range update.DeletedWorkspaces { |
| 1316 | workspaceID, err := uuid.FromBytes(dw.Id) |
| 1317 | if err != nil { |
| 1318 | return xerrors.Errorf("failed to parse workspace ID: %w", err) |
| 1319 | } |
| 1320 | deletedWorkspace, err := t.deleteWorkspaceLocked(workspaceID) |
no test coverage detected