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

New Data Source: azurerm_azuread_application #1552

Merged
merged 2 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
160 changes: 160 additions & 0 deletions azurerm/data_source_azuread_application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
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 dataSourceArmAzureADApplication() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmAzureADApplicationRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
// TODO: customizeDiff for validation of either name or object_id.

Schema: map[string]*schema.Schema{
"object_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"name"},
},

"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"object_id"},
},

"homepage": {
Type: schema.TypeString,
Computed: true,
},

"identifier_uris": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"reply_urls": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"available_to_other_tenants": {
Type: schema.TypeBool,
Computed: true,
},

"oauth2_allow_implicit_flow": {
Type: schema.TypeBool,
Computed: true,
},

"application_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceArmAzureADApplicationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).applicationsClient
ctx := meta.(*ArmClient).StopContext

var application graphrbac.Application

if oId, ok := d.GetOk("object_id"); ok {
objectId := oId.(string)
resp, err := client.Get(ctx, objectId)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: AzureAD Application with ID %q was not found", objectId)
}

return fmt.Errorf("Error making Read request on AzureAD Application with ID %q: %+v", objectId, err)
}

application = resp
} else {
resp, err := client.ListComplete(ctx, "")
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 {
if *v.DisplayName == name {
app = &v
break
}
}
}

if app == nil {
return fmt.Errorf("Couldn't locate an Azure AD Application with a name of %q", name)
}

application = *app
}

d.SetId(*application.ObjectID)

d.Set("object_id", application.ObjectID)
d.Set("name", application.DisplayName)
d.Set("application_id", application.AppID)
d.Set("homepage", application.Homepage)
d.Set("available_to_other_tenants", application.AvailableToOtherTenants)
d.Set("oauth2_allow_implicit_flow", application.Oauth2AllowImplicitFlow)

identifierUris := flattenAzureADDataSourceApplicationIdentifierUris(application.IdentifierUris)
if err := d.Set("identifier_uris", identifierUris); err != nil {
return fmt.Errorf("Error setting `identifier_uris`: %+v", err)
}

replyUrls := flattenAzureADDataSourceApplicationReplyUrls(application.ReplyUrls)
if err := d.Set("reply_urls", replyUrls); err != nil {
return fmt.Errorf("Error setting `reply_urls`: %+v", err)
}

return nil
}

func flattenAzureADDataSourceApplicationIdentifierUris(input *[]string) []string {
output := make([]string, 0)

if input != nil {
for _, v := range *input {
output = append(output, v)
}
}

return output
}

func flattenAzureADDataSourceApplicationReplyUrls(input *[]string) []string {
output := make([]string, 0)

if input != nil {
for _, v := range *input {
output = append(output, v)
}
}

return output
}
123 changes: 123 additions & 0 deletions azurerm/data_source_azuread_application_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package azurerm

import (
"fmt"
"testing"

"github.com/google/uuid"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMAzureADApplication_byObjectId(t *testing.T) {
dataSourceName := "data.azurerm_azuread_application.test"
id := uuid.New().String()
config := testAccDataSourceAzureRMAzureADApplication_objectId(id)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMActiveDirectoryApplicationDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMActiveDirectoryApplicationExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s", id)),
resource.TestCheckResourceAttr(dataSourceName, "homepage", fmt.Sprintf("http://acctest%s", id)),
resource.TestCheckResourceAttr(dataSourceName, "identifier_uris.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "reply_urls.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "oauth2_allow_implicit_flow", "false"),
resource.TestCheckResourceAttrSet(dataSourceName, "application_id"),
),
},
},
})
}

func TestAccDataSourceAzureRMAzureADApplication_byObjectIdComplete(t *testing.T) {
dataSourceName := "data.azurerm_azuread_application.test"
id := uuid.New().String()
config := testAccDataSourceAzureRMAzureADApplication_objectIdComplete(id)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMActiveDirectoryApplicationDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMActiveDirectoryApplicationExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s", id)),
resource.TestCheckResourceAttr(dataSourceName, "homepage", fmt.Sprintf("http://homepage-%s", id)),
resource.TestCheckResourceAttr(dataSourceName, "identifier_uris.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "reply_urls.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "oauth2_allow_implicit_flow", "true"),
resource.TestCheckResourceAttrSet(dataSourceName, "application_id"),
),
},
},
})
}

