PHP code example of skluck / terraform-plan-parser

1. Go to this page and download the library: Download skluck/terraform-plan-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

skluck / terraform-plan-parser example snippets




use SK\TerraformParser\TerraformOutputParser;
use SK\TerraformParser\Terraform11OutputParser;
use SK\TerraformParser\Terraform12OutputParser;

$filename = '/path/to/terrraform/output';
$parser = new TerraformOutputParser;

// It is also possible to use the desired version of Terraform directly:
// $parser = new Terraform11OutputParser;
// $parser = new Terraform12OutputParser;

$output = $parser->parseFile($filename);

var_export($output);
// [
//     'errors' => [
//         'Failed to parse resource name (line 63)',
//         'Failed to parse attribute name (line 102)',
//         'Failed to parse attribute name (line 103)',
//     ],
//     'changedResources' => [
//         ResourceChange,
//         ResourceChange,
//         ResourceChange,
//     ],
//     "modules": [
//         {
//             "name": "module.mymodule",
//             "source": "./path/to/submodule",
//             "version": null
//         },
//         {
//             "name": "root",
//             "source": "git::https://git.example.com/terraform/mymodule2.git",
//             "version": "2.3.1"
//         }
//     ]
// ];



use SK\TerraformParser\TerraformOutputParser;

$filename = '/path/to/terrraform/output';
$contents = file_get_contents($filename);
$parser = new TerraformOutputParser;
$output = $parser->parse($contents);

echo json_encode($output, JSON_PRETTY_PRINT);
// {
//     "errors": [
//         "Failed to parse resource name (line 63)",
//         "Failed to parse attribute name (line 102)",
//         "Failed to parse attribute name (line 103)"
//     ],
//     "changedResources": [
//         ResourceChange,
//         ResourceChange,
//         {
//             "action": "create",
//             "name": "test2",
//             "fully_qualified_name": "module.test1.module.test2.null_resource.test2",
//             "module_path": "test1.test2",
//             "is_new": true,
//             "is_tainted": false,
//             "attributes": {
//                 "triggers.deploy_job_hash": {
//                     "name": "triggers.deploy_job_hash",
//                     "force_new_resource": true,
//                     "old": {
//                         "type": "string",
//                         "value": "6c37ac7175bdf35e"
//                     },
//                     "new": {
//                         "type": "computed",
//                         "value": null
//                     }
//                 }
//             }
//         }
//     ],
//     "modules": [
//         {
//             "name": "module.mymodule",
//             "source": "./path/to/submodule",
//             "version": null
//         },
//         {
//             "name": "root",
//             "source": "git::https://git.example.com/terraform/mymodule2.git",
//             "version": "2.3.1"
//         }
//     ]
// }