Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "sync"
- "github.com/aws/aws-sdk-go/aws"
- "github.com/aws/aws-sdk-go/aws/session"
- "github.com/aws/aws-sdk-go/service/cloudwatch"
- )
- var (
- cwClient *CWClientSession
- once sync.Once
- )
- func main() {
- client := getCloudWatchClient()
- dashboards, _ := client.ListDashboards(&cloudwatch.ListDashboardsInput{})
- fmt.Print(dashboards)
- }
- type CWClientSession struct {
- client *cloudwatch.CloudWatch
- updateLock sync.Mutex
- }
- func getCloudWatchClient() *cloudwatch.CloudWatch {
- once.Do(func() {
- session := newAuthSession()
- cwClient = &CWClientSession{
- client: cloudwatch.New(session),
- }
- })
- // if expired, update
- if cwClient.client.Config.Credentials.IsExpired() {
- cwClient.updateCWClientSession()
- }
- return cwClient.client
- }
- func (cwClientSession *CWClientSession) updateCWClientSession() {
- cwClientSession.updateLock.Lock()
- session := newAuthSession()
- newClient := cloudwatch.New(session)
- cwClientSession.client = newClient
- defer cwClientSession.updateLock.Unlock()
- }
- func newAuthSession() *session.Session {
- var MaxRetries int = 3
- session, _ := session.NewSession(&aws.Config{
- Region: aws.String("us-east-1"),
- MaxRetries: &MaxRetries,
- })
- return session
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement