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

Container groups: Add support for env vars and command line #338

Merged
merged 7 commits into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
76 changes: 59 additions & 17 deletions azurerm/resource_arm_container_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,16 @@ func resourceArmContainerGroup() *schema.Resource {
}, true),
},

"env_var": {
Type: schema.TypeList,
"environment_variables": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},

"command": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"value": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
},
},
},
},
Expand Down Expand Up @@ -197,6 +188,10 @@ func resourceArmContainerGroupRead(d *schema.ResourceData, meta interface{}) err
resp, err := containterGroupsClient.Get(resGroup, name)

if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return err
}

Expand Down Expand Up @@ -265,12 +260,35 @@ func flattenContainerGroupContainers(containers *[]containerinstance.Container)
}
// protocol isn't returned in container config

if container.EnvironmentVariables != nil {
if len(*container.EnvironmentVariables) > 0 {
containerConfig["environment_variables"] = flattenContainerEnvironmentVariables(container.EnvironmentVariables)
}
}

if command := container.Command; command != nil {
containerConfig["command"] = strings.Join(*command, " ")
}

containerConfigs = append(containerConfigs, containerConfig)
}

return containerConfigs
}

func flattenContainerEnvironmentVariables(input *[]containerinstance.EnvironmentVariable) map[string]interface{} {
output := make(map[string]interface{})

for _, envVar := range *input {
k := *envVar.Name
v := *envVar.Value

output[k] = v
}

return output
}

func expandContainerGroupContainers(d *schema.ResourceData) (*[]containerinstance.Container, *[]containerinstance.Port) {
containersConfig := d.Get("container").([]interface{})
containers := make([]containerinstance.Container, 0, len(containersConfig))
Expand Down Expand Up @@ -321,8 +339,32 @@ func expandContainerGroupContainers(d *schema.ResourceData) (*[]containerinstanc
containerGroupPorts = append(containerGroupPorts, containerGroupPort)
}

if v, ok := data["environment_variables"]; ok {
container.EnvironmentVariables = expandContainerEnvironmentVariables(v)
}

if v, _ := data["command"]; v != "" {
command := strings.Split(v.(string), " ")
container.Command = &command
}

containers = append(containers, container)
}

return &containers, &containerGroupPorts
}

func expandContainerEnvironmentVariables(input interface{}) *[]containerinstance.EnvironmentVariable {
envVars := input.(map[string]interface{})
output := make([]containerinstance.EnvironmentVariable, 0, len(envVars))

for k, v := range envVars {
ev := containerinstance.EnvironmentVariable{
Name: utils.String(k),
Value: utils.String(v.(string)),
}
output = append(output, ev)
}

return &output
}
157 changes: 143 additions & 14 deletions azurerm/resource_arm_container_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func TestAccAzureRMContainerGroup_basicLinux(t *testing.T) {
func TestAccAzureRMContainerGroup_linuxBasic(t *testing.T) {
resourceName := "azurerm_container_group.test"
ri := acctest.RandInt()

config := testAccAzureRMContainerGroupBasicLinux(ri, testLocation())
config := testAccAzureRMContainerGroup_linuxBasic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -24,18 +25,21 @@ func TestAccAzureRMContainerGroup_basicLinux(t *testing.T) {
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMContainerGroupExists("azurerm_container_group.test"),
testCheckAzureRMContainerGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "container.#", "1"),
resource.TestCheckResourceAttr(resourceName, "os_type", "Linux"),
),
},
},
})
}
func TestAccAzureRMContainerGroup_basicLinuxUpdate(t *testing.T) {

func TestAccAzureRMContainerGroup_linuxBasicUpdate(t *testing.T) {
resourceName := "azurerm_container_group.test"
ri := acctest.RandInt()

config := testAccAzureRMContainerGroupBasicLinux(ri, testLocation())
updatedConfig := testAccAzureRMContainerGroupBasicLinuxUpdated(ri, testLocation())
config := testAccAzureRMContainerGroup_linuxBasic(ri, testLocation())
updatedConfig := testAccAzureRMContainerGroup_linuxBasicUpdated(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -60,10 +64,11 @@ func TestAccAzureRMContainerGroup_basicLinuxUpdate(t *testing.T) {
})
}

func TestAccAzureRMContainerGroup_basicWindows(t *testing.T) {
func TestAccAzureRMContainerGroup_linuxComplete(t *testing.T) {
resourceName := "azurerm_container_group.test"
ri := acctest.RandInt()

config := testAccAzureRMContainerGroupBasicWindows(ri, testLocation())
config := testAccAzureRMContainerGroup_linuxComplete(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -73,14 +78,70 @@ func TestAccAzureRMContainerGroup_basicWindows(t *testing.T) {
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMContainerGroupExists("azurerm_container_group.test"),
testCheckAzureRMContainerGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "container.#", "1"),
resource.TestCheckResourceAttr(resourceName, "container.0.command", "/bin/bash -c ls"),
resource.TestCheckResourceAttr(resourceName, "container.0.environment_variables.%", "2"),
resource.TestCheckResourceAttr(resourceName, "container.0.environment_variables.foo", "bar"),
resource.TestCheckResourceAttr(resourceName, "container.0.environment_variables.foo1", "bar1"),
resource.TestCheckResourceAttr(resourceName, "os_type", "Linux"),
),
},
},
})
}

