From aa17c50b2781359c2210e6b3972c75be7f363e14 Mon Sep 17 00:00:00 2001 From: Mike Wilson Date: Thu, 15 Jun 2023 15:07:28 -0400 Subject: [PATCH] Add HasPluginType to plugins interface. The plugins interface now supports HasPluginType, which returns true if a plugin of the given type has been detected within the backend. --- lib/services/local/plugins.go | 16 ++++++++++++++++ lib/services/local/plugins_test.go | 9 +++++++++ lib/services/plugins.go | 1 + 3 files changed, 26 insertions(+) diff --git a/lib/services/local/plugins.go b/lib/services/local/plugins.go index 20a166c090f9f..df44362d81f35 100644 --- a/lib/services/local/plugins.go +++ b/lib/services/local/plugins.go @@ -158,6 +158,22 @@ func (s *PluginsService) ListPlugins(ctx context.Context, limit int, startKey st return plugins, nextKey, nil } +// HasPluginType will return true if a plugin of the given type is registered. +func (s *PluginsService) HasPluginType(ctx context.Context, pluginType types.PluginType) (bool, error) { + plugins, err := s.GetPlugins(ctx, false) + if err != nil { + return false, trace.Wrap(err) + } + + for _, plugin := range plugins { + if plugin.GetType() == pluginType { + return true, nil + } + } + + return false, nil +} + // SetPluginCredentials implements services.Plugins func (s *PluginsService) SetPluginCredentials(ctx context.Context, name string, creds types.PluginCredentials) error { return s.updateAndSwap(ctx, name, func(p types.Plugin) error { diff --git a/lib/services/local/plugins_test.go b/lib/services/local/plugins_test.go index 09962a848fcb1..88d7ffc321d1c 100644 --- a/lib/services/local/plugins_test.go +++ b/lib/services/local/plugins_test.go @@ -109,6 +109,15 @@ func TestPluginsCRUD(t *testing.T) { )) require.Empty(t, cmp.Diff(status, cluster.GetStatus())) + // Test if plugin types exist. + exists, err := service.HasPluginType(ctx, types.PluginTypeOkta) + require.NoError(t, err) + require.False(t, exists) + + exists, err = service.HasPluginType(ctx, types.PluginTypeSlack) + require.NoError(t, err) + require.True(t, exists) + // Delete a plugin. err = service.DeletePlugin(ctx, plugin1.GetName()) require.NoError(t, err) diff --git a/lib/services/plugins.go b/lib/services/plugins.go index 21c227180fd66..96a8febe89f59 100644 --- a/lib/services/plugins.go +++ b/lib/services/plugins.go @@ -35,6 +35,7 @@ type Plugins interface { GetPlugin(ctx context.Context, name string, withSecrets bool) (types.Plugin, error) GetPlugins(ctx context.Context, withSecrets bool) ([]types.Plugin, error) ListPlugins(ctx context.Context, limit int, startKey string, withSecrets bool) ([]types.Plugin, string, error) + HasPluginType(ctx context.Context, pluginType types.PluginType) (bool, error) SetPluginCredentials(ctx context.Context, name string, creds types.PluginCredentials) error SetPluginStatus(ctx context.Context, name string, creds types.PluginStatus) error }