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

Use filter for azuread_service_principal and azuread_application data sources #1862

Merged
merged 3 commits into from
Sep 7, 2018
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
15 changes: 11 additions & 4 deletions azurerm/data_source_azuread_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ 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"
"log"
)

func dataSourceArmAzureADApplication() *schema.Resource {
Expand Down Expand Up @@ -73,6 +73,8 @@ func dataSourceArmAzureADApplicationRead(d *schema.ResourceData, meta interface{
var application graphrbac.Application

if oId, ok := d.GetOk("object_id"); ok {

// use the object_id to find the Azure AD application
objectId := oId.(string)
resp, err := client.Get(ctx, objectId)
if err != nil {
Expand All @@ -85,13 +87,18 @@ func dataSourceArmAzureADApplicationRead(d *schema.ResourceData, meta interface{

application = resp
} else {
resp, err := client.ListComplete(ctx, "")

// use the name to find the Azure AD application
name := d.Get("name").(string)
filter := fmt.Sprintf("displayName eq '%s'", name)
log.Printf("[DEBUG] [data_source_azuread_application] Using filter %q", filter)

resp, err := client.ListComplete(ctx, filter)

if err != nil {
return fmt.Errorf("Error listing Azure AD Applications: %+v", err)
}

name := d.Get("name").(string)

var app *graphrbac.Application
for _, v := range *resp.Response().Value {
if v.DisplayName != nil {
Expand Down
78 changes: 47 additions & 31 deletions azurerm/data_source_azuread_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ 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"
"log"
)

func dataSourceArmActiveDirectoryServicePrincipal() *schema.Resource {
Expand Down Expand Up @@ -44,6 +44,8 @@ func dataSourceArmActiveDirectoryServicePrincipalRead(d *schema.ResourceData, me
var servicePrincipal *graphrbac.ServicePrincipal

if v, ok := d.GetOk("object_id"); ok {

//use the object_id to find the Azure AD service principal
objectId := v.(string)
app, err := client.Get(ctx, objectId)
if err != nil {
Expand All @@ -55,47 +57,61 @@ func dataSourceArmActiveDirectoryServicePrincipalRead(d *schema.ResourceData, me
}

servicePrincipal = &app
} else {
apps, err := client.ListComplete(ctx, "")

} else if _, ok := d.GetOk("display_name"); ok {

// use the display_name to find the Azure AD service principal
displayName := d.Get("display_name").(string)
filter := fmt.Sprintf("displayName eq '%s'", displayName)
log.Printf("[DEBUG] [data_source_azuread_service_principal] Using filter %q", filter)

apps, err := client.ListComplete(ctx, filter)
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
}
for _, app := range *apps.Response().Value {
if app.DisplayName == nil {
continue
}

if servicePrincipal == nil {
return fmt.Errorf("A Service Principal with the Display Name %q was not found", displayName)
if *app.DisplayName == displayName {
servicePrincipal = &app
break
}
} 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 with the Display Name %q was not found", displayName)
}

} else {

// use the application_id to find the Azure AD service principal
applicationId := d.Get("application_id").(string)
filter := fmt.Sprintf("appId eq '%s'", applicationId)
log.Printf("[DEBUG] [data_source_azuread_service_principal] Using filter %q", filter)

apps, err := client.ListComplete(ctx, filter)
if err != nil {
return fmt.Errorf("Error listing Service Principals: %+v", err)
}

for _, app := range *apps.Response().Value {
if app.AppID == nil {
continue
}

if servicePrincipal == nil {
return fmt.Errorf("A Service Principal for Application ID %q was not found", applicationId)
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)
Expand Down