Skip to content

Commit

Permalink
add app entitlement
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed Dec 19, 2024
1 parent 57edeee commit f947e78
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/resources/application_entitlement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
page_title: "authentik_application_entitlement Resource - terraform-provider-authentik"
subcategory: "Applications"
description: |-
---

# authentik_application_entitlement (Resource)



## Example Usage

```terraform
# Create an application entitlement bound to a group
resource "authentik_application" "name" {
name = "example-app"
slug = "example-app"
}
resource "authentik_application_entitlement" "ent" {
name = "test-ent"
application = authentik_application.name.id
}
resource "authentik_group" "group" {
name = "test-ent-group"
}
resource "authentik_policy_binding" "test-ent-access" {
target = authentik_application.name.uuid
group = authentik_group.group.id
order = 0
}
```

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

### Required

- `application` (String)
- `name` (String)

### Optional

- `attributes` (String) JSON format expected. Use jsonencode() to pass objects. Defaults to `{}`.

### Read-Only

- `id` (String) The ID of this resource.
1 change: 1 addition & 0 deletions docs/resources/group.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ resource "authentik_user" "name" {
username = "user"
name = "User"
}
resource "authentik_group" "group" {
name = "tf_admins"
users = [authentik_user.name.id]
Expand Down
20 changes: 20 additions & 0 deletions examples/resources/authentik_application_entitlement/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Create an application entitlement bound to a group
resource "authentik_application" "name" {
name = "example-app"
slug = "example-app"
}

resource "authentik_application_entitlement" "ent" {
name = "test-ent"
application = authentik_application.name.id
}

resource "authentik_group" "group" {
name = "test-ent-group"
}

resource "authentik_policy_binding" "test-ent-access" {
target = authentik_application.name.uuid
group = authentik_group.group.id
order = 0
}
1 change: 1 addition & 0 deletions examples/resources/authentik_group/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ resource "authentik_user" "name" {
username = "user"
name = "User"
}

resource "authentik_group" "group" {
name = "tf_admins"
users = [authentik_user.name.id]
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func Provider(version string, testing bool) *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"authentik_application_entitlement": tr(resourceApplicationEntitlement),
"authentik_application": tr(resourceApplication),
"authentik_blueprint": tr(resourceBlueprintInstance),
"authentik_brand": tr(resourceBrand),
Expand Down
121 changes: 121 additions & 0 deletions internal/provider/resource_application_entitlement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package provider

import (
"context"
"encoding/json"
"strings"

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

func resourceApplicationEntitlement() *schema.Resource {
return &schema.Resource{
Description: "Applications --- ",
CreateContext: resourceApplicationEntitlementCreate,
ReadContext: resourceApplicationEntitlementRead,
UpdateContext: resourceApplicationEntitlementUpdate,
DeleteContext: resourceApplicationEntitlementDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"application": {
Type: schema.TypeString,
Required: true,
},
"attributes": {
Type: schema.TypeString,
Optional: true,
Default: "{}",
Description: "JSON format expected. Use jsonencode() to pass objects.",
DiffSuppressFunc: diffSuppressJSON,
},
},
}
}

func resourceApplicationEntitlementSchemaToModel(d *schema.ResourceData) (*api.ApplicationEntitlementRequest, diag.Diagnostics) {
m := api.ApplicationEntitlementRequest{
Name: d.Get("name").(string),
App: d.Get("application").(string),
}

attr := make(map[string]interface{})
if l, ok := d.Get("attributes").(string); ok && l != "" {
err := json.NewDecoder(strings.NewReader(l)).Decode(&attr)
if err != nil {
return nil, diag.FromErr(err)
}

Check warning on line 54 in internal/provider/resource_application_entitlement.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/resource_application_entitlement.go#L53-L54

Added lines #L53 - L54 were not covered by tests
}
m.Attributes = attr
return &m, nil
}

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

app, diags := resourceApplicationEntitlementSchemaToModel(d)
if diags != nil {
return diags
}

Check warning on line 66 in internal/provider/resource_application_entitlement.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/resource_application_entitlement.go#L65-L66

Added lines #L65 - L66 were not covered by tests

res, hr, err := c.client.CoreApi.CoreApplicationEntitlementsCreate(ctx).ApplicationEntitlementRequest(*app).Execute()
if err != nil {
return httpToDiag(d, hr, err)
}

d.SetId(res.PbmUuid)
return resourceApplicationEntitlementRead(ctx, d, m)

Check warning on line 74 in internal/provider/resource_application_entitlement.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/resource_application_entitlement.go#L73-L74

Added lines #L73 - L74 were not covered by tests
}

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

res, hr, err := c.client.CoreApi.CoreApplicationEntitlementsRetrieve(ctx, d.Id()).Execute()
if err != nil {
return httpToDiag(d, hr, err)
}

Check warning on line 84 in internal/provider/resource_application_entitlement.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/resource_application_entitlement.go#L77-L84

Added lines #L77 - L84 were not covered by tests

d.SetId(res.PbmUuid)
setWrapper(d, "name", res.Name)
setWrapper(d, "application", res.App)
b, err := json.Marshal(res.Attributes)
if err != nil {
return diag.FromErr(err)
}
setWrapper(d, "attributes", string(b))
return diags

Check warning on line 94 in internal/provider/resource_application_entitlement.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/resource_application_entitlement.go#L86-L94

Added lines #L86 - L94 were not covered by tests
}

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

app, diags := resourceApplicationEntitlementSchemaToModel(d)
if diags != nil {
return diags
}

Check warning on line 103 in internal/provider/resource_application_entitlement.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/resource_application_entitlement.go#L97-L103

Added lines #L97 - L103 were not covered by tests

res, hr, err := c.client.CoreApi.CoreApplicationEntitlementsUpdate(ctx, d.Id()).ApplicationEntitlementRequest(*app).Execute()
if err != nil {
return httpToDiag(d, hr, err)
}

Check warning on line 108 in internal/provider/resource_application_entitlement.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/resource_application_entitlement.go#L105-L108

Added lines #L105 - L108 were not covered by tests

d.SetId(res.PbmUuid)
return resourceApplicationEntitlementRead(ctx, d, m)

Check warning on line 111 in internal/provider/resource_application_entitlement.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/resource_application_entitlement.go#L110-L111

Added lines #L110 - L111 were not covered by tests
}

func resourceApplicationEntitlementDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*APIClient)
hr, err := c.client.CoreApi.CoreApplicationEntitlementsDestroy(ctx, d.Id()).Execute()
if err != nil {
return httpToDiag(d, hr, err)
}
return diag.Diagnostics{}

Check warning on line 120 in internal/provider/resource_application_entitlement.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/resource_application_entitlement.go#L114-L120

Added lines #L114 - L120 were not covered by tests
}
62 changes: 62 additions & 0 deletions internal/provider/resource_application_entitlement_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package provider

import (
"fmt"
"testing"

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

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

func testAccResourceApplicationEntitlementSimple(name string) string {
return fmt.Sprintf(`
data "authentik_flow" "default-authentication-flow" {
slug = "default-authentication-flow"
}
data "authentik_flow" "default-provider-invalidation-flow" {
slug = "default-provider-invalidation-flow"
}
data "authentik_certificate_key_pair" "generated" {
name = "authentik Self-signed Certificate"
}
resource "authentik_application" "name" {
name = "%[1]s"
slug = "%[1]s"
meta_icon = "http://localhost/%[1]s"
}
resource "authentik_application_entitlement" "ent" {
name = "%[1]s"
application = authentik_application.name.id
}
`, name)
}

0 comments on commit f947e78

Please sign in to comment.