Skip to content

Commit

Permalink
Updated azurerm_nginx_deployment to support latest API changes (scali…
Browse files Browse the repository at this point in the history
…ng & user_profile)

Users may now scale their NGINXaaS deployments and provide preferred contact information.
  • Loading branch information
agazeley committed Oct 18, 2023
1 parent 2831393 commit b9b6c0b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
78 changes: 77 additions & 1 deletion internal/services/nginx/nginx_deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ type NetworkInterface struct {
SubnetId string `tfschema:"subnet_id"`
}

type ScalingProperties struct {
Capacity int64 `tfschema:"capacity"`
}

type UserProfile struct {
PreferredEmail string `tfschema:"preferred_email"`
}

type DeploymentModel struct {
ResourceGroupName string `tfschema:"resource_group_name"`
Name string `tfschema:"name"`
Expand All @@ -51,7 +59,9 @@ type DeploymentModel struct {
FrontendPublic []FrontendPublic `tfschema:"frontend_public"`
FrontendPrivate []FrontendPrivate `tfschema:"frontend_private"`
NetworkInterface []NetworkInterface `tfschema:"network_interface"`
ScalingProperties []ScalingProperties `tfschema:"scaling"`
Tags map[string]string `tfschema:"tags"`
UserProfile []UserProfile `tfschema:"user_profile"`
}

type DeploymentResource struct{}
Expand Down Expand Up @@ -179,7 +189,37 @@ func (m DeploymentResource) Arguments() map[string]*pluginsdk.Schema {
},
},

"scaling": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: false,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"capacity": {
Type: pluginsdk.TypeInt,
Required: true,
ValidateFunc: validation.IntPositive,
},
},
},
},

"tags": commonschema.Tags(),

"user_profile": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: false,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"preferred_email": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},
}
}

Expand Down Expand Up @@ -222,7 +262,7 @@ func (m DeploymentResource) Create() sdk.ResourceFunc {

if !response.WasNotFound(existing.HttpResponse) {
if err != nil {
return fmt.Errorf("retreiving %s: %v", id, err)
return fmt.Errorf("retrieving %s: %v", id, err)
}
return meta.ResourceRequiresImport(m.ResourceType(), id)
}
Expand Down Expand Up @@ -282,6 +322,18 @@ func (m DeploymentResource) Create() sdk.ResourceFunc {
prop.NetworkProfile.NetworkInterfaceConfiguration.SubnetId = pointer.FromString(model.NetworkInterface[0].SubnetId)
}

if len(model.ScalingProperties) > 0 {
prop.ScalingProperties = &nginxdeployment.NginxDeploymentScalingProperties{
Capacity: pointer.FromInt64(model.ScalingProperties[0].Capacity),
}
}

if len(model.UserProfile) > 0 && model.UserProfile[0].PreferredEmail != "" {
prop.UserProfile = &nginxdeployment.NginxDeploymentUserProfile{
PreferredEmail: &model.UserProfile[0].PreferredEmail,
}
}

req.Properties = prop

req.Identity, err = identity.ExpandSystemAndUserAssignedMapFromModel(model.Identity)
Expand Down Expand Up @@ -377,6 +429,18 @@ func (m DeploymentResource) Read() sdk.ResourceFunc {
}
}

if scaling := props.ScalingProperties; scaling != nil {
output.ScalingProperties = []ScalingProperties{{
Capacity: pointer.ToInt64(props.ScalingProperties.Capacity),
}}
}

if userProfile := props.UserProfile; userProfile != nil && userProfile.PreferredEmail != nil {
output.UserProfile = []UserProfile{{
PreferredEmail: pointer.ToString(props.UserProfile.PreferredEmail),
}}
}

