Skip to content

Feature/add commit all action#509

Open
gmcarneiro-palo wants to merge 2 commits intomainfrom
feature/add-commit-all-action
Open

Feature/add commit all action#509
gmcarneiro-palo wants to merge 2 commits intomainfrom
feature/add-commit-all-action

Conversation

@gmcarneiro-palo
Copy link

Add panos_commit_all action for Panorama push to devices

Overview

This PR implements a new Terraform action panos_commit_all that enables pushing committed configuration from Panorama to managed device groups. This complements the existing panos_commit action to provide a complete commit-and-push workflow for Panorama users.

Motivation

Currently, the provider has a panos_commit action that commits pending changes to Panorama, but there's no way to push those committed changes to managed devices. Users need to manually push via the Panorama UI or use external scripts, breaking the infrastructure-as-code workflow.

This action fills that gap by providing a native Terraform way to perform the "Push to Devices" operation.

What's New

Core Features

  1. Automatic Device Group Discovery

    • Automatically detects all device groups in Panorama
    • Uses operational command (show dg-hierarchy) with config API fallback
    • Supports multiple XML response formats for cross-version compatibility
  2. Selective Device Group Targeting

    • Optional device_groups parameter to push to specific groups only
    • Useful for staged rollouts and testing
    • Example: Push to production only, skip staging
  3. Clear Workflow Separation

    • Explicitly separates "commit" from "push" operations
    • Mirrors Panorama UI workflow: Commit → Push to Devices
    • Prevents confusion about what the action does

Implementation Details

Files Added:

  • internal/provider/commit_all.go - Action framework and schema
  • internal/provider/commit_all_crud.go - Core API implementation
  • docs/actions/commit_all.md - Comprehensive documentation (505 lines)
  • examples/COMMIT_ALL_EXAMPLES.md - Usage guide and comparisons

Files Modified:

  • internal/provider/provider.go - Register new action

Key Technical Decisions:

  • Uses PAN-OS XML API type=commit&action=all with <commit-all><shared-policy> structure
  • Implements job polling to wait for completion (2-second intervals)
  • Provides detailed error messages for common issues
  • No state tracking (actions are stateless by design)

Usage Examples

Basic Usage

provider "panos" {
  hostname = "panorama.example.com"
  api_key  = var.panos_api_key
}

resource "panos_security_policy" "example" {
  location = {
    device_group = { name = "Production" }
  }

  rules = [{
    name                  = "Allow-Web"
    source_zones          = ["trust"]
    destination_zones     = ["untrust"]
    applications          = ["web-browsing"]
    action                = "allow"
  }]
}

# Step 1: Commit changes to Panorama
action "panos_commit" "commit_to_panorama" {}

# Step 2: Push to all device groups
action "panos_commit_all" "push_to_all" {}

# Step 3: Or push to specific device groups only
action "panos_commit_all" "push_to_production" {
  config {
    device_groups = ["Production", "DMZ"]
  }
}

Workflow

# 1. Apply resources (creates pending changes)
terraform apply

# 2. Commit changes to Panorama
terraform action -invoke commit_to_panorama

# 3. Push to devices
terraform action invoke push_to_all
# OR push to specific groups
terraform action invoke push_to_production

Automated Workflow with Lifecycle Hooks

resource "panos_security_policy" "auto_deploy" {
  # ... configuration ...

  lifecycle {
    action_trigger {
      events  = [after_create, after_update]
      actions = [
        action.panos_commit.commit_to_panorama,
        action.panos_commit_all.push_to_production
      ]
    }
  }
}

Configuration Reference

device_groups (Optional)

- Type: List of strings
- Default: All device groups in Panorama
- Example: ["Production", "DMZ", "Branch-Offices"]

When specified, only pushes configuration to the listed device groups. When omitted, pushes to all device groups.

Testing

Tested on:
- Panorama version: 11.2.6
- Terraform: 1.14.3
- Provider: 2.0.x (local build)

Test scenarios:
- ✅ Push to all device groups (auto-discovery)
- ✅ Push to specific device groups via device_groups parameter
- ✅ Push to multiple device groups
- ✅ Error handling when no device groups exist
- ✅ Job polling and completion
- ✅ Lifecycle action_trigger integration

Documentation

Comprehensive Documentation Included

- Action Reference (docs/actions/commit_all.md):
  - Overview and workflow explanation
  - Configuration reference
  - 10+ usage examples
  - Comparison with panos_commit
  - CI/CD integration examples
  - Troubleshooting guide
- Examples Guide (examples/COMMIT_ALL_EXAMPLES.md):
  - Comparison of Terraform Actions vs curl methods
  - Quick start guide
  - Best practices
  - Security considerations
  - Example Terraform configurations for older versions

## Breaking Changes

None. This is a new feature with no impact on existing functionality.

## Backward Compatibility

- ✅ Fully backward compatible
- ✅ Does not modify existing actions or resources
- ✅ Optional parameter (device_groups) defaults to safe behavior
- ✅ Works alongside existing panos_commit action

## Requirements

- Terraform 1.14+ (for Actions support)
- Panorama (not standalone firewalls)
- At least one device group configured in Panorama

## Migration Notes

For users currently using curl scripts or null_resource for commit-all:
1. Replace curl-based scripts with this action
2. Follow the documented workflow: commit → push
3. See examples/COMMIT_ALL_EXAMPLES.md for migration examples


## Checklist

[x] Implementation complete
[x] Documentation written
[x] Examples provided
[x] Manual testing performed
[x] No breaking changes
[x] Backward compatible


---
Note: This action requires Terraform 1.14+ which introduced Actions support. 

  Enhances the panos_commit_all action with device group targeting and
  automatic discovery capabilities. Clarifies the action's purpose as a
  push-only operation that requires panos_commit to be run first.

  Changes:

  Action configuration (internal/provider/commit_all.go):
  - Add device_groups parameter to allow specifying target device groups
  - Update description to clarify this action pushes (not commits) config
  - Add types.List field to CommitAllActionModel for device group list

  Core implementation (internal/provider/commit_all_crud.go):
  - Implement device group auto-discovery from Panorama
  - Support multiple XML response formats for cross-version compatibility
  - Add fetchAllDeviceGroups() using operational command (show dg-hierarchy)
  - Add fetchDeviceGroupsViaConfig() as fallback using config API
  - Parse action config to read device_groups parameter
  - Build proper XML structure with shared-policy and device group entries
  - Add comprehensive logging for debugging
  - Improve error messages with actionable solutions

  Documentation (docs/actions/commit_all.md):
  - Clarify this action does NOT commit, only pushes committed config
  - Add "Typical Workflow" section explaining commit-then-push pattern
  - Add Configuration section documenting device_groups parameter
  - Update all examples to show both panos_commit and panos_commit_all
  - Add examples for targeting specific device groups using config block
  - Update comparison table with clearer explanations
  - Add automated workflow example using lifecycle action_trigger hooks

  Examples (examples/COMMIT_ALL_EXAMPLES.md):
  - Add comprehensive comparison guide for different approaches
  - Include examples for both Terraform Actions and curl methods
  - Document the correct workflow with commit before push
  - Add quick start guide with step-by-step instructions

  Usage:
    # Push to all device groups
    action "panos_commit_all" "push_all" {}

    # Push to specific device groups
    action "panos_commit_all" "push_production" {
      config {
        device_groups = ["Production", "DMZ"]
      }
    }

  Breaking changes: None
  Backward compatible: Yes (device_groups is optional)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant