Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data source policy #546

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
33 changes: 33 additions & 0 deletions docs/data-sources/policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
page_title: "authentik_policy Data Source - terraform-provider-authentik"
subcategory: "Customization"
description: |-
Get policy by name
---

# authentik_policy (Data Source)

Get policy by name

## Example Usage

```terraform
# To get the ID of a policy by name

data "authentik_policy" "default-authentication-flow-password-stage" {
name = "default-authentication-flow-password-stage"
}

# Then use `data.authentik_policy.default-authentication-flow-password-stage.id`
```

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

### Optional

- `name` (String) Generated.

### Read-Only

- `id` (String) The ID of this resource.
7 changes: 7 additions & 0 deletions examples/data-sources/authentik_policy/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# To get the ID of a policy by name

data "authentik_policy" "default-authentication-flow-password-stage" {
name = "default-authentication-flow-password-stage"
}

# Then use `data.authentik_policy.default-authentication-flow-password-stage.id`
45 changes: 45 additions & 0 deletions internal/provider/data_source_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourcePolicy() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePolicyRead,
Description: "Customization --- Get policy by name",
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}

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

req := c.client.PoliciesApi.PoliciesAllList(ctx)
if s, ok := d.GetOk("name"); ok {
req = req.Search(s.(string))
}

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

Check warning on line 35 in internal/provider/data_source_policy.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/data_source_policy.go#L35

Added line #L35 was not covered by tests
}

if len(res.Results) < 1 {
return diag.Errorf("No matching policy found")
}
f := res.Results[0]
d.SetId(f.Pk)
setWrapper(d, "name", f.Name)
return diags
}
39 changes: 39 additions & 0 deletions internal/provider/data_source_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package provider

import (
"regexp"
"testing"

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

func TestAccDataSourcePolicy(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePolicySimple,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.authentik_policy.default-authentication-flow-password-stage", "name", "default-authentication-flow-password-stage"),
),
},
{
Config: testAccDataSourcePolicyNotExisting,
ExpectError: regexp.MustCompile(`No matching policy found`),
},
},
})
}

const testAccDataSourcePolicySimple = `
data "authentik_policy" "default-authentication-flow-password-stage" {
name = "default-authentication-flow-password-stage"
}
`

const testAccDataSourcePolicyNotExisting = `
data "authentik_policy" "not-exiting-policy" {
name = "not-exiting-policy"
}
`
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func Provider(version string, testing bool) *schema.Provider {
"authentik_flow": td(dataSourceFlow),
"authentik_group": td(dataSourceGroup),
"authentik_groups": td(dataSourceGroups),
"authentik_policy": td(dataSourcePolicy),
"authentik_property_mapping_provider_rac": td(dataSourcePropertyMappingProviderRAC),
"authentik_property_mapping_provider_radius": td(dataSourcePropertyMappingProviderRadius),
"authentik_property_mapping_provider_saml": td(dataSourcePropertyMappingProviderSAML),
Expand Down
Loading