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

tags update for azurerm_container_group #10210

Merged
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
27 changes: 26 additions & 1 deletion azurerm/internal/services/containers/container_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/containers/parse"
msiparse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/parse"
msivalidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
Expand All @@ -31,6 +32,7 @@ func resourceArmContainerGroup() *schema.Resource {
Create: resourceArmContainerGroupCreate,
Read: resourceArmContainerGroupRead,
Delete: resourceArmContainerGroupDelete,
Update: resourceArmContainerGroupUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -155,7 +157,7 @@ func resourceArmContainerGroup() *schema.Resource {
},
},

"tags": tags.ForceNewSchema(),
"tags": tags.Schema(),
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

"restart_policy": {
Type: schema.TypeString,
Expand Down Expand Up @@ -597,6 +599,29 @@ func resourceArmContainerGroupCreate(d *schema.ResourceData, meta interface{}) e
return resourceArmContainerGroupRead(d, meta)
}

func resourceArmContainerGroupUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Containers.GroupsClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ContainerGroupID(d.Id())
if err != nil {
return err
}

t := d.Get("tags").(map[string]interface{})

parameters := containerinstance.Resource{
Tags: tags.Expand(t),
}

if _, err := client.Update(ctx, id.ResourceGroup, id.Name, parameters); err != nil {
return fmt.Errorf("Error updating container group %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

return resourceArmContainerGroupRead(d, meta)
}

func resourceArmContainerGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Containers.GroupsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
Expand Down
69 changes: 69 additions & 0 deletions azurerm/internal/services/containers/parse/container_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type ContainerGroupId struct {
SubscriptionId string
ResourceGroup string
Name string
}

func NewContainerGroupID(subscriptionId, resourceGroup, name string) ContainerGroupId {
return ContainerGroupId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
Name: name,
}
}

func (id ContainerGroupId) String() string {
segments := []string{
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Container Group", segmentsStr)
}

func (id ContainerGroupId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ContainerInstance/containerGroups/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.Name)
}

// ContainerGroupID parses a ContainerGroup ID into an ContainerGroupId struct
func ContainerGroupID(input string) (*ContainerGroupId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := ContainerGroupId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.Name, err = id.PopSegment("containerGroups"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
112 changes: 112 additions & 0 deletions azurerm/internal/services/containers/parse/container_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = ContainerGroupId{}

func TestContainerGroupIDFormatter(t *testing.T) {
actual := NewContainerGroupID("12345678-1234-9876-4563-123456789012", "resGroup1", "containerGroup1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ContainerInstance/containerGroups/containerGroup1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestContainerGroupID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *ContainerGroupId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ContainerInstance/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ContainerInstance/containerGroups/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ContainerInstance/containerGroups/containerGroup1",
Expected: &ContainerGroupId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Name: "containerGroup1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.CONTAINERINSTANCE/CONTAINERGROUPS/CONTAINERGROUP1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := ContainerGroupID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/containers/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ package containers

//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Cluster -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ContainerService/managedClusters/cluster1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=NodePool -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ContainerService/managedClusters/cluster1/agentPools/pool1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ContainerGroup -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ContainerInstance/containerGroups/containerGroup1
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,33 @@ func TestAccAzureRMContainerGroup_linuxBasicUpdate(t *testing.T) {
})
}

func TestAccAzureRMContainerGroup_linuxBasicTagsUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_group", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMContainerGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMContainerGroup_linuxBasic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMContainerGroupExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "container.#", "1"),
),
},
{
Config: testAccAzureRMContainerGroup_linuxBasicTagsUpdated(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMContainerGroupExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "container.#", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "tags.OS", "Linux"),
),
},
},
})
}

func TestAccAzureRMContainerGroup_linuxComplete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_group", "test")

Expand Down Expand Up @@ -675,6 +702,43 @@ resource "azurerm_container_group" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func testAccAzureRMContainerGroup_linuxBasicTagsUpdated(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

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"
ports {
port = 80
protocol = "TCP"
}
}

tags = {
environment = "Testing"
OS = "Linux"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func testAccAzureRMContainerGroup_requiresImport(data acceptance.TestData) string {
template := testAccAzureRMContainerGroup_linuxBasic(data)
return fmt.Sprintf(`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package validate

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/containers/parse"
)

func ContainerGroupID(input interface{}, key string) (warnings []string, errors []error) {
v, ok := input.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
return
}

if _, err := parse.ContainerGroupID(v); err != nil {
errors = append(errors, err)
}

return
}
Loading