Skip to content

Commit

Permalink
fix: Move WG keys from datasource to resource (#265)
Browse files Browse the repository at this point in the history
* docs: Correct the path of datasource examples

* fix: Move WG  keys from datasource to resource

* fix: Add missing attributes
  • Loading branch information
vaerh authored Sep 27, 2023
1 parent b6b95fc commit a4eaf8c
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 56 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 0 additions & 11 deletions examples/data-sources/wireguard_keys.tf

This file was deleted.

12 changes: 12 additions & 0 deletions examples/resources/routeros_wireguard_keys/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "routeros_wireguard_keys" "wgk" {
number = 3
}

output "wg_keys" {
value = routeros_wireguard_keys.wgk.keys[*]
sensitive = true
}

output "wg_key" {
value = nonsensitive(routeros_wireguard_keys.wgk.keys[1].public)
}
39 changes: 0 additions & 39 deletions routeros/datasource_wireguard_keys_test.go

This file was deleted.

4 changes: 3 additions & 1 deletion routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,16 @@ func Provider() *schema.Provider {
// SNMP
"routeros_snmp": ResourceSNMP(),
"routeros_snmp_community": ResourceSNMPCommunity(),

// Helpers
"routeros_wireguard_keys": ResourceWireguardKeys(),
},
DataSourcesMap: map[string]*schema.Resource{
"routeros_interfaces": DatasourceInterfaces(),
"routeros_ip_addresses": DatasourceIPAddresses(),
"routeros_ip_routes": DatasourceIPRoutes(),
"routeros_firewall": DatasourceFirewall(),
"routeros_ipv6_addresses": DatasourceIPv6Addresses(),
"routeros_wireguard_keys": DatasourceWireguardKeys(),
},
ConfigureContextFunc: NewClient,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,39 @@ import (
"golang.org/x/crypto/curve25519"
)

func DatasourceWireguardKeys() *schema.Resource {
func ResourceWireguardKeys() *schema.Resource {
return &schema.Resource{
Description: "Creating key sets for WireGuard tunnels.",
ReadContext: datasourceMakeWGKeys,
Schema: map[string]*schema.Schema{
MetaId: {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Default: int(Name),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
},
MetaResourcePath: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "local",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
},
"number": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Default: 1,
Description: "The number of key sets.",
},
"keys": {
Type: schema.TypeList,
Computed: true,
Type: schema.TypeList,
Computed: true,
Sensitive: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"preshared": {
Expand All @@ -46,6 +65,14 @@ func DatasourceWireguardKeys() *schema.Resource {
},
},
},
CreateContext: wgKeysCreate,
ReadContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
},
DeleteContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
d.SetId("")
return nil
},
}
}

Expand Down Expand Up @@ -125,7 +152,7 @@ func (k Key) String() string {
return base64.StdEncoding.EncodeToString(k[:])
}

func datasourceMakeWGKeys(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func wgKeysCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var res []map[string]any

for i := 0; i < d.Get("number").(int); i++ {
Expand Down
41 changes: 41 additions & 0 deletions routeros/resource_wireguard_keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package routeros

import (
"testing"

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

const testResourceWireGuardKeys = "routeros_wireguard_keys.keys"

func TestAccResourceWireGuardKeys_basic(t *testing.T) {
t.Parallel()
t.Run("WG keys", func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccResourceWireGuardKeysConfig(),
Check: resource.ComposeTestCheckFunc(
testResourcePrimaryInstanceId(testResourceWireGuardKeys),
resource.TestCheckResourceAttr(testResourceWireGuardKeys, "number", "3"),
),
},
},
})

})
}

func testAccResourceWireGuardKeysConfig() string {
return `
provider "routeros" {
insecure = true
}
resource "routeros_wireguard_keys" "keys" {
number = 3
}
`
}

0 comments on commit a4eaf8c

Please sign in to comment.