Skip to content

Commit

Permalink
Merge pull request #914 from terraform-providers/data-source-app-secu…
Browse files Browse the repository at this point in the history
…rity-groups

New Data Source: `azurerm_application_security_group`
  • Loading branch information
tombuildsstuff authored Mar 1, 2018
2 parents 20f9746 + 578e40e commit 2f5bd9e
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 19 deletions.
57 changes: 57 additions & 0 deletions azurerm/data_source_application_security_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmApplicationSecurityGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmApplicationSecurityGroupRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"location": locationForDataSourceSchema(),

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"tags": tagsForDataSourceSchema(),
},
}
}

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

resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("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 Application Security Group %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
d.Set("resource_group_name", resourceGroup)
flattenAndSetTags(d, resp.Tags)

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

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMApplicationSecurityGroup_basic(t *testing.T) {
dataSourceName := "data.azurerm_application_security_group.test"
ri := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceApplicationSecurityGroup_basic(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "location"),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
),
},
},
})
}

func TestAccDataSourceAzureRMApplicationSecurityGroup_complete(t *testing.T) {
dataSourceName := "data.azurerm_application_security_group.test"
ri := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceApplicationSecurityGroup_complete(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "location"),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "tags.Hello", "World"),
),
},
},
})
}

func testAccDataSourceApplicationSecurityGroup_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_application_security_group" "test" {
name = "acctest-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
data "azurerm_application_security_group" "test" {
name = "${azurerm_application_security_group.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt)
}

func testAccDataSourceApplicationSecurityGroup_complete(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_application_security_group" "test" {
name = "acctest-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
"Hello" = "World"
}
}
data "azurerm_application_security_group" "test" {
name = "${azurerm_application_security_group.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt)
}
39 changes: 20 additions & 19 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,26 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(),
"azurerm_client_config": dataSourceArmClientConfig(),
"azurerm_dns_zone": dataSourceArmDnsZone(),
"azurerm_eventhub_namespace": dataSourceEventHubNamespace(),
"azurerm_image": dataSourceArmImage(),
"azurerm_key_vault_access_policy": dataSourceArmKeyVaultAccessPolicy(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
"azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(),
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_resource_group": dataSourceArmResourceGroup(),
"azurerm_role_definition": dataSourceArmRoleDefinition(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_snapshot": dataSourceArmSnapshot(),
"azurerm_subnet": dataSourceArmSubnet(),
"azurerm_subscription": dataSourceArmSubscription(),
"azurerm_virtual_network": dataSourceArmVirtualNetwork(),
"azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(),
"azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(),
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(),
"azurerm_client_config": dataSourceArmClientConfig(),
"azurerm_dns_zone": dataSourceArmDnsZone(),
"azurerm_eventhub_namespace": dataSourceEventHubNamespace(),
"azurerm_image": dataSourceArmImage(),
"azurerm_key_vault_access_policy": dataSourceArmKeyVaultAccessPolicy(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
"azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(),
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_resource_group": dataSourceArmResourceGroup(),
"azurerm_role_definition": dataSourceArmRoleDefinition(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_snapshot": dataSourceArmSnapshot(),
"azurerm_subnet": dataSourceArmSubnet(),
"azurerm_subscription": dataSourceArmSubscription(),
"azurerm_virtual_network": dataSourceArmVirtualNetwork(),
"azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(),
},

ResourcesMap: map[string]*schema.Resource{
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-network-application-security-group") %>>
<a href="/docs/providers/azurerm/d/application_security_group.html">azurerm_application_security_group</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-app-service-plan") %>>
<a href="/docs/providers/azurerm/d/app_service_plan.html">azurerm_app_service_plan</a>
</li>
Expand Down
44 changes: 44 additions & 0 deletions website/docs/d/application_security_group.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_application_security_group"
sidebar_current: "docs-azurerm-datasource-network-application-security-group"
description: |-
Get information about an Application Security Group.
---

# Data Source: azurerm_application_security_group

Get information about an Application Security Group.

-> **Note:** Application Security Groups are currently in Public Preview on an opt-in basis. [More information, including how you can register for the Preview, and which regions Application Security Groups are available in are available here](https://docs.microsoft.com/en-us/azure/virtual-network/create-network-security-group-preview)

## Example Usage

```hcl
data "azurerm_application_security_group" "test" {
name = "tf-appsecuritygroup"
resource_group_name = "my-resource-group"
}
output "application_security_group_id" {
value = "${data.azurerm_application_security_group.test.id}"
}
```

## Argument Reference

The following arguments are supported:

* `name` - The name of the Application Security Group.

* `resource_group_name` - The name of the resource group in which the Application Security Group exists.

## Attributes Reference

The following attributes are exported:

* `id` - The ID of the Application Security Group.

* `location` - The supported Azure location where the Application Security Group exists.

* `tags` - A mapping of tags to assign to the resource.

0 comments on commit 2f5bd9e

Please sign in to comment.