# cli: resolve entire-api cell/jurisdiction from a repo's mirror

`cb49244`·

Soph·2w ago·2 files·+176 added/-0 removed

entire-api is addressed per-cell, and (per the chosen design) the cell comes from the target repo's mirror — the same source entire.io/api's resolveCell uses. Add the placement selector: given a repo's mirrors (listed via the existing listMirrorsForRepo), pick one carrying both a cell and a jurisdiction, preferring a named cluster and otherwise taking the first routable row — matching resolveCell's `preferred ?? matches[0]` and its filter-before-select ordering.

Pure selector + forge→provider mapping only (both unit-tested); the repo-resolution glue that calls the control plane lands with the client wiring so nothing here is dead.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

## Sessions

5b0d9a00a86dView transcript

## Changes

2

- cmd/entire/cli

- Aentireapi_cell.go+60

- Aentireapi_cell_test.go+116

```
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

package cli

import (
	"strings"

"github.com/entireio/cli/internal/coreapi"
)

// entireAPIPlacement is a repo's entire-api routing coordinates, read from its
// mirror: the cell that hosts it (fills {cell} in ENTIRE_API_BASE_URL_TEMPLATE)
// and the jurisdiction that homes it (fills {jurisdiction} in the audience and
// token-minting-core templates). Both must be present for a repo to be routable
// to entire-api; older cores omit them.
type entireAPIPlacement struct {
	Cell         string
	Jurisdiction string
	ClusterHost  string
}

// forgeToMirrorProvider maps a gitremote forge identifier (e.g. "gh") to the
// upstream provider the control plane records mirrors under (e.g. "github").
// entire-api routing only supports GitHub mirrors today.
func forgeToMirrorProvider(forge string) (string, bool) {
	switch strings.ToLower(strings.TrimSpace(forge)) {
	case "gh", mirrorCloneProviderGitHub:
		return mirrorCloneProviderGitHub, true
	default:
		return "", false
	}
}

// selectRoutablePlacement picks the entire-api placement from a repo's mirrors.
// Only mirrors carrying both a cell and a jurisdiction are eligible. When the
// repo is mirrored on several clusters, preferredCluster wins if it matches;
// otherwise the first routable mirror is used. This mirrors entire.io/api's
// resolveCell (`preferred ?? matches[0]`), including its "filter to routable
// before selecting" ordering so a non-routable row first doesn't mask a
// routable sibling.
func selectRoutablePlacement(mirrors []coreapi.Mirror, preferredCluster string) (entireAPIPlacement, bool) {
	var first *entireAPIPlacement
	for i := range mirrors {
		cell := strings.TrimSpace(mirrors[i].Cell.Or(""))
		jur := strings.TrimSpace(mirrors[i].Jurisdiction.Or(""))
		if cell == "" || jur == "" {
			continue
		}
		p := entireAPIPlacement{Cell: cell, Jurisdiction: jur, ClusterHost: mirrors[i].ClusterHost}
		if preferredCluster != "" && strings.EqualFold(mirrors[i].ClusterHost, preferredCluster) {
			return p, true
		}
		if first == nil {
			routable := p
			first = &routable
		}
	}
	if first != nil {
		return *first, true
	}
	return entireAPIPlacement{}, false
}
```

Acmd/entire/cli/entireapi_cell.go+60

```
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

package cli

import (
	"testing"

"github.com/entireio/cli/internal/coreapi"
)

func mirror(cluster, cell, jurisdiction string) coreapi.Mirror {
	m := coreapi.Mirror{ClusterHost: cluster}
	if cell != "" {
		m.Cell = coreapi.NewOptString(cell)
	}
	if jurisdiction != "" {
		m.Jurisdiction = coreapi.NewOptString(jurisdiction)
	}
	return m
}

func TestSelectRoutablePlacement(t *testing.T) {
	t.Parallel()

tests := []struct {
		name      string
		mirrors   []coreapi.Mirror
		preferred string
		wantOK    bool
		wantCell  string
		wantJur   string
	}{
		{
			name:     "single routable mirror",
			mirrors:  []coreapi.Mirror{mirror("a.entire.io", "aws-us-east-2", "us")},
			wantOK:   true,
			wantCell: "aws-us-east-2",
			wantJur:  "us",
		},
		{
			name:    "no mirrors",
			mirrors: nil,
			wantOK:  false,
		},
		{
			name:    "mirror missing cell is not routable",
			mirrors: []coreapi.Mirror{mirror("a.entire.io", "", "us")},
			wantOK:  false,
		},
		{
			name:    "mirror missing jurisdiction is not routable",
			mirrors: []coreapi.Mirror{mirror("a.entire.io", "cell-1", "")},
			wantOK:  false,
		},
		{
			name: "non-routable row first does not mask routable sibling",
			mirrors: []coreapi.Mirror{
				mirror("a.entire.io", "", ""),
				mirror("b.entire.io", "eu-1", "eu"),
			},
			wantOK:   true,
			wantCell: "eu-1",
			wantJur:  "eu",
		},
		{
			name: "preferred cluster wins among several",
			mirrors: []coreapi.Mirror{
				mirror("a.entire.io", "us-1", "us"),
				mirror("b.entire.io", "eu-1", "eu"),
			},
			preferred: "b.entire.io",
			wantOK:    true,
			wantCell:  "eu-1",
			wantJur:   "eu",
		},
		{
			name: "preferred not found falls back to first routable",
			mirrors: []coreapi.Mirror{
				mirror("a.entire.io", "us-1", "us"),
				mirror("b.entire.io", "eu-1", "eu"),
			},
			preferred: "nope.entire.io",
			wantOK:    true,
			wantCell:  "us-1",
			wantJur:   "us",
		},
	}

for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

got, ok := selectRoutablePlacement(tt.mirrors, tt.preferred)
			if ok != tt.wantOK {
				t.Fatalf("ok = %v, want %v", ok, tt.wantOK)
			}
			if !tt.wantOK {
				return
			}
			if got.Cell != tt.wantCell || got.Jurisdiction != tt.wantJur {
				t.Fatalf("placement = %+v, want cell %q jurisdiction %q", got, tt.wantCell, tt.wantJur)
			}
		})
	}
}

func TestForgeToMirrorProvider(t *testing.T) {
	t.Parallel()

for _, forge := range []string{"gh", "github", "GitHub", " gh "} {
		if p, ok := forgeToMirrorProvider(forge); !ok || p != mirrorCloneProviderGitHub {
				t.Errorf("forgeToMirrorProvider(%q) = (%q, %v), want (%q, true)", forge, p, ok, mirrorCloneProviderGitHub)
			}
	}
	if _, ok := forgeToMirrorProvider("gitlab"); ok {
			t.Error("forgeToMirrorProvider(gitlab) = ok, want not ok")
	}
}
