diff --git a/.circleci/config.yml b/.circleci/config.yml index 10eee44..e7ab8e1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,6 +5,7 @@ workflows: build: jobs: - lint + - check-openapi - build - test @@ -20,6 +21,19 @@ jobs: - run: make checktidy - run: make staticcheck + check-openapi: + machine: true + working_directory: /home/circleci/go/src/github.com/influxdata/influx-cli + steps: + - checkout + - run: + name: Upgrade Go + command: | + wget https://golang.org/dl/go1.16.3.linux-amd64.tar.gz + tar -C ${HOME} -xzf go1.16.3.linux-amd64.tar.gz + echo 'export PATH=${HOME}/go/bin:${PATH}' >> $BASH_ENV + - run: make checkopenapi + build: docker: - image: cimg/go:1.16.3 diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 0cc03bc..0000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -api/types.gen.go linguist-generated=true -api/client.gen.go linguist-generated=true diff --git a/Makefile b/Makefile index 88f2e1f..1bd0c1a 100644 --- a/Makefile +++ b/Makefile @@ -26,13 +26,8 @@ export GO_TEST=go test GO_TEST_PATHS=./... ### Build / dependency management -internal/api/types.gen.go: internal/api/api.yml internal/api/gen.go - go generate ./api - -internal/api/client.gen.go: internal/api/api.yml internal/api/gen.go - go generate ./api - -openapi: internal/api/types.gen.go internal/api/client.gen.go +openapi: + ./etc/generate-openapi.sh fmt: $(SOURCES_NO_VENDOR) gofmt -w -s $^ @@ -41,17 +36,15 @@ fmt: $(SOURCES_NO_VENDOR) bin/$(GOOS)/influx: $(SOURCES) $(GO_BUILD) -o $@ ./cmd/$(shell basename "$@") +.DEFAULT_GOAL := influx influx: bin/$(GOOS)/influx vendor: go.mod go.sum go mod vendor -build: openapi fmt influx - clean: $(RM) -r bin $(RM) -r vendor - $(RM) internal/api/types.gen.go internal/api/client.gen.go ### Linters checkfmt: @@ -60,6 +53,9 @@ checkfmt: checktidy: ./etc/checktidy.sh +checkopenapi: + ./etc/checkopenapi.sh + staticcheck: $(SOURCES) vendor go run honnef.co/go/tools/cmd/staticcheck -go $(GOVERSION) ./... diff --git a/README.md b/README.md index 391fc61..6243051 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,17 @@ Our goals are to: 1. Make it easier to keep the CLI up-to-date with InfluxDB Cloud API changes 2. Enable faster turn-around on fixes/features that only affect the CLI 3. Allow the CLI to be built & released for a wider range of platforms than the server can support + +## Building + +Run `make` to build the CLI. The output binary will be written to `bin/$(GOOS)/influx`. + +### Regenerating OpenAPI client + +We use [`OpenAPITools/openapi-generator`](https://github.com/OpenAPITools/openapi-generator) to generate +the underlying HTTP client used by the CLI. Run `make openapi` to re-generate the code. You'll need Docker +running locally for the script to work. + +## Testing + +Run `make test` to run unit tests. diff --git a/cmd/influx/main.go b/cmd/influx/main.go index 8ead68e..6176380 100644 --- a/cmd/influx/main.go +++ b/cmd/influx/main.go @@ -1,10 +1,10 @@ package main import ( - "context" "crypto/tls" "fmt" "net/http" + "net/url" "os" "runtime" "time" @@ -29,6 +29,7 @@ var ( traceIdFlag = "trace-debug-id" configPathFlag = "config-path" configNameFlag = "active-config" + httpDebugFlag = "http-debug" ) // loadConfig reads CLI configs from disk, returning the config with the @@ -46,7 +47,7 @@ func loadConfig(ctx *cli.Context) (config.Config, error) { // newApiClient returns an API client configured to communicate with a remote InfluxDB instance over HTTP. // Client parameters are pulled from the CLI context. -func newApiClient(ctx *cli.Context, injectToken bool) (api.ClientWithResponsesInterface, error) { +func newApiClient(ctx *cli.Context, injectToken bool) (*api.APIClient, error) { cfg, err := loadConfig(ctx) if err != nil { return nil, err @@ -58,43 +59,35 @@ func newApiClient(ctx *cli.Context, injectToken bool) (api.ClientWithResponsesIn cfg.Host = ctx.String(hostFlag) } + parsedHost, err := url.Parse(cfg.Host) + if err != nil { + return nil, fmt.Errorf("host URL %q is invalid: %w", cfg.Host, err) + } + clientTransport := http.DefaultTransport.(*http.Transport) clientTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: ctx.Bool(skipVerifyFlag)} - client := &http.Client{Transport: clientTransport} - userAgent := fmt.Sprintf("influx/%s (%s) Sha/%s Date/%s", version, runtime.GOOS, commit, date) - - opts := []api.ClientOption{ - api.WithHTTPClient(client), - api.WithRequestEditorFn(func(_ context.Context, req *http.Request) error { - req.Header.Set("User-Agent", userAgent) - return nil - }), - } + apiConfig := api.NewConfiguration() + apiConfig.Host = parsedHost.Host + apiConfig.Scheme = parsedHost.Scheme + apiConfig.UserAgent = fmt.Sprintf("influx/%s (%s) Sha/%s Date/%s", version, runtime.GOOS, commit, date) + apiConfig.HTTPClient = &http.Client{Transport: clientTransport} if injectToken { - authHeader := fmt.Sprintf("Token %s", cfg.Token) - opts = append(opts, api.WithRequestEditorFn(func(_ context.Context, req *http.Request) error { - req.Header.Set("Authorization", authHeader) - return nil - })) + apiConfig.DefaultHeader["Authorization"] = fmt.Sprintf("Token %s", cfg.Token) } + apiConfig.Debug = ctx.Bool(httpDebugFlag) - return api.NewClientWithResponses(cfg.Host, opts...) + return api.NewAPIClient(apiConfig), nil } // newCli builds a CLI core that reads from stdin, writes to stdout/stderr, and // optionally tracks a trace ID specified over the CLI. func newCli(ctx *cli.Context) *internal.CLI { - var traceId *api.TraceSpan - if ctx.IsSet(traceIdFlag) { - tid := api.TraceSpan(ctx.String(traceIdFlag)) - traceId = &tid - } return &internal.CLI{ Stdin: ctx.App.Reader, Stdout: ctx.App.Writer, Stderr: ctx.App.ErrWriter, - TraceId: traceId, + TraceId: ctx.String(traceIdFlag), } } @@ -143,6 +136,10 @@ func main() { Hidden: true, EnvVars: []string{"INFLUX_TRACE_DEBUG_ID"}, }, + &cli.BoolFlag{ + Name: httpDebugFlag, + Hidden: true, + }, } app := cli.App{ @@ -167,7 +164,7 @@ func main() { if err != nil { return err } - return newCli(ctx).Ping(ctx.Context, client) + return newCli(ctx).Ping(ctx.Context, client.HealthApi) }, }, }, diff --git a/etc/checkopenapi.sh b/etc/checkopenapi.sh new file mode 100755 index 0000000..1d8ceb0 --- /dev/null +++ b/etc/checkopenapi.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -e + +declare -r ETC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" +${ETC_DIR}/generate-openapi.sh + +if ! git --no-pager diff --exit-code -- internal/api; then + >&2 echo "openapi generated client doesn't match spec, please run 'make openapi'" + exit 1 +fi diff --git a/etc/generate-openapi.sh b/etc/generate-openapi.sh new file mode 100755 index 0000000..743064d --- /dev/null +++ b/etc/generate-openapi.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash + +declare -r ETC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" +declare -r ROOT_DIR="$(dirname ${ETC_DIR})" + +declare -r GENERATOR_DOCKER_IMG=openapitools/openapi-generator-cli:v5.1.0 + +# Run the generator - This produces many more files than we want to track in git. +docker run --rm -it -u "$(id -u):$(id -g)" \ + -v "${ROOT_DIR}":/influx \ + ${GENERATOR_DOCKER_IMG} \ + generate \ + -g go \ + -i /influx/internal/api/api.yml \ + -o /influx/internal/api \ + --additional-properties packageName=api,enumClassPrefix=true,generateInterfaces=true + +# Clean up files we don't care about. +( + cd "${ROOT_DIR}/internal/api" + rm -rf go.mod go.sum git_push.sh api docs .openapi-generator .travis.yml .gitignore +) + +# Edit the generated files. +( + cd "${ROOT_DIR}" + + # Inject linter directives into generated files to make staticcheck happy. + cat < internal/api/client.go +//lint:file-ignore ST1005 Ignore capitalized errors, they're generated +//lint:file-ignore SA6005 Ignore old-fashioned way of comparing strings, it's generated + +$(cat internal/api/client.go) +EOF + cat < internal/api/configuration.go +//lint:file-ignore ST1005 Ignore capitalized errors, they're generated + +$(cat internal/api/configuration.go) +EOF + + # Remove the OAuth code from the generated files. + # We don't use OAuth, and it pulls in a huge dependency on the Google Cloud libraries. + # + # See https://github.com/OpenAPITools/openapi-generator/issues/9281 for a feature request + # to add a toggle that would prevent OAuth code from even being generated. + sed -i.bak -e '/OAuth2 authentication/,+10d' -e 's#"golang.org/x/oauth2"##' internal/api/client.go + sed -i.bak -e '/OAuth2/,+2d' internal/api/configuration.go + + # Replace all uses of int32 with int64 in our generated models. + # + # See https://github.com/OpenAPITools/openapi-generator/issues/9280 for a feature request + # to make the Go generator's number-handling compliant with the spec, so we can generate int64 + # fields directly without making our public docs invalid. + for m in internal/api/model_*.go; do + sed -i.bak -e 's/int32/int64/g' ${m} + done + + rm internal/api/*.bak +) + +# Since we deleted the generated go.mod, run `go mod tidy` to update parent dependencies. +( + cd "${ROOT_DIR}" + make fmt + go mod tidy +) diff --git a/go.mod b/go.mod index 7f5b5f1..2d30246 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,9 @@ go 1.16 require ( github.com/BurntSushi/toml v0.3.1 github.com/daixiang0/gci v0.2.8 - github.com/deepmap/oapi-codegen v1.6.0 + github.com/kr/pretty v0.1.0 // indirect github.com/stretchr/testify v1.7.0 github.com/urfave/cli/v2 v2.3.0 + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect honnef.co/go/tools v0.1.3 ) diff --git a/go.sum b/go.sum index b8eed4b..e328e93 100644 --- a/go.sum +++ b/go.sum @@ -2,51 +2,16 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= -github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4= github.com/daixiang0/gci v0.2.8 h1:1mrIGMBQsBu0P7j7m1M8Lb+ZeZxsZL+jyGX4YoMJJpg= github.com/daixiang0/gci v0.2.8/go.mod h1:+4dZ7TISfSmqfAGv59ePaHfNzgGtIkHAhhdKggP1JAc= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/deepmap/oapi-codegen v1.6.0 h1:w/d1ntwh91XI0b/8ja7+u5SvA4IFfM0UNNLmiDR1gg0= -github.com/deepmap/oapi-codegen v1.6.0/go.mod h1:ryDa9AgbELGeB+YEXE1dR53yAjHwFvE9iAUlWl9Al3M= -github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/getkin/kin-openapi v0.53.0 h1:7WzP+MZRRe7YQz2Kc74Ley3dukJmXDvifVbElGmQfoA= -github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4= -github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= -github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs= -github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY= -github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= -github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY= -github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= -github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219 h1:utua3L2IbQJmauC5IXdEA547bcoU5dozgQAfc8Onsg4= -github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGSh1pIozq4ZwCfxS0WA5JGXguxk94ar/4c87Y= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/labstack/echo/v4 v4.2.1 h1:LF5Iq7t/jrtUuSutNuiEWtB5eiHfZ5gSe2pcu5exjQw= -github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg= -github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= -github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= -github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= -github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8= -github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= -github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ= -github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= -github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= @@ -54,60 +19,30 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= -github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= -github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= -github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4= -github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew= -golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk= -golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= -golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201118003311-bd56c0adb394/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= @@ -118,10 +53,7 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= -gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.1.3 h1:qTakTkI6ni6LFD5sBwwsdSO+AQqbSIxOauHTTQKZ/7o= diff --git a/internal/api/.openapi-generator-ignore b/internal/api/.openapi-generator-ignore new file mode 100644 index 0000000..b43bf86 --- /dev/null +++ b/internal/api/.openapi-generator-ignore @@ -0,0 +1 @@ +README.md diff --git a/internal/api/README.md b/internal/api/README.md new file mode 100644 index 0000000..160ab26 --- /dev/null +++ b/internal/api/README.md @@ -0,0 +1,4 @@ +# Influx CLI - HTTP Client + +The `.go` files in this module are generated using [`OpenAPITools/openapi-generator`](https://github.com/OpenAPITools/openapi-generator), +based off of our public API documentation. Run `etc/generate-openapi.sh` to regenerate files as needed. diff --git a/internal/api/api.yml b/internal/api/api.yml index 01da477..156e1ca 100644 --- a/internal/api/api.yml +++ b/internal/api/api.yml @@ -1,13 +1,13 @@ openapi: 3.0.0 info: - title: Influx API Service + title: Subset of Influx API covered by Influx CLI version: 2.0.0 servers: - url: /api/v2 paths: /health: servers: - - url: / + - url: '' get: operationId: GetHealth tags: @@ -49,6 +49,42 @@ components: schema: type: string schemas: + 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: code is the 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 HealthCheck: type: object required: @@ -71,43 +107,6 @@ components: 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 diff --git a/internal/api/api_health.go b/internal/api/api_health.go new file mode 100644 index 0000000..31e0eba --- /dev/null +++ b/internal/api/api_health.go @@ -0,0 +1,169 @@ +/* + * Subset of Influx API covered by Influx CLI + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * API version: 2.0.0 + */ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package api + +import ( + "bytes" + _context "context" + _ioutil "io/ioutil" + _nethttp "net/http" + _neturl "net/url" +) + +// Linger please +var ( + _ _context.Context +) + +type HealthApi interface { + + /* + * GetHealth Get the health of an instance + * @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). + * @return ApiGetHealthRequest + */ + GetHealth(ctx _context.Context) ApiGetHealthRequest + + /* + * GetHealthExecute executes the request + * @return HealthCheck + */ + GetHealthExecute(r ApiGetHealthRequest) (HealthCheck, *_nethttp.Response, error) +} + +// HealthApiService HealthApi service +type HealthApiService service + +type ApiGetHealthRequest struct { + ctx _context.Context + ApiService HealthApi + zapTraceSpan *string +} + +func (r ApiGetHealthRequest) ZapTraceSpan(zapTraceSpan string) ApiGetHealthRequest { + r.zapTraceSpan = &zapTraceSpan + return r +} + +func (r ApiGetHealthRequest) Execute() (HealthCheck, *_nethttp.Response, error) { + return r.ApiService.GetHealthExecute(r) +} + +/* + * GetHealth Get the health of an instance + * @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). + * @return ApiGetHealthRequest + */ +func (a *HealthApiService) GetHealth(ctx _context.Context) ApiGetHealthRequest { + return ApiGetHealthRequest{ + ApiService: a, + ctx: ctx, + } +} + +/* + * Execute executes the request + * @return HealthCheck + */ +func (a *HealthApiService) GetHealthExecute(r ApiGetHealthRequest) (HealthCheck, *_nethttp.Response, error) { + var ( + localVarHTTPMethod = _nethttp.MethodGet + localVarPostBody interface{} + localVarFormFileName string + localVarFileName string + localVarFileBytes []byte + localVarReturnValue HealthCheck + ) + + localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "HealthApiService.GetHealth") + if err != nil { + return localVarReturnValue, nil, GenericOpenAPIError{error: err.Error()} + } + + localVarPath := localBasePath + "/health" + + localVarHeaderParams := make(map[string]string) + localVarQueryParams := _neturl.Values{} + localVarFormParams := _neturl.Values{} + + // to determine the Content-Type header + localVarHTTPContentTypes := []string{} + + // set Content-Type header + localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes) + if localVarHTTPContentType != "" { + localVarHeaderParams["Content-Type"] = localVarHTTPContentType + } + + // to determine the Accept header + localVarHTTPHeaderAccepts := []string{"application/json"} + + // set Accept header + localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts) + if localVarHTTPHeaderAccept != "" { + localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept + } + if r.zapTraceSpan != nil { + localVarHeaderParams["Zap-Trace-Span"] = parameterToString(*r.zapTraceSpan, "") + } + req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes) + if err != nil { + return localVarReturnValue, nil, err + } + + localVarHTTPResponse, err := a.client.callAPI(req) + if err != nil || localVarHTTPResponse == nil { + return localVarReturnValue, localVarHTTPResponse, err + } + + localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body) + localVarHTTPResponse.Body.Close() + localVarHTTPResponse.Body = _ioutil.NopCloser(bytes.NewBuffer(localVarBody)) + if err != nil { + return localVarReturnValue, localVarHTTPResponse, err + } + + if localVarHTTPResponse.StatusCode >= 300 { + newErr := GenericOpenAPIError{ + body: localVarBody, + error: localVarHTTPResponse.Status, + } + if localVarHTTPResponse.StatusCode == 503 { + var v HealthCheck + err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHTTPResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHTTPResponse, newErr + } + var v Error + err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHTTPResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHTTPResponse, newErr + } + + err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) + if err != nil { + newErr := GenericOpenAPIError{ + body: localVarBody, + error: err.Error(), + } + return localVarReturnValue, localVarHTTPResponse, newErr + } + + return localVarReturnValue, localVarHTTPResponse, nil +} diff --git a/internal/api/client.gen.go b/internal/api/client.gen.go deleted file mode 100644 index 5824927..0000000 --- a/internal/api/client.gen.go +++ /dev/null @@ -1,263 +0,0 @@ -// 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 -} diff --git a/internal/api/client.go b/internal/api/client.go new file mode 100644 index 0000000..ed19fe1 --- /dev/null +++ b/internal/api/client.go @@ -0,0 +1,525 @@ +//lint:file-ignore ST1005 Ignore capitalized errors, they're generated +//lint:file-ignore SA6005 Ignore old-fashioned way of comparing strings, it's generated + +/* + * Subset of Influx API covered by Influx CLI + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * API version: 2.0.0 + */ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package api + +import ( + "bytes" + "context" + "encoding/json" + "encoding/xml" + "errors" + "fmt" + "io" + "log" + "mime/multipart" + "net/http" + "net/http/httputil" + "net/url" + "os" + "path/filepath" + "reflect" + "regexp" + "strconv" + "strings" + "time" + "unicode/utf8" +) + +var ( + jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) + xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) +) + +// APIClient manages communication with the Subset of Influx API covered by Influx CLI API v2.0.0 +// In most cases there should be only one, shared, APIClient. +type APIClient struct { + cfg *Configuration + common service // Reuse a single struct instead of allocating one for each service on the heap. + + // API Services + + HealthApi HealthApi +} + +type service struct { + client *APIClient +} + +// NewAPIClient creates a new API client. Requires a userAgent string describing your application. +// optionally a custom http.Client to allow for advanced features such as caching. +func NewAPIClient(cfg *Configuration) *APIClient { + if cfg.HTTPClient == nil { + cfg.HTTPClient = http.DefaultClient + } + + c := &APIClient{} + c.cfg = cfg + c.common.client = c + + // API Services + c.HealthApi = (*HealthApiService)(&c.common) + + return c +} + +func atoi(in string) (int, error) { + return strconv.Atoi(in) +} + +// selectHeaderContentType select a content type from the available list. +func selectHeaderContentType(contentTypes []string) string { + if len(contentTypes) == 0 { + return "" + } + if contains(contentTypes, "application/json") { + return "application/json" + } + return contentTypes[0] // use the first content type specified in 'consumes' +} + +// selectHeaderAccept join all accept types and return +func selectHeaderAccept(accepts []string) string { + if len(accepts) == 0 { + return "" + } + + if contains(accepts, "application/json") { + return "application/json" + } + + return strings.Join(accepts, ",") +} + +// contains is a case insenstive match, finding needle in a haystack +func contains(haystack []string, needle string) bool { + for _, a := range haystack { + if strings.ToLower(a) == strings.ToLower(needle) { + return true + } + } + return false +} + +// Verify optional parameters are of the correct type. +func typeCheckParameter(obj interface{}, expected string, name string) error { + // Make sure there is an object. + if obj == nil { + return nil + } + + // Check the type is as expected. + if reflect.TypeOf(obj).String() != expected { + return fmt.Errorf("Expected %s to be of type %s but received %s.", name, expected, reflect.TypeOf(obj).String()) + } + return nil +} + +// parameterToString convert interface{} parameters to string, using a delimiter if format is provided. +func parameterToString(obj interface{}, collectionFormat string) string { + var delimiter string + + switch collectionFormat { + case "pipes": + delimiter = "|" + case "ssv": + delimiter = " " + case "tsv": + delimiter = "\t" + case "csv": + delimiter = "," + } + + if reflect.TypeOf(obj).Kind() == reflect.Slice { + return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]") + } else if t, ok := obj.(time.Time); ok { + return t.Format(time.RFC3339) + } + + return fmt.Sprintf("%v", obj) +} + +// helper for converting interface{} parameters to json strings +func parameterToJson(obj interface{}) (string, error) { + jsonBuf, err := json.Marshal(obj) + if err != nil { + return "", err + } + return string(jsonBuf), err +} + +// callAPI do the request. +func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) { + if c.cfg.Debug { + dump, err := httputil.DumpRequestOut(request, true) + if err != nil { + return nil, err + } + log.Printf("\n%s\n", string(dump)) + } + + resp, err := c.cfg.HTTPClient.Do(request) + if err != nil { + return resp, err + } + + if c.cfg.Debug { + dump, err := httputil.DumpResponse(resp, true) + if err != nil { + return resp, err + } + log.Printf("\n%s\n", string(dump)) + } + return resp, err +} + +// Allow modification of underlying config for alternate implementations and testing +// Caution: modifying the configuration while live can cause data races and potentially unwanted behavior +func (c *APIClient) GetConfig() *Configuration { + return c.cfg +} + +// prepareRequest build the request +func (c *APIClient) prepareRequest( + ctx context.Context, + path string, method string, + postBody interface{}, + headerParams map[string]string, + queryParams url.Values, + formParams url.Values, + formFileName string, + fileName string, + fileBytes []byte) (localVarRequest *http.Request, err error) { + + var body *bytes.Buffer + + // Detect postBody type and post. + if postBody != nil { + contentType := headerParams["Content-Type"] + if contentType == "" { + contentType = detectContentType(postBody) + headerParams["Content-Type"] = contentType + } + + body, err = setBody(postBody, contentType) + if err != nil { + return nil, err + } + } + + // add form parameters and file if available. + if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") { + if body != nil { + return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + } + body = &bytes.Buffer{} + w := multipart.NewWriter(body) + + for k, v := range formParams { + for _, iv := range v { + if strings.HasPrefix(k, "@") { // file + err = addFile(w, k[1:], iv) + if err != nil { + return nil, err + } + } else { // form value + w.WriteField(k, iv) + } + } + } + if len(fileBytes) > 0 && fileName != "" { + w.Boundary() + //_, fileNm := filepath.Split(fileName) + part, err := w.CreateFormFile(formFileName, filepath.Base(fileName)) + if err != nil { + return nil, err + } + _, err = part.Write(fileBytes) + if err != nil { + return nil, err + } + } + + // Set the Boundary in the Content-Type + headerParams["Content-Type"] = w.FormDataContentType() + + // Set Content-Length + headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len()) + w.Close() + } + + if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { + if body != nil { + return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + } + body = &bytes.Buffer{} + body.WriteString(formParams.Encode()) + // Set Content-Length + headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len()) + } + + // Setup path and query parameters + url, err := url.Parse(path) + if err != nil { + return nil, err + } + + // Override request host, if applicable + if c.cfg.Host != "" { + url.Host = c.cfg.Host + } + + // Override request scheme, if applicable + if c.cfg.Scheme != "" { + url.Scheme = c.cfg.Scheme + } + + // Adding Query Param + query := url.Query() + for k, v := range queryParams { + for _, iv := range v { + query.Add(k, iv) + } + } + + // Encode the parameters. + url.RawQuery = query.Encode() + + // Generate a new request + if body != nil { + localVarRequest, err = http.NewRequest(method, url.String(), body) + } else { + localVarRequest, err = http.NewRequest(method, url.String(), nil) + } + if err != nil { + return nil, err + } + + // add header parameters, if any + if len(headerParams) > 0 { + headers := http.Header{} + for h, v := range headerParams { + headers.Set(h, v) + } + localVarRequest.Header = headers + } + + // Add the user agent to the request. + localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent) + + if ctx != nil { + // add context to the request + localVarRequest = localVarRequest.WithContext(ctx) + + // Walk through any authentication. + + // Basic HTTP Authentication + if auth, ok := ctx.Value(ContextBasicAuth).(BasicAuth); ok { + localVarRequest.SetBasicAuth(auth.UserName, auth.Password) + } + + // AccessToken Authentication + if auth, ok := ctx.Value(ContextAccessToken).(string); ok { + localVarRequest.Header.Add("Authorization", "Bearer "+auth) + } + + } + + for header, value := range c.cfg.DefaultHeader { + localVarRequest.Header.Add(header, value) + } + return localVarRequest, nil +} + +func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) { + if len(b) == 0 { + return nil + } + if s, ok := v.(*string); ok { + *s = string(b) + return nil + } + if xmlCheck.MatchString(contentType) { + if err = xml.Unmarshal(b, v); err != nil { + return err + } + return nil + } + if jsonCheck.MatchString(contentType) { + if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas + if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined + if err = unmarshalObj.UnmarshalJSON(b); err != nil { + return err + } + } else { + return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + } + } else if err = json.Unmarshal(b, v); err != nil { // simple model + return err + } + return nil + } + return errors.New("undefined response type") +} + +// Add a file to the multipart request +func addFile(w *multipart.Writer, fieldName, path string) error { + file, err := os.Open(path) + if err != nil { + return err + } + defer file.Close() + + part, err := w.CreateFormFile(fieldName, filepath.Base(path)) + if err != nil { + return err + } + _, err = io.Copy(part, file) + + return err +} + +// Prevent trying to import "fmt" +func reportError(format string, a ...interface{}) error { + return fmt.Errorf(format, a...) +} + +// Set request body from an interface{} +func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err error) { + if bodyBuf == nil { + bodyBuf = &bytes.Buffer{} + } + + if reader, ok := body.(io.Reader); ok { + _, err = bodyBuf.ReadFrom(reader) + } else if fp, ok := body.(**os.File); ok { + _, err = bodyBuf.ReadFrom(*fp) + } else if b, ok := body.([]byte); ok { + _, err = bodyBuf.Write(b) + } else if s, ok := body.(string); ok { + _, err = bodyBuf.WriteString(s) + } else if s, ok := body.(*string); ok { + _, err = bodyBuf.WriteString(*s) + } else if jsonCheck.MatchString(contentType) { + err = json.NewEncoder(bodyBuf).Encode(body) + } else if xmlCheck.MatchString(contentType) { + err = xml.NewEncoder(bodyBuf).Encode(body) + } + + if err != nil { + return nil, err + } + + if bodyBuf.Len() == 0 { + err = fmt.Errorf("Invalid body type %s\n", contentType) + return nil, err + } + return bodyBuf, nil +} + +// detectContentType method is used to figure out `Request.Body` content type for request header +func detectContentType(body interface{}) string { + contentType := "text/plain; charset=utf-8" + kind := reflect.TypeOf(body).Kind() + + switch kind { + case reflect.Struct, reflect.Map, reflect.Ptr: + contentType = "application/json; charset=utf-8" + case reflect.String: + contentType = "text/plain; charset=utf-8" + default: + if b, ok := body.([]byte); ok { + contentType = http.DetectContentType(b) + } else if kind == reflect.Slice { + contentType = "application/json; charset=utf-8" + } + } + + return contentType +} + +// Ripped from https://github.com/gregjones/httpcache/blob/master/httpcache.go +type cacheControl map[string]string + +func parseCacheControl(headers http.Header) cacheControl { + cc := cacheControl{} + ccHeader := headers.Get("Cache-Control") + for _, part := range strings.Split(ccHeader, ",") { + part = strings.Trim(part, " ") + if part == "" { + continue + } + if strings.ContainsRune(part, '=') { + keyval := strings.Split(part, "=") + cc[strings.Trim(keyval[0], " ")] = strings.Trim(keyval[1], ",") + } else { + cc[part] = "" + } + } + return cc +} + +// CacheExpires helper function to determine remaining time before repeating a request. +func CacheExpires(r *http.Response) time.Time { + // Figure out when the cache expires. + var expires time.Time + now, err := time.Parse(time.RFC1123, r.Header.Get("date")) + if err != nil { + return time.Now() + } + respCacheControl := parseCacheControl(r.Header) + + if maxAge, ok := respCacheControl["max-age"]; ok { + lifetime, err := time.ParseDuration(maxAge + "s") + if err != nil { + expires = now + } else { + expires = now.Add(lifetime) + } + } else { + expiresHeader := r.Header.Get("Expires") + if expiresHeader != "" { + expires, err = time.Parse(time.RFC1123, expiresHeader) + if err != nil { + expires = now + } + } + } + return expires +} + +func strlen(s string) int { + return utf8.RuneCountInString(s) +} + +// GenericOpenAPIError Provides access to the body, error and model on returned errors. +type GenericOpenAPIError struct { + body []byte + error string + model interface{} +} + +// Error returns non-empty string if there was an error. +func (e GenericOpenAPIError) Error() string { + return e.error +} + +// Body returns the raw bytes of the response +func (e GenericOpenAPIError) Body() []byte { + return e.body +} + +// Model returns the unpacked model of the error +func (e GenericOpenAPIError) Model() interface{} { + return e.model +} diff --git a/internal/api/configuration.go b/internal/api/configuration.go new file mode 100644 index 0000000..c051c4f --- /dev/null +++ b/internal/api/configuration.go @@ -0,0 +1,235 @@ +//lint:file-ignore ST1005 Ignore capitalized errors, they're generated + +/* + * Subset of Influx API covered by Influx CLI + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * API version: 2.0.0 + */ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package api + +import ( + "context" + "fmt" + "net/http" + "strings" +) + +// contextKeys are used to identify the type of value in the context. +// Since these are string, it is possible to get a short description of the +// context key for logging and debugging using key.String(). + +type contextKey string + +func (c contextKey) String() string { + return "auth " + string(c) +} + +var ( + // ContextBasicAuth takes BasicAuth as authentication for the request. + ContextBasicAuth = contextKey("basic") + + // ContextAccessToken takes a string oauth2 access token as authentication for the request. + ContextAccessToken = contextKey("accesstoken") + + // ContextAPIKeys takes a string apikey as authentication for the request + ContextAPIKeys = contextKey("apiKeys") + + // ContextHttpSignatureAuth takes HttpSignatureAuth as authentication for the request. + ContextHttpSignatureAuth = contextKey("httpsignature") + + // ContextServerIndex uses a server configuration from the index. + ContextServerIndex = contextKey("serverIndex") + + // ContextOperationServerIndices uses a server configuration from the index mapping. + ContextOperationServerIndices = contextKey("serverOperationIndices") + + // ContextServerVariables overrides a server configuration variables. + ContextServerVariables = contextKey("serverVariables") + + // ContextOperationServerVariables overrides a server configuration variables using operation specific values. + ContextOperationServerVariables = contextKey("serverOperationVariables") +) + +// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth +type BasicAuth struct { + UserName string `json:"userName,omitempty"` + Password string `json:"password,omitempty"` +} + +// APIKey provides API key based authentication to a request passed via context using ContextAPIKey +type APIKey struct { + Key string + Prefix string +} + +// ServerVariable stores the information about a server variable +type ServerVariable struct { + Description string + DefaultValue string + EnumValues []string +} + +// ServerConfiguration stores the information about a server +type ServerConfiguration struct { + URL string + Description string + Variables map[string]ServerVariable +} + +// ServerConfigurations stores multiple ServerConfiguration items +type ServerConfigurations []ServerConfiguration + +// Configuration stores the configuration of the API client +type Configuration struct { + Host string `json:"host,omitempty"` + Scheme string `json:"scheme,omitempty"` + DefaultHeader map[string]string `json:"defaultHeader,omitempty"` + UserAgent string `json:"userAgent,omitempty"` + Debug bool `json:"debug,omitempty"` + Servers ServerConfigurations + OperationServers map[string]ServerConfigurations + HTTPClient *http.Client +} + +// NewConfiguration returns a new Configuration object +func NewConfiguration() *Configuration { + cfg := &Configuration{ + DefaultHeader: make(map[string]string), + UserAgent: "OpenAPI-Generator/1.0.0/go", + Debug: false, + Servers: ServerConfigurations{ + { + URL: "/api/v2", + Description: "No description provided", + }, + }, + OperationServers: map[string]ServerConfigurations{ + "HealthApiService.GetHealth": { + { + URL: "", + Description: "No description provided", + }, + }, + }, + } + return cfg +} + +// AddDefaultHeader adds a new HTTP header to the default header in the request +func (c *Configuration) AddDefaultHeader(key string, value string) { + c.DefaultHeader[key] = value +} + +// URL formats template on a index using given variables +func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) { + if index < 0 || len(sc) <= index { + return "", fmt.Errorf("Index %v out of range %v", index, len(sc)-1) + } + server := sc[index] + url := server.URL + + // go through variables and replace placeholders + for name, variable := range server.Variables { + if value, ok := variables[name]; ok { + found := bool(len(variable.EnumValues) == 0) + for _, enumValue := range variable.EnumValues { + if value == enumValue { + found = true + } + } + if !found { + return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) + } + url = strings.Replace(url, "{"+name+"}", value, -1) + } else { + url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) + } + } + return url, nil +} + +// ServerURL returns URL based on server settings +func (c *Configuration) ServerURL(index int, variables map[string]string) (string, error) { + return c.Servers.URL(index, variables) +} + +func getServerIndex(ctx context.Context) (int, error) { + si := ctx.Value(ContextServerIndex) + if si != nil { + if index, ok := si.(int); ok { + return index, nil + } + return 0, reportError("Invalid type %T should be int", si) + } + return 0, nil +} + +func getServerOperationIndex(ctx context.Context, endpoint string) (int, error) { + osi := ctx.Value(ContextOperationServerIndices) + if osi != nil { + if operationIndices, ok := osi.(map[string]int); !ok { + return 0, reportError("Invalid type %T should be map[string]int", osi) + } else { + index, ok := operationIndices[endpoint] + if ok { + return index, nil + } + } + } + return getServerIndex(ctx) +} + +func getServerVariables(ctx context.Context) (map[string]string, error) { + sv := ctx.Value(ContextServerVariables) + if sv != nil { + if variables, ok := sv.(map[string]string); ok { + return variables, nil + } + return nil, reportError("ctx value of ContextServerVariables has invalid type %T should be map[string]string", sv) + } + return nil, nil +} + +func getServerOperationVariables(ctx context.Context, endpoint string) (map[string]string, error) { + osv := ctx.Value(ContextOperationServerVariables) + if osv != nil { + if operationVariables, ok := osv.(map[string]map[string]string); !ok { + return nil, reportError("ctx value of ContextOperationServerVariables has invalid type %T should be map[string]map[string]string", osv) + } else { + variables, ok := operationVariables[endpoint] + if ok { + return variables, nil + } + } + } + return getServerVariables(ctx) +} + +// ServerURLWithContext returns a new server URL given an endpoint +func (c *Configuration) ServerURLWithContext(ctx context.Context, endpoint string) (string, error) { + sc, ok := c.OperationServers[endpoint] + if !ok { + sc = c.Servers + } + + if ctx == nil { + return sc.URL(0, nil) + } + + index, err := getServerOperationIndex(ctx, endpoint) + if err != nil { + return "", err + } + + variables, err := getServerOperationVariables(ctx, endpoint) + if err != nil { + return "", err + } + + return sc.URL(index, variables) +} diff --git a/internal/api/error.go b/internal/api/error.go new file mode 100644 index 0000000..ee2d7af --- /dev/null +++ b/internal/api/error.go @@ -0,0 +1,22 @@ +package api + +import ( + "fmt" + "strings" +) + +// Extension to let our API error type be used as a "standard" error. +func (o *Error) Error() string { + if o.Message != "" && o.Err != nil { + var b strings.Builder + b.WriteString(o.Message) + b.WriteString(": ") + b.WriteString(*o.Err) + return b.String() + } else if o.Message != "" { + return o.Message + } else if o.Err != nil { + return *o.Err + } + return fmt.Sprintf("<%s>", o.Code) +} diff --git a/internal/api/gen.go b/internal/api/gen.go deleted file mode 100644 index 2781bff..0000000 --- a/internal/api/gen.go +++ /dev/null @@ -1,4 +0,0 @@ -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 diff --git a/internal/api/model_error.go b/internal/api/model_error.go new file mode 100644 index 0000000..21dca6b --- /dev/null +++ b/internal/api/model_error.go @@ -0,0 +1,210 @@ +/* + * Subset of Influx API covered by Influx CLI + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * API version: 2.0.0 + */ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package api + +import ( + "encoding/json" +) + +// Error struct for Error +type Error struct { + Code ErrorCode `json:"code"` + // 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"` + // err is a stack of errors that occurred during processing of the request. Useful for debugging. + Err *string `json:"err,omitempty"` +} + +// NewError instantiates a new Error object +// This constructor will assign default values to properties that have it defined, +// and makes sure properties required by API are set, but the set of arguments +// will change when the set of required properties is changed +func NewError(code ErrorCode, message string) *Error { + this := Error{} + this.Code = code + this.Message = message + return &this +} + +// NewErrorWithDefaults instantiates a new Error object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewErrorWithDefaults() *Error { + this := Error{} + return &this +} + +// GetCode returns the Code field value +func (o *Error) GetCode() ErrorCode { + if o == nil { + var ret ErrorCode + return ret + } + + return o.Code +} + +// GetCodeOk returns a tuple with the Code field value +// and a boolean to check if the value has been set. +func (o *Error) GetCodeOk() (*ErrorCode, bool) { + if o == nil { + return nil, false + } + return &o.Code, true +} + +// SetCode sets field value +func (o *Error) SetCode(v ErrorCode) { + o.Code = v +} + +// GetMessage returns the Message field value +func (o *Error) GetMessage() string { + if o == nil { + var ret string + return ret + } + + return o.Message +} + +// GetMessageOk returns a tuple with the Message field value +// and a boolean to check if the value has been set. +func (o *Error) GetMessageOk() (*string, bool) { + if o == nil { + return nil, false + } + return &o.Message, true +} + +// SetMessage sets field value +func (o *Error) SetMessage(v string) { + o.Message = v +} + +// GetOp returns the Op field value if set, zero value otherwise. +func (o *Error) GetOp() string { + if o == nil || o.Op == nil { + var ret string + return ret + } + return *o.Op +} + +// GetOpOk returns a tuple with the Op field value if set, nil otherwise +// and a boolean to check if the value has been set. +func (o *Error) GetOpOk() (*string, bool) { + if o == nil || o.Op == nil { + return nil, false + } + return o.Op, true +} + +// HasOp returns a boolean if a field has been set. +func (o *Error) HasOp() bool { + if o != nil && o.Op != nil { + return true + } + + return false +} + +// SetOp gets a reference to the given string and assigns it to the Op field. +func (o *Error) SetOp(v string) { + o.Op = &v +} + +// GetErr returns the Err field value if set, zero value otherwise. +func (o *Error) GetErr() string { + if o == nil || o.Err == nil { + var ret string + return ret + } + return *o.Err +} + +// GetErrOk returns a tuple with the Err field value if set, nil otherwise +// and a boolean to check if the value has been set. +func (o *Error) GetErrOk() (*string, bool) { + if o == nil || o.Err == nil { + return nil, false + } + return o.Err, true +} + +// HasErr returns a boolean if a field has been set. +func (o *Error) HasErr() bool { + if o != nil && o.Err != nil { + return true + } + + return false +} + +// SetErr gets a reference to the given string and assigns it to the Err field. +func (o *Error) SetErr(v string) { + o.Err = &v +} + +func (o Error) MarshalJSON() ([]byte, error) { + toSerialize := map[string]interface{}{} + if true { + toSerialize["code"] = o.Code + } + if true { + toSerialize["message"] = o.Message + } + if o.Op != nil { + toSerialize["op"] = o.Op + } + if o.Err != nil { + toSerialize["err"] = o.Err + } + return json.Marshal(toSerialize) +} + +type NullableError struct { + value *Error + isSet bool +} + +func (v NullableError) Get() *Error { + return v.value +} + +func (v *NullableError) Set(val *Error) { + v.value = val + v.isSet = true +} + +func (v NullableError) IsSet() bool { + return v.isSet +} + +func (v *NullableError) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableError(val *Error) *NullableError { + return &NullableError{value: val, isSet: true} +} + +func (v NullableError) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableError) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} diff --git a/internal/api/model_error_code.go b/internal/api/model_error_code.go new file mode 100644 index 0000000..9a9f4d1 --- /dev/null +++ b/internal/api/model_error_code.go @@ -0,0 +1,93 @@ +/* + * Subset of Influx API covered by Influx CLI + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * API version: 2.0.0 + */ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package api + +import ( + "encoding/json" + "fmt" +) + +// ErrorCode code is the machine-readable error code. +type ErrorCode string + +// List of ErrorCode +const ( + ERRORCODE_INTERNAL_ERROR ErrorCode = "internal error" + ERRORCODE_NOT_FOUND ErrorCode = "not found" + ERRORCODE_CONFLICT ErrorCode = "conflict" + ERRORCODE_INVALID ErrorCode = "invalid" + ERRORCODE_UNPROCESSABLE_ENTITY ErrorCode = "unprocessable entity" + ERRORCODE_EMPTY_VALUE ErrorCode = "empty value" + ERRORCODE_UNAVAILABLE ErrorCode = "unavailable" + ERRORCODE_FORBIDDEN ErrorCode = "forbidden" + ERRORCODE_TOO_MANY_REQUESTS ErrorCode = "too many requests" + ERRORCODE_UNAUTHORIZED ErrorCode = "unauthorized" + ERRORCODE_METHOD_NOT_ALLOWED ErrorCode = "method not allowed" + ERRORCODE_REQUEST_TOO_LARGE ErrorCode = "request too large" +) + +func (v *ErrorCode) UnmarshalJSON(src []byte) error { + var value string + err := json.Unmarshal(src, &value) + if err != nil { + return err + } + enumTypeValue := ErrorCode(value) + for _, existing := range []ErrorCode{"internal error", "not found", "conflict", "invalid", "unprocessable entity", "empty value", "unavailable", "forbidden", "too many requests", "unauthorized", "method not allowed", "request too large"} { + if existing == enumTypeValue { + *v = enumTypeValue + return nil + } + } + + return fmt.Errorf("%+v is not a valid ErrorCode", value) +} + +// Ptr returns reference to ErrorCode value +func (v ErrorCode) Ptr() *ErrorCode { + return &v +} + +type NullableErrorCode struct { + value *ErrorCode + isSet bool +} + +func (v NullableErrorCode) Get() *ErrorCode { + return v.value +} + +func (v *NullableErrorCode) Set(val *ErrorCode) { + v.value = val + v.isSet = true +} + +func (v NullableErrorCode) IsSet() bool { + return v.isSet +} + +func (v *NullableErrorCode) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableErrorCode(val *ErrorCode) *NullableErrorCode { + return &NullableErrorCode{value: val, isSet: true} +} + +func (v NullableErrorCode) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableErrorCode) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} diff --git a/internal/api/model_health_check.go b/internal/api/model_health_check.go new file mode 100644 index 0000000..6fead02 --- /dev/null +++ b/internal/api/model_health_check.go @@ -0,0 +1,279 @@ +/* + * Subset of Influx API covered by Influx CLI + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * API version: 2.0.0 + */ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package api + +import ( + "encoding/json" +) + +// HealthCheck struct for HealthCheck +type HealthCheck struct { + Name string `json:"name"` + Message *string `json:"message,omitempty"` + Checks *[]HealthCheck `json:"checks,omitempty"` + Status HealthCheckStatus `json:"status"` + Version *string `json:"version,omitempty"` + Commit *string `json:"commit,omitempty"` +} + +// NewHealthCheck instantiates a new HealthCheck object +// This constructor will assign default values to properties that have it defined, +// and makes sure properties required by API are set, but the set of arguments +// will change when the set of required properties is changed +func NewHealthCheck(name string, status HealthCheckStatus) *HealthCheck { + this := HealthCheck{} + this.Name = name + this.Status = status + return &this +} + +// NewHealthCheckWithDefaults instantiates a new HealthCheck object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewHealthCheckWithDefaults() *HealthCheck { + this := HealthCheck{} + return &this +} + +// GetName returns the Name field value +func (o *HealthCheck) GetName() string { + if o == nil { + var ret string + return ret + } + + return o.Name +} + +// GetNameOk returns a tuple with the Name field value +// and a boolean to check if the value has been set. +func (o *HealthCheck) GetNameOk() (*string, bool) { + if o == nil { + return nil, false + } + return &o.Name, true +} + +// SetName sets field value +func (o *HealthCheck) SetName(v string) { + o.Name = v +} + +// GetMessage returns the Message field value if set, zero value otherwise. +func (o *HealthCheck) GetMessage() string { + if o == nil || o.Message == nil { + var ret string + return ret + } + return *o.Message +} + +// GetMessageOk returns a tuple with the Message field value if set, nil otherwise +// and a boolean to check if the value has been set. +func (o *HealthCheck) GetMessageOk() (*string, bool) { + if o == nil || o.Message == nil { + return nil, false + } + return o.Message, true +} + +// HasMessage returns a boolean if a field has been set. +func (o *HealthCheck) HasMessage() bool { + if o != nil && o.Message != nil { + return true + } + + return false +} + +// SetMessage gets a reference to the given string and assigns it to the Message field. +func (o *HealthCheck) SetMessage(v string) { + o.Message = &v +} + +// GetChecks returns the Checks field value if set, zero value otherwise. +func (o *HealthCheck) GetChecks() []HealthCheck { + if o == nil || o.Checks == nil { + var ret []HealthCheck + return ret + } + return *o.Checks +} + +// GetChecksOk returns a tuple with the Checks field value if set, nil otherwise +// and a boolean to check if the value has been set. +func (o *HealthCheck) GetChecksOk() (*[]HealthCheck, bool) { + if o == nil || o.Checks == nil { + return nil, false + } + return o.Checks, true +} + +// HasChecks returns a boolean if a field has been set. +func (o *HealthCheck) HasChecks() bool { + if o != nil && o.Checks != nil { + return true + } + + return false +} + +// SetChecks gets a reference to the given []HealthCheck and assigns it to the Checks field. +func (o *HealthCheck) SetChecks(v []HealthCheck) { + o.Checks = &v +} + +// GetStatus returns the Status field value +func (o *HealthCheck) GetStatus() HealthCheckStatus { + if o == nil { + var ret HealthCheckStatus + return ret + } + + return o.Status +} + +// GetStatusOk returns a tuple with the Status field value +// and a boolean to check if the value has been set. +func (o *HealthCheck) GetStatusOk() (*HealthCheckStatus, bool) { + if o == nil { + return nil, false + } + return &o.Status, true +} + +// SetStatus sets field value +func (o *HealthCheck) SetStatus(v HealthCheckStatus) { + o.Status = v +} + +// GetVersion returns the Version field value if set, zero value otherwise. +func (o *HealthCheck) GetVersion() string { + if o == nil || o.Version == nil { + var ret string + return ret + } + return *o.Version +} + +// GetVersionOk returns a tuple with the Version field value if set, nil otherwise +// and a boolean to check if the value has been set. +func (o *HealthCheck) GetVersionOk() (*string, bool) { + if o == nil || o.Version == nil { + return nil, false + } + return o.Version, true +} + +// HasVersion returns a boolean if a field has been set. +func (o *HealthCheck) HasVersion() bool { + if o != nil && o.Version != nil { + return true + } + + return false +} + +// SetVersion gets a reference to the given string and assigns it to the Version field. +func (o *HealthCheck) SetVersion(v string) { + o.Version = &v +} + +// GetCommit returns the Commit field value if set, zero value otherwise. +func (o *HealthCheck) GetCommit() string { + if o == nil || o.Commit == nil { + var ret string + return ret + } + return *o.Commit +} + +// GetCommitOk returns a tuple with the Commit field value if set, nil otherwise +// and a boolean to check if the value has been set. +func (o *HealthCheck) GetCommitOk() (*string, bool) { + if o == nil || o.Commit == nil { + return nil, false + } + return o.Commit, true +} + +// HasCommit returns a boolean if a field has been set. +func (o *HealthCheck) HasCommit() bool { + if o != nil && o.Commit != nil { + return true + } + + return false +} + +// SetCommit gets a reference to the given string and assigns it to the Commit field. +func (o *HealthCheck) SetCommit(v string) { + o.Commit = &v +} + +func (o HealthCheck) MarshalJSON() ([]byte, error) { + toSerialize := map[string]interface{}{} + if true { + toSerialize["name"] = o.Name + } + if o.Message != nil { + toSerialize["message"] = o.Message + } + if o.Checks != nil { + toSerialize["checks"] = o.Checks + } + if true { + toSerialize["status"] = o.Status + } + if o.Version != nil { + toSerialize["version"] = o.Version + } + if o.Commit != nil { + toSerialize["commit"] = o.Commit + } + return json.Marshal(toSerialize) +} + +type NullableHealthCheck struct { + value *HealthCheck + isSet bool +} + +func (v NullableHealthCheck) Get() *HealthCheck { + return v.value +} + +func (v *NullableHealthCheck) Set(val *HealthCheck) { + v.value = val + v.isSet = true +} + +func (v NullableHealthCheck) IsSet() bool { + return v.isSet +} + +func (v *NullableHealthCheck) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableHealthCheck(val *HealthCheck) *NullableHealthCheck { + return &NullableHealthCheck{value: val, isSet: true} +} + +func (v NullableHealthCheck) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableHealthCheck) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} diff --git a/internal/api/model_health_check_status.go b/internal/api/model_health_check_status.go new file mode 100644 index 0000000..197be66 --- /dev/null +++ b/internal/api/model_health_check_status.go @@ -0,0 +1,83 @@ +/* + * Subset of Influx API covered by Influx CLI + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * API version: 2.0.0 + */ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package api + +import ( + "encoding/json" + "fmt" +) + +// HealthCheckStatus the model 'HealthCheckStatus' +type HealthCheckStatus string + +// List of HealthCheckStatus +const ( + HEALTHCHECKSTATUS_PASS HealthCheckStatus = "pass" + HEALTHCHECKSTATUS_FAIL HealthCheckStatus = "fail" +) + +func (v *HealthCheckStatus) UnmarshalJSON(src []byte) error { + var value string + err := json.Unmarshal(src, &value) + if err != nil { + return err + } + enumTypeValue := HealthCheckStatus(value) + for _, existing := range []HealthCheckStatus{"pass", "fail"} { + if existing == enumTypeValue { + *v = enumTypeValue + return nil + } + } + + return fmt.Errorf("%+v is not a valid HealthCheckStatus", value) +} + +// Ptr returns reference to HealthCheckStatus value +func (v HealthCheckStatus) Ptr() *HealthCheckStatus { + return &v +} + +type NullableHealthCheckStatus struct { + value *HealthCheckStatus + isSet bool +} + +func (v NullableHealthCheckStatus) Get() *HealthCheckStatus { + return v.value +} + +func (v *NullableHealthCheckStatus) Set(val *HealthCheckStatus) { + v.value = val + v.isSet = true +} + +func (v NullableHealthCheckStatus) IsSet() bool { + return v.isSet +} + +func (v *NullableHealthCheckStatus) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableHealthCheckStatus(val *HealthCheckStatus) *NullableHealthCheckStatus { + return &NullableHealthCheckStatus{value: val, isSet: true} +} + +func (v NullableHealthCheckStatus) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableHealthCheckStatus) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} diff --git a/internal/api/response.go b/internal/api/response.go new file mode 100644 index 0000000..e425bc9 --- /dev/null +++ b/internal/api/response.go @@ -0,0 +1,47 @@ +/* + * Subset of Influx API covered by Influx CLI + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * API version: 2.0.0 + */ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package api + +import ( + "net/http" +) + +// APIResponse stores the API response returned by the server. +type APIResponse struct { + *http.Response `json:"-"` + Message string `json:"message,omitempty"` + // Operation is the name of the OpenAPI operation. + Operation string `json:"operation,omitempty"` + // RequestURL is the request URL. This value is always available, even if the + // embedded *http.Response is nil. + RequestURL string `json:"url,omitempty"` + // Method is the HTTP method used for the request. This value is always + // available, even if the embedded *http.Response is nil. + Method string `json:"method,omitempty"` + // Payload holds the contents of the response body (which may be nil or empty). + // This is provided here as the raw response.Body() reader will have already + // been drained. + Payload []byte `json:"-"` +} + +// NewAPIResponse returns a new APIResonse object. +func NewAPIResponse(r *http.Response) *APIResponse { + + response := &APIResponse{Response: r} + return response +} + +// NewAPIResponseWithError returns a new APIResponse object with the provided error message. +func NewAPIResponseWithError(errorMessage string) *APIResponse { + + response := &APIResponse{Message: errorMessage} + return response +} diff --git a/internal/api/types.gen.go b/internal/api/types.gen.go deleted file mode 100644 index 402714f..0000000 --- a/internal/api/types.gen.go +++ /dev/null @@ -1,68 +0,0 @@ -// 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"` -} diff --git a/internal/api/types.go b/internal/api/types.go deleted file mode 100644 index 794ca30..0000000 --- a/internal/api/types.go +++ /dev/null @@ -1,21 +0,0 @@ -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) -} diff --git a/internal/api/utils.go b/internal/api/utils.go new file mode 100644 index 0000000..77b4440 --- /dev/null +++ b/internal/api/utils.go @@ -0,0 +1,328 @@ +/* + * Subset of Influx API covered by Influx CLI + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * API version: 2.0.0 + */ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package api + +import ( + "encoding/json" + "time" +) + +// PtrBool is a helper routine that returns a pointer to given boolean value. +func PtrBool(v bool) *bool { return &v } + +// PtrInt is a helper routine that returns a pointer to given integer value. +func PtrInt(v int) *int { return &v } + +// PtrInt32 is a helper routine that returns a pointer to given integer value. +func PtrInt32(v int32) *int32 { return &v } + +// PtrInt64 is a helper routine that returns a pointer to given integer value. +func PtrInt64(v int64) *int64 { return &v } + +// PtrFloat32 is a helper routine that returns a pointer to given float value. +func PtrFloat32(v float32) *float32 { return &v } + +// PtrFloat64 is a helper routine that returns a pointer to given float value. +func PtrFloat64(v float64) *float64 { return &v } + +// PtrString is a helper routine that returns a pointer to given string value. +func PtrString(v string) *string { return &v } + +// PtrTime is helper routine that returns a pointer to given Time value. +func PtrTime(v time.Time) *time.Time { return &v } + +type NullableBool struct { + value *bool + isSet bool +} + +func (v NullableBool) Get() *bool { + return v.value +} + +func (v *NullableBool) Set(val *bool) { + v.value = val + v.isSet = true +} + +func (v NullableBool) IsSet() bool { + return v.isSet +} + +func (v *NullableBool) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableBool(val *bool) *NullableBool { + return &NullableBool{value: val, isSet: true} +} + +func (v NullableBool) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableBool) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + +type NullableInt struct { + value *int + isSet bool +} + +func (v NullableInt) Get() *int { + return v.value +} + +func (v *NullableInt) Set(val *int) { + v.value = val + v.isSet = true +} + +func (v NullableInt) IsSet() bool { + return v.isSet +} + +func (v *NullableInt) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableInt(val *int) *NullableInt { + return &NullableInt{value: val, isSet: true} +} + +func (v NullableInt) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableInt) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + +type NullableInt32 struct { + value *int32 + isSet bool +} + +func (v NullableInt32) Get() *int32 { + return v.value +} + +func (v *NullableInt32) Set(val *int32) { + v.value = val + v.isSet = true +} + +func (v NullableInt32) IsSet() bool { + return v.isSet +} + +func (v *NullableInt32) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableInt32(val *int32) *NullableInt32 { + return &NullableInt32{value: val, isSet: true} +} + +func (v NullableInt32) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableInt32) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + +type NullableInt64 struct { + value *int64 + isSet bool +} + +func (v NullableInt64) Get() *int64 { + return v.value +} + +func (v *NullableInt64) Set(val *int64) { + v.value = val + v.isSet = true +} + +func (v NullableInt64) IsSet() bool { + return v.isSet +} + +func (v *NullableInt64) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableInt64(val *int64) *NullableInt64 { + return &NullableInt64{value: val, isSet: true} +} + +func (v NullableInt64) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableInt64) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + +type NullableFloat32 struct { + value *float32 + isSet bool +} + +func (v NullableFloat32) Get() *float32 { + return v.value +} + +func (v *NullableFloat32) Set(val *float32) { + v.value = val + v.isSet = true +} + +func (v NullableFloat32) IsSet() bool { + return v.isSet +} + +func (v *NullableFloat32) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableFloat32(val *float32) *NullableFloat32 { + return &NullableFloat32{value: val, isSet: true} +} + +func (v NullableFloat32) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableFloat32) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + +type NullableFloat64 struct { + value *float64 + isSet bool +} + +func (v NullableFloat64) Get() *float64 { + return v.value +} + +func (v *NullableFloat64) Set(val *float64) { + v.value = val + v.isSet = true +} + +func (v NullableFloat64) IsSet() bool { + return v.isSet +} + +func (v *NullableFloat64) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableFloat64(val *float64) *NullableFloat64 { + return &NullableFloat64{value: val, isSet: true} +} + +func (v NullableFloat64) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableFloat64) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + +type NullableString struct { + value *string + isSet bool +} + +func (v NullableString) Get() *string { + return v.value +} + +func (v *NullableString) Set(val *string) { + v.value = val + v.isSet = true +} + +func (v NullableString) IsSet() bool { + return v.isSet +} + +func (v *NullableString) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableString(val *string) *NullableString { + return &NullableString{value: val, isSet: true} +} + +func (v NullableString) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableString) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + +type NullableTime struct { + value *time.Time + isSet bool +} + +func (v NullableTime) Get() *time.Time { + return v.value +} + +func (v *NullableTime) Set(val *time.Time) { + v.value = val + v.isSet = true +} + +func (v NullableTime) IsSet() bool { + return v.isSet +} + +func (v *NullableTime) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableTime(val *time.Time) *NullableTime { + return &NullableTime{value: val, isSet: true} +} + +func (v NullableTime) MarshalJSON() ([]byte, error) { + return v.value.MarshalJSON() +} + +func (v *NullableTime) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} diff --git a/internal/cli.go b/internal/cli.go index 27d95c0..03ba27f 100644 --- a/internal/cli.go +++ b/internal/cli.go @@ -2,8 +2,6 @@ package internal import ( "io" - - "github.com/influxdata/influx-cli/v2/internal/api" ) type CLI struct { @@ -11,5 +9,5 @@ type CLI struct { Stdout io.Writer Stderr io.Writer - TraceId *api.TraceSpan + TraceId string } diff --git a/internal/config/config.go b/internal/config/config.go index 46aceac..1a62729 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -93,7 +93,7 @@ func blockBadName(cfgs Configs) error { for n := range cfgs { if _, ok := badNames[n]; ok { return &api.Error{ - Code: api.ErrorCode_invalid, + Code: api.ERRORCODE_INVALID, Message: fmt.Sprintf("%q is not a valid config name", n), } } @@ -105,7 +105,7 @@ func blockBadName(cfgs Configs) error { func (cfgs Configs) Switch(name string) error { if _, ok := cfgs[name]; !ok { return &api.Error{ - Code: api.ErrorCode_not_found, + Code: api.ERRORCODE_NOT_FOUND, Message: fmt.Sprintf("config %q is not found", name), } } diff --git a/internal/config/local.go b/internal/config/local.go index ccb27ec..be476cf 100644 --- a/internal/config/local.go +++ b/internal/config/local.go @@ -35,7 +35,7 @@ func NewLocalConfigService(path string) Service { func (svc localConfigsSVC) CreateConfig(cfg Config) (Config, error) { if cfg.Name == "" { return Config{}, &api.Error{ - Code: api.ErrorCode_invalid, + Code: api.ERRORCODE_INVALID, Message: "config name is empty", } } @@ -45,7 +45,7 @@ func (svc localConfigsSVC) CreateConfig(cfg Config) (Config, error) { } if _, ok := cfgs[cfg.Name]; ok { return Config{}, &api.Error{ - Code: api.ErrorCode_conflict, + Code: api.ERRORCODE_CONFLICT, Message: fmt.Sprintf("config %q already exists", cfg.Name), } } @@ -69,7 +69,7 @@ func (svc localConfigsSVC) DeleteConfig(name string) (Config, error) { p, ok := cfgs[name] if !ok { return Config{}, &api.Error{ - Code: api.ErrorCode_not_found, + Code: api.ERRORCODE_NOT_FOUND, Message: fmt.Sprintf("config %q is not found", name), } } @@ -111,7 +111,7 @@ func (svc localConfigsSVC) UpdateConfig(up Config) (Config, error) { p0, ok := cfgs[up.Name] if !ok { return Config{}, &api.Error{ - Code: api.ErrorCode_not_found, + Code: api.ERRORCODE_NOT_FOUND, Message: fmt.Sprintf("config %q is not found", up.Name), } } @@ -213,7 +213,7 @@ func (s baseRW) parseActiveConfig(currentOrPrevious bool) (Config, error) { hasActive = true } else if check { return DefaultConfig, &api.Error{ - Code: api.ErrorCode_conflict, + Code: api.ERRORCODE_CONFLICT, Message: fmt.Sprintf("more than one %s activated configs found", previousText), } } @@ -222,7 +222,7 @@ func (s baseRW) parseActiveConfig(currentOrPrevious bool) (Config, error) { return activated, nil } return DefaultConfig, &api.Error{ - Code: api.ErrorCode_not_found, + Code: api.ERRORCODE_NOT_FOUND, Message: fmt.Sprintf("%s activated config is not found", previousText), } } diff --git a/internal/ping.go b/internal/ping.go index 8723c99..d992d3d 100644 --- a/internal/ping.go +++ b/internal/ping.go @@ -7,37 +7,27 @@ import ( "github.com/influxdata/influx-cli/v2/internal/api" ) -type Client interface { - GetHealthWithResponse(context.Context, *api.GetHealthParams, ...api.RequestEditorFn) (*api.GetHealthResponse, error) -} - // Ping checks the health of a remote InfluxDB instance. -func (c *CLI) Ping(ctx context.Context, client Client) error { - resp, err := client.GetHealthWithResponse(ctx, &api.GetHealthParams{ZapTraceSpan: c.TraceId}) +func (c *CLI) Ping(ctx context.Context, client api.HealthApi) error { + req := client.GetHealth(ctx) + if c.TraceId != "" { + req = req.ZapTraceSpan(c.TraceId) + } + resp, _, err := client.GetHealthExecute(req) if err != nil { return fmt.Errorf("failed to make health check request: %w", err) } - var failureMessage string - if resp.JSON503 != nil { - if resp.JSON503.Message != nil { - failureMessage = *resp.JSON503.Message + if resp.Status == api.HEALTHCHECKSTATUS_FAIL { + var message string + if resp.Message != nil { + message = *resp.Message } else { - failureMessage = "status 503" - } - } else if resp.JSONDefault != nil { - failureMessage = resp.JSONDefault.Error() - } else if resp.JSON200.Status != api.HealthCheckStatus_pass { - if resp.JSON200.Message != nil { - failureMessage = *resp.JSON200.Message - } else { - failureMessage = fmt.Sprintf("check %s failed", resp.JSON200.Name) + message = fmt.Sprintf("check %s failed", resp.Name) } + return fmt.Errorf("health check failed: %s", message) } - if failureMessage != "" { - return fmt.Errorf("health check failed: %s", failureMessage) - } c.Stdout.Write([]byte("OK\n")) return nil } diff --git a/internal/ping_test.go b/internal/ping_test.go index 8d66700..4a8f592 100644 --- a/internal/ping_test.go +++ b/internal/ping_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "net/http" "testing" "github.com/influxdata/influx-cli/v2/internal" @@ -12,22 +13,25 @@ import ( ) type testClient struct { - GetHealthFn func(context.Context, *api.GetHealthParams, ...api.RequestEditorFn) (*api.GetHealthResponse, error) + GetHealthExecuteFn func(api.ApiGetHealthRequest) (api.HealthCheck, *http.Response, error) } -func (tc *testClient) GetHealthWithResponse(ctx context.Context, p *api.GetHealthParams, fns ...api.RequestEditorFn) (*api.GetHealthResponse, error) { - return tc.GetHealthFn(ctx, p, fns...) +func (tc *testClient) GetHealth(context.Context) api.ApiGetHealthRequest { + return api.ApiGetHealthRequest{ + ApiService: tc, + } } -func Test_PingSuccessNoTracing(t *testing.T) { +func (tc *testClient) GetHealthExecute(req api.ApiGetHealthRequest) (api.HealthCheck, *http.Response, error) { + return tc.GetHealthExecuteFn(req) +} + +func Test_PingSuccess(t *testing.T) { t.Parallel() client := &testClient{ - GetHealthFn: func(_ context.Context, p *api.GetHealthParams, _ ...api.RequestEditorFn) (*api.GetHealthResponse, error) { - require.Nil(t, p.ZapTraceSpan) - return &api.GetHealthResponse{ - JSON200: &api.HealthCheck{Name: "test", Status: api.HealthCheckStatus_pass}, - }, nil + GetHealthExecuteFn: func(api.ApiGetHealthRequest) (api.HealthCheck, *http.Response, error) { + return api.HealthCheck{Status: api.HEALTHCHECKSTATUS_PASS}, nil, nil }, } @@ -38,33 +42,13 @@ func Test_PingSuccessNoTracing(t *testing.T) { require.Equal(t, "OK\n", out.String()) } -func Test_PingSuccessWithTracing(t *testing.T) { - t.Parallel() - - traceId := api.TraceSpan("trace-id") - client := &testClient{ - GetHealthFn: func(_ context.Context, p *api.GetHealthParams, _ ...api.RequestEditorFn) (*api.GetHealthResponse, error) { - require.Equal(t, traceId, *p.ZapTraceSpan) - return &api.GetHealthResponse{ - JSON200: &api.HealthCheck{Name: "test", Status: api.HealthCheckStatus_pass}, - }, nil - }, - } - - out := &bytes.Buffer{} - cli := &internal.CLI{Stdout: out, TraceId: &traceId} - - require.NoError(t, cli.Ping(context.Background(), client)) - require.Equal(t, "OK\n", out.String()) -} - func Test_PingFailedRequest(t *testing.T) { t.Parallel() e := "the internet is down" client := &testClient{ - GetHealthFn: func(context.Context, *api.GetHealthParams, ...api.RequestEditorFn) (*api.GetHealthResponse, error) { - return nil, errors.New(e) + GetHealthExecuteFn: func(api.ApiGetHealthRequest) (api.HealthCheck, *http.Response, error) { + return api.HealthCheck{}, nil, errors.New(e) }, } @@ -82,10 +66,8 @@ func Test_PingFailedStatus(t *testing.T) { e := "I broke" client := &testClient{ - GetHealthFn: func(context.Context, *api.GetHealthParams, ...api.RequestEditorFn) (*api.GetHealthResponse, error) { - return &api.GetHealthResponse{ - JSON503: &api.HealthCheck{Name: "test", Status: api.HealthCheckStatus_fail, Message: &e}, - }, nil + GetHealthExecuteFn: func(api.ApiGetHealthRequest) (api.HealthCheck, *http.Response, error) { + return api.HealthCheck{Status: api.HEALTHCHECKSTATUS_FAIL, Message: &e}, nil, nil }, } @@ -98,18 +80,13 @@ func Test_PingFailedStatus(t *testing.T) { require.Empty(t, out.String()) } -func Test_PingFailedUnhandledError(t *testing.T) { +func Test_PingFailedStatusNoMessage(t *testing.T) { t.Parallel() - e := "something went boom" + name := "foo" client := &testClient{ - GetHealthFn: func(context.Context, *api.GetHealthParams, ...api.RequestEditorFn) (*api.GetHealthResponse, error) { - return &api.GetHealthResponse{ - JSONDefault: &api.Error{ - Code: api.ErrorCode_internal_error, - Message: e, - }, - }, nil + GetHealthExecuteFn: func(api.ApiGetHealthRequest) (api.HealthCheck, *http.Response, error) { + return api.HealthCheck{Status: api.HEALTHCHECKSTATUS_FAIL, Name: name}, nil, nil }, } @@ -118,27 +95,6 @@ func Test_PingFailedUnhandledError(t *testing.T) { err := cli.Ping(context.Background(), client) require.Error(t, err) - require.Contains(t, err.Error(), e) - require.Empty(t, out.String()) -} - -func Test_PingFailedCheck(t *testing.T) { - t.Parallel() - - e := "oops, forgot to set the status code" - client := &testClient{ - GetHealthFn: func(_ context.Context, p *api.GetHealthParams, _ ...api.RequestEditorFn) (*api.GetHealthResponse, error) { - return &api.GetHealthResponse{ - JSON200: &api.HealthCheck{Name: "test", Status: api.HealthCheckStatus_fail, Message: &e}, - }, nil - }, - } - - out := &bytes.Buffer{} - cli := &internal.CLI{Stdout: out} - - err := cli.Ping(context.Background(), client) - require.Error(t, err) - require.Contains(t, err.Error(), e) + require.Contains(t, err.Error(), name) require.Empty(t, out.String()) } diff --git a/tools.go b/tools.go index feda55a..ac3cdab 100644 --- a/tools.go +++ b/tools.go @@ -11,6 +11,5 @@ package influxcli import ( _ "github.com/daixiang0/gci" - _ "github.com/deepmap/oapi-codegen/cmd/oapi-codegen" _ "honnef.co/go/tools/cmd/staticcheck" )