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_job - Add Identity property to Container App Job Scaling Rules #27489

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
278 changes: 278 additions & 0 deletions internal/services/containerapps/container_app_job_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,51 @@ func TestAccContainerAppJob_eventTrigger(t *testing.T) {
})
}

func TestAccContainerAppJob_eventTriggerScaleRuleUserIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_app_job", "test")
r := ContainerAppJobResource{}

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

func TestAccContainerAppJob_eventTriggerScaleRuleSystemIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_app_job", "test")
r := ContainerAppJobResource{}

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

func TestAccContainerAppJob_eventTriggerScaleRuleSystemAndUserIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_app_job", "test")
r := ContainerAppJobResource{}

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

func TestAccContainerAppJob_manualTrigger(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_app_job", "test")
r := ContainerAppJobResource{}
Expand Down Expand Up @@ -352,6 +397,239 @@ resource "azurerm_container_app_job" "test" {
`, template, data.RandomInteger)
}

func (r ContainerAppJobResource) eventTriggerScaleRuleUserIdentity(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%[1]s

resource "azurerm_user_assigned_identity" "test" {
name = "acct-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_container_app_job" "test" {
name = "acctest-cajob%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
container_app_environment_id = azurerm_container_app_environment.test.id

identity {
type = "UserAssigned"
identity_ids = [azurerm_user_assigned_identity.test.id]
}

replica_timeout_in_seconds = 10
replica_retry_limit = 10
event_trigger_config {
parallelism = 4
scale {
max_executions = 10
min_executions = 1
polling_interval_in_seconds = 10
rules {
authentication {
secret_name = "my-secret"
trigger_parameter = "my-trigger-parameter"
}
metadata = {
topic_name = "my-topic"
}
name = "servicebuscalingrule"
identity = azurerm_user_assigned_identity.test.id
custom_rule_type = "azure-servicebus"
}
}
}

template {
container {
image = "jackofallops/azure-containerapps-python-acctest:v0.0.1"
name = "testcontainerappsjob0"
liveness_probe {
transport = "HTTP"
port = 5000
path = "/health"

header {
name = "Cache-Control"
value = "no-cache"
}

initial_delay = 5
interval_seconds = 20
timeout = 2
failure_count_threshold = 1
}
cpu = 0.5
memory = "1Gi"
}
}
}
`, template, data.RandomInteger)
}

func (r ContainerAppJobResource) eventTriggerScaleRuleSystemIdentity(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%[1]s

resource "azurerm_container_app_job" "test" {
name = "acctest-cajob%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
container_app_environment_id = azurerm_container_app_environment.test.id

identity {
type = "SystemAssigned"
}

replica_timeout_in_seconds = 10
replica_retry_limit = 10
event_trigger_config {
parallelism = 4
scale {
max_executions = 10
min_executions = 1
polling_interval_in_seconds = 10
rules {
authentication {
secret_name = "my-secret"
trigger_parameter = "my-trigger-parameter"
}
metadata = {
topic_name = "my-topic"
}
name = "servicebuscalingrule"
identity = "system"
custom_rule_type = "azure-servicebus"
}
}
}

template {
container {
image = "jackofallops/azure-containerapps-python-acctest:v0.0.1"
name = "testcontainerappsjob0"
liveness_probe {
transport = "HTTP"
port = 5000
path = "/health"

header {
name = "Cache-Control"
value = "no-cache"
}

initial_delay = 5
interval_seconds = 20
timeout = 2
failure_count_threshold = 1
}
cpu = 0.5
memory = "1Gi"
}
}
}
`, template, data.RandomInteger)
}

func (r ContainerAppJobResource) eventTriggerScaleRuleSystemAndUserIdentity(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%[1]s

resource "azurerm_user_assigned_identity" "test" {
name = "acct-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_container_app_job" "test" {
name = "acctest-cajob%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
container_app_environment_id = azurerm_container_app_environment.test.id

identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [azurerm_user_assigned_identity.test.id]
}

replica_timeout_in_seconds = 10
replica_retry_limit = 10
event_trigger_config {
parallelism = 4
scale {
max_executions = 10
min_executions = 1
polling_interval_in_seconds = 10
rules {
authentication {
secret_name = "my-secret"
trigger_parameter = "my-trigger-parameter"
}
metadata = {
topic_name = "my-topic"
}
name = "servicebuscalingrule"
identity = azurerm_user_assigned_identity.test.id
custom_rule_type = "azure-servicebus"
}
rules {
authentication {
secret_name = "my-secret"
trigger_parameter = "my-trigger-parameter"
}
metadata = {
pool-name = "my-pool"
}
name = "otherrule"
identity = "system"
custom_rule_type = "azure-pipelines"
}
}
}

template {
container {
image = "jackofallops/azure-containerapps-python-acctest:v0.0.1"
name = "testcontainerappsjob0"
liveness_probe {
transport = "HTTP"
port = 5000
path = "/health"

header {
name = "Cache-Control"
value = "no-cache"
}

initial_delay = 5
interval_seconds = 20
timeout = 2
failure_count_threshold = 1
}
cpu = 0.5
memory = "1Gi"
}
}
}
`, template, data.RandomInteger)
}

