Skip to content

Commit

Permalink
fixing issues raised in code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Mar 6, 2019
1 parent c26bbdb commit 15f3cc2
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 129 deletions.
190 changes: 111 additions & 79 deletions azurerm/resource_arm_container_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

const (
LogAnalyticsWorkSpaceKeyPath = "log_analytics.0.workspace_key"
)

func resourceArmContainerGroup() *schema.Resource {
return &schema.Resource{
Create: resourceArmContainerGroupCreate,
Expand Down Expand Up @@ -283,41 +279,54 @@ func resourceArmContainerGroup() *schema.Resource {
},
},

"log_analytics": {
"diagnostics": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"workspace_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.UUID,
},
"workspace_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},
"log_type": {
Type: schema.TypeString,
"log_analytics": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(containerinstance.ContainerInsights),
string(containerinstance.ContainerInstanceLogs),
}, false),
},
"metadata": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"workspace_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.UUID,
},

"workspace_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"log_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(containerinstance.ContainerInsights),
string(containerinstance.ContainerInstanceLogs),
}, false),
},

"metadata": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
Expand Down Expand Up @@ -363,13 +372,17 @@ func resourceArmContainerGroupCreate(d *schema.ResourceData, meta interface{}) e
tags := d.Get("tags").(map[string]interface{})
restartPolicy := d.Get("restart_policy").(string)

diagnosticsRaw := d.Get("diagnostics").([]interface{})
diagnostics := expandContainerGroupDiagnostics(diagnosticsRaw)