func TestAccDataSourceAzureRMAzureADApplication_byName(t *testing.T) {
dataSourceName := "data.azurerm_azuread_application.test"
id := uuid.New().String()
config := testAccDataSourceAzureRMAzureADApplication_name(id)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMActiveDirectoryApplicationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMActiveDirectoryApplication_basic(id),
},
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMActiveDirectoryApplicationExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s", id)),
resource.TestCheckResourceAttr(dataSourceName, "homepage", fmt.Sprintf("http://acctest%s", id)),
resource.TestCheckResourceAttr(dataSourceName, "identifier_uris.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "reply_urls.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "oauth2_allow_implicit_flow", "false"),
resource.TestCheckResourceAttrSet(dataSourceName, "application_id"),
),
},
},
})
}

func testAccDataSourceAzureRMAzureADApplication_objectId(id string) string {
template := testAccAzureRMActiveDirectoryApplication_basic(id)
return fmt.Sprintf(`
%s

data "azurerm_azuread_application" "test" {
object_id = "${azurerm_azuread_application.test.id}"
}
`, template)
}

func testAccDataSourceAzureRMAzureADApplication_objectIdComplete(id string) string {
template := testAccAzureRMActiveDirectoryApplication_complete(id)
return fmt.Sprintf(`
%s

data "azurerm_azuread_application" "test" {
object_id = "${azurerm_azuread_application.test.id}"
}
`, template)
}

func testAccDataSourceAzureRMAzureADApplication_name(id string) string {
template := testAccAzureRMActiveDirectoryApplication_basic(id)
return fmt.Sprintf(`
%s

data "azurerm_azuread_application" "test" {
name = "${azurerm_azuread_application.test.name}"
}
`, template)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"azurerm_azuread_application": dataSourceArmAzureADApplication(),
"azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(),
"azurerm_app_service": dataSourceArmAppService(),
"azurerm_app_service_plan": dataSourceAppServicePlan(),
Expand Down
4 changes: 2 additions & 2 deletions azurerm/resource_arm_azuread_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func resourceArmActiveDirectoryApplicationRead(d *schema.ResourceData, meta inte

identifierUris := flattenAzureADApplicationIdentifierUris(resp.IdentifierUris)
if err := d.Set("identifier_uris", identifierUris); err != nil {
return fmt.Errorf("Error setting`identifier_uris`: %+v", err)
return fmt.Errorf("Error setting `identifier_uris`: %+v", err)
}

replyUrls := flattenAzureADApplicationReplyUrls(resp.ReplyUrls)
if err := d.Set("reply_urls", replyUrls); err != nil {
return fmt.Errorf("Error setting`reply_urls`: %+v", err)
return fmt.Errorf("Error setting `reply_urls`: %+v", err)
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<li<%= sidebar_current("docs-azurerm-datasource") %>>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-azurerm-datasource-azuread-application") %>>
<a href="/docs/providers/azurerm/d/azuread_application.html">azurerm_azuread_application</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-network-application-security-group") %>>
<a href="/docs/providers/azurerm/d/application_security_group.html">azurerm_application_security_group</a>
</li>
Expand Down
48 changes: 48 additions & 0 deletions website/docs/d/azurerm_azuread_application.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_azuread_application"
sidebar_current: "docs-azurerm-datasource-azuread-application"
description: |-
Gets information about an Application within Azure Active Directory.
---

# Data Source: azurerm_azuread_application

Gets information about an Application within Azure Active Directory.

## Example Usage

```hcl
data "azurerm_azuread_application" "test" {
name = "My First AzureAD Application"
}

output "azure_active_directory_object_id" {
value = "${data.azurerm_azuread_application.test.id}"
}
```

## Argument Reference

* `object_id` - (Optional) Specifies the Object ID of the Application within Azure Active Directory.

* `name` - (Optional) Specifies the name of the Application within Azure Active Directory.

-> **NOTE:** Either an `object_id` or `name` must be specified.

## Attributes Reference

* `id` - the Object ID of the Azure Active Directory Application.

* `application_id` - the Application ID of the Azure Active Directory Application.

* `available_to_other_tenants` - Is this Azure AD Application available to other tenants?

* `identifier_uris` - A list of user-defined URI(s) that uniquely identify a Web application within it's Azure AD tenant, or within a verified custom domain if the application is multi-tenant.

* `oauth2_allow_implicit_flow` - Does this Azure AD Application allow OAuth2.0 implicit flow tokens?

* `object_id` - the Object ID of the Azure Active Directory Application.

* `reply_urls` - A list of URLs that user tokens are sent to for sign in, or the redirect URIs that OAuth 2.0 authorization codes and access tokens are sent to.