Skip to content

Commit 7206db4

Browse files
authored
Standardize API responses (#1445)
1 parent 443ed02 commit 7206db4

File tree

20 files changed

+286
-260
lines changed

20 files changed

+286
-260
lines changed

cli/cluster/delete.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ func getReadyRealtimeAPIReplicasOrNil(operatorConfig OperatorConfig, apiName str
6060
return nil
6161
}
6262

63-
var apiRes schema.GetAPIResponse
63+
var apiRes schema.APIResponse
6464
if err = json.Unmarshal(httpRes, &apiRes); err != nil {
6565
return nil
6666
}
6767

68-
if apiRes.RealtimeAPI == nil {
68+
if apiRes.Status == nil {
6969
return nil
7070
}
7171

72-
totalReady := apiRes.RealtimeAPI.Status.Updated.Ready + apiRes.RealtimeAPI.Status.Stale.Ready
72+
totalReady := apiRes.Status.Updated.Ready + apiRes.Status.Stale.Ready
7373
return &totalReady
7474
}
7575

cli/cluster/deploy.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/cortexlabs/cortex/pkg/operator/schema"
2626
)
2727

28-
func Deploy(operatorConfig OperatorConfig, configPath string, deploymentBytesMap map[string][]byte, force bool) (schema.DeployResponse, error) {
28+
func Deploy(operatorConfig OperatorConfig, configPath string, deploymentBytesMap map[string][]byte, force bool) ([]schema.DeployResult, error) {
2929
params := map[string]string{
3030
"force": s.Bool(force),
3131
"configFileName": filepath.Base(configPath),
@@ -36,13 +36,13 @@ func Deploy(operatorConfig OperatorConfig, configPath string, deploymentBytesMap
3636

3737
response, err := HTTPUpload(operatorConfig, "/deploy", uploadInput, params)
3838
if err != nil {
39-
return schema.DeployResponse{}, err
39+
return nil, err
4040
}
4141

42-
var deployResponse schema.DeployResponse
43-
if err := json.Unmarshal(response, &deployResponse); err != nil {
44-
return schema.DeployResponse{}, errors.Wrap(err, "/deploy", string(response))
42+
var deployResults []schema.DeployResult
43+
if err := json.Unmarshal(response, &deployResults); err != nil {
44+
return nil, errors.Wrap(err, "/deploy", string(response))
4545
}
4646

47-
return deployResponse, nil
47+
return deployResults, nil
4848
}

cli/cluster/get.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,43 @@ import (
2424
"github.com/cortexlabs/cortex/pkg/operator/schema"
2525
)
2626

27-
func GetAPIs(operatorConfig OperatorConfig) (schema.GetAPIsResponse, error) {
27+
func GetAPIs(operatorConfig OperatorConfig) ([]schema.APIResponse, error) {
2828
httpRes, err := HTTPGet(operatorConfig, "/get")
2929
if err != nil {
30-
return schema.GetAPIsResponse{}, err
30+
return nil, err
3131
}
3232

33-
var apisRes schema.GetAPIsResponse
33+
var apisRes []schema.APIResponse
3434
if err = json.Unmarshal(httpRes, &apisRes); err != nil {
35-
return schema.GetAPIsResponse{}, errors.Wrap(err, "/get", string(httpRes))
35+
return nil, errors.Wrap(err, "/get", string(httpRes))
3636
}
3737
return apisRes, nil
3838
}
3939

40-
func GetAPI(operatorConfig OperatorConfig, apiName string) (schema.GetAPIResponse, error) {
40+
func GetAPI(operatorConfig OperatorConfig, apiName string) ([]schema.APIResponse, error) {
4141
httpRes, err := HTTPGet(operatorConfig, "/get/"+apiName)
4242
if err != nil {
43-
return schema.GetAPIResponse{}, err
43+
return nil, err
4444
}
4545

46-
var apiRes schema.GetAPIResponse
46+
var apiRes []schema.APIResponse
4747
if err = json.Unmarshal(httpRes, &apiRes); err != nil {
48-
return schema.GetAPIResponse{}, errors.Wrap(err, "/get/"+apiName, string(httpRes))
48+
return nil, errors.Wrap(err, "/get/"+apiName, string(httpRes))
4949
}
5050

5151
return apiRes, nil
5252
}
5353

54-
func GetJob(operatorConfig OperatorConfig, apiName string, jobID string) (schema.GetJobResponse, error) {
54+
func GetJob(operatorConfig OperatorConfig, apiName string, jobID string) (schema.JobResponse, error) {
5555
endpoint := path.Join("/batch", apiName, jobID)
5656
httpRes, err := HTTPGet(operatorConfig, endpoint)
5757
if err != nil {
58-
return schema.GetJobResponse{}, err
58+
return schema.JobResponse{}, err
5959
}
6060

61-
var jobRes schema.GetJobResponse
61+
var jobRes schema.JobResponse
6262
if err = json.Unmarshal(httpRes, &jobRes); err != nil {
63-
return schema.GetJobResponse{}, errors.Wrap(err, endpoint, string(httpRes))
63+
return schema.JobResponse{}, errors.Wrap(err, endpoint, string(httpRes))
6464
}
6565

6666
return jobRes, nil

cli/cmd/cluster.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import (
4545
"github.com/cortexlabs/cortex/pkg/types"
4646
"github.com/cortexlabs/cortex/pkg/types/clusterconfig"
4747
"github.com/cortexlabs/cortex/pkg/types/clusterstate"
48-
"github.com/cortexlabs/cortex/pkg/types/spec"
4948
"github.com/cortexlabs/cortex/pkg/types/userconfig"
5049
"github.com/spf13/cobra"
5150
)
@@ -623,21 +622,7 @@ var _exportCmd = &cobra.Command{
623622
exit.Error(err)
624623
}
625624

626-
var apiSpecs []spec.API
627-
628-
for _, batchAPI := range apisResponse.BatchAPIs {
629-
apiSpecs = append(apiSpecs, batchAPI.Spec)
630-
}
631-
632-
for _, realtimeAPI := range apisResponse.RealtimeAPIs {
633-
apiSpecs = append(apiSpecs, realtimeAPI.Spec)
634-
}
635-
636-
for _, trafficSplitter := range apisResponse.TrafficSplitters {
637-
apiSpecs = append(apiSpecs, trafficSplitter.Spec)
638-
}
639-
640-
if len(apiSpecs) == 0 {
625+
if len(apisResponse) == 0 {
641626
fmt.Println(fmt.Sprintf("no apis found in cluster named %s in %s", *accessConfig.ClusterName, *accessConfig.Region))
642627
exit.Ok()
643628
}
@@ -649,24 +634,24 @@ var _exportCmd = &cobra.Command{
649634
exit.Error(err)
650635
}
651636

652-
for _, apiSpec := range apiSpecs {
653-
baseDir := filepath.Join(exportPath, apiSpec.Name)
637+
for _, apiResponse := range apisResponse {
638+
baseDir := filepath.Join(exportPath, apiResponse.Spec.Name)
654639

655-
fmt.Println(fmt.Sprintf("exporting %s to %s", apiSpec.Name, baseDir))
640+
fmt.Println(fmt.Sprintf("exporting %s to %s", apiResponse.Spec.Name, baseDir))
656641

657642
err = files.CreateDir(baseDir)
658643
if err != nil {
659644
exit.Error(err)
660645
}
661646

662-
err = awsClient.DownloadFileFromS3(info.ClusterConfig.Bucket, apiSpec.RawAPIKey(info.ClusterConfig.ClusterName), path.Join(baseDir, apiSpec.FileName))
647+
err = awsClient.DownloadFileFromS3(info.ClusterConfig.Bucket, apiResponse.Spec.RawAPIKey(info.ClusterConfig.ClusterName), path.Join(baseDir, apiResponse.Spec.FileName))
663648
if err != nil {
664649
exit.Error(err)
665650
}
666651

667-
if apiSpec.Kind != userconfig.TrafficSplitterKind {
668-
zipFileLocation := path.Join(baseDir, path.Base(apiSpec.ProjectKey))
669-
err = awsClient.DownloadFileFromS3(info.ClusterConfig.Bucket, apiSpec.ProjectKey, zipFileLocation)
652+
if apiResponse.Spec.Kind != userconfig.TrafficSplitterKind {
653+
zipFileLocation := path.Join(baseDir, path.Base(apiResponse.Spec.ProjectKey))
654+
err = awsClient.DownloadFileFromS3(info.ClusterConfig.Bucket, apiResponse.Spec.ProjectKey, zipFileLocation)
670655
if err != nil {
671656
exit.Error(err)
672657
}

cli/cmd/deploy.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ var _deployCmd = &cobra.Command{
8989
exit.Error(ErrorDeployFromTopLevelDir("root", env.Provider))
9090
}
9191

92-
var deployResponse schema.DeployResponse
92+
var deployResults []schema.DeployResult
9393
if env.Provider == types.AWSProviderType {
9494
deploymentBytes, err := getDeploymentBytes(env.Provider, configPath)
9595
if err != nil {
9696
exit.Error(err)
9797
}
9898

99-
deployResponse, err = cluster.Deploy(MustGetOperatorConfig(env.Name), configPath, deploymentBytes, _flagDeployForce)
99+
deployResults, err = cluster.Deploy(MustGetOperatorConfig(env.Name), configPath, deploymentBytes, _flagDeployForce)
100100
if err != nil {
101101
exit.Error(err)
102102
}
@@ -106,22 +106,22 @@ var _deployCmd = &cobra.Command{
106106
exit.Error(err)
107107
}
108108

109-
deployResponse, err = local.Deploy(env, configPath, projectFiles, _flagDeployDisallowPrompt)
109+
deployResults, err = local.Deploy(env, configPath, projectFiles, _flagDeployDisallowPrompt)
110110
if err != nil {
111111
exit.Error(err)
112112
}
113113
}
114114

115115
if _flagOutput == flags.JSONOutputType {
116-
bytes, err := libjson.Marshal(deployResponse)
116+
bytes, err := libjson.Marshal(deployResults)
117117
if err != nil {
118118
exit.Error(err)
119119
}
120120
fmt.Println(string(bytes))
121121
return
122122
}
123123

124-
message := deployMessage(deployResponse.Results, env.Name)
124+
message := deployMessage(deployResults, env.Name)
125125
print.BoldFirstBlock(message)
126126
},
127127
}
@@ -292,7 +292,7 @@ func didAllResultsError(results []schema.DeployResult) bool {
292292
func getAPICommandsMessage(results []schema.DeployResult, envName string) string {
293293
apiName := "<api_name>"
294294
if len(results) == 1 {
295-
apiName = results[0].API.Name
295+
apiName = results[0].API.Spec.Name
296296
}
297297

298298
defaultEnv := getDefaultEnv(_generalCommandType)
@@ -309,7 +309,7 @@ func getAPICommandsMessage(results []schema.DeployResult, envName string) string
309309
if len(result.Error) > 0 {
310310
continue
311311
}
312-
if result.API.API != nil && result.API.Kind == userconfig.RealtimeAPIKind {
312+
if result.API != nil && result.API.Spec.Kind == userconfig.RealtimeAPIKind {
313313
items.Add(fmt.Sprintf("cortex logs %s%s", apiName, envArg), "(stream api logs)")
314314
break
315315
}

0 commit comments

Comments
 (0)