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

Add timezone field to Virtual Machine (Windows Configuration) #1265

Merged
merged 14 commits into from
Jun 26, 2018
18 changes: 18 additions & 0 deletions azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,13 @@ func resourceArmVirtualMachine() *schema.Resource {
Optional: true,
Default: false,
},
"timezone": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validateAzureVirtualMachineTimeZone(true),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also needs a state migration to update the resourceArmVirtualMachineStorageOsProfileWindowsConfigHash function and a default value (UTC)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Is there any samples? I found one here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that'll do as an example, there's a few in the codebase - this value also needs to be added to the Hash function, otherwise this won't be detected as ForceNew for existing Virtual Machines (which is why the state migrations needed)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

"winrm": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1019,6 +1026,10 @@ func flattenAzureRmVirtualMachineOsProfileWindowsConfiguration(config *compute.W
result["enable_automatic_upgrades"] = *config.EnableAutomaticUpdates
}

if config.TimeZone != nil {
result["timezone"] = *config.TimeZone
}

if config.WinRM != nil {
listeners := make([]map[string]interface{}, 0, len(*config.WinRM.Listeners))
for _, i := range *config.WinRM.Listeners {
Expand Down Expand Up @@ -1291,6 +1302,10 @@ func expandAzureRmVirtualMachineOsProfileWindowsConfig(d *schema.ResourceData) (
config.EnableAutomaticUpdates = &update
}

if v := osProfileConfig["timezone"]; v != nil && v.(string) != "" {
config.TimeZone = utils.String(v.(string))
}

if v := osProfileConfig["winrm"]; v != nil {
winRm := v.([]interface{})
if len(winRm) > 0 {
Expand Down Expand Up @@ -1595,6 +1610,9 @@ func resourceArmVirtualMachineStorageOsProfileWindowsConfigHash(v interface{}) i
if v, ok := m["enable_automatic_upgrades"]; ok {
buf.WriteString(fmt.Sprintf("%t-", v.(bool)))
}
if v, ok := m["timezone"]; ok {
buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(v.(string))))
}
}

return hashcode.String(buf.String())
Expand Down
105 changes: 105 additions & 0 deletions azurerm/resource_arm_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package azurerm
import (
"fmt"
"net/http"
"testing"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-12-01/compute"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
Expand Down Expand Up @@ -68,3 +70,106 @@ func testCheckAzureRMVirtualMachineDestroy(s *terraform.State) error {

return nil
}

func TestAccAzureRMVirtualMachine_winTimeZone(t *testing.T) {
resourceName := "azurerm_virtual_machine.test"
var vm compute.VirtualMachine
ri := acctest.RandInt()
config := testAccAzureRMVirtualMachine_winTimeZone(ri, testLocation())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
resource.TestCheckResourceAttr(resourceName, "os_profile_windows_config.2277357006.timezone", "Pacific Standard Time"),
),
},
},
})
}

func testAccAzureRMVirtualMachine_winTimeZone(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_virtual_network" "test" {
name = "acctvn-%d"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_subnet" "test" {
name = "acctsub-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
}

resource "azurerm_network_interface" "test" {
name = "acctni-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.test.id}"
private_ip_address_allocation = "dynamic"
}
}

