From c9b34ad2ab5d3055de3597f40e796c999a3b7330 Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Mon, 29 Apr 2019 23:25:41 -0700 Subject: [PATCH 1/7] Implement UAI data source --- azurerm/data_source_user_assigned_identity.go | 73 +++++++++++++++++++ azurerm/provider.go | 1 + 2 files changed, 74 insertions(+) create mode 100644 azurerm/data_source_user_assigned_identity.go diff --git a/azurerm/data_source_user_assigned_identity.go b/azurerm/data_source_user_assigned_identity.go new file mode 100644 index 000000000000..410234ac5481 --- /dev/null +++ b/azurerm/data_source_user_assigned_identity.go @@ -0,0 +1,73 @@ +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" + "github.com/hashicorp/terraform/helper/validation" +) + +func dataSourceArmUserAssignedIdentity() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmUserAssignedIdentityRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 24), + }, + + "resource_group_name": resourceGroupNameSchema(), + + "location": locationForDataSourceSchema(), + + "tags": tagsForDataSourceSchema(), + + "principal_id": { + Type: schema.TypeString, + Computed: true, + }, + + "client_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceArmUserAssignedIdentityRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).userAssignedIdentitiesClient + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + resp, err := client.Get(ctx, resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + return fmt.Errorf("Error making Read request on User Assigned Identity %q (Resource Group %q): %+v", name, resourceGroup, err) + } + + d.SetId(*resp.ID) + + d.Set("location", resp.Location) + + if props := resp.IdentityProperties; props != nil { + if principalId := props.PrincipalID; principalId != nil { + d.Set("principal_id", principalId.String()) + } + + if clientId := props.ClientID; clientId != nil { + d.Set("client_id", clientId.String()) + } + } + + flattenAndSetTags(d, resp.Tags) + + return nil +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 60eeed2e8886..0e0ac889663b 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -167,6 +167,7 @@ func Provider() terraform.ResourceProvider { "azurerm_subscription": dataSourceArmSubscription(), "azurerm_subscriptions": dataSourceArmSubscriptions(), "azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(), + "azurerm_user_assigned_identity": dataSourceArmUserAssignedIdentity(), "azurerm_virtual_machine": dataSourceArmVirtualMachine(), "azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(), "azurerm_virtual_network": dataSourceArmVirtualNetwork(), From d48fca9279b1b1037231b7642091ca64f56715f6 Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Tue, 30 Apr 2019 00:09:47 -0700 Subject: [PATCH 2/7] Add tests --- azurerm/data_source_user_assigned_identity.go | 7 ++- ...data_source_user_assigned_identity_test.go | 61 +++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 azurerm/data_source_user_assigned_identity_test.go diff --git a/azurerm/data_source_user_assigned_identity.go b/azurerm/data_source_user_assigned_identity.go index 410234ac5481..71920248a65f 100644 --- a/azurerm/data_source_user_assigned_identity.go +++ b/azurerm/data_source_user_assigned_identity.go @@ -18,12 +18,10 @@ func dataSourceArmUserAssignedIdentity() *schema.Resource { ValidateFunc: validation.StringLenBetween(1, 24), }, - "resource_group_name": resourceGroupNameSchema(), + "resource_group_name": resourceGroupNameForDataSourceSchema(), "location": locationForDataSourceSchema(), - "tags": tagsForDataSourceSchema(), - "principal_id": { Type: schema.TypeString, Computed: true, @@ -33,6 +31,9 @@ func dataSourceArmUserAssignedIdentity() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "tags": tagsForDataSourceSchema(), + }, } } diff --git a/azurerm/data_source_user_assigned_identity_test.go b/azurerm/data_source_user_assigned_identity_test.go new file mode 100644 index 000000000000..a48ce670090b --- /dev/null +++ b/azurerm/data_source_user_assigned_identity_test.go @@ -0,0 +1,61 @@ +package azurerm + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccDataSourceAzureRMUserAssignedIdentity_basic(t *testing.T) { + generatedUuidRegex := "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$" + dataSourceName := "data.azurerm_user_assigned_identity.test" + ri := tf.AccRandTimeInt() + rs := acctest.RandString(4) + + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAzureRMUserAssignedIdentity_basic(ri, testLocation(), rs), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s-uai", rs)), + resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", fmt.Sprintf("acctest%d-rg", ri)), + resource.TestCheckResourceAttr(dataSourceName, "location", azureRMNormalizeLocation(location)), + resource.TestMatchResourceAttr(dataSourceName, "principal_id", regexp.MustCompile(generatedUuidRegex)), + resource.TestMatchResourceAttr(dataSourceName, "client_id", regexp.MustCompile(generatedUuidRegex)), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"), + ), + }, + }, + }) +} + +func testAccDataSourceAzureRMUserAssignedIdentity_basic(rInt int, location string, rString string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctest%d-rg" + location = "%s" +} + +resource "azurerm_user_assigned_identity" "test" { + name = "acctest%s-uai" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "${azurerm_resource_group.test.location}" + tags = { + "foo" = "bar" + } +} + +data "azurerm_user_assigned_identity" "test" { + name = "${azurerm_user_assigned_identity.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, rInt, location, rString) +} From 6a25cde32d3d3d440f1307424bd32489bb272649 Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Tue, 30 Apr 2019 00:21:06 -0700 Subject: [PATCH 3/7] Improve tests --- ...data_source_user_assigned_identity_test.go | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/azurerm/data_source_user_assigned_identity_test.go b/azurerm/data_source_user_assigned_identity_test.go index a48ce670090b..2741dbeb3b63 100644 --- a/azurerm/data_source_user_assigned_identity_test.go +++ b/azurerm/data_source_user_assigned_identity_test.go @@ -7,12 +7,14 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" ) func TestAccDataSourceAzureRMUserAssignedIdentity_basic(t *testing.T) { generatedUuidRegex := "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$" dataSourceName := "data.azurerm_user_assigned_identity.test" + resourceName := "azurerm_user_assigned_identity.test" ri := tf.AccRandTimeInt() rs := acctest.RandString(4) @@ -31,12 +33,38 @@ func TestAccDataSourceAzureRMUserAssignedIdentity_basic(t *testing.T) { resource.TestMatchResourceAttr(dataSourceName, "principal_id", regexp.MustCompile(generatedUuidRegex)), resource.TestMatchResourceAttr(dataSourceName, "client_id", regexp.MustCompile(generatedUuidRegex)), resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"), + testEqualResourceAttr(dataSourceName, resourceName, "principal_id"), + testEqualResourceAttr(dataSourceName, resourceName, "client_id"), ), }, }, }) } +func testEqualResourceAttr(dataSourceName string, resourceName string, attrName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + // Ensure we have enough information in state to look up in API + ds, ok := s.RootModule().Resources[dataSourceName] + if !ok { + return fmt.Errorf("Not found: %s", dataSourceName) + } + + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Not found: %s", resourceName) + } + + dsAttr := ds.Primary.Attributes[attrName] + rsAttr := rs.Primary.Attributes[attrName] + + if dsAttr != rsAttr { + return fmt.Errorf("Attributes not equal: %s, %s", dsAttr, rsAttr) + } + + return nil + } +} + func testAccDataSourceAzureRMUserAssignedIdentity_basic(rInt int, location string, rString string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { From cd1bf1971b0fb14c5385be9b802aae41fd86759e Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Tue, 30 Apr 2019 00:37:07 -0700 Subject: [PATCH 4/7] Update docs --- website/azurerm.erb | 4 ++ .../d/user_assigned_identity.html.markdown | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 website/docs/d/user_assigned_identity.html.markdown diff --git a/website/azurerm.erb b/website/azurerm.erb index d1f74c33bfc8..659819b0e60c 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -327,6 +327,10 @@ azurerm_traffic_manager_geographical_location + > + azurerm_user_assigned_identity + + > azurerm_virtual_machine diff --git a/website/docs/d/user_assigned_identity.html.markdown b/website/docs/d/user_assigned_identity.html.markdown new file mode 100644 index 000000000000..5b63a5bc6e38 --- /dev/null +++ b/website/docs/d/user_assigned_identity.html.markdown @@ -0,0 +1,42 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azure_user_assigned_identity" +sidebar_current: "docs-azurerm-datasource-user-assigned-identity" +description: |- + Gets information about an existing User Assigned Identity. + +--- + +# Data Source: azurerm_user_assigned_identity + +Use this data source to access information about an existing User Assigned Identity. + +## Example Usage (reference an existing) + +```hcl +data "azurerm_user_assigned_identity" "test" { + name = "name_of_user_assigned_identity" + resource_group_name = "name_of_resource_group" +} + +output "uai_client_id" { + value = "${data.azurerm_user_assigned_identity.test.client_id}" +} + +output "uai_principal_id" { + value = "${data.azurerm_user_assigned_identity.test.principal_id}" +} +``` + +## Argument Reference + +* `name` - (Required) Specifies the name of the user assigned identity. +* `resource_group_name` - (Required) Specifies the name of the resource group. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The user assigned identity ID. +* `principal_id` - Service Principal ID associated with the user assigned identity. +* `client_id` - Client ID associated with the user assigned identity. From 0c3f0f01a0617bc72a6b5426785f9d07b068489f Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Tue, 30 Apr 2019 00:47:41 -0700 Subject: [PATCH 5/7] gofmt --- azurerm/data_source_user_assigned_identity.go | 147 +++++++-------- ...data_source_user_assigned_identity_test.go | 178 +++++++++--------- 2 files changed, 162 insertions(+), 163 deletions(-) diff --git a/azurerm/data_source_user_assigned_identity.go b/azurerm/data_source_user_assigned_identity.go index 71920248a65f..f3a70b9c434c 100644 --- a/azurerm/data_source_user_assigned_identity.go +++ b/azurerm/data_source_user_assigned_identity.go @@ -1,74 +1,73 @@ -package azurerm - -import ( - "fmt" - - "github.com/hashicorp/terraform/helper/schema" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" - "github.com/hashicorp/terraform/helper/validation" -) - -func dataSourceArmUserAssignedIdentity() *schema.Resource { - return &schema.Resource{ - Read: dataSourceArmUserAssignedIdentityRead, - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringLenBetween(1, 24), - }, - - "resource_group_name": resourceGroupNameForDataSourceSchema(), - - "location": locationForDataSourceSchema(), - - "principal_id": { - Type: schema.TypeString, - Computed: true, - }, - - "client_id": { - Type: schema.TypeString, - Computed: true, - }, - - "tags": tagsForDataSourceSchema(), - - }, - } -} - -func dataSourceArmUserAssignedIdentityRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*ArmClient).userAssignedIdentitiesClient - ctx := meta.(*ArmClient).StopContext - - name := d.Get("name").(string) - resourceGroup := d.Get("resource_group_name").(string) - - resp, err := client.Get(ctx, resourceGroup, name) - if err != nil { - if utils.ResponseWasNotFound(resp.Response) { - d.SetId("") - return nil - } - return fmt.Errorf("Error making Read request on User Assigned Identity %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - d.SetId(*resp.ID) - - d.Set("location", resp.Location) - - if props := resp.IdentityProperties; props != nil { - if principalId := props.PrincipalID; principalId != nil { - d.Set("principal_id", principalId.String()) - } - - if clientId := props.ClientID; clientId != nil { - d.Set("client_id", clientId.String()) - } - } - - flattenAndSetTags(d, resp.Tags) - - return nil -} +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmUserAssignedIdentity() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmUserAssignedIdentityRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 24), + }, + + "resource_group_name": resourceGroupNameForDataSourceSchema(), + + "location": locationForDataSourceSchema(), + + "principal_id": { + Type: schema.TypeString, + Computed: true, + }, + + "client_id": { + Type: schema.TypeString, + Computed: true, + }, + + "tags": tagsForDataSourceSchema(), + }, + } +} + +func dataSourceArmUserAssignedIdentityRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).userAssignedIdentitiesClient + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + resp, err := client.Get(ctx, resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + return fmt.Errorf("Error making Read request on User Assigned Identity %q (Resource Group %q): %+v", name, resourceGroup, err) + } + + d.SetId(*resp.ID) + + d.Set("location", resp.Location) + + if props := resp.IdentityProperties; props != nil { + if principalId := props.PrincipalID; principalId != nil { + d.Set("principal_id", principalId.String()) + } + + if clientId := props.ClientID; clientId != nil { + d.Set("client_id", clientId.String()) + } + } + + flattenAndSetTags(d, resp.Tags) + + return nil +} diff --git a/azurerm/data_source_user_assigned_identity_test.go b/azurerm/data_source_user_assigned_identity_test.go index 2741dbeb3b63..198498dab8fb 100644 --- a/azurerm/data_source_user_assigned_identity_test.go +++ b/azurerm/data_source_user_assigned_identity_test.go @@ -1,89 +1,89 @@ -package azurerm - -import ( - "fmt" - "regexp" - "testing" - - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" -) - -func TestAccDataSourceAzureRMUserAssignedIdentity_basic(t *testing.T) { - generatedUuidRegex := "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$" - dataSourceName := "data.azurerm_user_assigned_identity.test" - resourceName := "azurerm_user_assigned_identity.test" - ri := tf.AccRandTimeInt() - rs := acctest.RandString(4) - - location := testLocation() - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccDataSourceAzureRMUserAssignedIdentity_basic(ri, testLocation(), rs), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s-uai", rs)), - resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", fmt.Sprintf("acctest%d-rg", ri)), - resource.TestCheckResourceAttr(dataSourceName, "location", azureRMNormalizeLocation(location)), - resource.TestMatchResourceAttr(dataSourceName, "principal_id", regexp.MustCompile(generatedUuidRegex)), - resource.TestMatchResourceAttr(dataSourceName, "client_id", regexp.MustCompile(generatedUuidRegex)), - resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"), - testEqualResourceAttr(dataSourceName, resourceName, "principal_id"), - testEqualResourceAttr(dataSourceName, resourceName, "client_id"), - ), - }, - }, - }) -} - -func testEqualResourceAttr(dataSourceName string, resourceName string, attrName string) resource.TestCheckFunc { - return func(s *terraform.State) error { - // Ensure we have enough information in state to look up in API - ds, ok := s.RootModule().Resources[dataSourceName] - if !ok { - return fmt.Errorf("Not found: %s", dataSourceName) - } - - rs, ok := s.RootModule().Resources[resourceName] - if !ok { - return fmt.Errorf("Not found: %s", resourceName) - } - - dsAttr := ds.Primary.Attributes[attrName] - rsAttr := rs.Primary.Attributes[attrName] - - if dsAttr != rsAttr { - return fmt.Errorf("Attributes not equal: %s, %s", dsAttr, rsAttr) - } - - return nil - } -} - -func testAccDataSourceAzureRMUserAssignedIdentity_basic(rInt int, location string, rString string) string { - return fmt.Sprintf(` -resource "azurerm_resource_group" "test" { - name = "acctest%d-rg" - location = "%s" -} - -resource "azurerm_user_assigned_identity" "test" { - name = "acctest%s-uai" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - tags = { - "foo" = "bar" - } -} - -data "azurerm_user_assigned_identity" "test" { - name = "${azurerm_user_assigned_identity.test.name}" - resource_group_name = "${azurerm_resource_group.test.name}" -} -`, rInt, location, rString) -} +package azurerm + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccDataSourceAzureRMUserAssignedIdentity_basic(t *testing.T) { + generatedUuidRegex := "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$" + dataSourceName := "data.azurerm_user_assigned_identity.test" + resourceName := "azurerm_user_assigned_identity.test" + ri := tf.AccRandTimeInt() + rs := acctest.RandString(4) + + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAzureRMUserAssignedIdentity_basic(ri, testLocation(), rs), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s-uai", rs)), + resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", fmt.Sprintf("acctest%d-rg", ri)), + resource.TestCheckResourceAttr(dataSourceName, "location", azureRMNormalizeLocation(location)), + resource.TestMatchResourceAttr(dataSourceName, "principal_id", regexp.MustCompile(generatedUuidRegex)), + resource.TestMatchResourceAttr(dataSourceName, "client_id", regexp.MustCompile(generatedUuidRegex)), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"), + testEqualResourceAttr(dataSourceName, resourceName, "principal_id"), + testEqualResourceAttr(dataSourceName, resourceName, "client_id"), + ), + }, + }, + }) +} + +func testEqualResourceAttr(dataSourceName string, resourceName string, attrName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + // Ensure we have enough information in state to look up in API + ds, ok := s.RootModule().Resources[dataSourceName] + if !ok { + return fmt.Errorf("Not found: %s", dataSourceName) + } + + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Not found: %s", resourceName) + } + + dsAttr := ds.Primary.Attributes[attrName] + rsAttr := rs.Primary.Attributes[attrName] + + if dsAttr != rsAttr { + return fmt.Errorf("Attributes not equal: %s, %s", dsAttr, rsAttr) + } + + return nil + } +} + +func testAccDataSourceAzureRMUserAssignedIdentity_basic(rInt int, location string, rString string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctest%d-rg" + location = "%s" +} + +resource "azurerm_user_assigned_identity" "test" { + name = "acctest%s-uai" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "${azurerm_resource_group.test.location}" + tags = { + "foo" = "bar" + } +} + +data "azurerm_user_assigned_identity" "test" { + name = "${azurerm_user_assigned_identity.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, rInt, location, rString) +} From 63500e008e7a66995112db8d6e2fdd7c30d106d4 Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Tue, 30 Apr 2019 20:13:38 -0700 Subject: [PATCH 6/7] Review feedback --- azurerm/data_source_user_assigned_identity.go | 4 ++- ...data_source_user_assigned_identity_test.go | 28 ++++++++++--------- ...esource_arm_user_assigned_identity_test.go | 12 ++++---- .../d/user_assigned_identity.html.markdown | 18 ++++++------ 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/azurerm/data_source_user_assigned_identity.go b/azurerm/data_source_user_assigned_identity.go index f3a70b9c434c..f68999c6d839 100644 --- a/azurerm/data_source_user_assigned_identity.go +++ b/azurerm/data_source_user_assigned_identity.go @@ -55,7 +55,9 @@ func dataSourceArmUserAssignedIdentityRead(d *schema.ResourceData, meta interfac d.SetId(*resp.ID) - d.Set("location", resp.Location) + if location := resp.Location; location != nil { + d.Set("location", azure.NormalizeLocation(*location)) + } if props := resp.IdentityProperties; props != nil { if principalId := props.PrincipalID; principalId != nil { diff --git a/azurerm/data_source_user_assigned_identity_test.go b/azurerm/data_source_user_assigned_identity_test.go index 198498dab8fb..7107ca600687 100644 --- a/azurerm/data_source_user_assigned_identity_test.go +++ b/azurerm/data_source_user_assigned_identity_test.go @@ -5,6 +5,8 @@ import ( "regexp" "testing" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" @@ -12,7 +14,6 @@ import ( ) func TestAccDataSourceAzureRMUserAssignedIdentity_basic(t *testing.T) { - generatedUuidRegex := "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$" dataSourceName := "data.azurerm_user_assigned_identity.test" resourceName := "azurerm_user_assigned_identity.test" ri := tf.AccRandTimeInt() @@ -30,8 +31,8 @@ func TestAccDataSourceAzureRMUserAssignedIdentity_basic(t *testing.T) { resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s-uai", rs)), resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", fmt.Sprintf("acctest%d-rg", ri)), resource.TestCheckResourceAttr(dataSourceName, "location", azureRMNormalizeLocation(location)), - resource.TestMatchResourceAttr(dataSourceName, "principal_id", regexp.MustCompile(generatedUuidRegex)), - resource.TestMatchResourceAttr(dataSourceName, "client_id", regexp.MustCompile(generatedUuidRegex)), + resource.TestMatchResourceAttr(dataSourceName, "principal_id", validate.UUIDRegExp), + resource.TestMatchResourceAttr(dataSourceName, "client_id", validate.UUIDRegExp), resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"), testEqualResourceAttr(dataSourceName, resourceName, "principal_id"), testEqualResourceAttr(dataSourceName, resourceName, "client_id"), @@ -68,22 +69,23 @@ func testEqualResourceAttr(dataSourceName string, resourceName string, attrName func testAccDataSourceAzureRMUserAssignedIdentity_basic(rInt int, location string, rString string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { - name = "acctest%d-rg" - location = "%s" + name = "acctest%d-rg" + location = "%s" } resource "azurerm_user_assigned_identity" "test" { - name = "acctest%s-uai" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - tags = { - "foo" = "bar" - } + name = "acctest%s-uai" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "${azurerm_resource_group.test.location}" + + tags = { + "foo" = "bar" + } } data "azurerm_user_assigned_identity" "test" { - name = "${azurerm_user_assigned_identity.test.name}" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "${azurerm_user_assigned_identity.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" } `, rInt, location, rString) } diff --git a/azurerm/resource_arm_user_assigned_identity_test.go b/azurerm/resource_arm_user_assigned_identity_test.go index bc553d1d7a27..d85a67e94cc2 100644 --- a/azurerm/resource_arm_user_assigned_identity_test.go +++ b/azurerm/resource_arm_user_assigned_identity_test.go @@ -7,6 +7,8 @@ import ( "net/http" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" @@ -14,7 +16,6 @@ import ( ) func TestAccAzureRMUserAssignedIdentity_basic(t *testing.T) { - generatedUuidRegex := "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$" resourceName := "azurerm_user_assigned_identity.test" ri := tf.AccRandTimeInt() rs := acctest.RandString(14) @@ -28,8 +29,8 @@ func TestAccAzureRMUserAssignedIdentity_basic(t *testing.T) { Config: testAccAzureRMUserAssignedIdentity_basic(ri, testLocation(), rs), Check: resource.ComposeTestCheckFunc( testCheckAzureRMUserAssignedIdentityExists(resourceName), - resource.TestMatchResourceAttr(resourceName, "principal_id", regexp.MustCompile(generatedUuidRegex)), - resource.TestMatchResourceAttr(resourceName, "client_id", regexp.MustCompile(generatedUuidRegex)), + resource.TestMatchResourceAttr(resourceName, "principal_id", validate.UUIDRegExp), + resource.TestMatchResourceAttr(resourceName, "client_id", validate.UUIDRegExp), ), }, { @@ -46,7 +47,6 @@ func TestAccAzureRMUserAssignedIdentity_requiresImport(t *testing.T) { return } - generatedUuidRegex := "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$" resourceName := "azurerm_user_assigned_identity.test" ri := tf.AccRandTimeInt() rs := acctest.RandString(14) @@ -60,8 +60,8 @@ func TestAccAzureRMUserAssignedIdentity_requiresImport(t *testing.T) { Config: testAccAzureRMUserAssignedIdentity_basic(ri, testLocation(), rs), Check: resource.ComposeTestCheckFunc( testCheckAzureRMUserAssignedIdentityExists(resourceName), - resource.TestMatchResourceAttr(resourceName, "principal_id", regexp.MustCompile(generatedUuidRegex)), - resource.TestMatchResourceAttr(resourceName, "client_id", regexp.MustCompile(generatedUuidRegex)), + resource.TestMatchResourceAttr(resourceName, "principal_id", validate.UUIDRegExp), + resource.TestMatchResourceAttr(resourceName, "client_id", validate.UUIDRegExp), ), }, { diff --git a/website/docs/d/user_assigned_identity.html.markdown b/website/docs/d/user_assigned_identity.html.markdown index 5b63a5bc6e38..ddd6da471315 100644 --- a/website/docs/d/user_assigned_identity.html.markdown +++ b/website/docs/d/user_assigned_identity.html.markdown @@ -14,29 +14,31 @@ Use this data source to access information about an existing User Assigned Ident ## Example Usage (reference an existing) ```hcl -data "azurerm_user_assigned_identity" "test" { +data "azurerm_user_assigned_identity" "example" { name = "name_of_user_assigned_identity" resource_group_name = "name_of_resource_group" } output "uai_client_id" { - value = "${data.azurerm_user_assigned_identity.test.client_id}" + value = "${data.azurerm_user_assigned_identity.example.client_id}" } output "uai_principal_id" { - value = "${data.azurerm_user_assigned_identity.test.principal_id}" + value = "${data.azurerm_user_assigned_identity.example.principal_id}" } ``` ## Argument Reference -* `name` - (Required) Specifies the name of the user assigned identity. -* `resource_group_name` - (Required) Specifies the name of the resource group. +* `name` - (Required) The name of the User Assigned Identity. +* `resource_group_name` - (Required) The name of the Resource Group in which the User Assigned Identity exists. ## Attributes Reference The following attributes are exported: -* `id` - The user assigned identity ID. -* `principal_id` - Service Principal ID associated with the user assigned identity. -* `client_id` - Client ID associated with the user assigned identity. +* `id` - The Resource ID of the User Assigned Identity. +* `location` - The Azure location where the User Assigned Identity exists. +* `principal_id` - The Service Principal ID of the User Assigned Identity. +* `client_id` - The Client ID of the User Assigned Identity. +* `tags` - A mapping of tags assigned to the User Assigned Identity. \ No newline at end of file From 165fdf37c14ab3ffae82da6bb11f29e8f836ee8e Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Tue, 30 Apr 2019 20:20:36 -0700 Subject: [PATCH 7/7] Fix tests --- azurerm/data_source_user_assigned_identity.go | 2 ++ azurerm/data_source_user_assigned_identity_test.go | 1 - azurerm/resource_arm_user_assigned_identity_test.go | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/data_source_user_assigned_identity.go b/azurerm/data_source_user_assigned_identity.go index f68999c6d839..ec3c4c88b35d 100644 --- a/azurerm/data_source_user_assigned_identity.go +++ b/azurerm/data_source_user_assigned_identity.go @@ -5,6 +5,8 @@ import ( "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) diff --git a/azurerm/data_source_user_assigned_identity_test.go b/azurerm/data_source_user_assigned_identity_test.go index 7107ca600687..899375a0c280 100644 --- a/azurerm/data_source_user_assigned_identity_test.go +++ b/azurerm/data_source_user_assigned_identity_test.go @@ -2,7 +2,6 @@ package azurerm import ( "fmt" - "regexp" "testing" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" diff --git a/azurerm/resource_arm_user_assigned_identity_test.go b/azurerm/resource_arm_user_assigned_identity_test.go index d85a67e94cc2..424fcee51ff6 100644 --- a/azurerm/resource_arm_user_assigned_identity_test.go +++ b/azurerm/resource_arm_user_assigned_identity_test.go @@ -2,7 +2,6 @@ package azurerm import ( "fmt" - "regexp" "testing" "net/http"