Skip to content

Commit

Permalink
feat(/system/ntp/server): Add NTP Server resource
Browse files Browse the repository at this point in the history
Fixes #306
  • Loading branch information
vaerh committed Nov 29, 2023
1 parent dc4ff9a commit f7851ca
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/resources/routeros_system_ntp_server/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import routeros_system_ntp_server.test .
8 changes: 8 additions & 0 deletions examples/resources/routeros_system_ntp_server/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "routeros_system_ntp_server" "test" {
enabled = true
broadcast = true
multicast = true
manycast = true
use_local_clock = true
local_clock_stratum = 3
}
5 changes: 3 additions & 2 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ func Provider() *schema.Provider {

// System Objects
"routeros_ip_cloud": ResourceIpCloud(),
"routeros_system_certificate": ResourceSystemCertificate(),
"routeros_system_identity": ResourceSystemIdentity(),
"routeros_system_logging": ResourceSystemLogging(),
"routeros_system_ntp_server": ResourceSystemNtpServer(),
"routeros_system_scheduler": ResourceSystemScheduler(),
"routeros_system_certificate": ResourceSystemCertificate(),
"routeros_system_user": ResourceUser(),

// Aliases for system objects to retain compatibility between original and fork
Expand Down Expand Up @@ -179,7 +180,7 @@ func Provider() *schema.Provider {
"routeros_ppp_secret": ResourcePPPSecret(),

// RADIUS
"routeros_radius": ResourceRadius(),
"routeros_radius": ResourceRadius(),
"routeros_radius_incoming": ResourceRadiusIncoming(),

// SNMP
Expand Down
92 changes: 92 additions & 0 deletions routeros/resource_system_ntp_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package routeros

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

/*
{
"auth-key": "none",
"broadcast": "false",
"broadcast-addresses": "",
"enabled": "false",
"local-clock-stratum": "5",
"manycast": "false",
"multicast": "false",
"use-local-clock": "false",
"vrf": "main"
}
*/

// https://help.mikrotik.com/docs/display/ROS/NTP#NTP-NTPServersettings:.1
func ResourceSystemNtpServer() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/system/ntp/server"),
MetaId: PropId(Id),

"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable NTP server.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"broadcast": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable certain NTP server mode, for this mode to work you have to set up broadcast-addresses field.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"multicast": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable certain NTP server mode.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"manycast": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable certain NTP server mode.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"broadcast_addresses": {
Type: schema.TypeString,
Computed: true,
Description: "Set broadcast address to use for NTP server broadcast mode.",
},
KeyVrf: PropVrfRw,
"use_local_clock": {
Type: schema.TypeBool,
Optional: true,
Description: "The server will supply its local system time as valid if others are not available.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"local_clock_stratum": {
Type: schema.TypeInt,
Optional: true,
Description: "Manually set stratum if ```use_local_clock = true```.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
ValidateFunc: validation.IntBetween(1, 16),
},
"auth_key": {
Type: schema.TypeString,
Optional: true,
Description: "NTP symmetric key, used for authentication between the NTP client and server. Key Identifier " +
"(Key ID) - an integer identifying the cryptographic key used to generate the message-authentication code.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
}

return &schema.Resource{
CreateContext: DefaultSystemCreate(resSchema),
ReadContext: DefaultSystemRead(resSchema),
UpdateContext: DefaultSystemUpdate(resSchema),
DeleteContext: DefaultSystemDelete(resSchema),

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: resSchema,
}
}
56 changes: 56 additions & 0 deletions routeros/resource_system_ntp_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package routeros

import (
"fmt"
"testing"

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

const testSystemNtpServer = "routeros_system_ntp_server.test"

func TestAccSystemNtpServerTest_basic(t *testing.T) {
for _, name := range testNames {
t.Run(name, func(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testSetTransportEnv(t, name)
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccSystemNtpServerConfig("true", "10"),
Check: resource.ComposeTestCheckFunc(
testResourcePrimaryInstanceId(testSystemNtpServer),
resource.TestCheckResourceAttr(testSystemNtpServer, "enabled", "true"),
resource.TestCheckResourceAttr(testSystemNtpServer, "local_clock_stratum", "10"),
),
},
{
Config: testAccSystemNtpServerConfig("false", "3"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testSystemNtpServer, "enabled", "false"),
resource.TestCheckResourceAttr(testSystemNtpServer, "local_clock_stratum", "3"),
),
},
},
})

})
}
}

func testAccSystemNtpServerConfig(param, stratum string) string {
return fmt.Sprintf(`%v
resource "routeros_system_ntp_server" "test" {
enabled = %v
broadcast = %v
multicast = %v
manycast = %v
use_local_clock = %v
local_clock_stratum = %v
}
`, providerConfig, param, param, param, param, param, stratum)
}

0 comments on commit f7851ca

Please sign in to comment.