flattenedIdentity, err := identity.FlattenSystemAndUserAssignedMapToModel(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %v", err)
Expand Down Expand Up @@ -434,6 +498,18 @@ func (m DeploymentResource) Update() sdk.ResourceFunc {
req.Properties.EnableDiagnosticsSupport = pointer.FromBool(model.DiagnoseSupportEnabled)
}

if meta.ResourceData.HasChange("scaling") && len(model.ScalingProperties) > 0 {
req.Properties.ScalingProperties = &nginxdeployment.NginxDeploymentScalingProperties{
Capacity: pointer.FromInt64(model.ScalingProperties[0].Capacity),
}
}

if meta.ResourceData.HasChange("user_profile") && len(model.UserProfile) > 0 {
req.Properties.UserProfile = &nginxdeployment.NginxDeploymentUserProfile{
PreferredEmail: pointer.FromString(model.UserProfile[0].PreferredEmail),
}
}

if err := client.DeploymentsUpdateThenPoll(ctx, *id, req); err != nil {
return fmt.Errorf("updating %s: %v", id, err)
}
Expand Down
29 changes: 29 additions & 0 deletions internal/services/nginx/nginx_deployment_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func TestAccNginxDeployment_basic(t *testing.T) {
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("scaling").HasValue("10"),
check.That(data.ResourceName).Key("user_profile").Exists(),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -84,6 +86,8 @@ func (a DeploymentResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_nginx_deployment" "test" {
Expand All @@ -100,6 +104,15 @@ resource "azurerm_nginx_deployment" "test" {
network_interface {
subnet_id = azurerm_subnet.test.id
}
scaling {
capacity = 10
}
user_profile {
preferred_email = "[email protected]"
}
tags = {
foo = "bar"
}
Expand All @@ -111,6 +124,8 @@ func (a DeploymentResource) update(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_nginx_deployment" "test" {
Expand All @@ -128,6 +143,14 @@ resource "azurerm_nginx_deployment" "test" {
subnet_id = azurerm_subnet.test.id
}
scaling {
capacity = 20
}
user_profile {
preferred_email = "[email protected]"
}
tags = {
foo = "bar2"
}
Expand All @@ -139,6 +162,8 @@ func (a DeploymentResource) identityUser(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_user_assigned_identity" "test" {
Expand All @@ -165,6 +190,10 @@ resource "azurerm_nginx_deployment" "test" {
network_interface {
subnet_id = azurerm_subnet.test.id
}
scaling {
capacity = 10
}
}
`, a.template(data), data.RandomInteger)
}
Expand Down
26 changes: 26 additions & 0 deletions website/docs/r/nginx_deployment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ resource "azurerm_nginx_deployment" "example" {
network_interface {
subnet_id = azurerm_subnet.example.id
}
scaling {
capacity = 20
}
user_profile {
preferred_email = "[email protected]"
}
}
```

Expand Down Expand Up @@ -99,8 +107,12 @@ The following arguments are supported:

* `network_interface` - (Optional) One or more `network_interface` blocks as defined below. Changing this forces a new Nginx Deployment to be created.

* `scaling` - (Optional) One or more `scaling` blocks as defined below.

* `tags` - (Optional) A mapping of tags which should be assigned to the Nginx Deployment.

* `user_profile` - (Optional) One or more `user_profile` blocks as defined below.

---

A `identity` block supports the following:
Expand Down Expand Up @@ -139,6 +151,20 @@ A `network_interface` block supports the following:

* `subnet_id` - (Required) Specify The SubNet Resource ID to this Nginx Deployment.

---

A `scaling` block supports the following:

* `capacity` - (Required) Specify the number of NGINX capacity units for this NGINX deployment.

-> **Note** For more information on NGINX capacity units, please refer to the [NGINX scaling guidance documentation](https://docs.nginx.com/nginxaas/azure/quickstart/scaling/)

---

A `user_profile` block supports the following:

* `preferred_email` - (Required) Specify the preferred support contact email address of the user used for sending alerts and notification.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:
Expand Down

0 comments on commit b9b6c0b

Please sign in to comment.