Skip to content

Commit 244850b

Browse files
committed
move TenantID into types package
1 parent b2b1cdd commit 244850b

17 files changed

+90
-89
lines changed

src/cmd/cli/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import (
2626
"github.com/defang-io/defang/src/pkg"
2727
"github.com/defang-io/defang/src/pkg/cli"
2828
"github.com/defang-io/defang/src/pkg/scope"
29-
defangv1 "github.com/defang-io/defang/src/protos/io/defang/v1"
29+
"github.com/defang-io/defang/src/pkg/types"
30+
v1 "github.com/defang-io/defang/src/protos/io/defang/v1"
3031
"github.com/defang-io/defang/src/protos/io/defang/v1/defangv1connect"
3132
)
3233

@@ -41,7 +42,7 @@ var (
4142
hasTty = term.IsTerminal(int(os.Stdin.Fd())) && !pkg.GetenvBool("CI")
4243
nonInteractive bool // set by the --non-interactive flag
4344
server string // set by the --cluster flag
44-
tenantId = pkg.DEFAULT_TENANT
45+
tenantId = types.DEFAULT_TENANT
4546
)
4647

4748
const autoConnect = "auto-connect" // annotation to indicate that a command needs to connect to the cluster
@@ -380,15 +381,15 @@ var composeCmd = &cobra.Command{
380381
Short: "Work with local Compose files",
381382
}
382383

