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_service_plan - support premium service plan auto scale feature #28524

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions internal/services/appservice/helpers/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ var consumptionSkus = []string{
"Y1",
}

var premiumSkus = []string{
"P1v2", "P2v2", "P3v2", // Premium V2
"P0v3", "P1v3", "P2v3", "P3v3", // Premium V3
"P1mv3", "P2mv3", "P3mv3", "P4mv3", "P5mv3", // Premium V3 memory optimized
}

var flexConsumptionSkus = []string{
"FC1",
}
Expand Down Expand Up @@ -88,6 +94,19 @@ func PlanIsConsumption(input *string) bool {
return false
}

func PlanIsPremium(input *string) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to pass a pointer to a string for this function? It looks like every time we call this function, you're passing in the address of a string when we could just pass the string

Suggested change
func PlanIsPremium(input *string) bool {
func PlanIsPremium(input string) bool {

if input == nil {
return false
}
for _, v := range premiumSkus {
if strings.EqualFold(*input, v) {
return true
}
}

return false
}

func PlanIsFlexConsumption(input *string) bool {
if input == nil {
return false
Expand Down
81 changes: 67 additions & 14 deletions internal/services/appservice/service_plan_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var _ sdk.ResourceWithUpdate = ServicePlanResource{}

var _ sdk.ResourceWithStateMigration = ServicePlanResource{}

var _ sdk.ResourceWithCustomizeDiff = ServicePlanResource{}

type OSType string

const (
Expand All @@ -41,19 +43,20 @@ const (
)

type ServicePlanModel struct {
Name string `tfschema:"name"`
ResourceGroup string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
Kind string `tfschema:"kind"`
OSType OSType `tfschema:"os_type"`
Sku string `tfschema:"sku_name"`
AppServiceEnvironmentId string `tfschema:"app_service_environment_id"`
PerSiteScaling bool `tfschema:"per_site_scaling_enabled"`
Reserved bool `tfschema:"reserved"`
WorkerCount int64 `tfschema:"worker_count"`
MaximumElasticWorkerCount int64 `tfschema:"maximum_elastic_worker_count"`
ZoneBalancing bool `tfschema:"zone_balancing_enabled"`
Tags map[string]string `tfschema:"tags"`
Name string `tfschema:"name"`
ResourceGroup string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
Kind string `tfschema:"kind"`
OSType OSType `tfschema:"os_type"`
Sku string `tfschema:"sku_name"`
AppServiceEnvironmentId string `tfschema:"app_service_environment_id"`
PerSiteScaling bool `tfschema:"per_site_scaling_enabled"`
Reserved bool `tfschema:"reserved"`
WorkerCount int64 `tfschema:"worker_count"`
PremiumPlanAutoScaleEnabled bool `tfschema:"premium_plan_auto_scale_enabled"`
MaximumElasticWorkerCount int64 `tfschema:"maximum_elastic_worker_count"`
ZoneBalancing bool `tfschema:"zone_balancing_enabled"`
Tags map[string]string `tfschema:"tags"`
}

func (r ServicePlanResource) Arguments() map[string]*pluginsdk.Schema {
Expand Down Expand Up @@ -107,6 +110,12 @@ func (r ServicePlanResource) Arguments() map[string]*pluginsdk.Schema {
ValidateFunc: validation.IntAtLeast(1),
},

"premium_plan_auto_scale_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"maximum_elastic_worker_count": {
Type: pluginsdk.TypeInt,
Optional: true,
Expand Down Expand Up @@ -191,9 +200,19 @@ func (r ServicePlanResource) Create() sdk.ResourceFunc {
}
}

if servicePlan.PremiumPlanAutoScaleEnabled {
if !helpers.PlanIsPremium(&servicePlan.Sku) {
return fmt.Errorf("`premium_plan_auto_scale_enabled` can only be set for premium app service plan")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicate of what is in the CustomizeDiff

} else {
appServicePlan.Properties.ElasticScaleEnabled = pointer.To(servicePlan.PremiumPlanAutoScaleEnabled)
}
}

if servicePlan.MaximumElasticWorkerCount > 0 {
if !isServicePlanSupportScaleOut(servicePlan.Sku) {
return fmt.Errorf("`maximum_elastic_worker_count` can only be specified with Elastic Premium Skus")
if helpers.PlanIsPremium(&servicePlan.Sku) && !servicePlan.PremiumPlanAutoScaleEnabled {
return fmt.Errorf("`maximum_elastic_worker_count` can only be specified with Elastic Premium Skus or Premium Skus that has `premium_plan_auto_scale_enabled` set to `true`")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a duplicate of what is already in the CustomizeDiff. Do we need this check twice?

}
}
appServicePlan.Properties.MaximumElasticWorkerCount = pointer.To(servicePlan.MaximumElasticWorkerCount)
}
Expand Down Expand Up @@ -264,6 +283,10 @@ func (r ServicePlanResource) Read() sdk.ResourceFunc {
state.AppServiceEnvironmentId = *ase.Id
}

if props.ElasticScaleEnabled != nil && *props.ElasticScaleEnabled && state.Sku != "" && helpers.PlanIsPremium(&state.Sku) {
state.PremiumPlanAutoScaleEnabled = pointer.From(props.ElasticScaleEnabled)
}

state.PerSiteScaling = pointer.From(props.PerSiteScaling)
state.Reserved = pointer.From(props.Reserved)
state.ZoneBalancing = pointer.From(props.ZoneRedundant)
Expand Down Expand Up @@ -341,6 +364,14 @@ func (r ServicePlanResource) Update() sdk.ResourceFunc {
model.Sku.Capacity = pointer.To(state.WorkerCount)
}

if metadata.ResourceData.HasChange("premium_plan_auto_scale_enabled") {
if !helpers.PlanIsPremium(&state.Sku) {
return fmt.Errorf("`premium_plan_auto_scale_enabled` can only be set for premium app service plan")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems like a duplicate of the CustomizeDiff

} else {
model.Properties.ElasticScaleEnabled = pointer.To(state.PremiumPlanAutoScaleEnabled)
}
}

if metadata.ResourceData.HasChange("maximum_elastic_worker_count") {
if metadata.ResourceData.HasChange("maximum_elastic_worker_count") && !isServicePlanSupportScaleOut(state.Sku) {
return fmt.Errorf("`maximum_elastic_worker_count` can only be specified with Elastic Premium Skus")
Expand Down Expand Up @@ -373,3 +404,25 @@ func (r ServicePlanResource) StateUpgraders() sdk.StateUpgradeData {
},
}
}

func (r ServicePlanResource) CustomizeDiff() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
rd := metadata.ResourceDiff
servicePlanSku := rd.Get("sku_name").(string)
_, newAutoScaleEnabled := rd.GetChange("premium_plan_auto_scale_enabled")
_, newEcValue := rd.GetChange("maximum_elastic_worker_count")
if rd.HasChange("premium_plan_auto_scale_enabled") {
if !helpers.PlanIsPremium(&servicePlanSku) && newAutoScaleEnabled.(bool) {
return fmt.Errorf("`premium_plan_auto_scale_enabled` can only be set for premium app service plan")
}
}

if rd.HasChange("maximum_elastic_worker_count") && newEcValue.(int) > 1 && helpers.PlanIsPremium(&servicePlanSku) && !newAutoScaleEnabled.(bool) {
return fmt.Errorf("`maximum_elastic_worker_count` can only be specified when Premium Skus that has `premium_plan_auto_scale_enabled` set to `true`")
}
return nil
},
}
}
98 changes: 98 additions & 0 deletions internal/services/appservice/service_plan_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,50 @@ func TestAccServicePlan_linuxConsumption(t *testing.T) {
})
}

