diff --git a/service/client.go b/public/client.go similarity index 98% rename from service/client.go rename to public/client.go index ed801b2..ed77d9e 100644 --- a/service/client.go +++ b/public/client.go @@ -1,7 +1,7 @@ // Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -package service +package public import ( "bytes" @@ -15,7 +15,7 @@ import ( "net/url" "time" - "github.com/mattermost/calls-offloader/service/job" + "github.com/mattermost/calls-offloader/public/job" ) var ( @@ -431,3 +431,11 @@ func (c *Client) GetVersionInfo() (VersionInfo, error) { return info, nil } + +func (c *Client) AuthToken() string { + return c.authToken +} + +func (c *Client) URL() string { + return c.cfg.httpURL +} diff --git a/service/job/job.go b/public/job/job.go similarity index 100% rename from service/job/job.go rename to public/job/job.go diff --git a/service/job/job_test.go b/public/job/job_test.go similarity index 100% rename from service/job/job_test.go rename to public/job/job_test.go diff --git a/service/job/utils.go b/public/job/utils.go similarity index 100% rename from service/job/utils.go rename to public/job/utils.go diff --git a/service/job/utils_test.go b/public/job/utils_test.go similarity index 100% rename from service/job/utils_test.go rename to public/job/utils_test.go diff --git a/public/version.go b/public/version.go new file mode 100644 index 0000000..7cbf95f --- /dev/null +++ b/public/version.go @@ -0,0 +1,24 @@ +// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package public + +import ( + "github.com/mattermost/mattermost/server/public/shared/mlog" +) + +type VersionInfo struct { + BuildDate string `json:"buildDate"` + BuildVersion string `json:"buildVersion"` + BuildHash string `json:"buildHash"` + GoVersion string `json:"goVersion"` +} + +func (v VersionInfo) LogFields() []mlog.Field { + return []mlog.Field{ + mlog.String("buildDate", v.BuildDate), + mlog.String("buildVersion", v.BuildVersion), + mlog.String("buildHash", v.BuildHash), + mlog.String("goVersion", v.GoVersion), + } +} diff --git a/service/client_test.go b/service/client_test.go index 6e976bf..2763aec 100644 --- a/service/client_test.go +++ b/service/client_test.go @@ -8,6 +8,7 @@ import ( "net" "testing" + "github.com/mattermost/calls-offloader/public" "github.com/mattermost/calls-offloader/service/auth" "github.com/mattermost/calls-offloader/service/random" @@ -16,21 +17,21 @@ import ( func TestNewClient(t *testing.T) { t.Run("empty config", func(t *testing.T) { - c, err := NewClient(ClientConfig{}) + c, err := public.NewClient(public.ClientConfig{}) require.Error(t, err) require.Equal(t, "failed to parse config: invalid URL value: should not be empty", err.Error()) require.Nil(t, c) }) t.Run("invalid url", func(t *testing.T) { - c, err := NewClient(ClientConfig{URL: "not_a_url"}) + c, err := public.NewClient(public.ClientConfig{URL: "not_a_url"}) require.Error(t, err) require.Equal(t, "failed to parse config: invalid url host: should not be empty", err.Error()) require.Nil(t, c) }) t.Run("invalid scheme", func(t *testing.T) { - c, err := NewClient(ClientConfig{URL: "ftp://invalid"}) + c, err := public.NewClient(public.ClientConfig{URL: "ftp://invalid"}) require.Error(t, err) require.Equal(t, `failed to parse config: invalid url scheme: "ftp" is not valid`, err.Error()) require.Nil(t, c) @@ -38,20 +39,20 @@ func TestNewClient(t *testing.T) { t.Run("success http scheme", func(t *testing.T) { apiURL := "http://localhost" - c, err := NewClient(ClientConfig{URL: apiURL}) + c, err := public.NewClient(public.ClientConfig{URL: apiURL}) require.NoError(t, err) require.NotNil(t, c) require.NotEmpty(t, c) - require.Equal(t, apiURL, c.cfg.httpURL) + require.Equal(t, apiURL, c.URL()) }) t.Run("success https scheme", func(t *testing.T) { apiURL := "https://localhost" - c, err := NewClient(ClientConfig{URL: apiURL}) + c, err := public.NewClient(public.ClientConfig{URL: apiURL}) require.NoError(t, err) require.NotNil(t, c) require.NotEmpty(t, c) - require.Equal(t, apiURL, c.cfg.httpURL) + require.Equal(t, apiURL, c.URL()) }) t.Run("custom dialing function", func(t *testing.T) { @@ -62,11 +63,11 @@ func TestNewClient(t *testing.T) { } apiURL := "http://localhost" - c, err := NewClient(ClientConfig{URL: apiURL}, WithDialFunc(dialFn)) + c, err := public.NewClient(public.ClientConfig{URL: apiURL}, public.WithDialFunc(dialFn)) require.NoError(t, err) require.NotNil(t, c) require.NotEmpty(t, c) - require.Equal(t, apiURL, c.cfg.httpURL) + require.Equal(t, apiURL, c.URL()) _ = c.Register("", "") @@ -78,7 +79,7 @@ func TestClientRegister(t *testing.T) { th := SetupTestHelper(t, nil) defer th.Teardown() - c, err := NewClient(ClientConfig{ + c, err := public.NewClient(public.ClientConfig{ URL: th.apiURL, AuthKey: th.srvc.cfg.API.Security.AdminSecretKey, }) @@ -116,7 +117,7 @@ func TestClientRegister(t *testing.T) { }) t.Run("unauthorized", func(t *testing.T) { - c, err := NewClient(ClientConfig{ + c, err := public.NewClient(public.ClientConfig{ URL: th.apiURL, AuthKey: th.srvc.cfg.API.Security.AdminSecretKey + "_", }) @@ -126,11 +127,11 @@ func TestClientRegister(t *testing.T) { err = c.Register("", "") require.Error(t, err) - require.Equal(t, ErrUnauthorized, err) + require.Equal(t, public.ErrUnauthorized, err) }) t.Run("self registering", func(t *testing.T) { - c, err := NewClient(ClientConfig{ + c, err := public.NewClient(public.ClientConfig{ URL: th.apiURL, }) require.NoError(t, err) @@ -141,7 +142,7 @@ func TestClientRegister(t *testing.T) { require.NoError(t, err) err = c.Register("clientB", authKey) require.Error(t, err) - require.Equal(t, ErrUnauthorized, err) + require.Equal(t, public.ErrUnauthorized, err) th.srvc.cfg.API.Security.AllowSelfRegistration = true err = c.Register("clientB", authKey) @@ -153,7 +154,7 @@ func TestClientUnregister(t *testing.T) { th := SetupTestHelper(t, nil) defer th.Teardown() - c, err := NewClient(ClientConfig{ + c, err := public.NewClient(public.ClientConfig{ URL: th.apiURL, AuthKey: th.srvc.cfg.API.Security.AdminSecretKey, }) @@ -185,7 +186,7 @@ func TestClientUnregister(t *testing.T) { }) t.Run("unauthorized", func(t *testing.T) { - c, err := NewClient(ClientConfig{ + c, err := public.NewClient(public.ClientConfig{ URL: th.apiURL, AuthKey: th.srvc.cfg.API.Security.AdminSecretKey + "_", }) @@ -195,7 +196,7 @@ func TestClientUnregister(t *testing.T) { err = c.Unregister("clientA") require.Error(t, err) - require.Equal(t, ErrUnauthorized, err) + require.Equal(t, public.ErrUnauthorized, err) }) } @@ -203,7 +204,7 @@ func TestClientLogin(t *testing.T) { th := SetupTestHelper(t, nil) defer th.Teardown() - c, err := NewClient(ClientConfig{ + c, err := public.NewClient(public.ClientConfig{ URL: th.apiURL, AuthKey: th.srvc.cfg.API.Security.AdminSecretKey, }) @@ -220,7 +221,7 @@ func TestClientLogin(t *testing.T) { err = c.Login("clientA", authKey) require.NoError(t, err) - require.NotEmpty(t, c.authToken) + require.NotEmpty(t, c.AuthToken()) }) t.Run("not found", func(t *testing.T) { @@ -246,7 +247,7 @@ func TestClientGetVersionInfo(t *testing.T) { th := SetupTestHelper(t, nil) defer th.Teardown() - c, err := NewClient(ClientConfig{ + c, err := public.NewClient(public.ClientConfig{ URL: th.apiURL, AuthKey: th.srvc.cfg.API.Security.AdminSecretKey, }) diff --git a/service/docker/service.go b/service/docker/service.go index fe91a6e..c6ae388 100644 --- a/service/docker/service.go +++ b/service/docker/service.go @@ -13,7 +13,7 @@ import ( "runtime" "time" - "github.com/mattermost/calls-offloader/service/job" + "github.com/mattermost/calls-offloader/public/job" "github.com/mattermost/calls-offloader/service/random" recorder "github.com/mattermost/calls-recorder/cmd/recorder/config" diff --git a/service/docker/service_test.go b/service/docker/service_test.go index 6ce34b0..4201f4b 100644 --- a/service/docker/service_test.go +++ b/service/docker/service_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/mattermost/calls-offloader/service/job" + "github.com/mattermost/calls-offloader/public/job" "github.com/mattermost/mattermost/server/public/shared/mlog" diff --git a/service/helper_test.go b/service/helper_test.go index ed0b77e..74e1135 100644 --- a/service/helper_test.go +++ b/service/helper_test.go @@ -4,19 +4,21 @@ package service import ( - "github.com/mattermost/calls-offloader/logger" - "github.com/mattermost/calls-offloader/service/api" - "github.com/mattermost/calls-offloader/service/auth" "net" "os" "testing" + "github.com/mattermost/calls-offloader/logger" + "github.com/mattermost/calls-offloader/public" + "github.com/mattermost/calls-offloader/service/api" + "github.com/mattermost/calls-offloader/service/auth" + "github.com/stretchr/testify/require" ) type TestHelper struct { srvc *Service - adminClient *Client + adminClient *public.Client cfg Config tb testing.TB apiURL string @@ -49,7 +51,7 @@ func SetupTestHelper(tb testing.TB, cfg *Config) *TestHelper { require.NoError(th.tb, err) th.apiURL = "http://localhost:" + port - th.adminClient, err = NewClient(ClientConfig{ + th.adminClient, err = public.NewClient(public.ClientConfig{ URL: th.apiURL, AuthKey: th.srvc.cfg.API.Security.AdminSecretKey, }) diff --git a/service/job_store.go b/service/job_store.go index d2a5db1..8d4f100 100644 --- a/service/job_store.go +++ b/service/job_store.go @@ -7,7 +7,7 @@ import ( "encoding/json" "fmt" - "github.com/mattermost/calls-offloader/service/job" + "github.com/mattermost/calls-offloader/public/job" ) const jobKeyPrefix = "job_" diff --git a/service/jobs_api.go b/service/jobs_api.go index b14c286..f648401 100644 --- a/service/jobs_api.go +++ b/service/jobs_api.go @@ -10,7 +10,7 @@ import ( "net/http" "time" - "github.com/mattermost/calls-offloader/service/job" + "github.com/mattermost/calls-offloader/public/job" "github.com/gorilla/mux" diff --git a/service/jobs_service.go b/service/jobs_service.go index f6a2678..cc576f9 100644 --- a/service/jobs_service.go +++ b/service/jobs_service.go @@ -7,8 +7,8 @@ import ( "fmt" "io" + "github.com/mattermost/calls-offloader/public/job" "github.com/mattermost/calls-offloader/service/docker" - "github.com/mattermost/calls-offloader/service/job" "github.com/mattermost/calls-offloader/service/kubernetes" "github.com/mattermost/mattermost/server/public/shared/mlog" diff --git a/service/kubernetes/service.go b/service/kubernetes/service.go index 3caa9cb..b085b1b 100644 --- a/service/kubernetes/service.go +++ b/service/kubernetes/service.go @@ -11,7 +11,7 @@ import ( "os" "time" - "github.com/mattermost/calls-offloader/service/job" + "github.com/mattermost/calls-offloader/public/job" "github.com/mattermost/calls-offloader/service/random" recorder "github.com/mattermost/calls-recorder/cmd/recorder/config" diff --git a/service/service.go b/service/service.go index 59afcfb..9d1a9e5 100644 --- a/service/service.go +++ b/service/service.go @@ -44,7 +44,7 @@ func New(cfg Config) (*Service, error) { return nil, fmt.Errorf("failed to init logger: %w", err) } - s.log.Info("starting up", getVersionInfo().logFields()...) + s.log.Info("starting up", getVersionInfo().LogFields()...) s.store, err = store.New(cfg.Store.DataSource) if err != nil { diff --git a/service/version.go b/service/version.go index 64df0dc..09a2b33 100644 --- a/service/version.go +++ b/service/version.go @@ -8,6 +8,8 @@ import ( "net/http" "runtime" + "github.com/mattermost/calls-offloader/public" + "github.com/mattermost/mattermost/server/public/shared/mlog" ) @@ -17,15 +19,8 @@ var ( buildDate string ) -type VersionInfo struct { - BuildDate string `json:"buildDate"` - BuildVersion string `json:"buildVersion"` - BuildHash string `json:"buildHash"` - GoVersion string `json:"goVersion"` -} - -func getVersionInfo() VersionInfo { - return VersionInfo{ +func getVersionInfo() public.VersionInfo { + return public.VersionInfo{ BuildDate: buildDate, BuildVersion: buildVersion, BuildHash: buildHash, @@ -33,15 +28,6 @@ func getVersionInfo() VersionInfo { } } -func (v VersionInfo) logFields() []mlog.Field { - return []mlog.Field{ - mlog.String("buildDate", v.BuildDate), - mlog.String("buildVersion", v.BuildVersion), - mlog.String("buildHash", v.BuildHash), - mlog.String("goVersion", v.GoVersion), - } -} - func (s *Service) getVersion(w http.ResponseWriter, req *http.Request) { if req.Method != http.MethodGet { http.NotFound(w, req) diff --git a/service/version_test.go b/service/version_test.go index 64186a5..81623f1 100644 --- a/service/version_test.go +++ b/service/version_test.go @@ -9,6 +9,8 @@ import ( "runtime" "testing" + "github.com/mattermost/calls-offloader/public" + "github.com/stretchr/testify/require" ) @@ -36,10 +38,10 @@ func TestGetVersion(t *testing.T) { require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) defer resp.Body.Close() - var info VersionInfo + var info public.VersionInfo err = json.NewDecoder(resp.Body).Decode(&info) require.NoError(t, err) - require.Equal(t, VersionInfo{ + require.Equal(t, public.VersionInfo{ BuildHash: buildHash, BuildDate: buildDate, BuildVersion: buildVersion, @@ -53,10 +55,10 @@ func TestGetVersion(t *testing.T) { require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) defer resp.Body.Close() - var info VersionInfo + var info public.VersionInfo err = json.NewDecoder(resp.Body).Decode(&info) require.NoError(t, err) - require.Equal(t, VersionInfo{ + require.Equal(t, public.VersionInfo{ GoVersion: goVersion, }, info) })