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

azurerm_container_app_environment - add support for Consumption workload profile #25285

Merged
merged 3 commits into from
Mar 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type ContainerAppEnvironmentModel struct {

var _ sdk.ResourceWithUpdate = ContainerAppEnvironmentResource{}

var _ sdk.ResourceWithCustomizeDiff = ContainerAppEnvironmentResource{}

func (r ContainerAppEnvironmentResource) ModelObject() interface{} {
return &ContainerAppEnvironmentModel{}
}
Expand Down Expand Up @@ -283,6 +285,8 @@ func (r ContainerAppEnvironmentResource) Read() sdk.ResourceFunc {

var state ContainerAppEnvironmentModel

consumptionDefined := consumptionIsExplicitlyDefined(metadata)

if model := existing.Model; model != nil {
state.Name = id.ManagedEnvironmentName
state.ResourceGroup = id.ResourceGroupName
Expand All @@ -301,7 +305,7 @@ func (r ContainerAppEnvironmentResource) Read() sdk.ResourceFunc {
state.ZoneRedundant = pointer.From(props.ZoneRedundant)
state.StaticIP = pointer.From(props.StaticIP)
state.DefaultDomain = pointer.From(props.DefaultDomain)
state.WorkloadProfiles = helpers.FlattenWorkloadProfiles(props.WorkloadProfiles)
state.WorkloadProfiles = helpers.FlattenWorkloadProfiles(props.WorkloadProfiles, consumptionDefined)
state.InfrastructureResourceGroup = pointer.From(props.InfrastructureResourceGroup)
}
}
Expand Down Expand Up @@ -419,3 +423,53 @@ func (r ContainerAppEnvironmentResource) Update() sdk.ResourceFunc {
},
}
}

func consumptionIsExplicitlyDefined(metadata sdk.ResourceMetaData) bool {
config := ContainerAppEnvironmentModel{}
if err := metadata.Decode(&config); err != nil {
return false
}
for _, v := range config.WorkloadProfiles {
if v.Name == string(helpers.WorkloadProfileSkuConsumption) {
return true
}
}

return false
}

func (r ContainerAppEnvironmentResource) CustomizeDiff() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
if metadata.ResourceDiff == nil {
return nil
}

var env ContainerAppEnvironmentModel
if err := metadata.DecodeDiff(&env); err != nil {
return err
}

if metadata.ResourceDiff.HasChange("workload_profile") {
oldProfiles, newProfiles := metadata.ResourceDiff.GetChange("workload_profile")

oldProfileCount := oldProfiles.(*pluginsdk.Set).Len()
newProfileCount := newProfiles.(*pluginsdk.Set).Len()
if oldProfileCount > 0 && newProfileCount == 0 {
if err := metadata.ResourceDiff.ForceNew("workload_profile"); err != nil {
return err
}
}

if newProfileCount > 0 && oldProfileCount == 0 {
if err := metadata.ResourceDiff.ForceNew("workload_profile"); err != nil {
return err
}
}
}

return nil
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ func TestAccContainerAppEnvironment_basic(t *testing.T) {
})
}

func TestAccContainerAppEnvironment_consumptionWorkloadProfile(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_app_environment", "test")
r := ContainerAppEnvironmentResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.consumptionWorkloadProfile(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("log_analytics_workspace_id", "workload_profile"),
})
}

func TestAccContainerAppEnvironment_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_app_environment", "test")
r := ContainerAppEnvironmentResource{}
Expand Down Expand Up @@ -225,6 +240,28 @@ resource "azurerm_container_app_environment" "test" {
`, r.templateVNet(data), data.RandomInteger)
}

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

%[1]s

resource "azurerm_container_app_environment" "test" {
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id

workload_profile {
name = "Consumption"
workload_profile_type = "Consumption"
}
}
`, r.templateVNet(data), data.RandomInteger)
}

