-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yury Nikitin
committed
Jan 22, 2024
1 parent
c4bddda
commit 57eb711
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cloudwatch | ||
|
||
// Option represents an option that can be used to configure a cloudwatch metric. | ||
type Option func(target *Cloudwatch) | ||
|
||
// Cloudwatch represents a cloudwatch metric. | ||
type Cloudwatch struct { | ||
Ref string | ||
Namespace string | ||
MetricName string | ||
Region string | ||
Statistics []string | ||
Dimensions map[string]string | ||
Period string | ||
LegendFormat string | ||
} | ||
|
||
// New creates a new cloudwatch query. | ||
func New(metric string, namespace string, options ...Option) *Cloudwatch { | ||
cloudwatch := &Cloudwatch{ | ||
MetricName: metric, | ||
Namespace: namespace, | ||
} | ||
|
||
for _, opt := range options { | ||
opt(cloudwatch) | ||
} | ||
|
||
return cloudwatch | ||
} | ||
|
||
// Legend sets the legend format. | ||
func Legend(legend string) Option { | ||
return func(cloudwatch *Cloudwatch) { | ||
cloudwatch.LegendFormat = legend | ||
} | ||
} | ||
|
||
// Region sets the region. | ||
func Region(region string) Option { | ||
return func(cloudwatch *Cloudwatch) { | ||
cloudwatch.Region = region | ||
} | ||
} | ||
|
||
// Statistic sets the statistic. | ||
func Statistic(statistics []string) Option { | ||
return func(cloudwatch *Cloudwatch) { | ||
cloudwatch.Statistics = statistics | ||
} | ||
} | ||
|
||
// Dimensions sets the dimensions. | ||
func Dimensions(dimensions map[string]string) Option { | ||
return func(cloudwatch *Cloudwatch) { | ||
cloudwatch.Dimensions = dimensions | ||
} | ||
} | ||
|
||
// Ref sets the reference ID for this query. | ||
func Ref(ref string) Option { | ||
return func(cloudwatch *Cloudwatch) { | ||
cloudwatch.Ref = ref | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cloudwatch | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewCloudwatchTargetCanBeCreated(t *testing.T) { | ||
req := require.New(t) | ||
metricName := "Cloudwatch" | ||
namespace := "Test" | ||
|
||
target := New(metricName, namespace) | ||
|
||
req.Equal(metricName, target.MetricName) | ||
req.Equal(namespace, target.Namespace) | ||
} | ||
|
||
func TestLegendCanBeConfigured(t *testing.T) { | ||
req := require.New(t) | ||
legend := "lala" | ||
|
||
target := New("", "", Legend(legend)) | ||
|
||
req.Equal(legend, target.LegendFormat) | ||
} | ||
|
||
func TestRefCanBeConfigured(t *testing.T) { | ||
req := require.New(t) | ||
|
||
target := New("", "", Ref("A")) | ||
|
||
req.Equal("A", target.Ref) | ||
} | ||
|
||
func TestRegionCanBeSet(t *testing.T) { | ||
req := require.New(t) | ||
|
||
target := New("", "", Region("C")) | ||
|
||
req.Equal("C", target.Region) | ||
} | ||
|
||
func TestStatisticCanBeSet(t *testing.T) { | ||
req := require.New(t) | ||
statistics := []string{"A", "B", "C"} | ||
|
||
target := New("", "", Statistic(statistics)) | ||
|
||
req.Equal(statistics, target.Statistics) | ||
} |