Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/data-sources/property_mapping_source_telegram.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
page_title: "authentik_property_mapping_source_telegram Data Source - terraform-provider-authentik"
subcategory: "Customization"
description: |-
Get Telegram Source Property mappings
---

# authentik_property_mapping_source_telegram (Data Source)

Get Telegram Source Property mappings

## Example Usage

```terraform
# To get the ID of a Telegram Source Property mapping

data "authentik_property_mapping_source_telegram" "test" {
name = "custom-field"
}

# Then use `data.authentik_property_mapping_source_telegram.test.id`
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `ids` (List of String) List of ids when `managed_list` is set. Generated.
- `managed` (String)
- `managed_list` (List of String) Retrieve multiple property mappings
- `name` (String)

### Read-Only

- `expression` (String) Generated.
- `id` (String) The ID of this resource.
33 changes: 33 additions & 0 deletions docs/resources/property_mapping_source_telegram.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
page_title: "authentik_property_mapping_source_telegram Resource - terraform-provider-authentik"
subcategory: "Customization"
description: |-
Manage Telegram Source Property mappings
---

# authentik_property_mapping_source_telegram (Resource)

Manage Telegram Source Property mappings

## Example Usage

```terraform
# Create a custom Telegram source property mapping

resource "authentik_property_mapping_source_telegram" "name" {
name = "custom-field"
expression = "return {\"username\": data}"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `expression` (String)
- `name` (String)

### Read-Only

- `id` (String) The ID of this resource.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# To get the ID of a Telegram Source Property mapping

data "authentik_property_mapping_source_telegram" "test" {
name = "custom-field"
}

# Then use `data.authentik_property_mapping_source_telegram.test.id`
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Create a custom Telegram source property mapping

resource "authentik_property_mapping_source_telegram" "name" {
name = "custom-field"
expression = "return {\"username\": data}"
}
91 changes: 91 additions & 0 deletions pkg/provider/data_source_property_mapping_source_telegram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"goauthentik.io/terraform-provider-authentik/pkg/helpers"
)

func dataSourcePropertyMappingSourceTelegram() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePropertyMappingSourceTelegramRead,
Description: "Customization --- Get Telegram Source Property mappings",
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"managed_list"},
},
"managed": {
Type: schema.TypeString,
Optional: true,
},

"managed_list": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Retrieve multiple property mappings",
},

"ids": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of ids when `managed_list` is set.",
},

"expression": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourcePropertyMappingSourceTelegramRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*APIClient)

req := c.client.PropertymappingsAPI.PropertymappingsSourceTelegramList(ctx)

if _, ok := d.GetOk("managed_list"); ok {
req = req.Managed(helpers.CastSlice[string](d, "managed_list"))
} else if m, ok := d.GetOk("managed"); ok {
req = req.Managed([]string{m.(string)})
}

if n, ok := d.GetOk("name"); ok {
req = req.Name(n.(string))
}

res, hr, err := req.Execute()
if err != nil {
return helpers.HTTPToDiag(d, hr, err)
}

if len(res.Results) < 1 {
return diag.Errorf("No matching mappings found")
}
if _, ok := d.GetOk("managed_list"); ok {
d.SetId("-1")
ids := make([]string, len(res.Results))
for i, r := range res.Results {
ids[i] = r.Pk
}
helpers.SetWrapper(d, "ids", ids)
} else {
f := res.Results[0]
d.SetId(f.Pk)
helpers.SetWrapper(d, "name", f.Name)
helpers.SetWrapper(d, "expression", f.Expression)
}
return diags
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourcePropertyMappingSourceTelegram(t *testing.T) {
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePropertyMappingSourceTelegram(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.authentik_property_mapping_source_telegram.test", "name", rName),
resource.TestCheckResourceAttr("data.authentik_property_mapping_source_telegram.test", "expression", "return True"),
),
},
},
})
}

