Skip to content

Commit

Permalink
Merge pull request #4013 from miikka/optional-param
Browse files Browse the repository at this point in the history
azurerm_container_group - allow empty log_type
  • Loading branch information
tombuildsstuff committed Aug 8, 2019
2 parents 1ecb935 + 2a88977 commit d122aae
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 15 deletions.
31 changes: 17 additions & 14 deletions azurerm/resource_arm_container_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func resourceArmContainerGroup() *schema.Resource {

"log_type": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(containerinstance.ContainerInsights),
Expand Down Expand Up @@ -1362,23 +1362,26 @@ func expandContainerGroupDiagnostics(input []interface{}) *containerinstance.Con

workspaceId := analyticsV["workspace_id"].(string)
workspaceKey := analyticsV["workspace_key"].(string)
logType := containerinstance.LogAnalyticsLogType(analyticsV["log_type"].(string))

metadataMap := analyticsV["metadata"].(map[string]interface{})
metadata := make(map[string]*string)
for k, v := range metadataMap {
strValue := v.(string)
metadata[k] = &strValue
logAnalytics := containerinstance.LogAnalytics{
WorkspaceID: utils.String(workspaceId),
WorkspaceKey: utils.String(workspaceKey),
}

return &containerinstance.ContainerGroupDiagnostics{
LogAnalytics: &containerinstance.LogAnalytics{
WorkspaceID: utils.String(workspaceId),
WorkspaceKey: utils.String(workspaceKey),
LogType: logType,
Metadata: metadata,
},
if logType := analyticsV["log_type"].(string); logType != "" {
logAnalytics.LogType = containerinstance.LogAnalyticsLogType(logType)

metadataMap := analyticsV["metadata"].(map[string]interface{})
metadata := make(map[string]*string)
for k, v := range metadataMap {
strValue := v.(string)
metadata[k] = &strValue
}

logAnalytics.Metadata = metadata
}

return &containerinstance.ContainerGroupDiagnostics{LogAnalytics: &logAnalytics}
}

func flattenContainerGroupDiagnostics(d *schema.ResourceData, input *containerinstance.ContainerGroupDiagnostics) []interface{} {
Expand Down
75 changes: 75 additions & 0 deletions azurerm/resource_arm_container_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,38 @@ func TestAccAzureRMContainerGroup_imageRegistryCredentialsUpdate(t *testing.T) {
})
}

func TestAccAzureRMContainerGroup_logTypeUnset(t *testing.T) {
resourceName := "azurerm_container_group.test"
ri := tf.AccRandTimeInt()
config := testAccAzureRMContainerGroup_logTypeUnset(ri, testLocation())
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMContainerGroupDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMContainerGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "diagnostics.0.log_analytics.#", "1"),
resource.TestCheckResourceAttr(resourceName, "diagnostics.0.log_analytics.0.log_type", ""),
resource.TestCheckResourceAttr(resourceName, "diagnostics.0.log_analytics.0.metadata.%", "0"),
resource.TestCheckResourceAttrSet(resourceName, "diagnostics.0.log_analytics.0.workspace_id"),
resource.TestCheckResourceAttrSet(resourceName, "diagnostics.0.log_analytics.0.workspace_key"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"diagnostics.0.log_analytics.0.workspace_key",
},
},
},
})
}

func TestAccAzureRMContainerGroup_linuxBasic(t *testing.T) {
resourceName := "azurerm_container_group.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -747,6 +779,49 @@ resource "azurerm_container_group" "test" {
`, ri, location, ri)
}

func testAccAzureRMContainerGroup_logTypeUnset(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_log_analytics_workspace" "test" {
name = "acctestLAW-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "PerGB2018"
}
resource "azurerm_container_group" "test" {
name = "acctestcontainergroup-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
ip_address_type = "public"
os_type = "Linux"
container {
name = "hw"
image = "microsoft/aci-helloworld:latest"
cpu = "0.5"
memory = "0.5"
port = 80
}
diagnostics {
log_analytics {
workspace_id = "${azurerm_log_analytics_workspace.test.workspace_id}"
workspace_key = "${azurerm_log_analytics_workspace.test.primary_shared_key}"
}
}
tags = {
environment = "Testing"
}
}
`, ri, location, ri, ri)
}

func testAccAzureRMContainerGroup_linuxBasicUpdated(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/container_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ A `image_registry_credential` block supports:

A `log_analytics` block supports:

* `log_type` - (Required) The log type which should be used. Possible values are `ContainerInsights` and `ContainerInstanceLogs`. Changing this forces a new resource to be created.
* `log_type` - (Optional) The log type which should be used. Possible values are `ContainerInsights` and `ContainerInstanceLogs`. Changing this forces a new resource to be created.

* `workspace_id` - (Required) The Workspace ID of the Log Analytics Workspace. Changing this forces a new resource to be created.

Expand Down

0 comments on commit d122aae

Please sign in to comment.