Skip to content

Commit ef5fd06

Browse files
committed
Merge branch '191-dle-version-report' into 'master'
feat: report DLE version in CLI (#191): * create a new CLI command `dblab instance version` * make a request `GET /healthz` from CLI Closes #191 See merge request postgres-ai/database-lab!212
2 parents 98a2bb7 + ce53283 commit ef5fd06

File tree

5 files changed

+70
-22
lines changed

5 files changed

+70
-22
lines changed

cmd/cli/commands/instance/actions.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,45 @@ import (
1515
)
1616

1717
// status runs a request to get status of the instance.
18-
func status() func(*cli.Context) error {
19-
return func(cliCtx *cli.Context) error {
20-
dblabClient, err := commands.ClientByCLIContext(cliCtx)
21-
if err != nil {
22-
return err
23-
}
18+
func status(cliCtx *cli.Context) error {
19+
dblabClient, err := commands.ClientByCLIContext(cliCtx)
20+
if err != nil {
21+
return err
22+
}
23+
24+
list, err := dblabClient.Status(cliCtx.Context)
25+
if err != nil {
26+
return err
27+
}
28+
29+
commandResponse, err := json.MarshalIndent(list, "", " ")
30+
if err != nil {
31+
return err
32+
}
2433

25-
list, err := dblabClient.Status(cliCtx.Context)
26-
if err != nil {
27-
return err
28-
}
34+
_, err = fmt.Fprintln(cliCtx.App.Writer, string(commandResponse))
2935

30-
commandResponse, err := json.MarshalIndent(list, "", " ")
31-
if err != nil {
32-
return err
33-
}
36+
return err
37+
}
38+
39+
// health runs a request to get health info of the instance.
40+
func health(cliCtx *cli.Context) error {
41+
dblabClient, err := commands.ClientByCLIContext(cliCtx)
42+
if err != nil {
43+
return err
44+
}
3445

35-
_, err = fmt.Fprintln(cliCtx.App.Writer, string(commandResponse))
46+
list, err := dblabClient.Health(cliCtx.Context)
47+
if err != nil {
48+
return err
49+
}
3650

51+
commandResponse, err := json.MarshalIndent(list, "", " ")
52+
if err != nil {
3753
return err
3854
}
55+
56+
_, err = fmt.Fprintln(cliCtx.App.Writer, string(commandResponse))
57+
58+
return err
3959
}

cmd/cli/commands/instance/command_list.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ func CommandList() []*cli.Command {
1818
{
1919
Name: "status",
2020
Usage: "display instance's status",
21-
Action: status(),
21+
Action: status,
22+
},
23+
{
24+
Name: "version",
25+
Usage: "display instance's version",
26+
Action: health,
2227
},
2328
},
2429
},

pkg/client/dblabapi/status.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,26 @@ func (c *Client) Status(ctx context.Context) (*models.InstanceStatus, error) {
3838

3939
return &instanceStatus, nil
4040
}
41+
42+
// Health provides instance health info.
43+
func (c *Client) Health(ctx context.Context) (*models.Health, error) {
44+
request, err := http.NewRequest(http.MethodGet, c.URL("/healthz").String(), nil)
45+
if err != nil {
46+
return nil, errors.Wrap(err, "failed to make a request")
47+
}
48+
49+
response, err := c.Do(ctx, request)
50+
if err != nil {
51+
return nil, errors.Wrap(err, "failed to get response")
52+
}
53+
54+
defer func() { _ = response.Body.Close() }()
55+
56+
var health models.Health
57+
58+
if err := json.NewDecoder(response.Body).Decode(&health); err != nil {
59+
return nil, errors.Wrap(err, "failed to get response")
60+
}
61+
62+
return &health, nil
63+
}

pkg/models/instance_status.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ type InstanceStatus struct {
1212
NumClones uint64 `json:"numClones"`
1313
Clones []*Clone `json:"clones"`
1414
}
15+
16+
// Health represents a response for heath-check requests.
17+
type Health struct {
18+
Version string `json:"version"`
19+
}

pkg/srv/routes.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ import (
1919
"gitlab.com/postgres-ai/database-lab/version"
2020
)
2121

22-
// HealthResponse represents a response for heath-check requests.
23-
type HealthResponse struct {
24-
Version string `json:"version"`
25-
}
26-
2722
func (s *Server) getInstanceStatus(w http.ResponseWriter, r *http.Request) {
2823
status, err := s.Cloning.GetInstanceState()
2924
if err != nil {
@@ -295,7 +290,7 @@ func (s *Server) stopObservation(w http.ResponseWriter, r *http.Request) {
295290
func (s *Server) healthCheck(w http.ResponseWriter, _ *http.Request) {
296291
w.Header().Set("Content-Type", "application/json; charset=utf-8")
297292

298-
healthResponse := HealthResponse{
293+
healthResponse := models.Health{
299294
Version: version.GetVersion(),
300295
}
301296

0 commit comments

Comments
 (0)