From 43b5098a055dd94a97852ae873bd0eb7dc8eaaaf Mon Sep 17 00:00:00 2001 From: Mark Ellis Date: Thu, 7 Dec 2023 17:59:32 +0100 Subject: [PATCH] added data_tailscale_acl data source (#304) fixes #180 --- docs/data-sources/acl.md | 21 +++++++++++++++++ tailscale/data_source_acl.go | 45 ++++++++++++++++++++++++++++++++++++ tailscale/provider.go | 1 + 3 files changed, 67 insertions(+) create mode 100644 docs/data-sources/acl.md create mode 100644 tailscale/data_source_acl.go diff --git a/docs/data-sources/acl.md b/docs/data-sources/acl.md new file mode 100644 index 00000000..2c4dcbd5 --- /dev/null +++ b/docs/data-sources/acl.md @@ -0,0 +1,21 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "tailscale_acl Data Source - terraform-provider-tailscale" +subcategory: "" +description: |- + The acl data source gets the Tailscale ACL for a tailnet +--- + +# tailscale_acl (Data Source) + +The acl data source gets the Tailscale ACL for a tailnet + + + + +## Schema + +### Read-Only + +- `id` (String) The ID of this resource. +- `json` (String) The contents of Tailscale ACL as JSON diff --git a/tailscale/data_source_acl.go b/tailscale/data_source_acl.go new file mode 100644 index 00000000..923fde78 --- /dev/null +++ b/tailscale/data_source_acl.go @@ -0,0 +1,45 @@ +package tailscale + +import ( + "context" + "encoding/json" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/tailscale/tailscale-client-go/tailscale" +) + +func dataSourceACL() *schema.Resource { + return &schema.Resource{ + Description: "The acl data source gets the Tailscale ACL for a tailnet", + ReadContext: dataSourceACLRead, + Schema: map[string]*schema.Schema{ + "json": { + Computed: true, + Type: schema.TypeString, + Description: "The contents of Tailscale ACL as JSON", + }, + }, + } +} + +func dataSourceACLRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*tailscale.Client) + + acl, err := client.ACL(ctx) + if err != nil { + return diagnosticsError(err, "Failed to fetch ACL") + } + + aclJson, err := json.Marshal(acl) + if err != nil { + return diag.FromErr(err) + } + if err := d.Set("json", string(aclJson)); err != nil { + return diag.Errorf("setting json: %s", err) + } + + d.SetId(createUUID()) + return nil +} diff --git a/tailscale/provider.go b/tailscale/provider.go index 42f7845f..2754b72b 100644 --- a/tailscale/provider.go +++ b/tailscale/provider.go @@ -80,6 +80,7 @@ func Provider(options ...ProviderOption) *schema.Provider { "tailscale_device": dataSourceDevice(), "tailscale_devices": dataSourceDevices(), "tailscale_4via6": dataSource4Via6(), + "tailscale_acl": dataSourceACL(), }, }