-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1792 from liemnotliam/log-profile
New Resource: azurerm_log_profile
- Loading branch information
Showing
9 changed files
with
1,053 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.