github.com/mweagle/Sparta@v1.15.0/aws/cloudformation/resources/codeCommitLambdaEventSourceResource.go (about)

     1  package resources
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/aws/session"
     8  	"github.com/aws/aws-sdk-go/service/codecommit"
     9  	gocf "github.com/mweagle/go-cloudformation"
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  // CodeCommitLambdaEventSourceResourceRequest defines the request properties to configure
    14  // SNS
    15  type CodeCommitLambdaEventSourceResourceRequest struct {
    16  	LambdaTargetArn *gocf.StringExpr
    17  	RepositoryName  *gocf.StringExpr
    18  	TriggerName     *gocf.StringExpr
    19  	Events          []string `json:",omitempty"`
    20  	Branches        []string `json:",omitempty"`
    21  }
    22  
    23  // CodeCommitLambdaEventSourceResource is a simple POC showing how to create custom resources
    24  type CodeCommitLambdaEventSourceResource struct {
    25  	gocf.CloudFormationCustomResource
    26  	CodeCommitLambdaEventSourceResourceRequest
    27  }
    28  
    29  func (command CodeCommitLambdaEventSourceResource) updateRegistration(isTargetActive bool,
    30  	session *session.Session,
    31  	event *CloudFormationLambdaEvent,
    32  	logger *logrus.Logger) (map[string]interface{}, error) {
    33  
    34  	unmarshalErr := json.Unmarshal(event.ResourceProperties, &command)
    35  	if unmarshalErr != nil {
    36  		return nil, unmarshalErr
    37  	}
    38  	logger.WithFields(logrus.Fields{
    39  		"Event": command,
    40  	}).Info("CodeCommit Custom Resource info")
    41  
    42  	// We need the repo in here...
    43  	codeCommitSvc := codecommit.New(session)
    44  
    45  	// Get the current subscriptions...
    46  	ccInput := &codecommit.GetRepositoryTriggersInput{
    47  		RepositoryName: aws.String(command.RepositoryName.Literal),
    48  	}
    49  	triggers, triggersErr := codeCommitSvc.GetRepositoryTriggers(ccInput)
    50  	if triggersErr != nil {
    51  		return nil, triggersErr
    52  	}
    53  
    54  	// Find the lambda ARN for this function...
    55  	putTriggers := make([]*codecommit.RepositoryTrigger, 0)
    56  	var preexistingTrigger *codecommit.RepositoryTrigger
    57  	for _, eachTrigger := range triggers.Triggers {
    58  		// Treat the preexisting one specially
    59  		if *eachTrigger.DestinationArn == command.LambdaTargetArn.Literal {
    60  			preexistingTrigger = eachTrigger
    61  		} else {
    62  			putTriggers = append(putTriggers, eachTrigger)
    63  		}
    64  	}
    65  
    66  	// Just log it...
    67  	logger.WithFields(logrus.Fields{
    68  		"RepositoryName": command.RepositoryName.Literal,
    69  		"Trigger":        preexistingTrigger,
    70  		"LambdaArn":      *command.LambdaTargetArn,
    71  	}).Info("Current CodeCommit trigger status")
    72  
    73  	reqBranches := make([]*string, len(command.Branches))
    74  	for idx, eachBranch := range command.Branches {
    75  		reqBranches[idx] = aws.String(eachBranch)
    76  	}
    77  	reqEvents := make([]*string, len(command.Events))
    78  	for idx, eachEvent := range command.Events {
    79  		reqEvents[idx] = aws.String(eachEvent)
    80  	}
    81  	if len(reqEvents) <= 0 {
    82  		logger.Info("No events found. Defaulting to `all`.")
    83  		reqEvents = []*string{
    84  			aws.String("all"),
    85  		}
    86  	}
    87  	if isTargetActive && preexistingTrigger == nil {
    88  		// Add one...
    89  		putTriggers = append(putTriggers, &codecommit.RepositoryTrigger{
    90  			DestinationArn: aws.String(command.LambdaTargetArn.Literal),
    91  			Name:           aws.String(command.TriggerName.Literal),
    92  			Branches:       reqBranches,
    93  			Events:         reqEvents,
    94  		})
    95  	} else if !isTargetActive {
    96  		// It's already removed...
    97  	} else if isTargetActive && preexistingTrigger != nil {
    98  		putTriggers = append(putTriggers, preexistingTrigger)
    99  	}
   100  	// Put it back...
   101  	putTriggersInput := &codecommit.PutRepositoryTriggersInput{
   102  		RepositoryName: aws.String(command.RepositoryName.Literal),
   103  		Triggers:       putTriggers,
   104  	}
   105  	putTriggersResp, putTriggersRespErr := codeCommitSvc.PutRepositoryTriggers(putTriggersInput)
   106  	// Just log it...
   107  	logger.WithFields(logrus.Fields{
   108  		"Response": putTriggersResp,
   109  		"Error":    putTriggersRespErr,
   110  	}).Info("CodeCommit PutRepositoryTriggers")
   111  	return nil, putTriggersRespErr
   112  }
   113  
   114  // IAMPrivileges returns the IAM privs for this custom action
   115  func (command *CodeCommitLambdaEventSourceResource) IAMPrivileges() []string {
   116  	return []string{"codecommit:GetRepositoryTriggers",
   117  		"codecommit:PutRepositoryTriggers"}
   118  }
   119  
   120  // Create implements the custom resource create operation
   121  func (command CodeCommitLambdaEventSourceResource) Create(awsSession *session.Session,
   122  	event *CloudFormationLambdaEvent,
   123  	logger *logrus.Logger) (map[string]interface{}, error) {
   124  	return command.updateRegistration(true, awsSession, event, logger)
   125  }
   126  
   127  // Update implements the custom resource update operation
   128  func (command CodeCommitLambdaEventSourceResource) Update(awsSession *session.Session,
   129  	event *CloudFormationLambdaEvent,
   130  	logger *logrus.Logger) (map[string]interface{}, error) {
   131  	return command.updateRegistration(true, awsSession, event, logger)
   132  }
   133  
   134  // Delete implements the custom resource delete operation
   135  func (command CodeCommitLambdaEventSourceResource) Delete(awsSession *session.Session,
   136  	event *CloudFormationLambdaEvent,
   137  	logger *logrus.Logger) (map[string]interface{}, error) {
   138  	return command.updateRegistration(false, awsSession, event, logger)
   139  }