func testAccDataSourcePropertyMappingSourceTelegram(name string) string {
return fmt.Sprintf(`
resource "authentik_property_mapping_source_telegram" "test" {
name = "%[1]s"
expression = "return True"
}

data "authentik_property_mapping_source_telegram" "test" {
name = "%[1]s"
}
`, name)
}
2 changes: 2 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func Provider(version string, testing bool) *schema.Provider {
"authentik_property_mapping_source_saml": tr(resourcePropertyMappingSourceSAML),
"authentik_property_mapping_source_scim": tr(resourcePropertyMappingSourceSCIM),
"authentik_property_mapping_source_kerberos": tr(resourcePropertyMappingSourceKerberos),
"authentik_property_mapping_source_telegram": tr(resourcePropertyMappingSourceTelegram),
"authentik_provider_google_workspace": tr(resourceProviderGoogleWorkspace),
"authentik_provider_ldap": tr(resourceProviderLDAP),
"authentik_provider_microsoft_entra": tr(resourceProviderMicrosoftEntra),
Expand Down Expand Up @@ -192,6 +193,7 @@ func Provider(version string, testing bool) *schema.Provider {
"authentik_property_mapping_provider_scim": td(dataSourcePropertyMappingProviderSCIM),
"authentik_property_mapping_provider_scope": td(dataSourcePropertyMappingProviderScope),
"authentik_property_mapping_source_ldap": td(dataSourcePropertyMappingSourceLDAP),
"authentik_property_mapping_source_telegram": td(dataSourcePropertyMappingSourceTelegram),
"authentik_provider_oauth2_config": td(dataSourceProviderOAuth2Config),
"authentik_provider_saml_metadata": td(dataSourceProviderSAMLMetadata),
"authentik_rbac_permission": td(dataSourceRBACPermission),
Expand Down
93 changes: 93 additions & 0 deletions pkg/provider/resource_property_mapping_source_telegram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
api "goauthentik.io/api/v3"
"goauthentik.io/terraform-provider-authentik/pkg/helpers"
)

func resourcePropertyMappingSourceTelegram() *schema.Resource {
return &schema.Resource{
Description: "Customization --- Manage Telegram Source Property mappings",
CreateContext: resourcePropertyMappingSourceTelegramCreate,
ReadContext: resourcePropertyMappingSourceTelegramRead,
UpdateContext: resourcePropertyMappingSourceTelegramUpdate,
DeleteContext: resourcePropertyMappingSourceTelegramDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"expression": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: helpers.DiffSuppressExpression,
},
},
}
}

func resourcePropertyMappingSourceTelegramSchemaToProvider(d *schema.ResourceData) *api.TelegramSourcePropertyMappingRequest {
r := api.TelegramSourcePropertyMappingRequest{
Name: d.Get("name").(string),
Expression: d.Get("expression").(string),
}
return &r
}

func resourcePropertyMappingSourceTelegramCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
c := m.(*APIClient)

r := resourcePropertyMappingSourceTelegramSchemaToProvider(d)

res, hr, err := c.client.PropertymappingsAPI.PropertymappingsSourceTelegramCreate(ctx).TelegramSourcePropertyMappingRequest(*r).Execute()
if err != nil {
return helpers.HTTPToDiag(d, hr, err)
}

d.SetId(res.Pk)
return resourcePropertyMappingSourceTelegramRead(ctx, d, m)
}

func resourcePropertyMappingSourceTelegramRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*APIClient)

res, hr, err := c.client.PropertymappingsAPI.PropertymappingsSourceTelegramRetrieve(ctx, d.Id()).Execute()
if err != nil {
return helpers.HTTPToDiag(d, hr, err)
}

helpers.SetWrapper(d, "name", res.Name)
helpers.SetWrapper(d, "expression", res.Expression)
return diags
}

func resourcePropertyMappingSourceTelegramUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
c := m.(*APIClient)

app := resourcePropertyMappingSourceTelegramSchemaToProvider(d)

res, hr, err := c.client.PropertymappingsAPI.PropertymappingsSourceTelegramUpdate(ctx, d.Id()).TelegramSourcePropertyMappingRequest(*app).Execute()
if err != nil {
return helpers.HTTPToDiag(d, hr, err)
}

d.SetId(res.Pk)
return resourcePropertyMappingSourceTelegramRead(ctx, d, m)
}

func resourcePropertyMappingSourceTelegramDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
c := m.(*APIClient)
hr, err := c.client.PropertymappingsAPI.PropertymappingsSourceTelegramDestroy(ctx, d.Id()).Execute()
if err != nil {
return helpers.HTTPToDiag(d, hr, err)
}
return diag.Diagnostics{}
}
40 changes: 40 additions & 0 deletions pkg/provider/resource_property_mapping_source_telegram_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccResourcePropertyMappingSourceTelegram(t *testing.T) {
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccResourcePropertyMappingSourceTelegram(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("authentik_property_mapping_source_telegram.name", "name", rName),
),
},
{
Config: testAccResourcePropertyMappingSourceTelegram(rName + "test"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("authentik_property_mapping_source_telegram.name", "name", rName+"test"),
),
},
},
})
}

func testAccResourcePropertyMappingSourceTelegram(name string) string {
return fmt.Sprintf(`
resource "authentik_property_mapping_source_telegram" "name" {
name = "%[1]s"
expression = "return True"
}
`, name)
}