383-
func printEndpoints(serviceInfos []*defangv1.ServiceInfo) {
384+
func printEndpoints(serviceInfos []*v1.ServiceInfo) {
384385
for _, serviceInfo := range serviceInfos {
385386
andEndpoints := ""
386387
if len(serviceInfo.Endpoints) > 0 {
387388
andEndpoints = "and will be available at:"
388389
}
389390
cli.Info(" * Service", serviceInfo.Service.Name, "is in state", serviceInfo.Status, andEndpoints)
390391
for i, endpoint := range serviceInfo.Endpoints {
391-
if serviceInfo.Service.Ports[i].Mode == defangv1.Mode_INGRESS {
392+
if serviceInfo.Service.Ports[i].Mode == v1.Mode_INGRESS {
392393
endpoint = "https://" + endpoint
393394
}
394395
fmt.Println(" -", endpoint)
@@ -592,7 +593,7 @@ var tokenCmd = &cobra.Command{
592593
var expires, _ = cmd.Flags().GetDuration("expires")
593594

594595
// TODO: should default to use the current tenant, not the default tenant
595-
return cli.Token(cmd.Context(), client, clientId, pkg.DEFAULT_TENANT, expires, scope.Scope(s))
596+
return cli.Token(cmd.Context(), client, clientId, types.DEFAULT_TENANT, expires, scope.Scope(s))
596597
},
597598
}
598599

src/pkg/cli/compose.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/bufbuild/connect-go"
2323
"github.com/compose-spec/compose-go/v2/loader"
2424
"github.com/compose-spec/compose-go/v2/types"
25-
pb "github.com/defang-io/defang/src/protos/io/defang/v1"
25+
v1 "github.com/defang-io/defang/src/protos/io/defang/v1"
2626
"github.com/defang-io/defang/src/protos/io/defang/v1/defangv1connect"
2727
"github.com/moby/patternmatcher"
2828
"github.com/moby/patternmatcher/ignorefile"
@@ -80,17 +80,17 @@ func resolveEnv(k string) *string {
8080
return &v
8181
}
8282

83-
func convertPlatform(platform string) pb.Platform {
83+
func convertPlatform(platform string) v1.Platform {
8484
switch platform {
8585
default:
8686
logrus.Warnf("Unsupported platform: %q (assuming linux)", platform)
8787
fallthrough
8888
case "", "linux":
89-
return pb.Platform_LINUX_ANY
89+
return v1.Platform_LINUX_ANY
9090
case "linux/amd64":
91-
return pb.Platform_LINUX_AMD64
91+
return v1.Platform_LINUX_AMD64
9292
case "linux/arm64", "linux/arm64/v8", "linux/arm64/v7", "linux/arm64/v6":
93-
return pb.Platform_LINUX_ARM64
93+
return v1.Platform_LINUX_ARM64
9494
}
9595
}
9696

@@ -152,7 +152,7 @@ func getRemoteBuildContext(ctx context.Context, client defangv1connect.FabricCon
152152
return uploadTarball(ctx, client, buffer, digest)
153153
}
154154

155-
func convertPort(port types.ServicePortConfig) (*pb.Port, error) {
155+
func convertPort(port types.ServicePortConfig) (*v1.Port, error) {
156156
if port.Target < 1 || port.Target > 32767 {
157157
return nil, fmt.Errorf("port target must be an integer between 1 and 32767: %v", port.Target)
158158
}
@@ -163,7 +163,7 @@ func convertPort(port types.ServicePortConfig) (*pb.Port, error) {
163163
return nil, fmt.Errorf("port published must be empty or equal to target: %v", port.Published)
164164
}
165165

166-
pbPort := &pb.Port{
166+
pbPort := &v1.Port{
167167
// Mode string `yaml:",omitempty" json:"mode,omitempty"`
168168
// HostIP string `mapstructure:"host_ip" yaml:"host_ip,omitempty" json:"host_ip,omitempty"`
169169
// Published string `yaml:",omitempty" json:"published,omitempty"`
@@ -173,17 +173,17 @@ func convertPort(port types.ServicePortConfig) (*pb.Port, error) {
173173

174174
switch port.Protocol {
175175
case "":
176-
pbPort.Protocol = pb.Protocol_ANY // defaults to HTTP in CD
176+
pbPort.Protocol = v1.Protocol_ANY // defaults to HTTP in CD
177177
case "tcp":
178-
pbPort.Protocol = pb.Protocol_TCP
178+
pbPort.Protocol = v1.Protocol_TCP
179179
case "udp":
180-
pbPort.Protocol = pb.Protocol_UDP
180+
pbPort.Protocol = v1.Protocol_UDP
181181
case "http": // TODO: not per spec
182-
pbPort.Protocol = pb.Protocol_HTTP
182+
pbPort.Protocol = v1.Protocol_HTTP
183183
case "http2": // TODO: not per spec
184-
pbPort.Protocol = pb.Protocol_HTTP2
184+
pbPort.Protocol = v1.Protocol_HTTP2
185185
case "grpc": // TODO: not per spec
186-
pbPort.Protocol = pb.Protocol_GRPC
186+
pbPort.Protocol = v1.Protocol_GRPC
187187
default:
188188
return nil, fmt.Errorf("port protocol not one of [tcp udp http http2 grpc]: %v", port.Protocol)
189189
}
@@ -195,26 +195,26 @@ func convertPort(port types.ServicePortConfig) (*pb.Port, error) {
195195
logrus.Warn("No port mode was specified; assuming 'host' (add 'mode' to silence)")
196196
fallthrough
197197
case "host":
198-
pbPort.Mode = pb.Mode_HOST
198+
pbPort.Mode = v1.Mode_HOST
199199
case "ingress":
200200
// This code is unnecessarily complex because compose-go silently converts short syntax to ingress+tcp
201201
if port.Published != "" {
202202
logrus.Warn("Published ports are not supported in ingress mode; assuming 'host' (add 'mode' to silence)")
203203
break
204204
}
205-
pbPort.Mode = pb.Mode_INGRESS
206-
if pbPort.Protocol == pb.Protocol_TCP || pbPort.Protocol == pb.Protocol_UDP {
205+
pbPort.Mode = v1.Mode_INGRESS
206+
if pbPort.Protocol == v1.Protocol_TCP || pbPort.Protocol == v1.Protocol_UDP {
207207
logrus.Warn("TCP ingress is not supported; assuming HTTP")
208-
pbPort.Protocol = pb.Protocol_HTTP
208+
pbPort.Protocol = v1.Protocol_HTTP
209209
}
210210
default:
211211
return nil, fmt.Errorf("port mode not one of [host ingress]: %v", port.Mode)
212212
}
213213
return pbPort, nil
214214
}
215215

216-
func convertPorts(ports []types.ServicePortConfig) ([]*pb.Port, error) {
217-
var pbports []*pb.Port
216+
func convertPorts(ports []types.ServicePortConfig) ([]*v1.Port, error) {
217+
var pbports []*v1.Port
218218
for _, port := range ports {
219219
pbPort, err := convertPort(port)
220220
if err != nil {
@@ -227,7 +227,7 @@ func convertPorts(ports []types.ServicePortConfig) ([]*pb.Port, error) {
227227

228228
func uploadTarball(ctx context.Context, client defangv1connect.FabricControllerClient, body *bytes.Buffer, digest string) (string, error) {
229229
// Upload the tarball to the fabric controller storage; TODO: use a streaming API
230-
ureq := &pb.UploadURLRequest{Digest: digest}
230+
ureq := &v1.UploadURLRequest{Digest: digest}
231231
res, err := client.CreateUploadURL(ctx, connect.NewRequest(ureq))
232232
if err != nil {
233233
return "", err

src/pkg/cli/composeRestart.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package cli
33
import (
44
"context"
55

6-
pb "github.com/defang-io/defang/src/protos/io/defang/v1"
6+
v1 "github.com/defang-io/defang/src/protos/io/defang/v1"
77
"github.com/defang-io/defang/src/protos/io/defang/v1/defangv1connect"
88
)
99

10-
func ComposeRestart(ctx context.Context, client defangv1connect.FabricControllerClient, filePath, projectName string) ([]*pb.ServiceInfo, error) {
10+
func ComposeRestart(ctx context.Context, client defangv1connect.FabricControllerClient, filePath, projectName string) ([]*v1.ServiceInfo, error) {
1111
project, err := loadDockerCompose(filePath, projectName)
1212
if err != nil {
1313
return nil, err

src/pkg/cli/composeStart.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
"github.com/bufbuild/connect-go"
1212
"github.com/compose-spec/compose-go/v2/types"
1313
"github.com/defang-io/defang/src/pkg"
14-
pb "github.com/defang-io/defang/src/protos/io/defang/v1"
14+
v1 "github.com/defang-io/defang/src/protos/io/defang/v1"
1515
"github.com/defang-io/defang/src/protos/io/defang/v1/defangv1connect"
1616
"github.com/sirupsen/logrus"
1717
)
1818

1919
// ComposeStart reads a docker-compose.yml file and uploads the services to the fabric controller
20-
func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerClient, filePath, projectName string, force bool) ([]*pb.ServiceInfo, error) {
20+
func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerClient, filePath, projectName string, force bool) ([]*v1.ServiceInfo, error) {
2121
project, err := loadDockerCompose(filePath, projectName)
2222
if err != nil {
2323
return nil, &ComposeError{err}
@@ -221,11 +221,11 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
221221
//
222222
// Publish updates
223223
//
224-
var services []*pb.Service
224+
var services []*v1.Service
225225
for _, svccfg := range project.Services {
226-
var healthcheck *pb.HealthCheck
226+
var healthcheck *v1.HealthCheck
227227
if svccfg.HealthCheck != nil && len(svccfg.HealthCheck.Test) > 0 && !svccfg.HealthCheck.Disable {
228-
healthcheck = &pb.HealthCheck{
228+
healthcheck = &v1.HealthCheck{
229229
Test: svccfg.HealthCheck.Test,
230230
}
231231
if nil != svccfg.HealthCheck.Interval {
@@ -246,15 +246,15 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
246246
}
247247
// Show a warning when we have ingress ports but no explicit healthcheck
248248
for _, port := range ports {
249-
if port.Mode == pb.Mode_INGRESS && healthcheck == nil {
249+
if port.Mode == v1.Mode_INGRESS && healthcheck == nil {
250250
logrus.Warn("ingress port without healthcheck defaults to GET / HTTP/1.1")
251251
break
252252
}
253253
}
254254

255-
var deploy *pb.Deploy
255+
var deploy *v1.Deploy
256256
if svccfg.Deploy != nil {
257-
deploy = &pb.Deploy{}
257+
deploy = &v1.Deploy{}
258258
deploy.Replicas = 1 // default to one replica per service; allow the user to override this to 0
259259
if svccfg.Deploy.Replicas != nil {
260260
deploy.Replicas = uint32(*svccfg.Deploy.Replicas)
@@ -269,16 +269,16 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
269269
panic(err) // was already validated above
270270
}
271271
}
272-
var devices []*pb.Device
272+
var devices []*v1.Device
273273
for _, d := range reservations.Devices {
274-
devices = append(devices, &pb.Device{
274+
devices = append(devices, &v1.Device{
275275
Capabilities: d.Capabilities,
276276
Count: uint32(d.Count),
277277
Driver: d.Driver,
278278
})
279279
}
280-
deploy.Resources = &pb.Resources{
281-
Reservations: &pb.Resource{
280+
deploy.Resources = &v1.Resources{
281+
Reservations: &v1.Resource{
282282
Cpus: float32(cpus),
283283
Memory: float32(reservations.MemoryBytes) / MiB,
284284
Devices: devices,
@@ -292,15 +292,15 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
292292
}
293293

294294
// Upload the build context, if any; TODO: parallelize
295-
var build *pb.Build
295+
var build *v1.Build
296296
if svccfg.Build != nil {
297297
// Pack the build context into a tarball and upload
298298
url, err := getRemoteBuildContext(ctx, client, svccfg.Name, svccfg.Build, force)
299299
if err != nil {
300300
return nil, err
301301
}
302302

303-
build = &pb.Build{
303+
build = &v1.Build{
304304
Context: url,
305305
Dockerfile: svccfg.Build.Dockerfile,
306306
ShmSize: float32(svccfg.Build.ShmSize) / MiB,
@@ -331,9 +331,9 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
331331
}
332332

333333
// Extract secret references
334-
var secrets []*pb.Secret
334+
var secrets []*v1.Secret
335335
for _, secret := range svccfg.Secrets {
336-
secrets = append(secrets, &pb.Secret{
336+
secrets = append(secrets, &v1.Secret{
337337
Source: secret.Source,
338338
})
339339
}
@@ -343,7 +343,7 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
343343
init = *svccfg.Init
344344
}
345345

346-
services = append(services, &pb.Service{
346+
services = append(services, &v1.Service{
347347
Name: NormalizeServiceName(svccfg.Name),
348348
Image: svccfg.Image,
349349
Build: build,
@@ -364,7 +364,7 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
364364
return nil, &ComposeError{fmt.Errorf("no services found")}
365365
}
366366

367-
var serviceInfos []*pb.ServiceInfo
367+
var serviceInfos []*v1.ServiceInfo
368368
for _, service := range services {
369369
if DoDryRun {
370370
PrintObject(service.Name, service)

src/pkg/cli/compose_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
"github.com/bufbuild/connect-go"
1616
"github.com/compose-spec/compose-go/v2/types"
17-
pb "github.com/defang-io/defang/src/protos/io/defang/v1"
17+
v1 "github.com/defang-io/defang/src/protos/io/defang/v1"
1818
"github.com/defang-io/defang/src/protos/io/defang/v1/defangv1connect"
1919
)
2020

@@ -79,7 +79,7 @@ func TestConvertPort(t *testing.T) {
7979
tests := []struct {
8080
name string
8181
input types.ServicePortConfig
82-
expected *pb.Port
82+
expected *v1.Port
8383
wantErr string
8484
}{
8585
{
@@ -90,7 +90,7 @@ func TestConvertPort(t *testing.T) {
9090
{
9191
name: "Undefined mode and protocol, target only",
9292
input: types.ServicePortConfig{Target: 1234},
93-
expected: &pb.Port{Target: 1234, Mode: pb.Mode_HOST},
93+
expected: &v1.Port{Target: 1234, Mode: v1.Mode_HOST},
9494
},
9595
{
9696
name: "Published range xfail",
@@ -100,12 +100,12 @@ func TestConvertPort(t *testing.T) {
100100
{
101101
name: "Implied ingress mode, defined protocol, published equals target",
102102
input: types.ServicePortConfig{Mode: "ingress", Protocol: "tcp", Published: "1234", Target: 1234},
103-
expected: &pb.Port{Target: 1234, Mode: pb.Mode_HOST, Protocol: pb.Protocol_TCP},
103+
expected: &v1.Port{Target: 1234, Mode: v1.Mode_HOST, Protocol: v1.Protocol_TCP},
104104
},
105105
{
106106
name: "Implied ingress mode, udp protocol, published equals target",
107107
input: types.ServicePortConfig{Mode: "ingress", Protocol: "udp", Published: "1234", Target: 1234},
108-
expected: &pb.Port{Target: 1234, Mode: pb.Mode_HOST, Protocol: pb.Protocol_UDP},
108+
expected: &v1.Port{Target: 1234, Mode: v1.Mode_HOST, Protocol: v1.Protocol_UDP},
109109
},
110110
{
111111
name: "Localhost IP, unsupported mode and protocol xfail",
@@ -115,7 +115,7 @@ func TestConvertPort(t *testing.T) {
115115
{
116116
name: "Ingress mode without host IP, single target",
117117
input: types.ServicePortConfig{Mode: "ingress", Protocol: "tcp", Target: 1234},
118-
expected: &pb.Port{Target: 1234, Mode: pb.Mode_INGRESS, Protocol: pb.Protocol_HTTP},
118+
expected: &v1.Port{Target: 1234, Mode: v1.Mode_INGRESS, Protocol: v1.Protocol_HTTP},
119119
},
120120
{
121121
name: "Ingress mode without host IP, single target, published range xfail",
@@ -240,14 +240,14 @@ type mockGrpcClient struct {
240240
url string
241241
}
242242

243-
func (m mockGrpcClient) CreateUploadURL(ctx context.Context, req *connect.Request[pb.UploadURLRequest]) (*connect.Response[pb.UploadURLResponse], error) {
244-
return connect.NewResponse(&pb.UploadURLResponse{Url: m.url + req.Msg.Digest}), nil
243+
func (m mockGrpcClient) CreateUploadURL(ctx context.Context, req *connect.Request[v1.UploadURLRequest]) (*connect.Response[v1.UploadURLResponse], error) {
244+
return connect.NewResponse(&v1.UploadURLResponse{Url: m.url + req.Msg.Digest}), nil
245245
}
246246

247-
func (mockGrpcClient) Subscribe(context.Context, *connect.Request[pb.SubscribeRequest]) (*connect.ServerStreamForClient[pb.SubscribeResponse], error) {
247+
func (mockGrpcClient) Subscribe(context.Context, *connect.Request[v1.SubscribeRequest]) (*connect.ServerStreamForClient[v1.SubscribeResponse], error) {
248248
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("io.defang.v1.FabricController.Subscribe is not implemented"))
249249
}
250250

251-
func (mockGrpcClient) Tail(context.Context, *connect.Request[pb.TailRequest]) (*connect.ServerStreamForClient[pb.TailResponse], error) {
251+
func (mockGrpcClient) Tail(context.Context, *connect.Request[v1.TailRequest]) (*connect.ServerStreamForClient[v1.TailResponse], error) {
252252
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("io.defang.v1.FabricController.Tail is not implemented"))
253253
}

src/pkg/cli/connect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"strings"
66

77
"github.com/bufbuild/connect-go"
8-
"github.com/defang-io/defang/src/pkg"
98
"github.com/defang-io/defang/src/pkg/auth"
9+
"github.com/defang-io/defang/src/pkg/types"
1010
"github.com/defang-io/defang/src/protos/io/defang/v1/defangv1connect"
1111
)
1212

13-
func Connect(server string) (defangv1connect.FabricControllerClient, pkg.TenantID) {
13+
func Connect(server string) (defangv1connect.FabricControllerClient, types.TenantID) {
1414
accessToken := GetExistingToken(server)
1515
tenantId, _ := TenantFromAccessToken(accessToken)
1616
_, host := SplitTenantHost(server) // TODO: use this returned tenantId when we have no access token

src/pkg/cli/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55

66
"github.com/bufbuild/connect-go"
7-
pb "github.com/defang-io/defang/src/protos/io/defang/v1"
7+
v1 "github.com/defang-io/defang/src/protos/io/defang/v1"
88
"github.com/defang-io/defang/src/protos/io/defang/v1/defangv1connect"
99
)
1010

@@ -15,7 +15,7 @@ func Delete(ctx context.Context, client defangv1connect.FabricControllerClient,
1515
return "", nil
1616
}
1717

18-
resp, err := client.Delete(ctx, connect.NewRequest(&pb.DeleteRequest{Names: names}))
18+
resp, err := client.Delete(ctx, connect.NewRequest(&v1.DeleteRequest{Names: names}))
1919
if err != nil {
2020
return "", err
2121
}

0 commit comments

Comments
 (0)