Skip to content

Commit

Permalink
fix(terraform-provider-jans): update terraform module (#4164)
Browse files Browse the repository at this point in the history
* fix(terraform-provider-jans): update terraform provider

Signed-off-by: moabu <47318409+moabu@users.noreply.github.com>

* docs: update CHANGELOG.md

---------

Signed-off-by: moabu <47318409+moabu@users.noreply.github.com>
  • Loading branch information
moabu authored Mar 16, 2023
1 parent f06825a commit 073ef39
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 6 deletions.
11 changes: 9 additions & 2 deletions terraform-provider-jans/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 32 additions & 0 deletions terraform-provider-jans/docs/data-sources/plugins.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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.

<a id="nestedatt--enabled"></a>
### Nested Schema for `enabled`

Read-Only:

- `class_name` (String)
- `description` (String)
- `name` (String)


4 changes: 0 additions & 4 deletions terraform-provider-jans/jans/custom_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions terraform-provider-jans/jans/plugins.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions terraform-provider-jans/jans/plugins_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
66 changes: 66 additions & 0 deletions terraform-provider-jans/provider/data_source_plugins.go
Original file line number Diff line number Diff line change
@@ -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
}
62 changes: 62 additions & 0 deletions terraform-provider-jans/provider/data_source_plugins_test.go
Original file line number Diff line number Diff line change
@@ -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" {
}
`
}
1 change: 1 addition & 0 deletions terraform-provider-jans/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down

0 comments on commit 073ef39

Please sign in to comment.