forked from awsdocs/aws-doc-sdk-examples
/
DisableAlarm.go
73 lines (62 loc) · 1.93 KB
/
DisableAlarm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[cloudwatch.go.disable_alarm]
package main
// snippet-start:[cloudwatch.go.disable_alarm.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
// snippet-end:[cloudwatch.go.disable_alarm.imports]
// DisableAlarm disables an alarm
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
// alarmName is the name of the alarm
//
// Output:
//
// If successful, nil
// Otherwise, the error from a call to DisableAlarmActions
func DisableAlarm(sess *session.Session, alarmName *string) error {
// Create new CloudWatch client.
// snippet-start:[cloudwatch.go.disable_call]
svc := cloudwatch.New(sess)
_, err := svc.DisableAlarmActions(&cloudwatch.DisableAlarmActionsInput{
AlarmNames: []*string{
alarmName,
},
})
// snippet-end:[cloudwatch.go.disable_call]
if err != nil {
return err
}
return nil
}
func main() {
// snippet-start:[cloudwatch.go.disable_args]
alarmName := flag.String("a", "", "The name of the alarm to disable")
flag.Parse()
if *alarmName == "" {
fmt.Println("You must supply an alarm name to disable")
return
}
// snippet-end:[cloudwatch.go.disable_args]
// Initialize a session that the SDK uses to load
// credentials from the shared credentials file ~/.aws/credentials
// and configuration from the shared configuration file ~/.aws/config.
// snippet-start:[cloudwatch.go.disable_session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[cloudwatch.go.disable_session]
err := DisableAlarm(sess, alarmName)
if err != nil {
fmt.Println("Could not disable alarm " + *alarmName)
} else {
fmt.Println("Disabled alarm " + *alarmName)
}
}
// snippet-end:[cloudwatch.go.disable_alarm]