Skip to content

Commit

Permalink
Add autoscaling to NGINXaaS
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetsarna committed Feb 13, 2024
1 parent 6cf32cf commit b146452
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 6 deletions.
38 changes: 37 additions & 1 deletion internal/services/nginx/nginx_deployment_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {

Check failure on line 297 in internal/services/nginx/nginx_deployment_data_source.go

View workflow job for this annotation

GitHub Actions / test

scaling.AutoScaleSettings undefined (type *nginxdeployment.NginxDeploymentScalingProperties has no field or method AutoScaleSettings)

Check failure on line 297 in internal/services/nginx/nginx_deployment_data_source.go

View workflow job for this annotation

GitHub Actions / tflint

scaling.AutoScaleSettings undefined (type *nginxdeployment.NginxDeploymentScalingProperties has no field or method AutoScaleSettings)

Check failure on line 297 in internal/services/nginx/nginx_deployment_data_source.go

View workflow job for this annotation

GitHub Actions / golint

scaling.AutoScaleSettings undefined (type *nginxdeployment.NginxDeploymentScalingProperties has no field or method AutoScaleSettings)

Check failure on line 297 in internal/services/nginx/nginx_deployment_data_source.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

scaling.AutoScaleSettings undefined (type *nginxdeployment.NginxDeploymentScalingProperties has no field or method AutoScaleSettings)

Check failure on line 297 in internal/services/nginx/nginx_deployment_data_source.go

View workflow job for this annotation

GitHub Actions / document-lint

scaling.AutoScaleSettings undefined (type *nginxdeployment.NginxDeploymentScalingProperties has no field or method AutoScaleSettings)

Check failure on line 297 in internal/services/nginx/nginx_deployment_data_source.go

View workflow job for this annotation

GitHub Actions / detect

scaling.AutoScaleSettings undefined (type *nginxdeployment.NginxDeploymentScalingProperties has no field or method AutoScaleSettings)
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 {
Expand Down
27 changes: 27 additions & 0 deletions internal/services/nginx/nginx_deployment_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
})
}
93 changes: 88 additions & 5 deletions internal/services/nginx/nginx_deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -310,6 +345,24 @@ func (m DeploymentResource) Create() sdk.ResourceFunc {
}
}

if autoScaleProfile := model.AutoScaleProfile; len(autoScaleProfile) > 0 {
var autoScaleProfiles []nginxdeployment.ScaleProfile

Check failure on line 349 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / test

undefined: nginxdeployment.ScaleProfile

Check failure on line 349 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

undefined: nginxdeployment.ScaleProfile

Check failure on line 349 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / golint

undefined: nginxdeployment.ScaleProfile

Check failure on line 349 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

undefined: nginxdeployment.ScaleProfile

Check failure on line 349 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

undefined: nginxdeployment.ScaleProfile

Check failure on line 349 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / detect

undefined: nginxdeployment.ScaleProfile
for _, profile := range autoScaleProfile {
autoScaleProfiles = append(autoScaleProfiles, nginxdeployment.ScaleProfile{

Check failure on line 351 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / test

undefined: nginxdeployment.ScaleProfile

Check failure on line 351 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

undefined: nginxdeployment.ScaleProfile

Check failure on line 351 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / golint

undefined: nginxdeployment.ScaleProfile

Check failure on line 351 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

undefined: nginxdeployment.ScaleProfile

Check failure on line 351 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

undefined: nginxdeployment.ScaleProfile

Check failure on line 351 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / detect

undefined: nginxdeployment.ScaleProfile
Name: profile.Name,
Capacity: nginxdeployment.ScaleProfileCapacity{

Check failure on line 353 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / test

undefined: nginxdeployment.ScaleProfileCapacity

Check failure on line 353 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

undefined: nginxdeployment.ScaleProfileCapacity

Check failure on line 353 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / golint

undefined: nginxdeployment.ScaleProfileCapacity

Check failure on line 353 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

undefined: nginxdeployment.ScaleProfileCapacity

Check failure on line 353 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

undefined: nginxdeployment.ScaleProfileCapacity

Check failure on line 353 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / detect

undefined: nginxdeployment.ScaleProfileCapacity
Min: profile.Min,
Max: profile.Max,
},
})
}
prop.ScalingProperties = &nginxdeployment.NginxDeploymentScalingProperties{
AutoScaleSettings: &nginxdeployment.NginxDeploymentScalingPropertiesAutoScaleSettings{

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / test

unknown field AutoScaleSettings in struct literal of type nginxdeployment.NginxDeploymentScalingProperties

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / test

undefined: nginxdeployment.NginxDeploymentScalingPropertiesAutoScaleSettings

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

unknown field AutoScaleSettings in struct literal of type nginxdeployment.NginxDeploymentScalingProperties

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / tflint

undefined: nginxdeployment.NginxDeploymentScalingPropertiesAutoScaleSettings

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / golint

unknown field AutoScaleSettings in struct literal of type nginxdeployment.NginxDeploymentScalingProperties

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / golint

undefined: nginxdeployment.NginxDeploymentScalingPropertiesAutoScaleSettings

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

unknown field AutoScaleSettings in struct literal of type nginxdeployment.NginxDeploymentScalingProperties

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / compatibility-32bit-test

undefined: nginxdeployment.NginxDeploymentScalingPropertiesAutoScaleSettings

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

unknown field AutoScaleSettings in struct literal of type nginxdeployment.NginxDeploymentScalingProperties

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / document-lint

undefined: nginxdeployment.NginxDeploymentScalingPropertiesAutoScaleSettings

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / detect

unknown field AutoScaleSettings in struct literal of type nginxdeployment.NginxDeploymentScalingProperties

Check failure on line 360 in internal/services/nginx/nginx_deployment_resource.go

View workflow job for this annotation

GitHub Actions / detect

undefined: nginxdeployment.NginxDeploymentScalingPropertiesAutoScaleSettings
Profiles: autoScaleProfiles,
},
}
}

if model.Email != "" {
prop.UserProfile = &nginxdeployment.NginxDeploymentUserProfile{
PreferredEmail: pointer.FromString(model.Email),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
56 changes: 56 additions & 0 deletions internal/services/nginx/nginx_deployment_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"
tags = {
foo = "bar"
}
}
`, a.template(data), data.RandomInteger, data.Locations.Primary)
}

func (a DeploymentResource) update(data acceptance.TestData) string {
return fmt.Sprintf(`
Expand Down Expand Up @@ -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(),
})
}

0 comments on commit b146452

Please sign in to comment.