-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(/system/ntp/server): Add NTP Server resource
Fixes #306
- Loading branch information
Showing
5 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
terraform import routeros_system_ntp_server.test . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |