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

Added new data source azurerm_network_ddos_protection_plan #4228

Merged
merged 11 commits into from
Sep 5, 2019
Merged
69 changes: 69 additions & 0 deletions azurerm/data_source_network_ddos_protection_plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceNetworkDDoSProtectionPlan() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkDDoSProtectionPlanRead,

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

"location": azure.SchemaLocationForDataSource(),

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

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

"tags": tags.Schema(),
},
}
}

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

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

plan, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(plan.Response) {
return fmt.Errorf("Error DDoS Protection Plan %q (Resource Group %q) was not found", name, resourceGroup)
}

return fmt.Errorf("Error making Read request on DDoS Protection Plan %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.Set("name", plan.Name)
d.Set("resource_group_name", resourceGroup)
if location := plan.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if props := plan.DdosProtectionPlanPropertiesFormat; props != nil {
vNetIDs := flattenArmNetworkDDoSProtectionPlanVirtualNetworkIDs(props.VirtualNetworks)
if err := d.Set("virtual_network_ids", vNetIDs); err != nil {
return fmt.Errorf("Error setting `virtual_network_ids`: %+v", err)
}
}

return tags.FlattenAndSet(d, plan.Tags)
}
41 changes: 41 additions & 0 deletions azurerm/data_source_network_ddos_protection_plan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func testAccAzureRMNetworkDDoSProtectionPlanDataSource_basic(t *testing.T) {
dsn := "azurerm_network_ddos_protection_plan.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNetworkDDoSProtectionPlanDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMNetworkDDoSProtectionPlanDataSource_basicConfig(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkDDoSProtectionPlanExists(dsn),
resource.TestCheckResourceAttrSet(dsn, "virtual_network_ids.#"),
),
},
},
})
}

func testAccAzureRMNetworkDDoSProtectionPlanDataSource_basicConfig(rInt int, location string) string {
return fmt.Sprintf(`
%s

data "azurerm_network_ddos_protection_plan" "test" {
name = "${azurerm_network_ddos_protection_plan.test.name}"
resource_group_name = "${azurerm_network_ddos_protection_plan.test.resource_group_name}"
}
`, testAccAzureRMNetworkDDoSProtectionPlan_basicConfig(rInt, location))
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_monitor_diagnostic_categories": dataSourceArmMonitorDiagnosticCategories(),
"azurerm_monitor_log_profile": dataSourceArmMonitorLogProfile(),
"azurerm_mssql_elasticpool": dataSourceArmMsSqlElasticpool(),
"azurerm_network_ddos_protection_plan": dataSourceNetworkDDoSProtectionPlan(),
"azurerm_network_interface": dataSourceArmNetworkInterface(),
"azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(),
"azurerm_network_watcher": dataSourceArmNetworkWatcher(),
Expand Down
3 changes: 3 additions & 0 deletions azurerm/resource_arm_network_ddos_protection_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func TestAccAzureRMNetworkDDoSProtectionPlan(t *testing.T) {
"withTags": testAccAzureRMNetworkDDoSProtectionPlan_withTags,
"disappears": testAccAzureRMNetworkDDoSProtectionPlan_disappears,
},
"datasource": {
"basic": testAccAzureRMNetworkDDoSProtectionPlanDataSource_basic,
},
"deprecated": {
"basic": testAccAzureRMDDoSProtectionPlan_basic,
"requiresImport": testAccAzureRMDDoSProtectionPlan_requiresImport,
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@
<a href="/docs/providers/azurerm/d/mssql_elasticpool.html">azurerm_mssql_elasticpool</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/network_ddos_protection_plan.html">azurerm_network_ddos_protection_plan</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/network_interface.html">azurerm_network_interface</a>
</li>
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/network_ddos_protection_plan.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_network_ddos_protection_plan"
sidebar_current: "docs-azurerm-datasource-network-ddos-protection-plan-x"
description: |-
Use this data source to access information about an existing Azure Network DDoS Protection Plan.

---

# Data Source: azurerm_network_ddos_protection_plan

Use this data source to access information about an existing Azure Network DDoS Protection Plan.

## Example Usage

```hcl
data "azurerm_network_ddos_protection_plan" "example" {
name = "${azurerm_network_ddos_protection_plan.example.name}"
resource_group_name = "${azurerm_network_ddos_protection_plan.example.resource_group_name}"
}

output "ddos_protection_plan_id" {
value = "${data.azurerm_network_ddos_protection_plan.example.id}"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the Network DDoS Protection Plan.

* `resource_group_name` - (Required) The name of the resource group where the Network DDoS Protection Plan exists.

## Attributes Reference

The following attributes are exported:

* `id` - The Resource ID of the DDoS Protection Plan

* `location` - Specifies the supported Azure location where the resource exists.

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

* `virtual_network_ids` - The Resource ID list of the Virtual Networks associated with DDoS Protection Plan.
2 changes: 1 addition & 1 deletion website/docs/r/network_ddos_protection_plan.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description: |-

Manages an AzureNetwork DDoS Protection Plan.

-> **NOTE** Azure only allow `one` DDoS Protection Plan per region.
-> **NOTE** Azure only allows `one` DDoS Protection Plan per region.

## Example Usage

Expand Down