github.com/mweagle/Sparta@v1.15.0/hooks.go (about)

     1  package sparta
     2  
     3  import (
     4  	"archive/zip"
     5  
     6  	"github.com/aws/aws-sdk-go/aws/session"
     7  	gocf "github.com/mweagle/go-cloudformation"
     8  	"github.com/sirupsen/logrus"
     9  )
    10  
    11  ////////////////////////////////////////////////////////////////////////////////
    12  // TYPES
    13  ////////////////////////////////////////////////////////////////////////////////
    14  
    15  // TemplateDecorator allows Lambda functions to annotate the CloudFormation
    16  // template definition.  Both the resources and the outputs params
    17  // are initialized to an empty ArbitraryJSONObject and should
    18  // be populated with valid CloudFormation ArbitraryJSONObject values.  The
    19  // CloudFormationResourceName() function can be used to generate
    20  // logical CloudFormation-compatible resource names.
    21  // See http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html and
    22  // http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html for
    23  // more information.
    24  type TemplateDecorator func(serviceName string,
    25  	lambdaResourceName string,
    26  	lambdaResource gocf.LambdaFunction,
    27  	resourceMetadata map[string]interface{},
    28  	S3Bucket string,
    29  	S3Key string,
    30  	buildID string,
    31  	template *gocf.Template,
    32  	context map[string]interface{},
    33  	logger *logrus.Logger) error
    34  
    35  // TemplateDecoratorHookFunc is the adapter to transform an existing
    36  // TemplateHook into a TemplateDecoratorHandler satisfier
    37  type TemplateDecoratorHookFunc func(serviceName string,
    38  	lambdaResourceName string,
    39  	lambdaResource gocf.LambdaFunction,
    40  	resourceMetadata map[string]interface{},
    41  	S3Bucket string,
    42  	S3Key string,
    43  	buildID string,
    44  	template *gocf.Template,
    45  	context map[string]interface{},
    46  	logger *logrus.Logger) error
    47  
    48  // DecorateTemplate calls tdhf(...) to satisfy TemplateDecoratorHandler
    49  func (tdhf TemplateDecoratorHookFunc) DecorateTemplate(serviceName string,
    50  	lambdaResourceName string,
    51  	lambdaResource gocf.LambdaFunction,
    52  	resourceMetadata map[string]interface{},
    53  	S3Bucket string,
    54  	S3Key string,
    55  	buildID string,
    56  	template *gocf.Template,
    57  	context map[string]interface{},
    58  	logger *logrus.Logger) error {
    59  	return tdhf(serviceName,
    60  		lambdaResourceName,
    61  		lambdaResource,
    62  		resourceMetadata,
    63  		S3Bucket,
    64  		S3Key,
    65  		buildID,
    66  		template,
    67  		context,
    68  		logger)
    69  }
    70  
    71  // TemplateDecoratorHandler is the interface type to indicate a template
    72  // decoratorHook
    73  type TemplateDecoratorHandler interface {
    74  	DecorateTemplate(serviceName string,
    75  		lambdaResourceName string,
    76  		lambdaResource gocf.LambdaFunction,
    77  		resourceMetadata map[string]interface{},
    78  		S3Bucket string,
    79  		S3Key string,
    80  		buildID string,
    81  		template *gocf.Template,
    82  		context map[string]interface{},
    83  		logger *logrus.Logger) error
    84  }
    85  
    86  ////////////////////////////////////////////////////////////////////////////////
    87  // WorkflowHandler
    88  
    89  // WorkflowHook defines a user function that should be called at a specific
    90  // point in the larger Sparta workflow. The first argument is a map that
    91  // is shared across all LifecycleHooks and which Sparta treats as an opaque
    92  // value.
    93  type WorkflowHook func(context map[string]interface{},
    94  	serviceName string,
    95  	S3Bucket string,
    96  	buildID string,
    97  	awsSession *session.Session,
    98  	noop bool,
    99  	logger *logrus.Logger) error
   100  
   101  // WorkflowHookFunc is the adapter to transform an existing
   102  // WorkflowHook into a WorkflowHookHandler satisfier
   103  type WorkflowHookFunc func(context map[string]interface{},
   104  	serviceName string,
   105  	S3Bucket string,
   106  	buildID string,
   107  	awsSession *session.Session,
   108  	noop bool,
   109  	logger *logrus.Logger) error
   110  
   111  // DecorateWorkflow calls whf(...) to satisfy WorkflowHookHandler
   112  func (whf WorkflowHookFunc) DecorateWorkflow(context map[string]interface{},
   113  	serviceName string,
   114  	S3Bucket string,
   115  	buildID string,
   116  	awsSession *session.Session,
   117  	noop bool,
   118  	logger *logrus.Logger) error {
   119  	return whf(context,
   120  		serviceName,
   121  		S3Bucket,
   122  		buildID,
   123  		awsSession,
   124  		noop,
   125  		logger)
   126  }
   127  
   128  // WorkflowHookHandler is the interface type to indicate a workflow
   129  // hook
   130  type WorkflowHookHandler interface {
   131  	DecorateWorkflow(context map[string]interface{},
   132  		serviceName string,
   133  		S3Bucket string,
   134  		buildID string,
   135  		awsSession *session.Session,
   136  		noop bool,
   137  		logger *logrus.Logger) error
   138  }
   139  
   140  ////////////////////////////////////////////////////////////////////////////////
   141  
   142  ////////////////////////////////////////////////////////////////////////////////
   143  // ArchiveHandler
   144  
   145  // ArchiveHook provides callers an opportunity to insert additional
   146  // files into the ZIP archive deployed to S3
   147  type ArchiveHook func(context map[string]interface{},
   148  	serviceName string,
   149  	zipWriter *zip.Writer,
   150  	awsSession *session.Session,
   151  	noop bool,
   152  	logger *logrus.Logger) error
   153  
   154  // ArchiveHookFunc is the adapter to transform an existing
   155  // ArchiveHook into a WorkflowHookHandler satisfier
   156  type ArchiveHookFunc func(context map[string]interface{},
   157  	serviceName string,
   158  	zipWriter *zip.Writer,
   159  	awsSession *session.Session,
   160  	noop bool,
   161  	logger *logrus.Logger) error
   162  
   163  // DecorateArchive calls whf(...) to satisfy ArchiveHookHandler
   164  func (ahf ArchiveHookFunc) DecorateArchive(context map[string]interface{},
   165  	serviceName string,
   166  	zipWriter *zip.Writer,
   167  	awsSession *session.Session,
   168  	noop bool,
   169  	logger *logrus.Logger) error {
   170  	return ahf(context,
   171  		serviceName,
   172  		zipWriter,
   173  		awsSession,
   174  		noop,
   175  		logger)
   176  }
   177  
   178  // ArchiveHookHandler is the interface type to indicate a workflow
   179  // hook
   180  type ArchiveHookHandler interface {
   181  	DecorateArchive(context map[string]interface{},
   182  		serviceName string,
   183  		zipWriter *zip.Writer,
   184  		awsSession *session.Session,
   185  		noop bool,
   186  		logger *logrus.Logger) error
   187  }
   188  
   189  ////////////////////////////////////////////////////////////////////////////////
   190  // ServiceDecoratorHandler
   191  
   192  // ServiceDecoratorHook defines a user function that is called a single
   193  // time in the marshall workflow.
   194  type ServiceDecoratorHook func(context map[string]interface{},
   195  	serviceName string,
   196  	template *gocf.Template,
   197  	S3Bucket string,
   198  	S3Key string,
   199  	buildID string,
   200  	awsSession *session.Session,
   201  	noop bool,
   202  	logger *logrus.Logger) error
   203  
   204  // ServiceDecoratorHookFunc is the adapter to transform an existing
   205  // ArchiveHook into a WorkflowHookHandler satisfier
   206  type ServiceDecoratorHookFunc func(context map[string]interface{},
   207  	serviceName string,
   208  	template *gocf.Template,
   209  	S3Bucket string,
   210  	S3Key string,
   211  	buildID string,
   212  	awsSession *session.Session,
   213  	noop bool,
   214  	logger *logrus.Logger) error
   215  
   216  // DecorateService calls sdhf(...) to satisfy ServiceDecoratorHookHandler
   217  func (sdhf ServiceDecoratorHookFunc) DecorateService(context map[string]interface{},
   218  	serviceName string,
   219  	template *gocf.Template,
   220  	S3Bucket string,
   221  	S3Key string,
   222  	buildID string,
   223  	awsSession *session.Session,
   224  	noop bool,
   225  	logger *logrus.Logger) error {
   226  	return sdhf(context,
   227  		serviceName,
   228  		template,
   229  		S3Bucket,
   230  		S3Key,
   231  		buildID,
   232  		awsSession,
   233  		noop,
   234  		logger)
   235  }
   236  
   237  // ServiceDecoratorHookHandler is the interface type to indicate a workflow
   238  // hook
   239  type ServiceDecoratorHookHandler interface {
   240  	DecorateService(context map[string]interface{},
   241  		serviceName string,
   242  		template *gocf.Template,
   243  		S3Bucket string,
   244  		S3Key string,
   245  		buildID string,
   246  		awsSession *session.Session,
   247  		noop bool,
   248  		logger *logrus.Logger) error
   249  }
   250  
   251  ////////////////////////////////////////////////////////////////////////////////
   252  // ServiceValidationHookHandler
   253  
   254  // ServiceValidationHook defines a user function that is called a single
   255  // after all template annotations have been performed. It is where
   256  // policies should be applied
   257  type ServiceValidationHook func(context map[string]interface{},
   258  	serviceName string,
   259  	template *gocf.Template,
   260  	S3Bucket string,
   261  	S3Key string,
   262  	buildID string,
   263  	awsSession *session.Session,
   264  	noop bool,
   265  	logger *logrus.Logger) error
   266  
   267  // ServiceValidationHookFunc is the adapter to transform an existing
   268  // ArchiveHook into a WorkflowHookHandler satisfier
   269  type ServiceValidationHookFunc func(context map[string]interface{},
   270  	serviceName string,
   271  	template *gocf.Template,
   272  	S3Bucket string,
   273  	S3Key string,
   274  	buildID string,
   275  	awsSession *session.Session,
   276  	noop bool,
   277  	logger *logrus.Logger) error
   278  
   279  // ValidateService calls sdhf(...) to satisfy ServiceValidationHookHandler
   280  func (sdhf ServiceValidationHookFunc) ValidateService(context map[string]interface{},
   281  	serviceName string,
   282  	template *gocf.Template,
   283  	S3Bucket string,
   284  	S3Key string,
   285  	buildID string,
   286  	awsSession *session.Session,
   287  	noop bool,
   288  	logger *logrus.Logger) error {
   289  	return sdhf(context,
   290  		serviceName,
   291  		template,
   292  		S3Bucket,
   293  		S3Key,
   294  		buildID,
   295  		awsSession,
   296  		noop,
   297  		logger)
   298  }
   299  
   300  // ServiceValidationHookHandler is the interface type to indicate a workflow
   301  // hook
   302  type ServiceValidationHookHandler interface {
   303  	ValidateService(context map[string]interface{},
   304  		serviceName string,
   305  		template *gocf.Template,
   306  		S3Bucket string,
   307  		S3Key string,
   308  		buildID string,
   309  		awsSession *session.Session,
   310  		noop bool,
   311  		logger *logrus.Logger) error
   312  }
   313  
   314  ////////////////////////////////////////////////////////////////////////////////
   315  // RollbackHandler
   316  
   317  // RollbackHook provides callers an opportunity to handle failures
   318  // associated with failing to perform the requested operation
   319  type RollbackHook func(context map[string]interface{},
   320  	serviceName string,
   321  	awsSession *session.Session,
   322  	noop bool,
   323  	logger *logrus.Logger)
   324  
   325  // RollbackHookFunc the adapter to transform an existing
   326  // RollbackHook into a RollbackHookHandler satisfier
   327  type RollbackHookFunc func(context map[string]interface{},
   328  	serviceName string,
   329  	awsSession *session.Session,
   330  	noop bool,
   331  	logger *logrus.Logger)
   332  
   333  // Rollback calls sdhf(...) to satisfy ArchiveHookHandler
   334  func (rhf RollbackHookFunc) Rollback(context map[string]interface{},
   335  	serviceName string,
   336  	awsSession *session.Session,
   337  	noop bool,
   338  	logger *logrus.Logger) error {
   339  	rhf(context,
   340  		serviceName,
   341  		awsSession,
   342  		noop,
   343  		logger)
   344  	return nil
   345  }
   346  
   347  // RollbackHookHandler is the interface type to indicate a workflow
   348  // hook
   349  type RollbackHookHandler interface {
   350  	Rollback(context map[string]interface{},
   351  		serviceName string,
   352  		awsSession *session.Session,
   353  		noop bool,
   354  		logger *logrus.Logger) error
   355  }