diff --git a/internal/services/nginx/nginx_deployment_data_source.go b/internal/services/nginx/nginx_deployment_data_source.go index 182065c4de71..f0de8a80fda6 100644 --- a/internal/services/nginx/nginx_deployment_data_source.go +++ b/internal/services/nginx/nginx_deployment_data_source.go @@ -27,6 +27,7 @@ type DeploymentDataSourceModel struct { ManagedResourceGroup string `tfschema:"managed_resource_group"` Location string `tfschema:"location"` Capacity int64 `tfschema:"capacity"` + AutoScaleProfile []AutoScaleProfile `tfschema:"auto_scale_profile"` DiagnoseSupportEnabled bool `tfschema:"diagnose_support_enabled"` Email string `tfschema:"email"` IpAddress string `tfschema:"ip_address"` @@ -80,6 +81,29 @@ func (m DeploymentDataSource) Attributes() map[string]*pluginsdk.Schema { Computed: true, }, + "auto_scale_profile": { + Type: pluginsdk.TypeList, + Computed: true, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "min": { + Type: pluginsdk.TypeInt, + Computed: true, + }, + + "max": { + Type: pluginsdk.TypeInt, + Computed: true, + }, + }, + }, + }, + "diagnose_support_enabled": { Type: pluginsdk.TypeBool, Computed: true, @@ -267,7 +291,19 @@ func (m DeploymentDataSource) Read() sdk.ResourceFunc { } if scaling := props.ScalingProperties; scaling != nil { - output.Capacity = pointer.ToInt64(props.ScalingProperties.Capacity) + if capacity := scaling.Capacity; capacity != nil { + output.Capacity = pointer.ToInt64(props.ScalingProperties.Capacity) + } + if autoScaleProfiles := scaling.AutoScaleSettings; autoScaleProfiles != nil { + profiles := autoScaleProfiles.Profiles + for _, profile := range profiles { + output.AutoScaleProfile = append(output.AutoScaleProfile, AutoScaleProfile{ + Name: profile.Name, + Min: profile.Capacity.Min, + Max: profile.Capacity.Max, + }) + } + } } if userProfile := props.UserProfile; userProfile != nil && userProfile.PreferredEmail != nil { diff --git a/internal/services/nginx/nginx_deployment_data_source_test.go b/internal/services/nginx/nginx_deployment_data_source_test.go index 757f05783b4f..2bd5e8ff0308 100644 --- a/internal/services/nginx/nginx_deployment_data_source_test.go +++ b/internal/services/nginx/nginx_deployment_data_source_test.go @@ -43,3 +43,30 @@ data "azurerm_nginx_deployment" "test" { } `, DeploymentResource{}.basic(data)) } + +func (d NginxDeploymentDataSource) basicAutoscaling(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +data "azurerm_nginx_deployment" "test" { + name = azurerm_nginx_deployment.test.name + resource_group_name = azurerm_nginx_deployment.test.resource_group_name +} +`, DeploymentResource{}.basicAutoscaling(data)) +} + +func TestAccNginxDeploymentDataSource_autoscaling(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_nginx_deployment", "test") + r := NginxDeploymentDataSource{} + + data.DataSourceTest(t, []acceptance.TestStep{ + { + Config: r.basicAutoscaling(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("auto_scale_profile.0.name").HasValue("test"), + check.That(data.ResourceName).Key("auto_scale_profile.0.min").HasValue("10"), + check.That(data.ResourceName).Key("auto_scale_profile.0.min").HasValue("30"), + ), + }, + }) +} diff --git a/internal/services/nginx/nginx_deployment_resource.go b/internal/services/nginx/nginx_deployment_resource.go index 1d45034305c4..c89aba699bdb 100644 --- a/internal/services/nginx/nginx_deployment_resource.go +++ b/internal/services/nginx/nginx_deployment_resource.go @@ -37,6 +37,12 @@ type NetworkInterface struct { SubnetId string `tfschema:"subnet_id"` } +type AutoScaleProfile struct { + Name string `tfschema:"name"` + Min int64 `tfschema:"min"` + Max int64 `tfschema:"max"` +} + type DeploymentModel struct { ResourceGroupName string `tfschema:"resource_group_name"` Name string `tfschema:"name"` @@ -46,6 +52,7 @@ type DeploymentModel struct { ManagedResourceGroup string `tfschema:"managed_resource_group"` Location string `tfschema:"location"` Capacity int64 `tfschema:"capacity"` + AutoScaleProfile []AutoScaleProfile `tfschema:"auto_scale_profile"` DiagnoseSupportEnabled bool `tfschema:"diagnose_support_enabled"` Email string `tfschema:"email"` IpAddress string `tfschema:"ip_address"` @@ -98,10 +105,38 @@ func (m DeploymentResource) Arguments() map[string]*pluginsdk.Schema { "location": commonschema.Location(), "capacity": { - Type: pluginsdk.TypeInt, - Optional: true, - Default: 20, - ValidateFunc: validation.IntPositive, + Type: pluginsdk.TypeInt, + Optional: true, + ConflictsWith: []string{"auto_scale_profile"}, + Default: 20, + ValidateFunc: validation.IntPositive, + }, + + "auto_scale_profile": { + Type: pluginsdk.TypeList, + Optional: true, + ConflictsWith: []string{"capacity"}, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "min": { + Type: pluginsdk.TypeInt, + Required: true, + ValidateFunc: validation.IntPositive, + }, + + "max": { + Type: pluginsdk.TypeInt, + Required: true, + ValidateFunc: validation.IntPositive, + }, + }, + }, }, "diagnose_support_enabled": { @@ -310,6 +345,24 @@ func (m DeploymentResource) Create() sdk.ResourceFunc { } } + if autoScaleProfile := model.AutoScaleProfile; len(autoScaleProfile) > 0 { + var autoScaleProfiles []nginxdeployment.ScaleProfile + for _, profile := range autoScaleProfile { + autoScaleProfiles = append(autoScaleProfiles, nginxdeployment.ScaleProfile{ + Name: profile.Name, + Capacity: nginxdeployment.ScaleProfileCapacity{ + Min: profile.Min, + Max: profile.Max, + }, + }) + } + prop.ScalingProperties = &nginxdeployment.NginxDeploymentScalingProperties{ + AutoScaleSettings: &nginxdeployment.NginxDeploymentScalingPropertiesAutoScaleSettings{ + Profiles: autoScaleProfiles, + }, + } + } + if model.Email != "" { prop.UserProfile = &nginxdeployment.NginxDeploymentUserProfile{ PreferredEmail: pointer.FromString(model.Email), @@ -418,7 +471,19 @@ func (m DeploymentResource) Read() sdk.ResourceFunc { } if scaling := props.ScalingProperties; scaling != nil { - output.Capacity = pointer.ToInt64(props.ScalingProperties.Capacity) + if capacity := scaling.Capacity; capacity != nil { + output.Capacity = pointer.ToInt64(props.ScalingProperties.Capacity) + } + if autoScaleProfiles := scaling.AutoScaleSettings; autoScaleProfiles != nil { + profiles := autoScaleProfiles.Profiles + for _, profile := range profiles { + output.AutoScaleProfile = append(output.AutoScaleProfile, AutoScaleProfile{ + Name: profile.Name, + Min: profile.Capacity.Min, + Max: profile.Capacity.Max, + }) + } + } } if userProfile := props.UserProfile; userProfile != nil && userProfile.PreferredEmail != nil { @@ -492,6 +557,24 @@ func (m DeploymentResource) Update() sdk.ResourceFunc { } } + if meta.ResourceData.HasChange("auto_scale_profile") && len(model.AutoScaleProfile) > 0 { + var autoScaleProfiles []nginxdeployment.ScaleProfile + for _, profile := range model.AutoScaleProfile { + autoScaleProfiles = append(autoScaleProfiles, nginxdeployment.ScaleProfile{ + Name: profile.Name, + Capacity: nginxdeployment.ScaleProfileCapacity{ + Min: profile.Min, + Max: profile.Max, + }, + }) + } + req.Properties.ScalingProperties = &nginxdeployment.NginxDeploymentScalingProperties{ + AutoScaleSettings: &nginxdeployment.NginxDeploymentScalingPropertiesAutoScaleSettings{ + Profiles: autoScaleProfiles, + }, + } + } + if meta.ResourceData.HasChange("email") { req.Properties.UserProfile = &nginxdeployment.NginxDeploymentUserProfile{ PreferredEmail: pointer.FromString(model.Email), diff --git a/internal/services/nginx/nginx_deployment_resource_test.go b/internal/services/nginx/nginx_deployment_resource_test.go index 43ca2d9e56f8..2f2204ecb6fc 100644 --- a/internal/services/nginx/nginx_deployment_resource_test.go +++ b/internal/services/nginx/nginx_deployment_resource_test.go @@ -131,6 +131,45 @@ resource "azurerm_nginx_deployment" "test" { `, a.template(data), data.RandomInteger, data.Locations.Primary) } +func (a DeploymentResource) basicAutoscaling(data acceptance.TestData) string { + return fmt.Sprintf(` + + + + +%s + +resource "azurerm_nginx_deployment" "test" { + name = "acctest-%[2]d" + resource_group_name = azurerm_resource_group.test.name + sku = "standard_Monthly" + location = azurerm_resource_group.test.location + diagnose_support_enabled = true + automatic_upgrade_channel = "stable" + + frontend_public { + ip_address = [azurerm_public_ip.test.id] + } + + network_interface { + subnet_id = azurerm_subnet.test.id + } + + auto_scale_profile { + name = "test" + min = 10 + max = 30 + } + + email = "test@test.com" + + tags = { + foo = "bar" + } +} +`, a.template(data), data.RandomInteger, data.Locations.Primary) +} + func (a DeploymentResource) update(data acceptance.TestData) string { return fmt.Sprintf(` @@ -281,3 +320,20 @@ resource "azurerm_subnet" "test" { } `, data.RandomInteger, data.Locations.Primary) } + +func TestAccNginxDeployment_autoscaling(t *testing.T) { + data := acceptance.BuildTestData(t, nginx.DeploymentResource{}.ResourceType(), "test") + r := DeploymentResource{} + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basicAutoscaling(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("auto_scale_profile.0.name").HasValue("test"), + check.That(data.ResourceName).Key("auto_scale_profile.0.min").HasValue("10"), + check.That(data.ResourceName).Key("auto_scale_profile.0.min").HasValue("30"), + ), + }, + data.ImportStep(), + }) +}