resource "azurerm_storage_account" "test" {
name = "accsa%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_container" "test" {
name = "vhds"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_name = "${azurerm_storage_account.test.name}"
container_access_type = "private"
}

resource "azurerm_virtual_machine" "test" {
name = "acctvm-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_D1_v2"

storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2012-Datacenter"
version = "latest"
}

storage_os_disk {
name = "myosdisk1"
vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
caching = "ReadWrite"
create_option = "FromImage"
}

os_profile {
computer_name = "winhost01"
admin_username = "testadmin"
admin_password = "Password1234!"
}

os_profile_windows_config {
timezone = "Pacific Standard Time"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make the spacing consistent here? we use spaces for indentation for terraform configurations

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

`, rInt, location, rInt, rInt, rInt, rInt, rInt)
}
117 changes: 117 additions & 0 deletions azurerm/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/satori/uuid"
)

Expand Down Expand Up @@ -76,6 +77,122 @@ func validateIso8601Duration() schema.SchemaValidateFunc {
}
}

func validateAzureVirtualMachineTimeZone(acceptEmpty bool) schema.SchemaValidateFunc {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is only used in a single place, we can assume this is the case - I'll push a commit to remove this.

// Candidates are listed here: http://jackstromberg.com/2017/01/list-of-time-zones-consumed-by-azure/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a Microsoft source for this information?

Copy link
Author

@JunyiYi JunyiYi May 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. I didn't find any official Azure list. But I tried that list, seems to be the correct one. And of course we don't need to provide the validation here, because the API call will return error.

So what is the best practice? Waiting the API to return error code or provide client-side validation?

candidates := []string{
"Afghanistan Standard Time",
"Alaskan Standard Time",
"Arab Standard Time",
"Arabian Standard Time",
"Arabic Standard Time",
"Argentina Standard Time",
"Atlantic Standard Time",
"AUS Central Standard Time",
"AUS Eastern Standard Time",
"Azerbaijan Standard Time",
"Azores Standard Time",
"Bahia Standard Time",
"Bangladesh Standard Time",
"Belarus Standard Time",
"Canada Central Standard Time",
"Cape Verde Standard Time",
"Caucasus Standard Time",
"Cen. Australia Standard Time",
"Central America Standard Time",
"Central Asia Standard Time",
"Central Brazilian Standard Time",
"Central Europe Standard Time",
"Central European Standard Time",
"Central Pacific Standard Time",
"Central Standard Time (Mexico)",
"Central Standard Time",
"China Standard Time",
"Dateline Standard Time",
"E. Africa Standard Time",
"E. Australia Standard Time",
"E. Europe Standard Time",
"E. South America Standard Time",
"Eastern Standard Time (Mexico)",
"Eastern Standard Time",
"Egypt Standard Time",
"Ekaterinburg Standard Time",
"Fiji Standard Time",
"FLE Standard Time",
"Georgian Standard Time",
"GMT Standard Time",
"Greenland Standard Time",
"Greenwich Standard Time",
"GTB Standard Time",
"Hawaiian Standard Time",
"India Standard Time",
"Iran Standard Time",
"Israel Standard Time",
"Jordan Standard Time",
"Kaliningrad Standard Time",
"Korea Standard Time",
"Libya Standard Time",
"Line Islands Standard Time",
"Magadan Standard Time",
"Mauritius Standard Time",
"Middle East Standard Time",
"Montevideo Standard Time",
"Morocco Standard Time",
"Mountain Standard Time (Mexico)",
"Mountain Standard Time",
"Myanmar Standard Time",
"N. Central Asia Standard Time",
"Namibia Standard Time",
"Nepal Standard Time",
"New Zealand Standard Time",
"Newfoundland Standard Time",
"North Asia East Standard Time",
"North Asia Standard Time",
"Pacific SA Standard Time",
"Pacific Standard Time (Mexico)",
"Pacific Standard Time",
"Pakistan Standard Time",
"Paraguay Standard Time",
"Romance Standard Time",
"Russia Time Zone 10",
"Russia Time Zone 11",
"Russia Time Zone 3",
"Russian Standard Time",
"SA Eastern Standard Time",
"SA Pacific Standard Time",
"SA Western Standard Time",
"Samoa Standard Time",
"SE Asia Standard Time",
"Singapore Standard Time",
"South Africa Standard Time",
"Sri Lanka Standard Time",
"Syria Standard Time",
"Taipei Standard Time",
"Tasmania Standard Time",
"Tokyo Standard Time",
"Tonga Standard Time",
"Turkey Standard Time",
"Ulaanbaatar Standard Time",
"US Eastern Standard Time",
"US Mountain Standard Time",
"UTC",
"UTC+12",
"UTC-02",
"UTC-11",
"Venezuela Standard Time",
"Vladivostok Standard Time",
"W. Australia Standard Time",
"W. Central Africa Standard Time",
"W. Europe Standard Time",
"West Asia Standard Time",
"West Pacific Standard Time",
"Yakutsk Standard Time",
}
if acceptEmpty {
candidates = append(candidates, "")
}
return validation.StringInSlice(candidates, true)
}

// intBetweenDivisibleBy returns a SchemaValidateFunc which tests if the provided value
// is of type int and is between min and max (inclusive) and is divisible by a given number
func validateIntBetweenDivisibleBy(min, max, divisor int) schema.SchemaValidateFunc {
Expand Down
49 changes: 49 additions & 0 deletions azurerm/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,52 @@ func TestValidateIntBetweenDivisibleBy(t *testing.T) {
}
}
}

func TestValidateAzureVirtualMachineTimeZone(t *testing.T) {
cases := []struct {
Value string
Empty bool
Errors int
}{
{
Value: "",
Empty: true,
Errors: 0,
},
{
Value: "",
Empty: false,
Errors: 1,
},
{
Value: "UTC",
Empty: true,
Errors: 0,
},
{
Value: "China Standard Time",
Empty: false,
Errors: 0,
},
{
// Valid UTC time zone
Value: "utc-11",
Empty: true,
Errors: 0,
},
{
// Invalid UTC time zone
Value: "UTC-30",
Empty: false,
Errors: 1,
},
}

for _, tc := range cases {
_, errors := validateAzureVirtualMachineTimeZone(tc.Empty)(tc.Value, "unittest")

if len(errors) != tc.Errors {
t.Fatalf("Expected validateAzureVMTimeZone to trigger '%d' errors for '%s' - got '%d'", tc.Errors, tc.Value, len(errors))
}
}
}
1 change: 1 addition & 0 deletions website/docs/r/virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ output "principal_id" {

* `provision_vm_agent` - (Optional) This value defaults to false.
* `enable_automatic_upgrades` - (Optional) This value defaults to false.
* `timezone` - (Optional) Specifies the time zone of the virtual machine, [the possible values are defined here](http://jackstromberg.com/2017/01/list-of-time-zones-consumed-by-azure/). Defaults to `""`.
* `winrm` - (Optional) A collection of WinRM configuration blocks as documented below.
* `additional_unattend_config` - (Optional) An Additional Unattended Config block as documented below.

Expand Down