-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source:
azurerm_azuread_service_principal
Tests pass: ``` $ acctests azurerm TestAccDataSourceAzureRMAzureADServicePrincipal_ === RUN TestAccDataSourceAzureRMAzureADServicePrincipal_byApplicationId --- PASS: TestAccDataSourceAzureRMAzureADServicePrincipal_byApplicationId (34.96s) === RUN TestAccDataSourceAzureRMAzureADServicePrincipal_byDisplayName --- PASS: TestAccDataSourceAzureRMAzureADServicePrincipal_byDisplayName (23.48s) === RUN TestAccDataSourceAzureRMAzureADServicePrincipal_byObjectId --- PASS: TestAccDataSourceAzureRMAzureADServicePrincipal_byObjectId (62.43s) PASS ok github.com/terraform-providers/terraform-provider-azurerm/azurerm 120.900s ```
- Loading branch information
1 parent
c9b21a1
commit 19a01f9
Showing
7 changed files
with
295 additions
and
5 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
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,112 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmActiveDirectoryServicePrincipal() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmActiveDirectoryServicePrincipalRead, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
// TODO: customiseDiff to ensure either `object_id` or `display_name` or `application_id` is set | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"object_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ConflictsWith: []string{"display_name", "application_id"}, | ||
}, | ||
|
||
"display_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ConflictsWith: []string{"object_id", "application_id"}, | ||
}, | ||
|
||
"application_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ConflictsWith: []string{"object_id", "display_name"}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmActiveDirectoryServicePrincipalRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).servicePrincipalsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
var servicePrincipal *graphrbac.ServicePrincipal | ||
|
||
if v, ok := d.GetOk("object_id"); ok { | ||
objectId := v.(string) | ||
app, err := client.Get(ctx, objectId) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(app.Response) { | ||
return fmt.Errorf("Service Principal with Object ID %q was not found!", objectId) | ||
} | ||
|
||
return fmt.Errorf("Error retrieving Service Principal ID %q: %+v", objectId, err) | ||
} | ||
|
||
servicePrincipal = &app | ||
} else { | ||
apps, err := client.ListComplete(ctx, "") | ||
if err != nil { | ||
return fmt.Errorf("Error listing Service Principals: %+v", err) | ||
} | ||
|
||
if v, ok := d.GetOk("display_name"); ok { | ||
displayName := v.(string) | ||
|
||
for _, app := range *apps.Response().Value { | ||
if app.DisplayName == nil { | ||
continue | ||
} | ||
|
||
if *app.DisplayName == displayName { | ||
servicePrincipal = &app | ||
break | ||
} | ||
} | ||
|
||
if servicePrincipal == nil { | ||
return fmt.Errorf("A Service Principal with the Display Name %q was not found", displayName) | ||
} | ||
} else { | ||
applicationId := d.Get("application_id").(string) | ||
|
||
for _, app := range *apps.Response().Value { | ||
if app.AppID == nil { | ||
continue | ||
} | ||
|
||
if *app.AppID == applicationId { | ||
servicePrincipal = &app | ||
break | ||
} | ||
} | ||
|
||
if servicePrincipal == nil { | ||
return fmt.Errorf("A Service Principal for Application ID %q was not found", applicationId) | ||
} | ||
} | ||
} | ||
|
||
d.SetId(*servicePrincipal.ObjectID) | ||
|
||
d.Set("application_id", servicePrincipal.AppID) | ||
d.Set("display_name", servicePrincipal.DisplayName) | ||
d.Set("object_id", servicePrincipal.ObjectID) | ||
|
||
return nil | ||
} |
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,111 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAzureRMAzureADServicePrincipal_byApplicationId(t *testing.T) { | ||
dataSourceName := "data.azurerm_azuread_service_principal.test" | ||
id := uuid.New().String() | ||
config := testAccDataSourceAzureRMAzureADServicePrincipal_byApplicationId(id) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMActiveDirectoryServicePrincipalExists(dataSourceName), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "application_id"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "object_id"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "display_name"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMAzureADServicePrincipal_byDisplayName(t *testing.T) { | ||
dataSourceName := "data.azurerm_azuread_service_principal.test" | ||
id := uuid.New().String() | ||
config := testAccDataSourceAzureRMAzureADServicePrincipal_byDisplayName(id) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMActiveDirectoryServicePrincipalExists(dataSourceName), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "application_id"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "object_id"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "display_name"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMAzureADServicePrincipal_byObjectId(t *testing.T) { | ||
dataSourceName := "data.azurerm_azuread_service_principal.test" | ||
id := uuid.New().String() | ||
config := testAccDataSourceAzureRMAzureADServicePrincipal_byObjectId(id) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMActiveDirectoryServicePrincipalExists(dataSourceName), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "application_id"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "object_id"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "display_name"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAzureRMAzureADServicePrincipal_byApplicationId(id string) string { | ||
template := testAccAzureRMActiveDirectoryServicePrincipal_basic(id) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_azuread_service_principal" "test" { | ||
application_id = "${azurerm_azuread_service_principal.test.application_id}" | ||
} | ||
`, template) | ||
} | ||
|
||
func testAccDataSourceAzureRMAzureADServicePrincipal_byDisplayName(id string) string { | ||
template := testAccAzureRMActiveDirectoryServicePrincipal_basic(id) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_azuread_service_principal" "test" { | ||
display_name = "${azurerm_azuread_service_principal.test.display_name}" | ||
} | ||
`, template) | ||
} | ||
|
||
func testAccDataSourceAzureRMAzureADServicePrincipal_byObjectId(id string) string { | ||
template := testAccAzureRMActiveDirectoryServicePrincipal_basic(id) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_azuread_service_principal" "test" { | ||
object_id = "${azurerm_azuread_service_principal.test.id}" | ||
} | ||
`, template) | ||
} |
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
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,55 @@ | ||
--- | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: azurerm_azuread_service_principal" | ||
sidebar_current: "docs-azurerm-datasource-azuread-service-principal" | ||
description: |- | ||
Gets information about a Service Principal associated with an Application within Azure Active Directory. | ||
--- | ||
|
||
# Data Source: azurerm_azuread_service_principal | ||
|
||
Gets information about a Service Principal associated with an Application within Azure Active Directory. | ||
|
||
-> **NOTE:** If you're authenticating using a Service Principal then it must have permissions to both `Read and write all applications` and `Sign in and read user profile` within the `Windows Azure Active Directory` API. | ||
|
||
## Example Usage (by Application Display Name) | ||
|
||
```hcl | ||
data "azurerm_azuread_service_principal" "test" { | ||
display_name = "my-awesome-application" | ||
} | ||
## Example Usage (by Application ID) | ||
```hcl | ||
data "azurerm_azuread_service_principal" "test" { | ||
application_id = "00000000-0000-0000-0000-000000000000" | ||
} | ||
``` | ||
|
||
## Example Usage (by Object ID) | ||
|
||
```hcl | ||
data "azurerm_azuread_service_principal" "test" { | ||
object_id = "00000000-0000-0000-0000-000000000000" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `application_id` - (Optional) The ID of the Azure AD Application for which to create a Service Principal. | ||
|
||
* `object_id` - (Optional) The ID of the Azure AD Service Principal. | ||
|
||
* `display_name` - (Optional) The Display Name of the Azure AD Application associated with this Service Principal. | ||
|
||
-> **NOTE:** At least one of `application_id`, `display_name` or `object_id` must be specified. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - The Object ID for the Service Principal. |