-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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 Managed Service Identity (MSI) support to VM Scale Sets #1018
Changes from 7 commits
a26b836
af340d4
2eba517
8a22743
44ac146
642bddf
0060fa3
4598daf
c70715f
03a9c31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,29 @@ func resourceArmVirtualMachineScaleSet() *schema.Resource { | |
|
||
"zones": zonesSchema(), | ||
|
||
"identity": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
DiffSuppressFunc: ignoreCaseDiffSuppressFunc, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"SystemAssigned", | ||
}, true), | ||
}, | ||
"principal_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"sku": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
|
@@ -639,6 +662,10 @@ func resourceArmVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interf | |
Zones: zones, | ||
} | ||
|
||
if _, ok := d.GetOk("identity"); ok { | ||
scaleSetParams.Identity = expandAzureRmVirtualMachineScaleSetIdentity(d) | ||
} | ||
|
||
if _, ok := d.GetOk("plan"); ok { | ||
plan, err := expandAzureRmVirtualMachineScaleSetPlan(d) | ||
if err != nil { | ||
|
@@ -701,6 +728,8 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac | |
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Scale Set Sku error: %#v", err) | ||
} | ||
|
||
d.Set("identity", flattenAzureRmVirtualMachineScaleSetIdentity(resp.Identity)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we change this to:
this allows any bugs (such as the schema not matching) to be caught There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
properties := resp.VirtualMachineScaleSetProperties | ||
|
||
d.Set("upgrade_policy_mode", properties.UpgradePolicy.Mode) | ||
|
@@ -803,6 +832,20 @@ func resourceArmVirtualMachineScaleSetDelete(d *schema.ResourceData, meta interf | |
return nil | ||
} | ||
|
||
func flattenAzureRmVirtualMachineScaleSetIdentity(identity *compute.VirtualMachineScaleSetIdentity) []interface{} { | ||
if identity == nil { | ||
return make([]interface{}, 0) | ||
} | ||
|
||
result := make(map[string]interface{}) | ||
result["type"] = string(identity.Type) | ||
if identity.PrincipalID != nil { | ||
result["principal_id"] = *identity.PrincipalID | ||
} | ||
|
||
return []interface{}{result} | ||
} | ||
|
||
func flattenAzureRmVirtualMachineScaleSetOsProfileLinuxConfig(config *compute.LinuxConfiguration) []interface{} { | ||
result := make(map[string]interface{}) | ||
result["disable_password_authentication"] = *config.DisablePasswordAuthentication | ||
|
@@ -1422,6 +1465,16 @@ func expandAzureRMVirtualMachineScaleSetsDiagnosticProfile(d *schema.ResourceDat | |
return diagnosticsProfile | ||
} | ||
|
||
func expandAzureRmVirtualMachineScaleSetIdentity(d *schema.ResourceData) *compute.VirtualMachineScaleSetIdentity { | ||
v := d.Get("identity") | ||
identities := v.([]interface{}) | ||
identity := identities[0].(map[string]interface{}) | ||
identityType := identity["type"].(string) | ||
return &compute.VirtualMachineScaleSetIdentity{ | ||
Type: compute.ResourceIdentityType(identityType), | ||
} | ||
} | ||
|
||
func expandAzureRMVirtualMachineScaleSetsStorageProfileOsDisk(d *schema.ResourceData) (*compute.VirtualMachineScaleSetOSDisk, error) { | ||
osDiskConfigs := d.Get("storage_profile_os_disk").(*schema.Set).List() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -418,6 +418,26 @@ func TestAccAzureRMVirtualMachineScaleSet_overprovision(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccAzureRMVirtualMachineScaleSet_MSI(t *testing.T) { | ||
resourceName := "azurerm-vmss-msi-test" | ||
ri := acctest.RandInt() | ||
config := testAccAzureRMVirtualMachineScaleSetMSITemplate(ri, testLocation()) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMVirtualMachineScaleSetDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMVirtualMachineScaleSetExists(resourceName), | ||
testCheckAzureRMVirtualMachineScaleSetMSI(resourceName), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we remove this function (which checks the API) in favour of checking the value stored in the state (which users will consume)? we can do this via:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMVirtualMachineScaleSet_extension(t *testing.T) { | ||
ri := acctest.RandInt() | ||
config := testAccAzureRMVirtualMachineScaleSetExtensionTemplate(ri, testLocation()) | ||
|
@@ -765,6 +785,27 @@ func testCheckAzureRMVirtualMachineScaleSetSinglePlacementGroup(name string, exp | |
} | ||
} | ||
|
||
func testCheckAzureRMVirtualMachineScaleSetMSI(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
resp, err := testGetAzureRMVirtualMachineScaleSet(s, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
identityType := resp.Identity.Type | ||
if identityType != "systemAssigned" { | ||
return fmt.Errorf("Bad: Identity Type is not systemAssigned for scale set %v", name) | ||
} | ||
|
||
principalID := *resp.Identity.PrincipalID | ||
if len(principalID) == 0 { | ||
return fmt.Errorf("Bad: Could not get principal_id for scale set %v", name) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testCheckAzureRMVirtualMachineScaleSetExtension(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
resp, err := testGetAzureRMVirtualMachineScaleSet(s, name) | ||
|
@@ -878,7 +919,7 @@ resource "azurerm_virtual_machine_scale_set" "test" { | |
tier = "Standard" | ||
capacity = 2 | ||
} | ||
|
||
os_profile { | ||
computer_name_prefix = "testvm-%d" | ||
admin_username = "myadmin" | ||
|
@@ -2299,6 +2340,101 @@ resource "azurerm_virtual_machine_scale_set" "test" { | |
`, rInt, location, rInt, rInt, rInt, rInt, rInt) | ||
} | ||
|
||
func testAccAzureRMVirtualMachineScaleSetMSITemplate(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_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_scale_set" "test" { | ||
name = "acctvmss-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
upgrade_policy_mode = "Manual" | ||
overprovision = false | ||
|
||
sku { | ||
name = "Standard_D1_v2" | ||
tier = "Standard" | ||
capacity = 1 | ||
} | ||
|
||
identity { | ||
type = "systemAssigned" | ||
} | ||
|
||
extension { | ||
name = "MSILinuxExtension" | ||
publisher = "Microsoft.ManagedIdentity" | ||
type = "ManagedIdentityExtensionForLinux" | ||
type_handler_version = "1.0" | ||
settings = "{\"port\": 50342}" | ||
} | ||
|
||
os_profile { | ||
computer_name_prefix = "testvm-%d" | ||
admin_username = "myadmin" | ||
admin_password = "Passwword1234" | ||
} | ||
|
||
network_profile { | ||
name = "TestNetworkProfile" | ||
primary = true | ||
|
||
ip_configuration { | ||
name = "TestIPConfiguration" | ||
subnet_id = "${azurerm_subnet.test.id}" | ||
} | ||
} | ||
|
||
storage_profile_os_disk { | ||
name = "os-disk" | ||
caching = "ReadWrite" | ||
create_option = "FromImage" | ||
vhd_containers = ["${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}"] | ||
} | ||
|
||
storage_profile_image_reference { | ||
publisher = "Canonical" | ||
offer = "UbuntuServer" | ||
sku = "16.04-LTS" | ||
version = "latest" | ||
} | ||
} | ||
|
||
`, rInt, location, rInt, rInt, rInt, rInt, rInt) | ||
} | ||
|
||
func testAccAzureRMVirtualMachineScaleSetExtensionTemplate(rInt int, location string) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add an acceptance test covering this use-case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done