Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_hpc_cache - support for ntp_server and dns #11236

Merged
merged 4 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 90 additions & 8 deletions azurerm/internal/services/hpccache/hpc_cache_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,38 @@ func resourceHPCCache() *schema.Resource {
ValidateFunc: validation.IntBetween(576, 1500),
},

"ntp_server": {
Type: schema.TypeString,
Optional: true,
Default: "time.windows.com",
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: validation.StringIsNotEmpty,
},

"dns": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"servers": {
Type: schema.TypeList,
Required: true,
MaxItems: 3,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsIPAddress,
},
},

"search_domain": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

// TODO 3.0: remove this property
"root_squash_enabled": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -207,7 +239,6 @@ func resourceHPCCacheCreateOrUpdate(d *schema.ResourceData, meta interface{}) er
cacheSize := d.Get("cache_size_in_gb").(int)
subnet := d.Get("subnet_id").(string)
skuName := d.Get("sku_name").(string)
mtu := d.Get("mtu").(int)

var accessPolicies []storagecache.NfsAccessPolicy
if !d.IsNewResource() {
Expand Down Expand Up @@ -236,11 +267,9 @@ func resourceHPCCacheCreateOrUpdate(d *schema.ResourceData, meta interface{}) er
Name: utils.String(name),
Location: utils.String(location),
CacheProperties: &storagecache.CacheProperties{
CacheSizeGB: utils.Int32(int32(cacheSize)),
Subnet: utils.String(subnet),
NetworkSettings: &storagecache.CacheNetworkSettings{
Mtu: utils.Int32(int32(mtu)),
},
CacheSizeGB: utils.Int32(int32(cacheSize)),
Subnet: utils.String(subnet),
NetworkSettings: expandStorageCacheNetworkSettings(d),
SecuritySettings: &storagecache.CacheSecuritySettings{
AccessPolicies: &accessPolicies,
},
Expand Down Expand Up @@ -300,9 +329,14 @@ func resourceHPCCacheRead(d *schema.ResourceData, meta interface{}) error {
d.Set("cache_size_in_gb", props.CacheSizeGB)
d.Set("subnet_id", props.Subnet)
d.Set("mount_addresses", utils.FlattenStringSlice(props.MountAddresses))
if props.NetworkSettings != nil {
d.Set("mtu", props.NetworkSettings.Mtu)

mtu, ntpServer, dnsSetting := flattenStorageCacheNetworkSettings(props.NetworkSettings)
d.Set("mtu", mtu)
d.Set("ntp_server", ntpServer)
if err := d.Set("dns", dnsSetting); err != nil {
return fmt.Errorf("setting `dns`: %v", err)
}

if securitySettings := props.SecuritySettings; securitySettings != nil {
if securitySettings.AccessPolicies != nil {
defaultPolicy := CacheGetAccessPolicyByName(*securitySettings.AccessPolicies, "default")
Expand Down Expand Up @@ -454,3 +488,51 @@ func flattenStorageCacheNfsAccessRules(input *[]storagecache.NfsAccessRule) ([]i

return rules, nil
}

func expandStorageCacheNetworkSettings(d *schema.ResourceData) *storagecache.CacheNetworkSettings {
out := &storagecache.CacheNetworkSettings{
Mtu: utils.Int32(int32(d.Get("mtu").(int))),
NtpServer: utils.String(d.Get("ntp_server").(string)),
}

if dnsSetting, ok := d.GetOk("dns"); ok {
dnsSetting := dnsSetting.([]interface{})[0].(map[string]interface{})
out.DNSServers = utils.ExpandStringSlice(dnsSetting["servers"].([]interface{}))
searchDomain := dnsSetting["search_domain"].(string)
if searchDomain != "" {
out.DNSSearchDomain = &searchDomain
}
}
return out
}

func flattenStorageCacheNetworkSettings(settings *storagecache.CacheNetworkSettings) (mtu int, ntpServer string, dnsSetting []interface{}) {
if settings == nil {
return
}

if settings.Mtu != nil {
mtu = int(*settings.Mtu)
}

if settings.NtpServer != nil {
ntpServer = *settings.NtpServer
}

if settings.DNSServers != nil {
dnsServers := utils.FlattenStringSlice(settings.DNSServers)

searchDomain := ""
if settings.DNSSearchDomain != nil {
searchDomain = *settings.DNSSearchDomain
}

dnsSetting = []interface{}{
map[string]interface{}{
"servers": dnsServers,
"search_domain": searchDomain,
},
}
}
return
}
97 changes: 97 additions & 0 deletions azurerm/internal/services/hpccache/hpc_cache_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,68 @@ func TestAccHPCCache_mtu(t *testing.T) {
})
}

func TestAccHPCCache_ntpServer(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_hpc_cache", "test")
r := HPCCacheResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.ntpServer(data, "time.microsoft.com"),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("mount_addresses.#").Exists(),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("mount_addresses.#").Exists(),
),
},
data.ImportStep(),
})
}

func TestAccHPCCache_dnsSetting(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_hpc_cache", "test")
r := HPCCacheResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.dnsSetting(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("mount_addresses.#").Exists(),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("mount_addresses.#").Exists(),
),
},
data.ImportStep(),
})
}

func TestAccHPCCache_rootSquashDeprecated(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_hpc_cache", "test")
r := HPCCacheResource{}
Expand Down Expand Up @@ -286,6 +348,41 @@ resource "azurerm_hpc_cache" "test" {
`, r.template(data), data.RandomInteger)
}

func (r HPCCacheResource) ntpServer(data acceptance.TestData, server string) string {
return fmt.Sprintf(`
%s

resource "azurerm_hpc_cache" "test" {
name = "acctest-HPCC-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
cache_size_in_gb = 3072
subnet_id = azurerm_subnet.test.id
sku_name = "Standard_2G"
ntp_server = %q
}
`, r.template(data), data.RandomInteger, server)
}

func (r HPCCacheResource) dnsSetting(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_hpc_cache" "test" {
name = "acctest-HPCC-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
cache_size_in_gb = 3072
subnet_id = azurerm_subnet.test.id
sku_name = "Standard_2G"
dns {
servers = ["8.8.8.8"]
search_domain = "foo.com"
}
}
`, r.template(data), data.RandomInteger)
}

func (HPCCacheResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
12 changes: 12 additions & 0 deletions website/docs/r/hpc_cache.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ The following arguments are supported:
* `mtu` - (Optional) The IPv4 maximum transmission unit configured for the subnet of the HPC Cache. Possible values range from 576 - 1500. Defaults to 1500.

* `default_access_policy` - (Optional) A `default_access_policy` block as defined below.

* `ntp_server` - (Optional) The NTP server IP Address or FQDN for the HPC Cache. Defaults to `time.windows.com`.

* `dns` - (Optional) A `dns` block as defined below.

---

Expand Down Expand Up @@ -96,6 +100,14 @@ A `default_access_policy` block contains the following:

* `access_rule` - (Required) One to three `access_rule` blocks as defined above.

---

A `dns` block contains the following:

* `servers` - (Required) A list of DNS servers for the HPC Cache. At most three IP(s) are allowed to set.

* `search_domain` - (Optional) The DNS search domain for the HPC Cache.

## Attributes Reference

The following attributes are exported:
Expand Down