Skip to content

Commit

Permalink
Merge pull request #1792 from liemnotliam/log-profile
Browse files Browse the repository at this point in the history
New Resource: azurerm_log_profile
  • Loading branch information
katbyte authored Oct 18, 2018
2 parents 3302f1c + cb5471a commit 1aef180
Show file tree
Hide file tree
Showing 9 changed files with 1,053 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ type ArmClient struct {
monitorActionGroupsClient insights.ActionGroupsClient
monitorActivityLogAlertsClient insights.ActivityLogAlertsClient
monitorAlertRulesClient insights.AlertRulesClient
monitorLogProfilesClient insights.LogProfilesClient
monitorMetricAlertsClient insights.MetricAlertsClient

// MSI
Expand Down Expand Up @@ -871,6 +872,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 @@ -219,6 +220,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

0 comments on commit 1aef180

Please sign in to comment.