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 16 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 {
// Monitor
monitorActionGroupsClient insights.ActionGroupsClient
monitorAlertRulesClient insights.AlertRulesClient
monitorLogProfilesClient insights.LogProfilesClient

// MSI
userAssignedIdentitiesClient msi.UserAssignedIdentitiesClient
Expand Down Expand Up @@ -850,6 +851,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

autoscaleSettingsClient := insights.NewAutoscaleSettingsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&autoscaleSettingsClient.Client, auth)
c.autoscaleSettingsClient = autoscaleSettingsClient
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
}
168 changes: 168 additions & 0 deletions azurerm/data_source_monitor_log_profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package azurerm

import (
"fmt"
"testing"

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

func TestAccDataSourceAzureRMMonitorLogProfile(t *testing.T) {
// NOTE: this is a combined test rather than separate split out tests due to
// Azure only being happy about provisioning one per subscription at once
// (which our test suite can't easily workaround)
testCases := map[string]map[string]func(t *testing.T){
"basic": {
"eventhub": testAccDataSourceAzureRMMonitorLogProfile_eventhub,
"storageaccount": testAccDataSourceAzureRMMonitorLogProfile_storageaccount,
},
}

for group, m := range testCases {
m := m
t.Run(group, func(t *testing.T) {
for name, tc := range m {
tc := tc
t.Run(name, func(t *testing.T) {
tc(t)
})
}
})
}
}

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" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor and not a blocker, but could we left align theres:

	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 @@ -215,6 +216,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_management_group": resourceArmManagementGroup(),
"azurerm_metric_alertrule": resourceArmMetricAlertRule(),
"azurerm_monitor_action_group": resourceArmMonitorActionGroup(),
"azurerm_monitor_log_profile": resourceArmMonitorLogProfile(),
"azurerm_mysql_configuration": resourceArmMySQLConfiguration(),
"azurerm_mysql_database": resourceArmMySqlDatabase(),
"azurerm_mysql_firewall_rule": resourceArmMySqlFirewallRule(),
Expand Down
Loading