mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Expose function to retrieve gcom info (#99372)
This commit is contained in:
parent
23f495d4cd
commit
003f0e5918
@ -272,6 +272,10 @@ func (r *FakePluginRepo) PluginVersion(ctx context.Context, pluginID, version st
|
||||
return repo.VersionData{}, nil
|
||||
}
|
||||
|
||||
func (r *FakePluginRepo) PluginInfo(ctx context.Context, pluginID string) (*repo.PluginInfo, error) {
|
||||
return &repo.PluginInfo{}, nil
|
||||
}
|
||||
|
||||
type fakeTracerProvider struct {
|
||||
noop.TracerProvider
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ type Service interface {
|
||||
GetPluginArchiveInfo(ctx context.Context, pluginID, version string, opts CompatOpts) (*PluginArchiveInfo, error)
|
||||
// PluginVersion will return plugin version based on the requested information.
|
||||
PluginVersion(ctx context.Context, pluginID, version string, compatOpts CompatOpts) (VersionData, error)
|
||||
// PluginInfo will return generic plugin information from grafana.com/api/plugins.
|
||||
PluginInfo(ctx context.Context, pluginID string) (*PluginInfo, error)
|
||||
}
|
||||
|
||||
type CompatOpts struct {
|
||||
|
@ -32,3 +32,10 @@ type ArchMeta struct {
|
||||
PackageName string `json:"packageName"`
|
||||
DownloadURL string `json:"downloadUrl"`
|
||||
}
|
||||
|
||||
// PluginInfo is (a subset of) the JSON response from grafana.com/api/plugins/$pluginID
|
||||
type PluginInfo struct {
|
||||
ID int `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
|
@ -131,3 +131,26 @@ func (m *Manager) grafanaCompatiblePluginVersions(ctx context.Context, pluginID
|
||||
|
||||
return v.Versions, nil
|
||||
}
|
||||
|
||||
func (m *Manager) PluginInfo(ctx context.Context, pluginID string) (*PluginInfo, error) {
|
||||
u, err := url.Parse(m.client.grafanaComAPIURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u.Path = path.Join(u.Path, pluginID)
|
||||
|
||||
body, err := m.client.SendReq(ctx, u, CompatOpts{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var v PluginInfo
|
||||
err = json.Unmarshal(body, &v)
|
||||
if err != nil {
|
||||
m.log.Error("Failed to unmarshal plugin repo response", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &v, nil
|
||||
}
|
||||
|
@ -108,6 +108,31 @@ func TestGetPluginArchive(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginInfo(t *testing.T) {
|
||||
const (
|
||||
pluginID = "grafana-test-datasource"
|
||||
)
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
require.Equal(t, fmt.Sprintf("/%s", pluginID), r.URL.Path)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(fmt.Sprintf(`{ "id": 1, "slug": "%s", "status": "active" }`, pluginID)))
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
m := NewManager(ManagerCfg{
|
||||
SkipTLSVerify: false,
|
||||
BaseURL: srv.URL,
|
||||
Logger: log.NewTestPrettyLogger(),
|
||||
})
|
||||
pi, err := m.PluginInfo(context.Background(), pluginID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, pi.ID)
|
||||
require.Equal(t, pluginID, pi.Slug)
|
||||
require.Equal(t, "active", pi.Status)
|
||||
}
|
||||
|
||||
func verifyArchive(t *testing.T, archive *PluginArchive) {
|
||||
t.Helper()
|
||||
require.NotNil(t, archive)
|
||||
|
Loading…
Reference in New Issue
Block a user