func TestAccAzureRMContainerGroup_windowsBasic(t *testing.T) {
resourceName := "azurerm_container_group.test"
ri := acctest.RandInt()

config := testAccAzureRMContainerGroup_windowsBasic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMContainerGroupDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMContainerGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "container.#", "1"),
resource.TestCheckResourceAttr(resourceName, "os_type", "Windows"),
),
},
},
})
}

func testAccAzureRMContainerGroupBasicLinux(ri int, location string) string {
func TestAccAzureRMContainerGroup_windowsComplete(t *testing.T) {
resourceName := "azurerm_container_group.test"
ri := acctest.RandInt()

config := testAccAzureRMContainerGroup_windowsComplete(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMContainerGroupDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMContainerGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "container.#", "1"),
resource.TestCheckResourceAttr(resourceName, "container.0.command", "cmd.exe echo hi"),
resource.TestCheckResourceAttr(resourceName, "container.0.environment_variables.%", "2"),
resource.TestCheckResourceAttr(resourceName, "container.0.environment_variables.foo", "bar"),
resource.TestCheckResourceAttr(resourceName, "container.0.environment_variables.foo1", "bar1"),
resource.TestCheckResourceAttr(resourceName, "os_type", "Windows"),
),
},
},
})
}

func testAccAzureRMContainerGroup_linuxBasic(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
Expand All @@ -99,7 +160,7 @@ resource "azurerm_container_group" "test" {
image = "microsoft/aci-helloworld:latest"
cpu = "0.5"
memory = "0.5"
port = "80"
port = "80"
}

tags {
Expand All @@ -109,7 +170,7 @@ resource "azurerm_container_group" "test" {
`, ri, location, ri)
}

func testAccAzureRMContainerGroupBasicLinuxUpdated(ri int, location string) string {
func testAccAzureRMContainerGroup_linuxBasicUpdated(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
Expand All @@ -128,7 +189,7 @@ resource "azurerm_container_group" "test" {
image = "microsoft/aci-helloworld:latest"
cpu = "0.5"
memory = "0.5"
port = "80"
port = "80"
}

container {
Expand All @@ -145,7 +206,41 @@ resource "azurerm_container_group" "test" {
`, ri, location, ri)
}

func testAccAzureRMContainerGroupBasicWindows(ri int, location string) string {
func testAccAzureRMContainerGroup_linuxComplete(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

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"
environment_variables {
"foo" = "bar"
"foo1" = "bar1"
}
command = "/bin/bash -c ls"
}

tags {
environment = "testing"
}
}
`, ri, location, ri)
}

func testAccAzureRMContainerGroup_windowsBasic(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
Expand Down Expand Up @@ -174,6 +269,40 @@ resource "azurerm_container_group" "test" {
`, ri, location, ri)
}

func testAccAzureRMContainerGroup_windowsComplete(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

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 = "windows"

container {
name = "winapp"
image = "winappimage:latest"
cpu = "2.0"
memory = "3.5"
port = "80"
environment_variables {
"foo" = "bar"
"foo1" = "bar1"
}
command = "cmd.exe echo hi"
}

tags {
environment = "testing"
}
}
`, ri, location, ri)
}

func testCheckAzureRMContainerGroupExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/container_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ resource "azurerm_container_group" "aci-helloworld" {
cpu ="0.5"
memory = "1.5"
port = "80"
environment_variables {
"NODE_ENV"="Staging"
}
command = "/bin/bash -c '/path to/myscript.sh'"
}
container {
name = "sidecar"
Expand Down Expand Up @@ -76,6 +80,10 @@ The `container` block supports:

* `port` - (Optional) A public port for the container. Changing this forces a new resource to be created.

* `environment_variables` - (Optional) A list of environment variables to be set on the container. Specified as a map of name/value pairs. Changing this forces a new resource to be created.

* `command` - (Optional) A command line to be run on the container. Changing this forces a new resource to be created.

## Attributes Reference

The following attributes are exported:
Expand Down