func (r ContainerAppJobResource) manualTrigger(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
Expand Down
10 changes: 8 additions & 2 deletions internal/services/containerapps/helpers/container_app_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type ScaleModel struct {

type ScaleRule struct {
Auth []ScaleRuleAuth `tfschema:"authentication"`
Identity string `tfschema:"identity"`
Metadata map[string]interface{} `tfschema:"metadata"`
Name string `tfschema:"name"`
Type string `tfschema:"custom_rule_type"`
Expand Down Expand Up @@ -239,6 +240,10 @@ func ExpandContainerAppJobScaleRules(input []ScaleRule) *[]jobs.JobScaleRule {

rule.Auth = ExpandContainerAppJobScaleRulesAuth(v.Auth)

if v.Identity != "" {
rule.Identity = pointer.To(v.Identity)
}

if v.Metadata != nil {
metadata := reflect.ValueOf(v.Metadata)
rule.Metadata = pointer.To(metadata.Interface())
Expand Down Expand Up @@ -957,8 +962,9 @@ func flattenContainerAppJobScaleRules(input *[]jobs.JobScaleRule) []ScaleRule {

for _, v := range *input {
rule := ScaleRule{
Name: pointer.From(v.Name),
Type: pointer.From(v.Type),
Identity: pointer.From(v.Identity),
Name: pointer.From(v.Name),
Type: pointer.From(v.Type),
}

if v.Metadata != nil {
Expand Down
17 changes: 17 additions & 0 deletions internal/services/containerapps/helpers/container_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3041,6 +3041,7 @@ func AzureQueueScaleRuleSchemaComputed() *pluginsdk.Schema {
type CustomScaleRule struct {
Name string `tfschema:"name"`
Metadata map[string]string `tfschema:"metadata"`
Identity string `tfschema:"identity"`
CustomRuleType string `tfschema:"custom_rule_type"`
Authentications []ScaleRuleAuthentication `tfschema:"authentication"`
}
Expand All @@ -3065,6 +3066,16 @@ func CustomScaleRuleSchema() *pluginsdk.Schema {
},
},

"identity": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.Any(
commonids.ValidateUserAssignedIdentityID,
validation.StringInSlice([]string{"system"}, true),
),
Description: "The identity to use for accessing key vault reference.",
},

"custom_rule_type": {
Type: pluginsdk.TypeString,
Required: true,
Expand Down Expand Up @@ -3128,6 +3139,12 @@ func CustomScaleRuleSchemaComputed() *pluginsdk.Schema {
},
},

"identity": {
Type: pluginsdk.TypeString,
Computed: true,
Description: "The identity to use for accessing key vault reference.",
},

"custom_rule_type": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/container_app_job.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ A `rules` block supports the following:

* `custom_rule_type` - (Optional) Type of the scale rule.

* `identity` - (Optional) The identity to use for the scale rule. This can either be the Resource ID of a User Assigned Identity, or `System` for the System Assigned Identity.
Copy link

@spawluk-zartis spawluk-zartis Feb 3, 2025

Choose a reason for hiding this comment

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

I think this doc section should be added to azurerm_container_app.template.custom_scale_rule as well


* `metadata` - (Optional) Metadata properties to describe the scale rule.

* `authentication` - (Optional) A `authentication` block as defined below.
Expand Down
Loading