Skip to content

Commit 0d37260

Browse files
authored
Merge pull request #7 from mgrzybek/healthcheck
Call /public/healthcheck
2 parents 6df288c + fc7b149 commit 0d37260

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

cmd/health.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"github.com/spf13/cobra"
20+
"onyxiactl/utils"
21+
)
22+
23+
func init() {
24+
rootCmd.AddCommand(healthCmd)
25+
}
26+
27+
var healthCmd = &cobra.Command{
28+
Use: "health",
29+
Aliases: []string{"ping"},
30+
Short: "Ping the provider.",
31+
Long: `Call the healthcheck URL.`,
32+
Run: func(cmd *cobra.Command, args []string) { health(cmd) },
33+
}
34+
35+
func health(cmd *cobra.Command) error {
36+
onyxiaURL, _ := cmd.Flags().GetString("onyxiaURL")
37+
38+
_, e := utils.CallAPIGet(onyxiaURL+"/public/healthcheck", "")
39+
if e != nil {
40+
utils.PrintErrorAndExit("CRIT: provider is NOT healthy, " + e.Error() + ".")
41+
}
42+
43+
utils.PrintMessage("OK: provider is healthy.")
44+
return nil
45+
}

utils/api.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99

1010
func CallAPIGet(url, token string) ([]byte, error) {
1111
req, _ := http.NewRequest("GET", url, nil)
12-
req.Header.Add("Authorization", "Bearer "+token)
12+
if token != "" {
13+
req.Header.Add("Authorization", "Bearer "+token)
14+
}
1315
req.Header.Set("Content-Type", "application/json")
1416

1517
client := &http.Client{}

utils/console.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"fmt"
55
"encoding/json"
6+
"os"
67
)
78

89
func PrintStruct(v interface{}) {
@@ -15,3 +16,12 @@ func PrintStructs(v []interface{}) {
1516
PrintStruct(i)
1617
}
1718
}
19+
20+
func PrintMessage(m string) {
21+
fmt.Print(m)
22+
}
23+
24+
func PrintErrorMessageAndExit(m string) {
25+
fmt.Print(m)
26+
os.Exit(1)
27+
}

0 commit comments

Comments
 (0)