func TestAccServicePlan_linuxPremiumAutoScaleFeature(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_service_plan", "test")
r := ServicePlanResource{}

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

func TestAccServicePlan_linuxPremiumAutoScaleFeatureUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_service_plan", "test")
r := ServicePlanResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.linuxPremium(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.linuxPremiumAutoScaleFeature(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.linuxPremium(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccServicePlan_linuxFlexConsumption(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_service_plan", "test")
r := ServicePlanResource{}
Expand Down Expand Up @@ -310,6 +354,60 @@ resource "azurerm_service_plan" "test" {
`, data.RandomInteger, data.Locations.Primary)
}

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

resource "azurerm_resource_group" "test" {
name = "acctestRG-appserviceplan-%[1]d"
location = "%s"
}

resource "azurerm_service_plan" "test" {
name = "acctest-SP-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku_name = "P1v3"
os_type = "Linux"

tags = {
environment = "AccTest"
Foo = "bar"
}
}
`, data.RandomInteger, data.Locations.Primary)
}

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

resource "azurerm_resource_group" "test" {
name = "acctestRG-appserviceplan-%[1]d"
location = "%s"
}

resource "azurerm_service_plan" "test" {
name = "acctest-SP-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku_name = "P1v3"
os_type = "Linux"
premium_plan_auto_scale_enabled = true
maximum_elastic_worker_count = 52

tags = {
environment = "AccTest"
Foo = "bar"
}
}
`, data.RandomInteger, data.Locations.Primary)
}

// (@jackofallops) - `complete` deliberately omits ASE testing for the moment and will be tested separately later
func (r ServicePlanResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/service_plan.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ The following arguments are supported:

~> **NOTE:** Requires an Isolated SKU. Use one of `I1`, `I2`, `I3` for `azurerm_app_service_environment`, or `I1v2`, `I2v2`, `I3v2` for `azurerm_app_service_environment_v3`

* `maximum_elastic_worker_count` - (Optional) The maximum number of workers to use in an Elastic SKU Plan. Cannot be set unless using an Elastic SKU.
* `premium_plan_auto_scale_enabled` - (Optional) Should automatic scaling be enabled for the Premium SKU Plan. Defaults to `false`.Cannot be set unless using a Premium SKU.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `premium_plan_auto_scale_enabled` - (Optional) Should automatic scaling be enabled for the Premium SKU Plan. Defaults to `false`.Cannot be set unless using a Premium SKU.
* `premium_plan_auto_scale_enabled` - (Optional) Should automatic scaling be enabled for the Premium SKU Plan. Defaults to `false`. Cannot be set unless using a Premium SKU.


* `maximum_elastic_worker_count` - (Optional) The maximum number of workers to use in an Elastic SKU Plan or Premium Plan that have `premium_plan_auto_scale_enabled` set to `true`. Cannot be set unless using an Elastic or Premium SKU.

* `worker_count` - (Optional) The number of Workers (instances) to be allocated.

Expand Down
Loading