Skip to content

Commit

Permalink
Added test and fixes from pr hashicorp#1509
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrencegripper committed Jul 10, 2018
1 parent 73b236a commit eb822f3
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 16 deletions.
43 changes: 28 additions & 15 deletions azurerm/resource_arm_container_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,25 @@ func resourceArmContainerGroup() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"server": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
ForceNew: true,
},

"username": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
ForceNew: true,
},

"password": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validation.NoZeroValues,
ForceNew: true,
},
},
},
Expand Down Expand Up @@ -229,7 +236,6 @@ func resourceArmContainerGroupCreate(d *schema.ResourceData, meta interface{}) e
IPAddressType := d.Get("ip_address_type").(string)
tags := d.Get("tags").(map[string]interface{})
restartPolicy := d.Get("restart_policy").(string)
imageCredentials := expandContainerImageRegistryCredentials(d)

containers, containerGroupPorts, containerGroupVolumes := expandContainerGroupContainers(d)
containerGroup := containerinstance.ContainerGroup{
Expand All @@ -245,7 +251,7 @@ func resourceArmContainerGroupCreate(d *schema.ResourceData, meta interface{}) e
},
OsType: containerinstance.OperatingSystemTypes(OSType),
Volumes: containerGroupVolumes,
ImageRegistryCredentials: imageCredentials,
ImageRegistryCredentials: expandContainerImageRegistryCredentials(d),
},
}

Expand Down Expand Up @@ -303,9 +309,8 @@ func resourceArmContainerGroupRead(d *schema.ResourceData, meta interface{}) err
}
flattenAndSetTags(d, resp.Tags)

imageRegistryCredentials := flattenContainerImageRegistryCredentials(resp.ImageRegistryCredentials)
if imageRegistryCredentials != nil {
d.Set("image_registry_credential", imageRegistryCredentials)
if imageRegCreds := flattenContainerImageRegistryCredentials(d, resp.ImageRegistryCredentials); imageRegCreds != nil {
d.Set("image_registry_credential", imageRegCreds)
}

d.Set("os_type", string(resp.OsType))
Expand Down Expand Up @@ -588,10 +593,11 @@ func expandContainerImageRegistryCredentials(d *schema.ResourceData) *[]containe
return &output
}

func flattenContainerImageRegistryCredentials(credsPtr *[]containerinstance.ImageRegistryCredential) []interface{} {
func flattenContainerImageRegistryCredentials(d *schema.ResourceData, credsPtr *[]containerinstance.ImageRegistryCredential) []interface{} {
if credsPtr == nil {
return nil
}
configsOld := d.Get("image_registry_credential").([]interface{})

creds := *credsPtr
output := make([]interface{}, len(creds))
Expand All @@ -600,13 +606,20 @@ func flattenContainerImageRegistryCredentials(credsPtr *[]containerinstance.Imag
if cred.Server != nil {
credConfig["server"] = *cred.Server
}
if cred.Password != nil {
credConfig["password"] = *cred.Password
}
if cred.Username != nil {
credConfig["username"] = *cred.Username
}

for i, configsOld := range configsOld {
data := configsOld.(map[string]interface{})
oldServer := data["server"].(string)
if cred.Server != nil && *cred.Server == oldServer {
if v, ok := d.GetOk(fmt.Sprintf("image_registry_credential.%d.password", i)); ok {
credConfig["password"] = v.(string)
}
}
}

output = append(output, credConfig)
}
return output
Expand Down
78 changes: 77 additions & 1 deletion azurerm/resource_arm_container_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,40 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

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

config := testAccAzureRMContainerGroup_linuxBasic(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, "image_registry_credential.#:", "2"),
resource.TestCheckResourceAttr(resourceName, "image_registry_credential.0.server", "hub.docker.com"),
resource.TestCheckResourceAttr(resourceName, "image_registry_credential.0.username", "yourusername"),
resource.TestCheckResourceAttr(resourceName, "image_registry_credential.0.password:", "yourpassword"),
resource.TestCheckResourceAttr(resourceName, "image_registry_credential.1.server", "mine.acr.io"),
resource.TestCheckResourceAttr(resourceName, "image_registry_credential.1.username", "acrusername"),
resource.TestCheckResourceAttr(resourceName, "image_registry_credential.1.password:", "acrpassword"),
),
},
},
})
}

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

config := testAccAzureRMContainerGroup_imageRegistryCredentials(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand Down Expand Up @@ -176,6 +204,54 @@ resource "azurerm_container_group" "test" {
`, ri, location, ri)
}

func testAccAzureRMContainerGroup_imageRegistryCredentials(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"
}
image_registry_credential {
server = "hub.docker.com"
username = "yourusername"
password = "yourpassword"
}
image_registry_credential {
server = "mine.acr.io"
username = "acrusername"
password = "acrpassword"
}
container {
name = "sidecar"
image = "microsoft/aci-tutorial-sidecar"
cpu = "0.5"
memory = "0.5"
}
tags {
environment = "Testing"
}
}
`, ri, location, ri)
}

func testAccAzureRMContainerGroup_linuxBasicUpdated(ri int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down

0 comments on commit eb822f3

Please sign in to comment.