2022-09-30 21:12:06 +02:00
|
|
|
package zrokdir
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-openapi/runtime"
|
|
|
|
httptransport "github.com/go-openapi/runtime/client"
|
|
|
|
"github.com/go-openapi/strfmt"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/build"
|
|
|
|
"github.com/openziti/zrok/rest_client_zrok"
|
2022-09-30 21:12:06 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"net/url"
|
2022-09-30 21:20:15 +02:00
|
|
|
"os"
|
2023-04-07 22:14:10 +02:00
|
|
|
"regexp"
|
2022-09-30 21:12:06 +02:00
|
|
|
)
|
|
|
|
|
2023-01-09 20:16:08 +01:00
|
|
|
func (zrd *ZrokDir) Client() (*rest_client_zrok.Zrok, error) {
|
2023-01-09 20:33:17 +01:00
|
|
|
apiEndpoint, _ := zrd.ApiEndpoint()
|
2023-01-09 20:16:08 +01:00
|
|
|
apiUrl, err := url.Parse(apiEndpoint)
|
2022-09-30 21:12:06 +02:00
|
|
|
if err != nil {
|
2023-01-09 20:16:08 +01:00
|
|
|
return nil, errors.Wrapf(err, "error parsing api endpoint '%v'", zrd)
|
2022-09-30 21:12:06 +02:00
|
|
|
}
|
|
|
|
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()
|
2022-11-21 20:02:51 +01:00
|
|
|
|
|
|
|
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)
|
2022-11-21 20:02:51 +01:00
|
|
|
}
|
2023-04-07 22:14:10 +02:00
|
|
|
// 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)) {
|
2022-11-21 20:02:51 +01:00
|
|
|
return nil, errors.Errorf("expected a '%v' version, received: '%v'", build.Series, v.Payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
return zrok, nil
|
2022-09-30 21:12:06 +02:00
|
|
|
}
|
2023-01-09 20:16:08 +01:00
|
|
|
|
2023-01-09 20:33:17 +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
|
2023-01-09 20:33:17 +01:00
|
|
|
from = "config"
|
2023-01-09 20:16:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
env := os.Getenv("ZROK_API_ENDPOINT")
|
|
|
|
if env != "" {
|
|
|
|
apiEndpoint = env
|
2023-01-09 20:33:17 +01:00
|
|
|
from = "ZROK_API_ENDPOINT"
|
2023-01-09 20:16:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if zrd.Env != nil && zrd.Env.ApiEndpoint != "" {
|
|
|
|
apiEndpoint = zrd.Env.ApiEndpoint
|
2023-01-09 20:33:17 +01:00
|
|
|
from = "env"
|
2023-01-09 20:16:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-09 20:33:17 +01:00
|
|
|
return apiEndpoint, from
|
2023-01-09 20:16:08 +01:00
|
|
|
}
|