From f182f0c93ab0ada397db2305eacd868e4a9b837a Mon Sep 17 00:00:00 2001 From: Michael Dokolin Date: Sun, 4 Feb 2024 08:49:12 +0100 Subject: [PATCH] feat: Add `routeros_zerotier` resource to manage ZeroTier instances --- .../resources/routeros_zerotier/import.sh | 3 + .../resources/routeros_zerotier/resource.tf | 8 ++ routeros/provider.go | 3 + routeros/resource_zerotier.go | 89 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 examples/resources/routeros_zerotier/import.sh create mode 100644 examples/resources/routeros_zerotier/resource.tf create mode 100644 routeros/resource_zerotier.go diff --git a/examples/resources/routeros_zerotier/import.sh b/examples/resources/routeros_zerotier/import.sh new file mode 100644 index 00000000..9ea7ccd3 --- /dev/null +++ b/examples/resources/routeros_zerotier/import.sh @@ -0,0 +1,3 @@ +#The ID can be found via API or the terminal +#The command for the terminal is -> :put [/zerotier get [print show-ids]] +terraform import routeros_zerotier.zt1 '*1' diff --git a/examples/resources/routeros_zerotier/resource.tf b/examples/resources/routeros_zerotier/resource.tf new file mode 100644 index 00000000..8fdcf11f --- /dev/null +++ b/examples/resources/routeros_zerotier/resource.tf @@ -0,0 +1,8 @@ +resource "zerotier_identity" "identity" {} + +resource "routeros_zerotier" "zt1" { + comment = "ZeroTier Central" + identity = zerotier_identity.identity.private_key + interfaces = ["all"] + name = "zt1" +} diff --git a/routeros/provider.go b/routeros/provider.go index 151507ee..5bcc826c 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -257,6 +257,9 @@ func Provider() *schema.Provider { "routeros_wifi_provisioning": ResourceWifiProvisioning(), "routeros_wifi_security": ResourceWifiSecurity(), "routeros_wifi_steering": ResourceWifiSteering(), + + // ZeroTier + "routeros_zerotier": ResourceZerotier(), }, DataSourcesMap: map[string]*schema.Resource{ "routeros_firewall": DatasourceFirewall(), diff --git a/routeros/resource_zerotier.go b/routeros/resource_zerotier.go new file mode 100644 index 00000000..4fba527a --- /dev/null +++ b/routeros/resource_zerotier.go @@ -0,0 +1,89 @@ +package routeros + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +/* +{ + ".id": "*1", + "comment": "ZeroTier Central controller - https://my.zerotier.com/", + "disabled": "false", + "identity": "...", + "identity.public": "...", + "interfaces": "all", + "name": "zt1", + "online": "true", + "port": "9993", + "route-distance": "1", + "state": "running" +} +*/ + +// https://help.mikrotik.com/docs/display/ROS/ZeroTier#ZeroTier-Parameters +func ResourceZerotier() *schema.Resource { + resSchema := map[string]*schema.Schema{ + MetaResourcePath: PropResourcePath("/zerotier"), + MetaId: PropId(Id), + MetaTransformSet: PropTransformSet(`"identity.public": "identity_public"`), + + KeyComment: PropCommentRw, + KeyDisabled: PropDisabledRw, + "identity": { + Type: schema.TypeString, + Optional: true, + Description: "The 40-bit unique instance address.", + DiffSuppressFunc: AlwaysPresentNotUserProvided, + }, + "identity_public": { + Type: schema.TypeString, + Computed: true, + Description: "The public identity of the ZeroTier instance.", + }, + "interfaces": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Description: "The interfaces to discover ZeroTier peers by ARP and IP type connections.", + DiffSuppressFunc: AlwaysPresentNotUserProvided, + }, + KeyName: PropName("Name of the ZeroTier instance."), + "online": { + Type: schema.TypeBool, + Computed: true, + Description: "A flag whether the ZeroTier instance is currently online.", + }, + "port": { + Type: schema.TypeInt, + Optional: true, + Default: 9993, + Description: "The port number the instance listens to.", + ValidateFunc: validation.IntBetween(1, 65535), + }, + "route_distance": { + Type: schema.TypeInt, + Optional: true, + Default: 1, + Description: "The route distance for routes obtained from the planet/moon server.", + }, + "state": { + Type: schema.TypeString, + Computed: true, + Description: "The state of the ZeroTier instance.", + }, + } + + return &schema.Resource{ + CreateContext: DefaultCreate(resSchema), + ReadContext: DefaultRead(resSchema), + UpdateContext: DefaultUpdate(resSchema), + DeleteContext: DefaultDelete(resSchema), + + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: resSchema, + } +}