feat: implement ping command (#31)
* build: use deepmap/oapi-codegen to generate an HTTP client * feat: add global CLI options * feat: load local config to find host and token * feat: implement ping command * test: add unit tests for ping command
This commit is contained in:
113
internal/api/api.yml
Normal file
113
internal/api/api.yml
Normal file
@ -0,0 +1,113 @@
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: Influx API Service
|
||||
version: 2.0.0
|
||||
servers:
|
||||
- url: /api/v2
|
||||
paths:
|
||||
/health:
|
||||
servers:
|
||||
- url: /
|
||||
get:
|
||||
operationId: GetHealth
|
||||
tags:
|
||||
- Health
|
||||
summary: Get the health of an instance
|
||||
parameters:
|
||||
- $ref: '#/components/parameters/TraceSpan'
|
||||
responses:
|
||||
'200':
|
||||
description: The instance is healthy
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/HealthCheck'
|
||||
'503':
|
||||
description: The instance is unhealthy
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/HealthCheck'
|
||||
default:
|
||||
description: Unexpected error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Error'
|
||||
components:
|
||||
parameters:
|
||||
TraceSpan:
|
||||
in: header
|
||||
name: Zap-Trace-Span
|
||||
description: OpenTracing span context
|
||||
example:
|
||||
trace_id: '1'
|
||||
span_id: '1'
|
||||
baggage:
|
||||
key: value
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
schemas:
|
||||
HealthCheck:
|
||||
type: object
|
||||
required:
|
||||
- name
|
||||
- status
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
message:
|
||||
type: string
|
||||
checks:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/HealthCheck'
|
||||
status:
|
||||
$ref: '#/components/schemas/HealthCheckStatus'
|
||||
version:
|
||||
type: string
|
||||
commit:
|
||||
type: string
|
||||
HealthCheckStatus:
|
||||
type: string
|
||||
readOnly: true
|
||||
enum:
|
||||
- pass
|
||||
- fail
|
||||
Error:
|
||||
properties:
|
||||
code:
|
||||
$ref: '#/components/schemas/ErrorCode'
|
||||
message:
|
||||
readOnly: true
|
||||
description: message is a human-readable message.
|
||||
type: string
|
||||
op:
|
||||
readOnly: true
|
||||
description: op describes the logical code operation during error. Useful for debugging.
|
||||
type: string
|
||||
err:
|
||||
readOnly: true
|
||||
description: err is a stack of errors that occurred during processing of the request. Useful for debugging.
|
||||
type: string
|
||||
required:
|
||||
- code
|
||||
- message
|
||||
ErrorCode:
|
||||
description: machine-readable error code.
|
||||
readOnly: true
|
||||
type: string
|
||||
enum:
|
||||
- internal error
|
||||
- not found
|
||||
- conflict
|
||||
- invalid
|
||||
- unprocessable entity
|
||||
- empty value
|
||||
- unavailable
|
||||
- forbidden
|
||||
- too many requests
|
||||
- unauthorized
|
||||
- method not allowed
|
||||
- request too large
|
||||
263
internal/api/client.gen.go
Normal file
263
internal/api/client.gen.go
Normal file
@ -0,0 +1,263 @@
|
||||
// Package api provides primitives to interact with the openapi HTTP API.
|
||||
//
|
||||
// Code generated by github.com/deepmap/oapi-codegen DO NOT EDIT.
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/deepmap/oapi-codegen/pkg/runtime"
|
||||
)
|
||||
|
||||
// RequestEditorFn is the function signature for the RequestEditor callback function
|
||||
type RequestEditorFn func(ctx context.Context, req *http.Request) error
|
||||
|
||||
// Doer performs HTTP requests.
|
||||
//
|
||||
// The standard http.Client implements this interface.
|
||||
type HttpRequestDoer interface {
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// Client which conforms to the OpenAPI3 specification for this service.
|
||||
type Client struct {
|
||||
// The endpoint of the server conforming to this interface, with scheme,
|
||||
// https://api.deepmap.com for example. This can contain a path relative
|
||||
// to the server, such as https://api.deepmap.com/dev-test, and all the
|
||||
// paths in the swagger spec will be appended to the server.
|
||||
Server string
|
||||
|
||||
// Doer for performing requests, typically a *http.Client with any
|
||||
// customized settings, such as certificate chains.
|
||||
Client HttpRequestDoer
|
||||
|
||||
// A list of callbacks for modifying requests which are generated before sending over
|
||||
// the network.
|
||||
RequestEditors []RequestEditorFn
|
||||
}
|
||||
|
||||
// ClientOption allows setting custom parameters during construction
|
||||
type ClientOption func(*Client) error
|
||||
|
||||
// Creates a new Client, with reasonable defaults
|
||||
func NewClient(server string, opts ...ClientOption) (*Client, error) {
|
||||
// create a client with sane default values
|
||||
client := Client{
|
||||
Server: server,
|
||||
}
|
||||
// mutate client and add all optional params
|
||||
for _, o := range opts {
|
||||
if err := o(&client); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
// ensure the server URL always has a trailing slash
|
||||
if !strings.HasSuffix(client.Server, "/") {
|
||||
client.Server += "/"
|
||||
}
|
||||
// create httpClient, if not already present
|
||||
if client.Client == nil {
|
||||
client.Client = &http.Client{}
|
||||
}
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
// WithHTTPClient allows overriding the default Doer, which is
|
||||
// automatically created using http.Client. This is useful for tests.
|
||||
func WithHTTPClient(doer HttpRequestDoer) ClientOption {
|
||||
return func(c *Client) error {
|
||||
c.Client = doer
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithRequestEditorFn allows setting up a callback function, which will be
|
||||
// called right before sending the request. This can be used to mutate the request.
|
||||
func WithRequestEditorFn(fn RequestEditorFn) ClientOption {
|
||||
return func(c *Client) error {
|
||||
c.RequestEditors = append(c.RequestEditors, fn)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// The interface specification for the client above.
|
||||
type ClientInterface interface {
|
||||
// GetHealth request
|
||||
GetHealth(ctx context.Context, params *GetHealthParams, reqEditors ...RequestEditorFn) (*http.Response, error)
|
||||
}
|
||||
|
||||
func (c *Client) GetHealth(ctx context.Context, params *GetHealthParams, reqEditors ...RequestEditorFn) (*http.Response, error) {
|
||||
req, err := NewGetHealthRequest(c.Server, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
if err := c.applyEditors(ctx, req, reqEditors); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.Client.Do(req)
|
||||
}
|
||||
|
||||
// NewGetHealthRequest generates requests for GetHealth
|
||||
func NewGetHealthRequest(server string, params *GetHealthParams) (*http.Request, error) {
|
||||
var err error
|
||||
|
||||
serverURL, err := url.Parse(server)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
operationPath := fmt.Sprintf("/health")
|
||||
if operationPath[0] == '/' {
|
||||
operationPath = operationPath[1:]
|
||||
}
|
||||
operationURL := url.URL{
|
||||
Path: operationPath,
|
||||
}
|
||||
|
||||
queryURL := serverURL.ResolveReference(&operationURL)
|
||||
|
||||
req, err := http.NewRequest("GET", queryURL.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if params.ZapTraceSpan != nil {
|
||||
var headerParam0 string
|
||||
|
||||
headerParam0, err = runtime.StyleParamWithLocation("simple", false, "Zap-Trace-Span", runtime.ParamLocationHeader, *params.ZapTraceSpan)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Zap-Trace-Span", headerParam0)
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *Client) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error {
|
||||
for _, r := range c.RequestEditors {
|
||||
if err := r(ctx, req); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, r := range additionalEditors {
|
||||
if err := r(ctx, req); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClientWithResponses builds on ClientInterface to offer response payloads
|
||||
type ClientWithResponses struct {
|
||||
ClientInterface
|
||||
}
|
||||
|
||||
// NewClientWithResponses creates a new ClientWithResponses, which wraps
|
||||
// Client with return type handling
|
||||
func NewClientWithResponses(server string, opts ...ClientOption) (*ClientWithResponses, error) {
|
||||
client, err := NewClient(server, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ClientWithResponses{client}, nil
|
||||
}
|
||||
|
||||
// WithBaseURL overrides the baseURL.
|
||||
func WithBaseURL(baseURL string) ClientOption {
|
||||
return func(c *Client) error {
|
||||
newBaseURL, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Server = newBaseURL.String()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ClientWithResponsesInterface is the interface specification for the client with responses above.
|
||||
type ClientWithResponsesInterface interface {
|
||||
// GetHealth request
|
||||
GetHealthWithResponse(ctx context.Context, params *GetHealthParams, reqEditors ...RequestEditorFn) (*GetHealthResponse, error)
|
||||
}
|
||||
|
||||
type GetHealthResponse struct {
|
||||
Body []byte
|
||||
HTTPResponse *http.Response
|
||||
JSON200 *HealthCheck
|
||||
JSON503 *HealthCheck
|
||||
JSONDefault *Error
|
||||
}
|
||||
|
||||
// Status returns HTTPResponse.Status
|
||||
func (r GetHealthResponse) Status() string {
|
||||
if r.HTTPResponse != nil {
|
||||
return r.HTTPResponse.Status
|
||||
}
|
||||
return http.StatusText(0)
|
||||
}
|
||||
|
||||
// StatusCode returns HTTPResponse.StatusCode
|
||||
func (r GetHealthResponse) StatusCode() int {
|
||||
if r.HTTPResponse != nil {
|
||||
return r.HTTPResponse.StatusCode
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetHealthWithResponse request returning *GetHealthResponse
|
||||
func (c *ClientWithResponses) GetHealthWithResponse(ctx context.Context, params *GetHealthParams, reqEditors ...RequestEditorFn) (*GetHealthResponse, error) {
|
||||
rsp, err := c.GetHealth(ctx, params, reqEditors...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ParseGetHealthResponse(rsp)
|
||||
}
|
||||
|
||||
// ParseGetHealthResponse parses an HTTP response from a GetHealthWithResponse call
|
||||
func ParseGetHealthResponse(rsp *http.Response) (*GetHealthResponse, error) {
|
||||
bodyBytes, err := ioutil.ReadAll(rsp.Body)
|
||||
defer rsp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response := &GetHealthResponse{
|
||||
Body: bodyBytes,
|
||||
HTTPResponse: rsp,
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200:
|
||||
var dest HealthCheck
|
||||
if err := json.Unmarshal(bodyBytes, &dest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response.JSON200 = &dest
|
||||
|
||||
case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 503:
|
||||
var dest HealthCheck
|
||||
if err := json.Unmarshal(bodyBytes, &dest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response.JSON503 = &dest
|
||||
|
||||
case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true:
|
||||
var dest Error
|
||||
if err := json.Unmarshal(bodyBytes, &dest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response.JSONDefault = &dest
|
||||
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
4
internal/api/gen.go
Normal file
4
internal/api/gen.go
Normal file
@ -0,0 +1,4 @@
|
||||
package api
|
||||
|
||||
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen -package api -generate types -o types.gen.go api.yml
|
||||
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen -package api -generate client -o client.gen.go api.yml
|
||||
68
internal/api/types.gen.go
Normal file
68
internal/api/types.gen.go
Normal file
@ -0,0 +1,68 @@
|
||||
// Package api provides primitives to interact with the openapi HTTP API.
|
||||
//
|
||||
// Code generated by github.com/deepmap/oapi-codegen DO NOT EDIT.
|
||||
package api
|
||||
|
||||
// Error defines model for Error.
|
||||
type Error struct {
|
||||
|
||||
// machine-readable error code.
|
||||
Code ErrorCode `json:"code"`
|
||||
|
||||
// err is a stack of errors that occurred during processing of the request. Useful for debugging.
|
||||
Err *string `json:"err,omitempty"`
|
||||
|
||||
// message is a human-readable message.
|
||||
Message string `json:"message"`
|
||||
|
||||
// op describes the logical code operation during error. Useful for debugging.
|
||||
Op *string `json:"op,omitempty"`
|
||||
}
|
||||
|
||||
// machine-readable error code.
|
||||
type ErrorCode string
|
||||
|
||||
// List of ErrorCode
|
||||
const (
|
||||
ErrorCode_conflict ErrorCode = "conflict"
|
||||
ErrorCode_empty_value ErrorCode = "empty value"
|
||||
ErrorCode_forbidden ErrorCode = "forbidden"
|
||||
ErrorCode_internal_error ErrorCode = "internal error"
|
||||
ErrorCode_invalid ErrorCode = "invalid"
|
||||
ErrorCode_method_not_allowed ErrorCode = "method not allowed"
|
||||
ErrorCode_not_found ErrorCode = "not found"
|
||||
ErrorCode_request_too_large ErrorCode = "request too large"
|
||||
ErrorCode_too_many_requests ErrorCode = "too many requests"
|
||||
ErrorCode_unauthorized ErrorCode = "unauthorized"
|
||||
ErrorCode_unavailable ErrorCode = "unavailable"
|
||||
ErrorCode_unprocessable_entity ErrorCode = "unprocessable entity"
|
||||
)
|
||||
|
||||
// HealthCheck defines model for HealthCheck.
|
||||
type HealthCheck struct {
|
||||
Checks *[]HealthCheck `json:"checks,omitempty"`
|
||||
Commit *string `json:"commit,omitempty"`
|
||||
Message *string `json:"message,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Status HealthCheckStatus `json:"status"`
|
||||
Version *string `json:"version,omitempty"`
|
||||
}
|
||||
|
||||
// HealthCheckStatus defines model for HealthCheckStatus.
|
||||
type HealthCheckStatus string
|
||||
|
||||
// List of HealthCheckStatus
|
||||
const (
|
||||
HealthCheckStatus_fail HealthCheckStatus = "fail"
|
||||
HealthCheckStatus_pass HealthCheckStatus = "pass"
|
||||
)
|
||||
|
||||
// TraceSpan defines model for TraceSpan.
|
||||
type TraceSpan string
|
||||
|
||||
// GetHealthParams defines parameters for GetHealth.
|
||||
type GetHealthParams struct {
|
||||
|
||||
// OpenTracing span context
|
||||
ZapTraceSpan *TraceSpan `json:"Zap-Trace-Span,omitempty"`
|
||||
}
|
||||
21
internal/api/types.go
Normal file
21
internal/api/types.go
Normal file
@ -0,0 +1,21 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (e *Error) Error() string {
|
||||
if e.Message != "" && e.Err != nil {
|
||||
var b strings.Builder
|
||||
b.WriteString(e.Message)
|
||||
b.WriteString(": ")
|
||||
b.WriteString(*e.Err)
|
||||
return b.String()
|
||||
} else if e.Message != "" {
|
||||
return e.Message
|
||||
} else if e.Err != nil {
|
||||
return *e.Err
|
||||
}
|
||||
return fmt.Sprintf("<%s>", e.Code)
|
||||
}
|
||||
Reference in New Issue
Block a user