func (r ContainerAppEnvironmentResource) completeUpdate(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down Expand Up @@ -284,6 +321,13 @@ resource "azurerm_container_app_environment" "test" {
workload_profile_type = "E4"
}

workload_profile {
maximum_count = 2
minimum_count = 0
name = "D4-02"
workload_profile_type = "E4"
}

workload_profile {
maximum_count = 2
minimum_count = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,43 @@ import (

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/containerapps/2023-05-01/managedenvironments"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

const consumption = "Consumption"
type WorkloadProfileSku string

// NOTE: the Workload Profile SKUs aren't defined in the Swagger definition so we define them here
const (
WorkloadProfileSkuConsumption WorkloadProfileSku = "Consumption"
WorkloadProfileSkuD4 WorkloadProfileSku = "D4"
WorkloadProfileSkuD8 WorkloadProfileSku = "D8"
WorkloadProfileSkuD16 WorkloadProfileSku = "D16"
WorkloadProfileSkuD32 WorkloadProfileSku = "D32"
WorkloadProfileSkuE4 WorkloadProfileSku = "E4"
WorkloadProfileSkuE8 WorkloadProfileSku = "E8"
WorkloadProfileSkuE16 WorkloadProfileSku = "E16"
WorkloadProfileSkuE32 WorkloadProfileSku = "E32"
)
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

func PossibleValuesForWorkloadProfileSku() []string {
return []string{
string(WorkloadProfileSkuConsumption),
string(WorkloadProfileSkuD4),
string(WorkloadProfileSkuD8),
string(WorkloadProfileSkuD16),
string(WorkloadProfileSkuD32),
string(WorkloadProfileSkuE4),
string(WorkloadProfileSkuE8),
string(WorkloadProfileSkuE16),
string(WorkloadProfileSkuE32),
}
}

type WorkloadProfileModel struct {
MaximumCount int `tfschema:"maximum_count"`
MinimumCount int `tfschema:"minimum_count"`
MaximumCount int64 `tfschema:"maximum_count"`
MinimumCount int64 `tfschema:"minimum_count"`
Name string `tfschema:"name"`
WorkloadProfileType string `tfschema:"workload_profile_type"`
}
Expand All @@ -34,28 +62,19 @@ func WorkloadProfileSchema() *pluginsdk.Schema {
},

"workload_profile_type": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"D4",
"D8",
"D16",
"D32",
"E4",
"E8",
"E16",
"E32",
}, false),
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(PossibleValuesForWorkloadProfileSku(), false),
},

"maximum_count": {
Type: pluginsdk.TypeInt,
Required: true,
Optional: true,
},

"minimum_count": {
Type: pluginsdk.TypeInt,
Required: true,
Optional: true,
},
},
},
Expand All @@ -69,42 +88,49 @@ func ExpandWorkloadProfiles(input []WorkloadProfileModel) *[]managedenvironments

result := make([]managedenvironments.WorkloadProfile, 0)

consumptionDefined := false

for _, v := range input {
r := managedenvironments.WorkloadProfile{
Name: v.Name,
WorkloadProfileType: v.WorkloadProfileType,
Name: v.Name,
}

if v.Name != consumption {
r.MaximumCount = pointer.To(int64(v.MaximumCount))
r.MinimumCount = pointer.To(int64(v.MinimumCount))
if v.Name != string(WorkloadProfileSkuConsumption) {
r.WorkloadProfileType = v.WorkloadProfileType
r.MaximumCount = pointer.To(v.MaximumCount)
r.MinimumCount = pointer.To(v.MinimumCount)
} else {
r.WorkloadProfileType = string(WorkloadProfileSkuConsumption)
consumptionDefined = true
}

result = append(result, r)
}

result = append(result, managedenvironments.WorkloadProfile{
Name: consumption,
WorkloadProfileType: consumption,
})
if !features.FourPointOhBeta() && !consumptionDefined {
result = append(result, managedenvironments.WorkloadProfile{
Name: string(WorkloadProfileSkuConsumption),
WorkloadProfileType: string(WorkloadProfileSkuConsumption),
})
}

return &result
}

func FlattenWorkloadProfiles(input *[]managedenvironments.WorkloadProfile) []WorkloadProfileModel {
func FlattenWorkloadProfiles(input *[]managedenvironments.WorkloadProfile, consumptionDefined bool) []WorkloadProfileModel {
if input == nil || len(*input) == 0 {
return []WorkloadProfileModel{}
}
result := make([]WorkloadProfileModel, 0)

for _, v := range *input {
if strings.EqualFold(v.WorkloadProfileType, consumption) {
if strings.EqualFold(v.WorkloadProfileType, string(WorkloadProfileSkuConsumption)) && !consumptionDefined {
continue
}
result = append(result, WorkloadProfileModel{
Name: v.Name,
MaximumCount: int(pointer.From(v.MaximumCount)),
MinimumCount: int(pointer.From(v.MinimumCount)),
MaximumCount: pointer.From(v.MaximumCount),
MinimumCount: pointer.From(v.MinimumCount),
WorkloadProfileType: v.WorkloadProfileType,
})
}
Expand Down
6 changes: 5 additions & 1 deletion website/docs/r/container_app_environment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ A `workload_profile` block supports the following:

* `name` - (Required) The name of the workload profile.

* `workload_profile_type` - (Required) Workload profile type for the workloads to run on. Possible values include `D4`, `D8`, `D16`, `D32`, `E4`, `E8`, `E16` and `E32`.
* `workload_profile_type` - (Required) Workload profile type for the workloads to run on. Possible values include `Consumption`, `D4`, `D8`, `D16`, `D32`, `E4`, `E8`, `E16` and `E32`.

~> **NOTE:** A `Consumption` type must have a name of `Consumption` and an environment may only have one `Consumption` Workload Profile.

~> **NOTE:** Defining a `Consumption` profile is optional, however, Environments created without an initial Workload Profile cannot have them added at a later time and must be recreated. Similarly, an environment created with Profiles must always have at least one defined Profile, removing all profiles will force a recreation of the resource.

* `maximum_count` - (Required) The maximum number of instances of workload profile that can be deployed in the Container App Environment.

Expand Down
Loading