From 3dbfe6454af73edc593a9e2311b1703bd2379f5d Mon Sep 17 00:00:00 2001
From: magodo <wztdyl@sina.com>
Date: Wed, 6 Jul 2022 10:34:40 +0800
Subject: [PATCH 1/2] `azurerm_container_group` - Add supports of
 `http_headers`

---
 .../containers/container_group_resource.go    | 48 +++++++++++++++++--
 .../container_group_resource_test.go          | 12 +++++
 internal/services/containers/probe.go         |  8 ++++
 website/docs/r/container_group.html.markdown  |  2 +
 4 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/internal/services/containers/container_group_resource.go b/internal/services/containers/container_group_resource.go
index a30d9734acd6..afb64ddb522f 100644
--- a/internal/services/containers/container_group_resource.go
+++ b/internal/services/containers/container_group_resource.go
@@ -5,6 +5,7 @@ import (
 	"context"
 	"fmt"
 	"log"
+	"sort"
 	"strings"
 	"time"
 
@@ -1477,9 +1478,10 @@ func expandContainerProbe(input interface{}) *containerinstance.ContainerProbe {
 
 				httpGetScheme := containerinstance.Scheme(scheme)
 				probe.HttpGet = &containerinstance.ContainerHttpGet{
-					Path:   pointer.FromString(path),
-					Port:   int64(port),
-					Scheme: &httpGetScheme,
+					Path:        pointer.FromString(path),
+					Port:        int64(port),
+					Scheme:      &httpGetScheme,
+					HttpHeaders: expandContainerProbeHttpHeaders(x["http_headers"].(map[string]interface{})),
 				}
 			}
 		}
@@ -1487,6 +1489,45 @@ func expandContainerProbe(input interface{}) *containerinstance.ContainerProbe {
 	return &probe
 }
 
+func expandContainerProbeHttpHeaders(input map[string]interface{}) *[]containerinstance.HttpHeader {
+	if len(input) == 0 {
+		return nil
+	}
+
+	headers := []containerinstance.HttpHeader{}
+	for k, v := range input {
+		header := containerinstance.HttpHeader{
+			Name:  pointer.FromString(k),
+			Value: pointer.FromString(v.(string)),
+		}
+		headers = append(headers, header)
+	}
+	sort.Slice(headers, func(i, j int) bool {
+		return *headers[i].Name < *headers[j].Name
+	})
+	return &headers
+}
+
+func flattenContainerProbeHttpHeaders(input *[]containerinstance.HttpHeader) map[string]interface{} {
+	if input == nil {
+		return nil
+	}
+
+	output := map[string]interface{}{}
+	for _, header := range *input {
+		name := ""
+		if header.Name != nil {
+			name = *header.Name
+		}
+		value := ""
+		if header.Value != nil {
+			value = *header.Value
+		}
+		output[name] = value
+	}
+	return output
+}
+
 func flattenContainerImageRegistryCredentials(d *pluginsdk.ResourceData, input *[]containerinstance.ImageRegistryCredential) []interface{} {
 	if input == nil {
 		return nil
@@ -1786,6 +1827,7 @@ func flattenContainerProbes(input *containerinstance.ContainerProbe) []interface
 		}
 		httpGet["port"] = get.Port
 		httpGet["scheme"] = get.Scheme
+		httpGet["http_headers"] = flattenContainerProbeHttpHeaders(get.HttpHeaders)
 		httpGets = append(httpGets, httpGet)
 	}
 	output["http_get"] = httpGets
diff --git a/internal/services/containers/container_group_resource_test.go b/internal/services/containers/container_group_resource_test.go
index 491aadb9308a..d793fadc6903 100644
--- a/internal/services/containers/container_group_resource_test.go
+++ b/internal/services/containers/container_group_resource_test.go
@@ -740,6 +740,10 @@ resource "azurerm_container_group" "test" {
         path   = "/"
         port   = 443
         scheme = "Http"
+        http_headers = {
+          h1 = "v1"
+          h2 = "v2"
+        }
       }
     }
   }
@@ -1764,6 +1768,10 @@ resource "azurerm_container_group" "test" {
         path   = "/"
         port   = 443
         scheme = "Http"
+        http_headers = {
+          h1 = "v1"
+          h2 = "v2"
+        }
       }
 
       initial_delay_seconds = 1
@@ -1910,6 +1918,10 @@ resource "azurerm_container_group" "test" {
         path   = "/"
         port   = 443
         scheme = "Http"
+        http_headers = {
+          h1 = "v1"
+          h2 = "v2"
+        }
       }
 
       initial_delay_seconds = 1
diff --git a/internal/services/containers/probe.go b/internal/services/containers/probe.go
index b9ac2e7a45f7..cddb1e444106 100644
--- a/internal/services/containers/probe.go
+++ b/internal/services/containers/probe.go
@@ -53,6 +53,14 @@ func SchemaContainerGroupProbe() *pluginsdk.Schema {
 									"Https",
 								}, false),
 							},
+							"http_headers": {
+								Type:     pluginsdk.TypeMap,
+								Optional: true,
+								ForceNew: true,
+								Elem: &pluginsdk.Schema{
+									Type: pluginsdk.TypeString,
+								},
+							},
 						},
 					},
 				},
diff --git a/website/docs/r/container_group.html.markdown b/website/docs/r/container_group.html.markdown
index 90336b4fcec8..c6c3821186a3 100644
--- a/website/docs/r/container_group.html.markdown
+++ b/website/docs/r/container_group.html.markdown
@@ -308,6 +308,8 @@ The `http_get` block supports:
 
 * `scheme` - (Optional) Scheme to use for connecting to the host. Possible values are `Http` and `Https`. Changing this forces a new resource to be created.
 
+* `http_headers` - (Optional) A map of HTTP headers used to access on the container. Changing this forces a new resource to be created.
+
 ---
 
 The `dns_config` block supports:

From 2eeb9336819fef13bf66239711e4d36939892eaa Mon Sep 17 00:00:00 2001
From: magodo <wztdyl@sina.com>
Date: Fri, 8 Jul 2022 13:24:55 +0800
Subject: [PATCH 2/2] remove the sorting

---
 internal/services/containers/container_group_resource.go | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/internal/services/containers/container_group_resource.go b/internal/services/containers/container_group_resource.go
index afb64ddb522f..92ff87e02f7a 100644
--- a/internal/services/containers/container_group_resource.go
+++ b/internal/services/containers/container_group_resource.go
@@ -5,7 +5,6 @@ import (
 	"context"
 	"fmt"
 	"log"
-	"sort"
 	"strings"
 	"time"
 
@@ -1502,9 +1501,6 @@ func expandContainerProbeHttpHeaders(input map[string]interface{}) *[]containeri
 		}
 		headers = append(headers, header)
 	}
-	sort.Slice(headers, func(i, j int) bool {
-		return *headers[i].Name < *headers[j].Name
-	})
 	return &headers
 }