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 Resource: azurerm_log_profile #1792

Merged
merged 20 commits into from
Oct 18, 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
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ type ArmClient struct {
monitorActionGroupsClient insights.ActionGroupsClient
monitorActivityLogAlertsClient insights.ActivityLogAlertsClient
monitorAlertRulesClient insights.AlertRulesClient
monitorLogProfilesClient insights.LogProfilesClient
monitorMetricAlertsClient insights.MetricAlertsClient

// MSI
Expand Down Expand Up @@ -856,6 +857,10 @@ func (c *ArmClient) registerMonitorClients(endpoint, subscriptionId string, auth
c.configureClient(&arc.Client, auth)
c.monitorAlertRulesClient = arc

monitorLogProfilesClient := insights.NewLogProfilesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&monitorLogProfilesClient.Client, auth)
c.monitorLogProfilesClient = monitorLogProfilesClient

mac := insights.NewMetricAlertsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&mac.Client, auth)
c.monitorMetricAlertsClient = mac
Expand Down
91 changes: 91 additions & 0 deletions azurerm/data_source_monitor_log_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package azurerm

import (
"fmt"

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

func dataSourceArmMonitorLogProfile() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmLogProfileRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"storage_account_id": {
Type: schema.TypeString,
Computed: true,
},
"servicebus_rule_id": {
Type: schema.TypeString,
Computed: true,
},
"locations": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"categories": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"retention_policy": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"days": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}

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

name := d.Get("name").(string)
resp, err := client.Get(ctx, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Log Profile %q was not found", name)
}
return fmt.Errorf("Error reading Log Profile: %+v", err)
}

d.SetId(*resp.ID)

if props := resp.LogProfileProperties; props != nil {
d.Set("storage_account_id", props.StorageAccountID)
d.Set("servicebus_rule_id", props.ServiceBusRuleID)
d.Set("categories", props.Categories)

if err := d.Set("locations", flattenAzureRmLogProfileLocations(props.Locations)); err != nil {
return fmt.Errorf("Error flattening `locations`: %+v", err)
}

if err := d.Set("retention_policy", flattenAzureRmLogProfileRetentionPolicy(props.RetentionPolicy)); err != nil {
return fmt.Errorf("Error flattening `retention_policy`: %+v", err)
}
}

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

import (
"fmt"
"testing"

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

// These tests are actualy run as part of the resoure ones due to
// Azure only being happy about provisioning one per subscription at once
// (which our test suite can't easily workaround)

func testAccDataSourceAzureRMMonitorLogProfile_storageaccount(t *testing.T) {
dataSourceName := "data.azurerm_monitor_log_profile.test"
ri := acctest.RandInt()
rs := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogProfileDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMMonitorLogProfile_storageaccountConfig(ri, rs, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "categories.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "locations.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "storage_account_id"),
resource.TestCheckResourceAttr(dataSourceName, "servicebus_rule_id", ""),
resource.TestCheckResourceAttr(dataSourceName, "retention_policy.#", "1"),
resource.TestCheckResourceAttrSet(dataSourceName, "retention_policy.0.enabled"),
resource.TestCheckResourceAttrSet(dataSourceName, "retention_policy.0.days"),
),
},
},
})
}

func testAccDataSourceAzureRMMonitorLogProfile_eventhub(t *testing.T) {
dataSourceName := "data.azurerm_monitor_log_profile.test"
ri := acctest.RandInt()
rs := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogProfileDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMMonitorLogProfile_eventhubConfig(ri, rs, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "categories.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "locations.#"),
resource.TestCheckResourceAttr(dataSourceName, "storage_account_id", ""),
resource.TestCheckResourceAttrSet(dataSourceName, "servicebus_rule_id"),
resource.TestCheckResourceAttr(dataSourceName, "retention_policy.#", "1"),
resource.TestCheckResourceAttrSet(dataSourceName, "retention_policy.0.enabled"),
resource.TestCheckResourceAttrSet(dataSourceName, "retention_policy.0.days"),
),
},
},
})
}

func testAccDataSourceAzureRMMonitorLogProfile_storageaccountConfig(rInt int, rString string, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
location = "%s"
}

resource "azurerm_storage_account" "test" {
name = "acctestsa%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
account_tier = "Standard"
account_replication_type = "GRS"
}

resource "azurerm_monitor_log_profile" "test" {
name = "acctestlp-%d"

categories = [
"Action",
]

locations = [
"%s"
]

storage_account_id = "${azurerm_storage_account.test.id}"

retention_policy {
enabled = true
days = 7
}
}

data "azurerm_monitor_log_profile" "test" {
name = "${azurerm_monitor_log_profile.test.name}"
}
`, rInt, location, rString, rInt, location)
}

func testAccDataSourceAzureRMMonitorLogProfile_eventhubConfig(rInt int, rString string, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
location = "%s"
}

resource "azurerm_eventhub_namespace" "test" {
name = "acctestehns-%s"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Standard"
capacity = 2
}

resource "azurerm_monitor_log_profile" "test" {
name = "acctestlp-%d"

categories = [
"Action",
]

locations = [
"%s"
]

# RootManageSharedAccessKey is created by default with listen, send, manage permissions
servicebus_rule_id = "${azurerm_eventhub_namespace.test.id}/authorizationrules/RootManageSharedAccessKey"

retention_policy {
enabled = true
days = 7
}
}

data "azurerm_monitor_log_profile" "test" {
name = "${azurerm_monitor_log_profile.test.name}"
}
`, rInt, location, rString, rInt, location)
}
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_logic_app_workflow": dataSourceArmLogicAppWorkflow(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
"azurerm_management_group": dataSourceArmManagementGroup(),
"azurerm_monitor_log_profile": dataSourceArmMonitorLogProfile(),
"azurerm_network_interface": dataSourceArmNetworkInterface(),
"azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(),
"azurerm_notification_hub": dataSourceNotificationHub(),
Expand Down Expand Up @@ -216,6 +217,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_metric_alertrule": resourceArmMetricAlertRule(),
"azurerm_monitor_action_group": resourceArmMonitorActionGroup(),
"azurerm_monitor_activity_log_alert": resourceArmMonitorActivityLogAlert(),
"azurerm_monitor_log_profile": resourceArmMonitorLogProfile(),
"azurerm_monitor_metric_alert": resourceArmMonitorMetricAlert(),
"azurerm_mysql_configuration": resourceArmMySQLConfiguration(),
"azurerm_mysql_database": resourceArmMySqlDatabase(),
Expand Down
Loading