diff --git a/docs/resources/property_mapping_source_plex.md b/docs/resources/property_mapping_source_plex.md new file mode 100644 index 0000000..1ba816e --- /dev/null +++ b/docs/resources/property_mapping_source_plex.md @@ -0,0 +1,24 @@ +--- +page_title: "authentik_property_mapping_source_plex Resource - terraform-provider-authentik" +subcategory: "Customization" +description: |- + Manage Plex Source Property mappings +--- + +# authentik_property_mapping_source_plex (Resource) + +Manage Plex Source Property mappings + + + + +## Schema + +### Required + +- `expression` (String) +- `name` (String) + +### Read-Only + +- `id` (String) The ID of this resource. diff --git a/docs/resources/property_mapping_source_saml.md b/docs/resources/property_mapping_source_saml.md new file mode 100644 index 0000000..62a22f8 --- /dev/null +++ b/docs/resources/property_mapping_source_saml.md @@ -0,0 +1,24 @@ +--- +page_title: "authentik_property_mapping_source_saml Resource - terraform-provider-authentik" +subcategory: "Customization" +description: |- + Manage SAML Source Property mappings +--- + +# authentik_property_mapping_source_saml (Resource) + +Manage SAML Source Property mappings + + + + +## Schema + +### Required + +- `expression` (String) +- `name` (String) + +### Read-Only + +- `id` (String) The ID of this resource. diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 42cf807..70e5733 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -98,6 +98,8 @@ func Provider(version string, testing bool) *schema.Provider { "authentik_property_mapping_provider_scope": tr(resourcePropertyMappingProviderScope), "authentik_property_mapping_source_ldap": tr(resourcePropertyMappingSourceLDAP), "authentik_property_mapping_source_oauth": tr(resourcePropertyMappingSourceOAuth), + "authentik_property_mapping_source_plex": tr(resourcePropertyMappingSourcePlex), + "authentik_property_mapping_source_saml": tr(resourcePropertyMappingSourceSAML), "authentik_property_mapping_source_scim": tr(resourcePropertyMappingSourceSCIM), "authentik_provider_google_workspace": tr(resourceProviderGoogleWorkspace), "authentik_provider_ldap": tr(resourceProviderLDAP), diff --git a/internal/provider/resource_property_mapping_source_plex.go b/internal/provider/resource_property_mapping_source_plex.go new file mode 100644 index 0000000..94c336e --- /dev/null +++ b/internal/provider/resource_property_mapping_source_plex.go @@ -0,0 +1,92 @@ +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" +) + +func resourcePropertyMappingSourcePlex() *schema.Resource { + return &schema.Resource{ + Description: "Customization --- Manage Plex Source Property mappings", + CreateContext: resourcePropertyMappingSourcePlexCreate, + ReadContext: resourcePropertyMappingSourcePlexRead, + UpdateContext: resourcePropertyMappingSourcePlexUpdate, + DeleteContext: resourcePropertyMappingSourcePlexDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "expression": { + Type: schema.TypeString, + Required: true, + DiffSuppressFunc: diffSuppressExpression, + }, + }, + } +} + +func resourcePropertyMappingSourcePlexSchemaToProvider(d *schema.ResourceData) *api.PlexSourcePropertyMappingRequest { + r := api.PlexSourcePropertyMappingRequest{ + Name: d.Get("name").(string), + Expression: d.Get("expression").(string), + } + return &r +} + +func resourcePropertyMappingSourcePlexCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(*APIClient) + + r := resourcePropertyMappingSourcePlexSchemaToProvider(d) + + res, hr, err := c.client.PropertymappingsApi.PropertymappingsSourcePlexCreate(ctx).PlexSourcePropertyMappingRequest(*r).Execute() + if err != nil { + return httpToDiag(d, hr, err) + } + + d.SetId(res.Pk) + return resourcePropertyMappingSourcePlexRead(ctx, d, m) +} + +func resourcePropertyMappingSourcePlexRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + var diags diag.Diagnostics + c := m.(*APIClient) + + res, hr, err := c.client.PropertymappingsApi.PropertymappingsSourcePlexRetrieve(ctx, d.Id()).Execute() + if err != nil { + return httpToDiag(d, hr, err) + } + + setWrapper(d, "name", res.Name) + setWrapper(d, "expression", res.Expression) + return diags +} + +func resourcePropertyMappingSourcePlexUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(*APIClient) + + app := resourcePropertyMappingSourcePlexSchemaToProvider(d) + + res, hr, err := c.client.PropertymappingsApi.PropertymappingsSourcePlexUpdate(ctx, d.Id()).PlexSourcePropertyMappingRequest(*app).Execute() + if err != nil { + return httpToDiag(d, hr, err) + } + + d.SetId(res.Pk) + return resourcePropertyMappingSourcePlexRead(ctx, d, m) +} + +func resourcePropertyMappingSourcePlexDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(*APIClient) + hr, err := c.client.PropertymappingsApi.PropertymappingsSourcePlexDestroy(ctx, d.Id()).Execute() + if err != nil { + return httpToDiag(d, hr, err) + } + return diag.Diagnostics{} +} diff --git a/internal/provider/resource_property_mapping_source_plex_test.go b/internal/provider/resource_property_mapping_source_plex_test.go new file mode 100644 index 0000000..fd86026 --- /dev/null +++ b/internal/provider/resource_property_mapping_source_plex_test.go @@ -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 TestAccResourcePropertyMappingSourcePlex(t *testing.T) { + rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: testAccResourcePropertyMappingSourcePlex(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("authentik_property_mapping_source_plex.name", "name", rName), + ), + }, + { + Config: testAccResourcePropertyMappingSourcePlex(rName + "test"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("authentik_property_mapping_source_plex.name", "name", rName+"test"), + ), + }, + }, + }) +} + +func testAccResourcePropertyMappingSourcePlex(name string) string { + return fmt.Sprintf(` +resource "authentik_property_mapping_source_plex" "name" { + name = "%[1]s" + expression = "return True" +} +`, name) +} diff --git a/internal/provider/resource_property_mapping_source_saml.go b/internal/provider/resource_property_mapping_source_saml.go new file mode 100644 index 0000000..62db48b --- /dev/null +++ b/internal/provider/resource_property_mapping_source_saml.go @@ -0,0 +1,92 @@ +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" +) + +func resourcePropertyMappingSourceSAML() *schema.Resource { + return &schema.Resource{ + Description: "Customization --- Manage SAML Source Property mappings", + CreateContext: resourcePropertyMappingSourceSAMLCreate, + ReadContext: resourcePropertyMappingSourceSAMLRead, + UpdateContext: resourcePropertyMappingSourceSAMLUpdate, + DeleteContext: resourcePropertyMappingSourceSAMLDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "expression": { + Type: schema.TypeString, + Required: true, + DiffSuppressFunc: diffSuppressExpression, + }, + }, + } +} + +func resourcePropertyMappingSourceSAMLSchemaToProvider(d *schema.ResourceData) *api.SAMLSourcePropertyMappingRequest { + r := api.SAMLSourcePropertyMappingRequest{ + Name: d.Get("name").(string), + Expression: d.Get("expression").(string), + } + return &r +} + +func resourcePropertyMappingSourceSAMLCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(*APIClient) + + r := resourcePropertyMappingSourceSAMLSchemaToProvider(d) + + res, hr, err := c.client.PropertymappingsApi.PropertymappingsSourceSamlCreate(ctx).SAMLSourcePropertyMappingRequest(*r).Execute() + if err != nil { + return httpToDiag(d, hr, err) + } + + d.SetId(res.Pk) + return resourcePropertyMappingSourceSAMLRead(ctx, d, m) +} + +func resourcePropertyMappingSourceSAMLRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + var diags diag.Diagnostics + c := m.(*APIClient) + + res, hr, err := c.client.PropertymappingsApi.PropertymappingsSourceSamlRetrieve(ctx, d.Id()).Execute() + if err != nil { + return httpToDiag(d, hr, err) + } + + setWrapper(d, "name", res.Name) + setWrapper(d, "expression", res.Expression) + return diags +} + +func resourcePropertyMappingSourceSAMLUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(*APIClient) + + app := resourcePropertyMappingSourceSAMLSchemaToProvider(d) + + res, hr, err := c.client.PropertymappingsApi.PropertymappingsSourceSamlUpdate(ctx, d.Id()).SAMLSourcePropertyMappingRequest(*app).Execute() + if err != nil { + return httpToDiag(d, hr, err) + } + + d.SetId(res.Pk) + return resourcePropertyMappingSourceSAMLRead(ctx, d, m) +} + +func resourcePropertyMappingSourceSAMLDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(*APIClient) + hr, err := c.client.PropertymappingsApi.PropertymappingsSourceSamlDestroy(ctx, d.Id()).Execute() + if err != nil { + return httpToDiag(d, hr, err) + } + return diag.Diagnostics{} +} diff --git a/internal/provider/resource_property_mapping_source_saml_test.go b/internal/provider/resource_property_mapping_source_saml_test.go new file mode 100644 index 0000000..f8d03d9 --- /dev/null +++ b/internal/provider/resource_property_mapping_source_saml_test.go @@ -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 TestAccResourcePropertyMappingSourceSAML(t *testing.T) { + rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: testAccResourcePropertyMappingSourceSAML(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("authentik_property_mapping_source_saml.name", "name", rName), + ), + }, + { + Config: testAccResourcePropertyMappingSourceSAML(rName + "test"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("authentik_property_mapping_source_saml.name", "name", rName+"test"), + ), + }, + }, + }) +} + +func testAccResourcePropertyMappingSourceSAML(name string) string { + return fmt.Sprintf(` +resource "authentik_property_mapping_source_saml" "name" { + name = "%[1]s" + expression = "return True" +} +`, name) +}