utils

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2021 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package utils provides cli specifics methods for interacting with AWS services

Package utils provides cli specifics methods for interacting with AWS services

Package utils provides cli specifics methods for interacting with AWS services

Package utils provides cli specifics methods for interacting with AWS services

Index

Constants

View Source
const (
	STACK_DELETION_WAIT_TIME_IN_SEC int16 = 30 // STACK_DELETION_WAIT_TIME_IN_SEC is the time to wait for stacks before peforming status checks after delete requests have been sent.
	MAX_DELETE_RETRY_COUNT          int16 = 5  // MAX_DELETE_RETRY_COUNT specifies the number of times we should retry deleting a stack before giving up.
)

-------------- configs ---------------

Variables

View Source
var (
	NUKE_START_TIME       = CurrentUTCDateTime() // NUKE_START_TIME is the start timestamp of teardown.
	NUKE_END_TIME         = CurrentUTCDateTime() // NUKE_END_TIME is the end timestamp of teardown.
	AWS_SDK_MAX_RETRY int = 5                    // AWS_SDK_MAX_RETRY is max retry count for AWS SDK.

	// stats
	TOTAL_STACK_COUNT    int     // TOTAL_STACK_COUNT is the number of stacks found to be eligible for deletion/
	DELETED_STACK_COUNT  int     // DELETED_STACK_COUNT is the number of stacks deleted so far.
	ACTIVE_STACK_COUNT   int     // ACTIVE_STACK_COUNT is the number of stacks yet to be deleted or in the process of being deleted.
	NUKE_DURATION_IN_HRS float64 // NUKE_DURATION_IN_HRS is the total run time of teardown until now.
)
View Source
var ColorMapping map[string]string = map[string]string{"Start": "#f0e62e", "Complete": "#25db2e", "Error": "#e81e1e"}

ColorMapping is the mapping of slack message color based on teardown event types 'Start', 'Complete', 'Error'

Functions

func CurrentUTCDateTime

func CurrentUTCDateTime() string

CurrentUTCDateTime returns current time in ISO string

func InitiateTearDown

func InitiateTearDown(config models.Config)

InitiateTearDown scans and deletes cloudformation stacks respecting the dependencies. A stack is eligible for deletion when it's exports has not been imported by any other stacks.

func TimeDiff

func TimeDiff(startTime, endTime string) string

TimeDiff returns difference of two timestamps in minutes

func UpdateNukeStats

func UpdateNukeStats(dt map[string]models.StackDetails)

UpdateNukeStats updates global variables used for capturing teardown stats

Types

type AlertMessage

type AlertMessage struct {
	Message     string // Long message with details about the event
	Event       string // Start | Complete | Error
	FailedStack models.StackDetails
	Attachment  map[string]interface{}
}

AlertMessage is the structure of a alert event which is translated to slack message later.

type CFNManager

type CFNManager struct {
	TargetAccountId string
	NukeRoleARN     string
	StackPattern    string
	AWSProfile      string
	AWSRegion       string
}

CFNManager exposes methods to interact with CloudFormation via SDK.

func (CFNManager) AWSSessionAccountID added in v0.1.1

func (dm CFNManager) AWSSessionAccountID(sess *session.Session) (acID string, err error)

AWSSessionAccountID fetches account id from current aws session

func (CFNManager) DeleteStack

func (dm CFNManager) DeleteStack(stackName string) error

DeleteStack sends delete request for a stack. Returns success if the stack we are trying to delete has already been deleted.

func (CFNManager) DescribeStack

func (dm CFNManager) DescribeStack(stackName string) (*cloudformation.Stack, error)

DescribeStack returns description for particular stack.

func (CFNManager) ListEnvironmentExports

func (dm CFNManager) ListEnvironmentExports() (map[string][]string, error)

ListEnvironmentExports finds all exported values for our matching stacks in this format:

	{
 	 "stack-1-name": ["export-1", "export-2"],
  	"stack-2-name": []
	}

func (CFNManager) ListEnvironmentStacks

func (dm CFNManager) ListEnvironmentStacks() (map[string]models.StackDetails, error)

ListEnvironmentStacks lists matching stacks for the given regex.

func (CFNManager) ListImports

func (dm CFNManager) ListImports(exportNames []string) (map[string]struct{}, error)

ListImports lists all stacks importing given exported names.

func (CFNManager) ListStackResources

func (dm CFNManager) ListStackResources(stackName string) ([]*cloudformation.StackResourceSummary, error)

ListStackResources lists description of all resources in a stack.

func (CFNManager) RegexMatch

func (dm CFNManager) RegexMatch(stackName string) bool

RegexMatch matches stack name with the supplied regex so that we can filter desired stacks for deletion.

func (CFNManager) Session

func (dm CFNManager) Session() (*cloudformation.CloudFormation, error)

Session creates a new aws cloudformation session. By default it uses given aws profile and region but it also provides option to assume a different role. It also has validation for target account id to ensure we are deleting in the correct aws account.

type NotificationManager

type NotificationManager struct {
	StackPattern    string
	DryRun          string
	SlackWebHookURL string // Webhook url is specific to channel
}

NotificationManager exposes methods for sending alerts to slack channel.

func (NotificationManager) Alert

func (nm NotificationManager) Alert(am AlertMessage) error

Alert posts message to Slack channel using webhook Only posts the message if it's not a dry run and webhook url is present

func (NotificationManager) ErrorAlert

func (nm NotificationManager) ErrorAlert(am AlertMessage)

ErrorAlert prepares slack message for stack deletion error

func (NotificationManager) GenericAlert

func (nm NotificationManager) GenericAlert(am AlertMessage)

GenericAlert prepares slack message for a generic message

func (NotificationManager) StartAlert

func (nm NotificationManager) StartAlert(am AlertMessage)

StartAlert prepares slack message for teardown start event

func (NotificationManager) StuckAlert

func (nm NotificationManager) StuckAlert(am AlertMessage)

StuckAlert prepares slack message when stack teardown is stuck

func (NotificationManager) SuccessAlert

func (nm NotificationManager) SuccessAlert(am AlertMessage)

SuccessAlert prepares slack message for successful completion of stack teardown

type S3Manager

type S3Manager struct {
	TargetAccountId string
	NukeRoleARN     string
	AWSProfile      string
	AWSRegion       string
}

S3Manager exposes methods to interact with AWS S3 service via SDK.

func (S3Manager) AWSSessionAccountID added in v0.1.1

func (sm S3Manager) AWSSessionAccountID(sess *session.Session) (acID string, err error)

AWSSessionAccountID fetches account id from current aws session

func (S3Manager) EmptyBucket

func (sm S3Manager) EmptyBucket(bucketName string) error

EmptyBucket deletes all objects from a particular S3 bucket.

func (S3Manager) Session

func (sm S3Manager) Session() (*s3.S3, error)

Session creates a new aws S3 session. By default, it uses given aws profile and region but it also provides option to assume a different role. It also has validation for target account id to ensure we are deleting in the correct aws account.

type SlackMessage

type SlackMessage struct {
	Attachments []map[string]interface{} `json:"attachments"`
}

SlackMessage is the structure accepted by Slack post message api. More info: https://app.slack.com/block-kit-builder

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL