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

data_protection_backup_policy_postgresql_resource: fix provider crash if given an empty criteria block #17904

Merged
merged 1 commit into from
Aug 12, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ func resourceDataProtectionBackupPolicyPostgreSQLCreate(d *pluginsdk.ResourceDat
return tf.ImportAsExistsError("azurerm_data_protection_backup_policy_postgresql", id.ID())
}

taggingCriteria := expandBackupPolicyPostgreSQLTaggingCriteriaArray(d.Get("retention_rule").([]interface{}))
taggingCriteria, err := expandBackupPolicyPostgreSQLTaggingCriteriaArray(d.Get("retention_rule").([]interface{}))
if err != nil {
return err
}
policyRules := make([]dataprotection.BasicBasePolicyRule, 0)
policyRules = append(policyRules, expandBackupPolicyPostgreSQLAzureBackupRuleArray(d.Get("backup_repeating_time_intervals").([]interface{}), taggingCriteria)...)
policyRules = append(policyRules, expandBackupPolicyPostgreSQLDefaultAzureRetentionRule(d.Get("default_retention_duration")))
Expand Down Expand Up @@ -351,7 +354,7 @@ func expandBackupPolicyPostgreSQLDefaultAzureRetentionRule(input interface{}) da
}
}

func expandBackupPolicyPostgreSQLTaggingCriteriaArray(input []interface{}) *[]dataprotection.TaggingCriteria {
func expandBackupPolicyPostgreSQLTaggingCriteriaArray(input []interface{}) (*[]dataprotection.TaggingCriteria, error) {
results := []dataprotection.TaggingCriteria{
{
Criteria: nil,
Expand All @@ -365,20 +368,29 @@ func expandBackupPolicyPostgreSQLTaggingCriteriaArray(input []interface{}) *[]da
}
for _, item := range input {
v := item.(map[string]interface{})
results = append(results, dataprotection.TaggingCriteria{
Criteria: expandBackupPolicyPostgreSQLCriteriaArray(v["criteria"].([]interface{})),
result := dataprotection.TaggingCriteria{
IsDefault: utils.Bool(false),
TaggingPriority: utils.Int64(int64(v["priority"].(int))),
TagInfo: &dataprotection.RetentionTag{
ID: utils.String(v["name"].(string) + "_"),
TagName: utils.String(v["name"].(string)),
},
})
}
criteria, err := expandBackupPolicyPostgreSQLCriteriaArray(v["criteria"].([]interface{}))
if err != nil {
return nil, err
}
result.Criteria = criteria

results = append(results, result)
}
return &results
return &results, nil
}

func expandBackupPolicyPostgreSQLCriteriaArray(input []interface{}) *[]dataprotection.BasicBackupCriteria {
func expandBackupPolicyPostgreSQLCriteriaArray(input []interface{}) (*[]dataprotection.BasicBackupCriteria, error) {
if len(input) == 0 || input[0] == nil {
return nil, fmt.Errorf("criteria is a required field, cannot leave blank")
}
Comment on lines +391 to +393
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than doing this, we can add at least one of to the schema for this block - can we update this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @tombuildsstuff , looks like AtLeastOneOf can only be used with TypeList has MaxItems: 1. But here the parent block retention_rule can have multiple items, we cannot reference the attribute under it with AtLeastOneOf.

results := make([]dataprotection.BasicBackupCriteria, 0)
for _, item := range input {
v := item.(map[string]interface{})
Expand Down Expand Up @@ -429,7 +441,7 @@ func expandBackupPolicyPostgreSQLCriteriaArray(input []interface{}) *[]dataprote
ObjectType: dataprotection.ObjectTypeBasicBackupCriteriaObjectTypeScheduleBasedBackupCriteria,
})
}
return &results
return &results, nil
}

func flattenBackupPolicyPostgreSQLBackupRuleArray(input *[]dataprotection.BasicBasePolicyRule) []interface{} {
Expand Down