Skip to content

Commit

Permalink
Plugins: Expose function to retrieve gcom info (grafana#99372)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresmgot authored Jan 22, 2025
1 parent 23f495d commit 003f0e5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/plugins/manager/fakes/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugins/repo/ifaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/plugins/repo/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
23 changes: 23 additions & 0 deletions pkg/plugins/repo/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 25 additions & 0 deletions pkg/plugins/repo/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 003f0e5

Please sign in to comment.