Advertisement
Guest User

Untitled

a guest
Jun 19th, 2023
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.26 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "sync"
  6.  
  7.     "github.com/aws/aws-sdk-go/aws"
  8.     "github.com/aws/aws-sdk-go/aws/session"
  9.     "github.com/aws/aws-sdk-go/service/cloudwatch"
  10. )
  11.  
  12. var (
  13.     cwClient *CWClientSession
  14.     once     sync.Once
  15. )
  16.  
  17. func main() {
  18.     client := getCloudWatchClient()
  19.     dashboards, _ := client.ListDashboards(&cloudwatch.ListDashboardsInput{})
  20.  
  21.     fmt.Print(dashboards)
  22. }
  23.  
  24. type CWClientSession struct {
  25.     client     *cloudwatch.CloudWatch
  26.     updateLock sync.Mutex
  27. }
  28.  
  29. func getCloudWatchClient() *cloudwatch.CloudWatch {
  30.     once.Do(func() {
  31.         session := newAuthSession()
  32.         cwClient = &CWClientSession{
  33.             client: cloudwatch.New(session),
  34.         }
  35.     })
  36.  
  37.     // if expired, update
  38.     if cwClient.client.Config.Credentials.IsExpired() {
  39.         cwClient.updateCWClientSession()
  40.     }
  41.  
  42.     return cwClient.client
  43. }
  44.  
  45. func (cwClientSession *CWClientSession) updateCWClientSession() {
  46.     cwClientSession.updateLock.Lock()
  47.  
  48.     session := newAuthSession()
  49.     newClient := cloudwatch.New(session)
  50.     cwClientSession.client = newClient
  51.  
  52.     defer cwClientSession.updateLock.Unlock()
  53. }
  54.  
  55. func newAuthSession() *session.Session {
  56.     var MaxRetries int = 3
  57.     session, _ := session.NewSession(&aws.Config{
  58.         Region:     aws.String("us-east-1"),
  59.         MaxRetries: &MaxRetries,
  60.     })
  61.  
  62.     return session
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement