Skip to content

Commit

Permalink
Replace Version by Health in the CLI and SDK
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Imperiale <[email protected]>
  • Loading branch information
manuio committed Jan 11, 2022
1 parent 794e871 commit 247d5b4
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 36 deletions.
6 changes: 3 additions & 3 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ make cli

## Usage
### Service
#### Get the version of Mainflux services
#### Get Mainflux Things services Health Check
```bash
mainflux-cli version
mainflux-cli health
```

### Users management
Expand Down Expand Up @@ -239,4 +239,4 @@ mainflux-cli groups members <group_id> <user_auth_token>
#### List groups that user belongs to
```bash
mainflux-cli groups membership <user_id> <user_auth_token>
```
```
12 changes: 6 additions & 6 deletions cli/version.go → cli/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ package cli

import "github.com/spf13/cobra"

// NewVersionCmd returns version command.
func NewVersionCmd() *cobra.Command {
// NewHealthCmd returns health check command.
func NewHealthCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Mainflux services version",
Long: `Mainflux services version: get version of Mainflux Things Service`,
Use: "health",
Short: "Health Check",
Long: `Mainflux Things service Health Check`,
Run: func(cmd *cobra.Command, args []string) {
v, err := sdk.Version()
v, err := sdk.Health()
if err != nil {
logError(err)
return
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
}

// API commands
versionCmd := cli.NewVersionCmd()
healthCmd := cli.NewHealthCmd()
usersCmd := cli.NewUsersCmd()
thingsCmd := cli.NewThingsCmd()
groupsCmd := cli.NewGroupsCmd()
Expand All @@ -52,7 +52,7 @@ func main() {
certsCmd := cli.NewCertsCmd()

// Root Commands
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(healthCmd)
rootCmd.AddCommand(usersCmd)
rootCmd.AddCommand(groupsCmd)
rootCmd.AddCommand(thingsCmd)
Expand Down
2 changes: 1 addition & 1 deletion coap/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
service coap.Service
)

//MakeHTTPHandler creates handler for version endpoint.
// MakeHandler returns a HTTP handler for API endpoints.
func MakeHTTPHandler() http.Handler {
b := bone.New()
b.GetFunc("/health", mainflux.Health(protocol))
Expand Down
2 changes: 1 addition & 1 deletion health.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type HealthInfo struct {
// Status contains service status.
Status string `json:"status"`

// Version contains service current version.
// Version contains current service version.
Version string `json:"version"`

// Description contains service description.
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ func (sdk mfSDK) UpdateChannel(channel Channel, token string) error
func (sdk mfSDK) UpdateThing(thing Thing, token string) error
UpdateThing - updates thing by ID

func (sdk mfSDK) Version() (string, error)
Version - server health check
func (sdk mfSDK) Health() (mainflux.Health, error)
Health - things service health check
```
19 changes: 8 additions & 11 deletions pkg/sdk/go/version.go → pkg/sdk/go/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,32 @@ import (
"io/ioutil"
"net/http"

"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/pkg/errors"
)

type health struct {
Version string `json:"version"`
}

func (sdk mfSDK) Version() (string, error) {
func (sdk mfSDK) Health() (mainflux.HealthInfo, error) {
url := fmt.Sprintf("%s/health", sdk.thingsURL)

resp, err := sdk.client.Get(url)
if err != nil {
return "", err
return mainflux.HealthInfo{}, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
return mainflux.HealthInfo{}, err
}

if resp.StatusCode != http.StatusOK {
return "", errors.Wrap(ErrFetchVersion, errors.New(resp.Status))
return mainflux.HealthInfo{}, errors.Wrap(ErrFetchHealth, errors.New(resp.Status))
}

var h health
var h mainflux.HealthInfo
if err := json.Unmarshal(body, &h); err != nil {
return "", err
return mainflux.HealthInfo{}, err
}

return h.Version, nil
return h, nil
}
8 changes: 4 additions & 4 deletions pkg/sdk/go/version_test.go → pkg/sdk/go/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestVersion(t *testing.T) {
func TestHealth(t *testing.T) {
svc := newThingsService(map[string]string{token: email})
ts := newThingsServer(svc)
defer ts.Close()
Expand All @@ -24,14 +24,14 @@ func TestVersion(t *testing.T) {
empty bool
err error
}{
"get version": {
"get things service version": {
empty: false,
err: nil,
},
}
for desc, tc := range cases {
ver, err := mainfluxSDK.Version()
h, err := mainfluxSDK.Health()
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", desc, tc.err, err))
assert.Equal(t, tc.empty, ver == "", fmt.Sprintf("%s: expected non-empty result version, got %s", desc, ver))
assert.Equal(t, tc.empty, h.Version == "", fmt.Sprintf("%s: expected non-empty result version, got %s", desc, h.Version))
}
}
10 changes: 6 additions & 4 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"crypto/tls"
"errors"
"net/http"

"github.com/mainflux/mainflux"
)

const (
Expand Down Expand Up @@ -52,8 +54,8 @@ var (
// was passed.
ErrInvalidContentType = errors.New("Unknown Content Type")

// ErrFetchVersion indicates that fetching of version failed.
ErrFetchVersion = errors.New("failed to fetch version")
// ErrFetchHealth indicates that fetching of health check failed.
ErrFetchHealth = errors.New("failed to fetch health check")

// ErrFailedWhitelist failed to whitelist configs
ErrFailedWhitelist = errors.New("failed to whitelist")
Expand Down Expand Up @@ -224,8 +226,8 @@ type SDK interface {
// SetContentType sets message content type.
SetContentType(ct ContentType) error

// Version returns used mainflux version.
Version() (string, error)
// Health returns things service health check.
Health() (mainflux.HealthInfo, error)

// AddBootstrap add bootstrap configuration
AddBootstrap(token string, cfg BootstrapConfig) (string, error)
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/spf13/cobra/user_guide.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 247d5b4

Please sign in to comment.