diff --git a/terraform-provider-jans/CHANGELOG.md b/terraform-provider-jans/CHANGELOG.md index cebb159a58d..5b039a6da66 100644 --- a/terraform-provider-jans/CHANGELOG.md +++ b/terraform-provider-jans/CHANGELOG.md @@ -1,13 +1,20 @@ # Changelog -## [0.3.0](https://github.com/techtative/terraform-provider-jans/compare/v0.2.0...v0.3.0) (2023-03-15) +## 0.4.0 (2023-03-16) + + +### Features + +* added data source for plugins + +## 0.3.0 (2023-03-15) ### Features * added new resource for manaing api app config -## [0.2.0](https://github.com/techtative/terraform-provider-jans/compare/v0.1.0...v0.2.0) (2023-02-16) +## 0.2.0 (2023-02-16) ### Features diff --git a/terraform-provider-jans/docs/data-sources/plugins.md b/terraform-provider-jans/docs/data-sources/plugins.md new file mode 100644 index 00000000000..38c17e53fa9 --- /dev/null +++ b/terraform-provider-jans/docs/data-sources/plugins.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "jans_plugins Data Source - terraform-provider-jans" +subcategory: "" +description: |- + Data source for retrieving the plugins that are configured in the Janssen server +--- + +# jans_plugins (Data Source) + +Data source for retrieving the plugins that are configured in the Janssen server + + + + +## Schema + +### Read-Only + +- `enabled` (List of Object) List of all enabled plugins (see [below for nested schema](#nestedatt--enabled)) +- `id` (String) The ID of this resource. + + +### Nested Schema for `enabled` + +Read-Only: + +- `class_name` (String) +- `description` (String) +- `name` (String) + + diff --git a/terraform-provider-jans/jans/custom_user_test.go b/terraform-provider-jans/jans/custom_user_test.go index 2e0a24c69d1..7d8ca866e5b 100644 --- a/terraform-provider-jans/jans/custom_user_test.go +++ b/terraform-provider-jans/jans/custom_user_test.go @@ -74,10 +74,6 @@ func TestCustomUsers(t *testing.T) { DisplayValue: "exampleUsr1", }, }, - CustomObjectClasses: []string{ - "top", - "jansCustomPerson", - }, Mail: "exampleUsr1@jans.io", OxAuthPersistentJwt: []string{"jwt1", "jwt2"}, DisplayName: "Default Test User", diff --git a/terraform-provider-jans/jans/plugins.go b/terraform-provider-jans/jans/plugins.go new file mode 100644 index 00000000000..8fc32ea81b1 --- /dev/null +++ b/terraform-provider-jans/jans/plugins.go @@ -0,0 +1,35 @@ +package jans + +import ( + "context" + "fmt" +) + +// PluginConf represents a single plugin enabled on the server. +type PluginConf struct { + Name string `schema:"name" json:"name,omitempty"` + Description string `schema:"description" json:"description,omitempty"` + ClassName string `schema:"class_name" json:"className,omitempty"` +} + +// Plugins holds the list of all plugins currently enabled on the server. +type Plugins struct { + Enabled []PluginConf `schema:"enabled"` +} + +// GetPlugins returns the list of plugins currently enabled on the server. +func (c *Client) GetPlugins(ctx context.Context) ([]PluginConf, error) { + + token, err := c.getToken(ctx, "https://jans.io/oauth/config/plugin.readonly") + if err != nil { + return nil, fmt.Errorf("failed to get token: %w", err) + } + + ret := []PluginConf{} + + if err := c.get(ctx, "/jans-config-api/api/v1/plugin", token, &ret); err != nil { + return nil, fmt.Errorf("get request failed: %w", err) + } + + return ret, nil +} diff --git a/terraform-provider-jans/jans/plugins_test.go b/terraform-provider-jans/jans/plugins_test.go new file mode 100644 index 00000000000..e60efaa5b59 --- /dev/null +++ b/terraform-provider-jans/jans/plugins_test.go @@ -0,0 +1,25 @@ +package jans + +import ( + "context" + "testing" +) + +func TestPlugins(t *testing.T) { + + client, err := NewInsecureClient(host, user, pass) + if err != nil { + t.Fatal(err) + } + + ctx := context.Background() + + plugins, err := client.GetPlugins(ctx) + if err != nil { + t.Fatal(err) + } + + if len(plugins) != 3 { + t.Fatal("expected 3 plugins, got ", len(plugins)) + } +} diff --git a/terraform-provider-jans/provider/data_source_plugins.go b/terraform-provider-jans/provider/data_source_plugins.go new file mode 100644 index 00000000000..98b9b758377 --- /dev/null +++ b/terraform-provider-jans/provider/data_source_plugins.go @@ -0,0 +1,66 @@ +package provider + +import ( + "context" + "strconv" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/jans/terraform-provider-jans/jans" +) + +func dataSourcePlugins() *schema.Resource { + + return &schema.Resource{ + Description: "Data source for retrieving the plugins that are configured in the Janssen server", + ReadContext: dataSourcePluginsRead, + Schema: map[string]*schema.Schema{ + "enabled": { + Type: schema.TypeList, + Computed: true, + Description: "List of all enabled plugins", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Description: "Name of the plugin", + Computed: true, + }, + "description": { + Type: schema.TypeString, + Description: "Description of the plugin", + Computed: true, + }, + "class_name": { + Type: schema.TypeString, + Description: "Class name of the plugin", + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourcePluginsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + c := meta.(*jans.Client) + + pluginConfig, err := c.GetPlugins(ctx) + if err != nil { + return diag.FromErr(err) + } + + plugins := jans.Plugins{ + Enabled: pluginConfig, + } + + if err := toSchemaResource(d, plugins); err != nil { + return diag.FromErr(err) + } + + d.SetId(strconv.FormatInt(time.Now().Unix(), 10)) + + return nil +} diff --git a/terraform-provider-jans/provider/data_source_plugins_test.go b/terraform-provider-jans/provider/data_source_plugins_test.go new file mode 100644 index 00000000000..050a53b0c85 --- /dev/null +++ b/terraform-provider-jans/provider/data_source_plugins_test.go @@ -0,0 +1,62 @@ +package provider + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/jans/terraform-provider-jans/jans" +) + +func TestDatasourcePlugins_Mapping(t *testing.T) { + + schema := dataSourcePlugins() + + data := schema.Data(nil) + + plugins := jans.Plugins{ + Enabled: []jans.PluginConf{ + { + Name: "fidoe2", + }, + }, + } + + if err := toSchemaResource(data, &plugins); err != nil { + t.Fatal(err) + } + + newPlugins := jans.Plugins{} + + if err := fromSchemaResource(data, &newPlugins); err != nil { + t.Fatal(err) + } + + if diff := cmp.Diff(plugins, newPlugins); diff != "" { + t.Errorf("Got different plugins after mapping: %s", diff) + } + +} + +func TestAccDataSourcePlugins_basic(t *testing.T) { + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourcePlugins_basic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.jans_plugins.all", "enabled.0.name", "fido2"), + ), + }, + }, + }) +} + +func testAccDataSourcePlugins_basic() string { + return ` +data "jans_plugins" "all" { +} +` +} diff --git a/terraform-provider-jans/provider/provider.go b/terraform-provider-jans/provider/provider.go index 1b13b08389e..97c8f7f8f27 100644 --- a/terraform-provider-jans/provider/provider.go +++ b/terraform-provider-jans/provider/provider.go @@ -95,6 +95,7 @@ func Provider() *schema.Provider { DataSourcesMap: map[string]*schema.Resource{ "jans_fido2_configuration": dataSourceFido2Configuration(), "jans_persistence_config": dataSourcePersistenceConfiguration(), + "jans_plugins": dataSourcePlugins(), "jans_schema": dataSourceSchema(), "jans_service_provider_config": dataSourceServiceProviderConfig(), },