<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0


// snippet-start:[cloudwatch.php.enable_alarm.complete]
// snippet-start:[cloudwatch.php.enable_alarm.import]
require 'vendor/autoload.php';

use Aws\CloudWatch\CloudWatchClient;
use Aws\Exception\AwsException;

// snippet-end:[cloudwatch.php.enable_alarm.import]

/* ////////////////////////////////////////////////////////////////////////////
 * Purpose: Enables actions for specified alarms in Amazon CloudWatch.
 *
 * Prerequisites: At least one existing CloudWatch alarm.
 *
 * Inputs:
 * - $cloudWatchClient: An initialized CloudWatch client.
 * - $alarmNames: The names of the alarms to enable actions for.
 *
 * Returns: Information about the results of the request;
 * otherwise, the error message.
 * ///////////////////////////////////////////////////////////////////////// */

// snippet-start:[cloudwatch.php.enable_alarm.main]
function enableAlarmActions($cloudWatchClient, $alarmNames)
{
    try {
        $result = $cloudWatchClient->enableAlarmActions([
            'AlarmNames' => $alarmNames
        ]);

        if (isset($result['@metadata']['effectiveUri'])) {
            return 'At the effective URI of ' .
                $result['@metadata']['effectiveUri'] .
                ', actions for any matching alarms have been enabled.';
        } else {
            return'Actions for some matching alarms ' .
                'might not have been enabled.';
        }
    } catch (AwsException $e) {
        return 'Error: ' . $e->getAwsErrorMessage();
    }
}

function enableTheAlarmActions()
{
    $alarmNames = array('my-alarm');

    $cloudWatchClient = new CloudWatchClient([
        'profile' => 'default',
        'region' => 'us-east-1',
        'version' => '2010-08-01'
    ]);

    echo enableAlarmActions($cloudWatchClient, $alarmNames);
}

// Uncomment the following line to run this code in an AWS account.
// enableTheAlarmActions();
// snippet-end:[cloudwatch.php.enable_alarm.main]
// snippet-end:[cloudwatch.php.enable_alarm.complete]
