| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sort" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/hostclaim" |
| 8 | "github.com/carlosframework/platform/internal/hostops" |
| 9 | ) |
| 10 | |
| 11 | type claimPendingView struct { |
| 12 | Host string `json:"host"` |
| 13 | Name string `json:"name,omitempty"` |
| 14 | Incarnation string `json:"incarnation"` |
| 15 | OperationID string `json:"operation_id"` |
| 16 | Phase string `json:"phase"` |
| 17 | } |
| 18 | |
| 19 | func (s *Server) claimPendingViews(ctx context.Context, account, app, family string) ([]claimPendingView, error) { |
| 20 | out := []claimPendingView{} |
| 21 | hosts, err := hostops.ListClaims(ctx, s.cfg.Releases) |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | for _, host := range hosts { |
| 26 | obs, err := hostclaim.Read(ctx, s.cfg.Releases, host) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | if obs.Absent || obs.Claim.Holder == nil { |
| 31 | continue |
| 32 | } |
| 33 | h, op := obs.Claim.Holder, obs.Claim.Op |
| 34 | if h.Account != account || h.App != app || h.Family != family || op.ID == "" || op.Kind == hostclaim.KindServing { |
| 35 | continue |
| 36 | } |
| 37 | phase := op.Phase |
| 38 | if phase == "" { |
| 39 | phase = op.Kind |
| 40 | } |
| 41 | out = append(out, claimPendingView{Host: host, Name: h.Name, Incarnation: h.Incarnation, OperationID: op.ID, Phase: phase}) |
| 42 | } |
| 43 | sort.Slice(out, func(i, j int) bool { return out[i].Host < out[j].Host }) |
| 44 | return out, nil |
| 45 | } |
| 46 | |