containers, containerGroupPorts, containerGroupVolumes := expandContainerGroupContainers(d)
containerGroup := containerinstance.ContainerGroup{
Name: &name,
Location: &location,
Tags: expandTags(tags),
ContainerGroupProperties: &containerinstance.ContainerGroupProperties{
Containers: containers,
Diagnostics: diagnostics,
RestartPolicy: containerinstance.ContainerGroupRestartPolicy(restartPolicy),
IPAddress: &containerinstance.IPAddress{
Type: containerinstance.ContainerGroupIPAddressType(IPAddressType),
Expand All @@ -385,11 +398,6 @@ func resourceArmContainerGroupCreate(d *schema.ResourceData, meta interface{}) e
containerGroup.ContainerGroupProperties.IPAddress.DNSNameLabel = &dnsNameLabel
}

logList := d.Get("log_analytics").([]interface{})
containerGroup.Diagnostics = &containerinstance.ContainerGroupDiagnostics{
LogAnalytics: expandContainerLogAnalytics(logList),
}

future, err := client.CreateOrUpdate(ctx, resGroup, name, containerGroup)
if err != nil {
return fmt.Errorf("Error creating/updating container group %q (Resource Group %q): %+v", name, resGroup, err)
Expand Down Expand Up @@ -449,7 +457,7 @@ func resourceArmContainerGroupRead(d *schema.ResourceData, meta interface{}) err
}

if err := d.Set("image_registry_credential", flattenContainerImageRegistryCredentials(d, props.ImageRegistryCredentials)); err != nil {
return fmt.Errorf("Error setting `capabilities`: %+v", err)
return fmt.Errorf("Error setting `image_registry_credential`: %+v", err)
}

if address := props.IPAddress; address != nil {
Expand All @@ -462,10 +470,8 @@ func resourceArmContainerGroupRead(d *schema.ResourceData, meta interface{}) err
d.Set("restart_policy", string(props.RestartPolicy))
d.Set("os_type", string(props.OsType))

if diag := props.Diagnostics; diag != nil {
if err := d.Set("log_analytics", flattenContainerLogAnalytics(d, diag.LogAnalytics)); err != nil {
return fmt.Errorf("Error setting `log_analytics`: %+v", err)
}
if err := d.Set("diagnostics", flattenContainerGroupDiagnostics(d, props.Diagnostics)); err != nil {
return fmt.Errorf("Error setting `diagnostics`: %+v", err)
}
}

Expand Down Expand Up @@ -709,30 +715,6 @@ func expandContainerVolumes(input interface{}) (*[]containerinstance.VolumeMount
return &volumeMounts, &containerGroupVolumes
}

func expandContainerLogAnalytics(logList []interface{}) *containerinstance.LogAnalytics {
if len(logList) <= 0 {
return nil
}

log := logList[0].(map[string]interface{})
ws_id := log["workspace_id"].(string)
ws_key := log["workspace_key"].(string)
log_type := log["log_type"].(string)
metadataMap := log["metadata"].(map[string]interface{})
metadata := make(map[string]*string)
for k, v := range metadataMap {
strValue := v.(string)
metadata[k] = &strValue
}

return &containerinstance.LogAnalytics{
WorkspaceID: &ws_id,
WorkspaceKey: &ws_key,
LogType: containerinstance.LogAnalyticsLogType(log_type),
Metadata: metadata,
}
}

func flattenContainerImageRegistryCredentials(d *schema.ResourceData, input *[]containerinstance.ImageRegistryCredential) []interface{} {
if input == nil {
return nil
Expand Down Expand Up @@ -962,30 +944,80 @@ func flattenContainerVolumes(volumeMounts *[]containerinstance.VolumeMount, cont
return volumeConfigs
}

func flattenContainerLogAnalytics(d *schema.ResourceData, input *containerinstance.LogAnalytics) []interface{} {
if input == nil {
return []interface{}{}
func expandContainerGroupDiagnostics(input []interface{}) *containerinstance.ContainerGroupDiagnostics {
if len(input) == 0 {
return nil
}

log := make(map[string]interface{})
vs := input[0].(map[string]interface{})

analyticsVs := vs["log_analytics"].([]interface{})
analyticsV := analyticsVs[0].(map[string]interface{})

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
}

if input.WorkspaceID != nil {
log["workspace_id"] = *input.WorkspaceID
return &containerinstance.ContainerGroupDiagnostics{
LogAnalytics: &containerinstance.LogAnalytics{
WorkspaceID: utils.String(workspaceId),
WorkspaceKey: utils.String(workspaceKey),
LogType: logType,
Metadata: metadata,
},
}
}

if v, ok := d.GetOk(LogAnalyticsWorkSpaceKeyPath); ok {
log["workspace_key"] = v
func flattenContainerGroupDiagnostics(d *schema.ResourceData, input *containerinstance.ContainerGroupDiagnostics) []interface{} {
if input == nil {
return []interface{}{}
}

log["log_type"] = input.LogType
logAnalytics := make([]interface{}, 0)

if la := input.LogAnalytics; la != nil {
output := make(map[string]interface{})

output["log_type"] = string(la.LogType)

metadata := make(map[string]interface{})
for k, v := range input.Metadata {
metadata[k] = *v
metadata := make(map[string]interface{})
for k, v := range la.Metadata {
metadata[k] = *v
}
output["metadata"] = metadata

if la.WorkspaceID != nil {
output["workspace_id"] = *la.WorkspaceID
}

// the existing config may not exist at Import time, protect against it.
workspaceKey := ""
if existingDiags := d.Get("diagnostics").([]interface{}); len(existingDiags) > 0 {
existingDiag := existingDiags[0].(map[string]interface{})
if existingLA := existingDiag["log_analytics"].([]interface{}); len(existingLA) > 0 {
vs := existingLA[0].(map[string]interface{})
if key := vs["workspace_key"]; key != nil && key.(string) != "" {
workspaceKey = key.(string)
}
}
}
output["workspace_key"] = workspaceKey

logAnalytics = append(logAnalytics, output)
}
log["metadata"] = metadata

return []interface{}{log}
return []interface{}{
map[string]interface{}{
"log_analytics": logAnalytics,
},
}
}

func resourceArmContainerGroupPortsHash(v interface{}) int {
Expand Down
51 changes: 27 additions & 24 deletions azurerm/resource_arm_container_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"testing"

"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
Expand Down Expand Up @@ -218,11 +217,11 @@ func TestAccAzureRMContainerGroup_linuxComplete(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "container.0.volume.0.read_only", "false"),
resource.TestCheckResourceAttr(resourceName, "os_type", "Linux"),
resource.TestCheckResourceAttr(resourceName, "restart_policy", "OnFailure"),
resource.TestCheckResourceAttr(resourceName, "log_analytics.#", "1"),
resource.TestCheckResourceAttr(resourceName, "log_analytics.0.log_type", string(containerinstance.ContainerInsights)),
resource.TestCheckResourceAttr(resourceName, "log_analytics.0.metadata.%", "1"),
resource.TestCheckResourceAttrSet(resourceName, "log_analytics.0.workspace_id"),
resource.TestCheckResourceAttrSet(resourceName, "log_analytics.0.workspace_key"),
resource.TestCheckResourceAttr(resourceName, "diagnostics.0.log_analytics.#", "1"),
resource.TestCheckResourceAttr(resourceName, "diagnostics.0.log_analytics.0.log_type", "ContainerInsights"),
resource.TestCheckResourceAttr(resourceName, "diagnostics.0.log_analytics.0.metadata.%", "1"),
resource.TestCheckResourceAttrSet(resourceName, "diagnostics.0.log_analytics.0.workspace_id"),
resource.TestCheckResourceAttrSet(resourceName, "diagnostics.0.log_analytics.0.workspace_key"),
),
},
{
Expand All @@ -234,7 +233,7 @@ func TestAccAzureRMContainerGroup_linuxComplete(t *testing.T) {
"container.0.secure_environment_variables.%",
"container.0.secure_environment_variables.secureFoo",
"container.0.secure_environment_variables.secureFoo1",
"log_analytics.0.workspace_key",
"diagnostics.0.log_analytics.0.workspace_key",
},
},
},
Expand Down Expand Up @@ -300,11 +299,11 @@ func TestAccAzureRMContainerGroup_windowsComplete(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "container.0.secure_environment_variables.secureFoo1", "secureBar1"),
resource.TestCheckResourceAttr(resourceName, "os_type", "Windows"),
resource.TestCheckResourceAttr(resourceName, "restart_policy", "Never"),
resource.TestCheckResourceAttr(resourceName, "log_analytics.#", "1"),
resource.TestCheckResourceAttr(resourceName, "log_analytics.0.log_type", string(containerinstance.ContainerInsights)),
resource.TestCheckResourceAttr(resourceName, "log_analytics.0.metadata.%", "1"),
resource.TestCheckResourceAttrSet(resourceName, "log_analytics.0.workspace_id"),
resource.TestCheckResourceAttrSet(resourceName, "log_analytics.0.workspace_key"),
resource.TestCheckResourceAttr(resourceName, "diagnostics.0.log_analytics.#", "1"),
resource.TestCheckResourceAttr(resourceName, "diagnostics.0.log_analytics.0.log_type", "ContainerInsights"),
resource.TestCheckResourceAttr(resourceName, "diagnostics.0.log_analytics.0.metadata.%", "1"),
resource.TestCheckResourceAttrSet(resourceName, "diagnostics.0.log_analytics.0.workspace_id"),
resource.TestCheckResourceAttrSet(resourceName, "diagnostics.0.log_analytics.0.workspace_key"),
),
},
{
Expand All @@ -315,7 +314,7 @@ func TestAccAzureRMContainerGroup_windowsComplete(t *testing.T) {
"container.0.secure_environment_variables.%",
"container.0.secure_environment_variables.secureFoo",
"container.0.secure_environment_variables.secureFoo1",
"log_analytics.0.workspace_key",
"diagnostics.0.log_analytics.0.workspace_key",
},
},
},
Expand Down Expand Up @@ -608,12 +607,14 @@ resource "azurerm_container_group" "test" {
commands = ["cmd.exe", "echo", "hi"]
}
log_analytics {
workspace_id = "${azurerm_log_analytics_workspace.test.workspace_id}"
workspace_key = "${azurerm_log_analytics_workspace.test.primary_shared_key}"
log_type = "ContainerInsights"
metadata {
"node-name" = "acctestContainerGroup"
diagnostics {
log_analytics {
workspace_id = "${azurerm_log_analytics_workspace.test.workspace_id}"
workspace_key = "${azurerm_log_analytics_workspace.test.primary_shared_key}"
log_type = "ContainerInsights"
metadata {
"node-name" = "acctestContainerGroup"
}
}
}
Expand Down Expand Up @@ -711,12 +712,14 @@ resource "azurerm_container_group" "test" {
commands = ["/bin/bash", "-c", "ls"]
}
diagnostics {
log_analytics {
workspace_id = "${azurerm_log_analytics_workspace.test.workspace_id}"
workspace_key = "${azurerm_log_analytics_workspace.test.primary_shared_key}"
log_type = "ContainerInsights"
metadata {
"node-name" = "acctestContainerGroup"
workspace_id = "${azurerm_log_analytics_workspace.test.workspace_id}"
workspace_key = "${azurerm_log_analytics_workspace.test.primary_shared_key}"
log_type = "ContainerInsights"
metadata {
"node-name" = "acctestContainerGroup"
}
}
}
Expand Down
Loading

0 comments on commit 15f3cc2

Please sign in to comment.