Skip to content

Commit c317b45

Browse files
authored
Merge pull request #7 from awsdocs/master
update from master
2 parents 1057ba6 + c375818 commit c317b45

File tree

118 files changed

+6193
-2537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+6193
-2537
lines changed

gov2/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.exe
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX - License - Identifier: Apache - 2.0
3+
// snippet-start:[cloudwatch.go-v2.CreateCustomMetric]
4+
package main
5+
6+
import (
7+
"context"
8+
"flag"
9+
"fmt"
10+
11+
"github.com/aws/aws-sdk-go-v2/config"
12+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
13+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
14+
)
15+
16+
// CWPutMetricDataAPI defines the interface for the PutMetricData function.
17+
// We use this interface to test the function using a mocked service.
18+
type CWPutMetricDataAPI interface {
19+
PutMetricData(ctx context.Context,
20+
params *cloudwatch.PutMetricDataInput,
21+
optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error)
22+
}
23+
24+
// CreateCustomMetric creates a new Amazon CloudWatch metric in a namespace
25+
// Inputs:
26+
// c is the context of the method call, which includes the Region
27+
// api is the interface that defines the method call
28+
// input defines the input arguments to the service call.
29+
// Output:
30+
// If success, a METHODOutput object containing the result of the service call and nil
31+
// Otherwise, nil and an error from the call to PutMetricData
32+
func CreateCustomMetric(c context.Context, api CWPutMetricDataAPI, input *cloudwatch.PutMetricDataInput) (*cloudwatch.PutMetricDataOutput, error) {
33+
resp, err := api.PutMetricData(c, input)
34+
35+
return resp, err
36+
}
37+
38+
func main() {
39+
namespace := flag.String("n", "", "The namespace for the metric")
40+
metricName := flag.String("m", "", "The name of the metric")
41+
value := flag.Float64("s", 0.0, "The number of seconds for the units")
42+
dimensionName := flag.String("dn", "", "The name of the dimension")
43+
dimensionValue := flag.String("dv", "", "The value of the dimension")
44+
flag.Parse()
45+
46+
if *namespace == "" || *metricName == "" || *dimensionName == "" || *dimensionValue == "" {
47+
fmt.Println("You must supply a namespace, metric name, dimension name, and dimension value")
48+
return
49+
}
50+
51+
cfg, err := config.LoadDefaultConfig()
52+
if err != nil {
53+
panic("configuration error, " + err.Error())
54+
}
55+
56+
client := cloudwatch.NewFromConfig(cfg)
57+
58+
input := &cloudwatch.PutMetricDataInput{
59+
Namespace: namespace,
60+
MetricData: []*types.MetricDatum{
61+
&types.MetricDatum{
62+
MetricName: metricName,
63+
Unit: types.StandardUnitSeconds,
64+
Value: value,
65+
Dimensions: []*types.Dimension{
66+
&types.Dimension{
67+
Name: dimensionName,
68+
Value: dimensionValue,
69+
},
70+
},
71+
},
72+
},
73+
}
74+
75+
_, err = CreateCustomMetric(context.Background(), client, input)
76+
if err != nil {
77+
fmt.Println()
78+
return
79+
}
80+
81+
fmt.Println("Created a custom metric")
82+
}
83+
84+
// snippet-end:[cloudwatch.go-v2.CreateCustomMetric]
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX - License - Identifier: Apache - 2.0
3+
4+
package main
5+
6+
import (
7+
"context"
8+
"encoding/json"
9+
"errors"
10+
"io/ioutil"
11+
"strconv"
12+
"testing"
13+
"time"
14+
15+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
16+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
17+
)
18+
19+
type CWPutMetricDataImpl struct{}
20+
21+
func (dt CWPutMetricDataImpl) PutMetricData(ctx context.Context,
22+
params *cloudwatch.PutMetricDataInput,
23+
optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error) {
24+
25+
output := &cloudwatch.PutMetricDataOutput{}
26+
27+
return output, nil
28+
}
29+
30+
type Config struct {
31+
Namespace string `json:"Namespace"`
32+
MetricName string `json:"MetricName"`
33+
MetricValueS string `json:"MetricValue"`
34+
MetricValue float64
35+
DimensionName string `json:"DimensionName"`
36+
DimensionValue string `json:"DimensionValue"`
37+
}
38+
39+
var configFileName = "config.json"
40+
41+
var globalConfig Config
42+
43+
func populateConfiguration(t *testing.T) error {
44+
content, err := ioutil.ReadFile(configFileName)
45+
if err != nil {
46+
return err
47+
}
48+
49+
text := string(content)
50+
51+
err = json.Unmarshal([]byte(text), &globalConfig)
52+
if err != nil {
53+
return err
54+
}
55+
56+
if globalConfig.Namespace == "" || globalConfig.MetricName == "" || globalConfig.MetricValueS == "" || globalConfig.DimensionName == "" || globalConfig.DimensionValue == "" {
57+
msg := "You must specify a value for Namespace, MetricName, MetricValue, DimensionName, and DimensionValue in " + configFileName
58+
return errors.New(msg)
59+
}
60+
61+
// Make sure metric value is a float64
62+
f, err := strconv.ParseFloat(globalConfig.MetricValueS, 64)
63+
if err != nil {
64+
return err
65+
}
66+
67+
globalConfig.MetricValue = f
68+
69+
return nil
70+
}
71+
72+
func TestCreateCustomMetric(t *testing.T) {
73+
thisTime := time.Now()
74+
nowString := thisTime.Format("2006-01-02 15:04:05 Monday")
75+
t.Log("Starting unit test at " + nowString)
76+
77+
err := populateConfiguration(t)
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
82+
input := &cloudwatch.PutMetricDataInput{
83+
Namespace: &globalConfig.Namespace,
84+
MetricData: []*types.MetricDatum{
85+
&types.MetricDatum{
86+
MetricName: &globalConfig.MetricName,
87+
Unit: types.StandardUnitSeconds,
88+
Value: &globalConfig.MetricValue,
89+
Dimensions: []*types.Dimension{
90+
&types.Dimension{
91+
Name: &globalConfig.DimensionName,
92+
Value: &globalConfig.DimensionValue,
93+
},
94+
},
95+
},
96+
},
97+
}
98+
99+
api := &CWPutMetricDataImpl{}
100+
101+
_, err = CreateCustomMetric(context.Background(), *api, input)
102+
if err != nil {
103+
t.Log("Got an error ...:")
104+
t.Log(err)
105+
return
106+
}
107+
108+
t.Log("Created a custom metric")
109+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"Namespace": "aws-docs-example-namespace",
3+
"MetricName": "aws-docs-example-metric-name",
4+
"MetricValue": "4",
5+
"DimensionName": "aws-docs-example-dimension-name",
6+
"DimensionValue": "aws-docs-example-dimension-value"
7+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX - License - Identifier: Apache - 2.0
3+
// snippet-start:[cloudwatch.go-v2.CreateEnableMetricAlarm]
4+
package main
5+
6+
import (
7+
"context"
8+
"flag"
9+
"fmt"
10+
11+
"github.com/aws/aws-sdk-go-v2/aws"
12+
"github.com/aws/aws-sdk-go-v2/config"
13+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
14+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
15+
)
16+
17+
// CWEnableAlarmAPI defines the interface for the PutMetricAlarm function.
18+
// We use this interface to test the function using a mocked service.
19+
type CWEnableAlarmAPI interface {
20+
PutMetricAlarm(ctx context.Context,
21+
params *cloudwatch.PutMetricAlarmInput,
22+
optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricAlarmOutput, error)
23+
EnableAlarmActions(ctx context.Context,
24+
params *cloudwatch.EnableAlarmActionsInput,
25+
optFns ...func(*cloudwatch.Options)) (*cloudwatch.EnableAlarmActionsOutput, error)
26+
}
27+
28+
// CreateMetricAlarm creates a metric alarm
29+
// Inputs:
30+
// c is the context of the method call, which includes the Region
31+
// api is the interface that defines the method call
32+
// input defines the input arguments to the service call.
33+
// Output:
34+
// If success, a METHODOutput object containing the result of the service call and nil
35+
// Otherwise, the error from a call to PutMetricAlarm
36+
func CreateMetricAlarm(c context.Context, api CWEnableAlarmAPI, input *cloudwatch.PutMetricAlarmInput) (*cloudwatch.PutMetricAlarmOutput, error) {
37+
resp, err := api.PutMetricAlarm(c, input)
38+
39+
return resp, err
40+
}
41+
42+
// EnableAlarm enables the specified Amazon CloudWatch alarm
43+
// Inputs:
44+
// c is the context of the method call, which includes the Region
45+
// api is the interface that defines the method call
46+
// input defines the input arguments to the service call.
47+
// Output:
48+
// If success, a EnableAlarmActionsOutput object containing the result of the service call and nil
49+
// Otherwise, the error from a call to PutMetricAlarm
50+
func EnableAlarm(c context.Context, api CWEnableAlarmAPI, input *cloudwatch.EnableAlarmActionsInput) (*cloudwatch.EnableAlarmActionsOutput, error) {
51+
// Enable the alarm for the instance
52+
resp, err := api.EnableAlarmActions(c, input)
53+
54+
return resp, err
55+
}
56+
57+
func main() {
58+
instanceName := flag.String("n", "", "The instance name")
59+
instanceID := flag.String("i", "", "The instance ID")
60+
alarmName := flag.String("a", "", "The alarm name")
61+
flag.Parse()
62+
63+
if *instanceName == "" || *instanceID == "" || *alarmName == "" {
64+
fmt.Println("You must supply an instance name, instance ID, and alarm name")
65+
return
66+
}
67+
68+
cfg, err := config.LoadDefaultConfig()
69+
if err != nil {
70+
panic("configuration error, " + err.Error())
71+
}
72+
73+
client := cloudwatch.NewFromConfig(cfg)
74+
75+
putInput := &cloudwatch.PutMetricAlarmInput{
76+
AlarmName: alarmName,
77+
ComparisonOperator: types.ComparisonOperatorGreaterthanorequaltothreshold,
78+
EvaluationPeriods: aws.Int32(1),
79+
MetricName: aws.String("CPUUtilization"),
80+
Namespace: aws.String("AWS/EC2"),
81+
Period: aws.Int32(60),
82+
Statistic: types.StatisticAverage,
83+
Threshold: aws.Float64(70.0),
84+
ActionsEnabled: aws.Bool(true),
85+
AlarmDescription: aws.String("Alarm when server CPU exceeds 70%"),
86+
Unit: types.StandardUnitSeconds,
87+
AlarmActions: []*string{
88+
aws.String(fmt.Sprintf("arn:aws:swf:"+cfg.Region+":%s:action/actions/AWS_EC2.InstanceId.Reboot/1.0", instanceName)),
89+
},
90+
Dimensions: []*types.Dimension{
91+
{
92+
Name: aws.String("InstanceId"),
93+
Value: instanceID,
94+
},
95+
},
96+
}
97+
98+
_, err = CreateMetricAlarm(context.Background(), client, putInput)
99+
if err != nil {
100+
fmt.Println(err)
101+
return
102+
}
103+
104+
enableInput := &cloudwatch.EnableAlarmActionsInput{
105+
AlarmNames: []*string{
106+
instanceID,
107+
},
108+
}
109+
110+
_, err = EnableAlarm(context.Background(), client, enableInput)
111+
if err != nil {
112+
fmt.Println(err)
113+
return
114+
}
115+
116+
fmt.Println("Enabled alarm " + *alarmName + " for EC2 instance " + *instanceName)
117+
}
118+
119+
// snippet-end:[cloudwatch.go-v2.CreateEnableMetricAlarm]

0 commit comments

Comments
 (0)