zrok/zrokdir/client.go

62 lines
1.7 KiB
Go
Raw Normal View History

package zrokdir
import (
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/openziti/zrok/build"
"github.com/openziti/zrok/rest_client_zrok"
"github.com/pkg/errors"
"net/url"
"os"
"regexp"
)
2023-01-09 20:16:08 +01:00
func (zrd *ZrokDir) Client() (*rest_client_zrok.Zrok, error) {
apiEndpoint, _ := zrd.ApiEndpoint()
2023-01-09 20:16:08 +01:00
apiUrl, err := url.Parse(apiEndpoint)
if err != nil {
2023-01-09 20:16:08 +01:00
return nil, errors.Wrapf(err, "error parsing api endpoint '%v'", zrd)
}
transport := httptransport.New(apiUrl.Host, "/api/v1", []string{apiUrl.Scheme})
transport.Producers["application/zrok.v1+json"] = runtime.JSONProducer()
transport.Consumers["application/zrok.v1+json"] = runtime.JSONConsumer()
zrok := rest_client_zrok.New(transport, strfmt.Default)
v, err := zrok.Metadata.Version(nil)
if err != nil {
2023-01-09 20:16:08 +01:00
return nil, errors.Wrapf(err, "error getting version from api endpoint '%v': %v", apiEndpoint, err)
}
// allow reported version string to be optionally prefixed with
// "refs/heads/" or "refs/tags/"
re := regexp.MustCompile(`^(refs/(heads|tags)/)?` + build.Series)
if ! re.MatchString(string(v.Payload)) {
return nil, errors.Errorf("expected a '%v' version, received: '%v'", build.Series, v.Payload)
}
return zrok, nil
}
2023-01-09 20:16:08 +01:00
func (zrd *ZrokDir) ApiEndpoint() (apiEndpoint string, from string) {
apiEndpoint = "https://api.zrok.io"
from = "binary"
2023-01-09 20:16:08 +01:00
if zrd.Cfg != nil && zrd.Cfg.ApiEndpoint != "" {
apiEndpoint = zrd.Cfg.ApiEndpoint
from = "config"
2023-01-09 20:16:08 +01:00
}
env := os.Getenv("ZROK_API_ENDPOINT")
if env != "" {
apiEndpoint = env
from = "ZROK_API_ENDPOINT"
2023-01-09 20:16:08 +01:00
}
if zrd.Env != nil && zrd.Env.ApiEndpoint != "" {
apiEndpoint = zrd.Env.ApiEndpoint
from = "env"
2023-01-09 20:16:08 +01:00
}
return apiEndpoint, from
2023-01-09 20:16:08 +01:00
}