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

     1  package decorator
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/aws/session"
     7  	sparta "github.com/mweagle/Sparta"
     8  	cfCustomResources "github.com/mweagle/Sparta/aws/cloudformation/resources"
     9  	gocf "github.com/mweagle/go-cloudformation"
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  // S3ArtifactPublisherDecorator returns a ServiceDecoratorHookHandler
    14  // function that publishes the given data to an S3 Bucket
    15  // using the given bucket and key.
    16  func S3ArtifactPublisherDecorator(bucket gocf.Stringable,
    17  	key gocf.Stringable,
    18  	data map[string]interface{}) sparta.ServiceDecoratorHookHandler {
    19  
    20  	// Setup the CF distro
    21  	artifactDecorator := func(context map[string]interface{},
    22  		serviceName string,
    23  		template *gocf.Template,
    24  		S3Bucket string,
    25  		S3Key string,
    26  		buildID string,
    27  		awsSession *session.Session,
    28  		noop bool,
    29  		logger *logrus.Logger) error {
    30  
    31  		// Ensure the custom action handler...
    32  		sourceArnExpr := gocf.Join("",
    33  			gocf.String("arn:aws:s3:::"),
    34  			bucket.String(),
    35  			gocf.String("/*"))
    36  
    37  		configuratorResName, err := sparta.EnsureCustomResourceHandler(serviceName,
    38  			cfCustomResources.S3ArtifactPublisher,
    39  			sourceArnExpr,
    40  			[]string{},
    41  			template,
    42  			S3Bucket,
    43  			S3Key,
    44  			logger)
    45  
    46  		if err != nil {
    47  			return err
    48  		}
    49  
    50  		// Create the invocation of the custom action...
    51  		s3PublishResource := &cfCustomResources.S3ArtifactPublisherResource{}
    52  		s3PublishResource.ServiceToken = gocf.GetAtt(configuratorResName, "Arn")
    53  		s3PublishResource.Bucket = bucket.String()
    54  		s3PublishResource.Key = key.String()
    55  		s3PublishResource.Body = data
    56  
    57  		// Name?
    58  		resourceInvokerName := sparta.CloudFormationResourceName("ArtifactS3",
    59  			fmt.Sprintf("%v", bucket.String()),
    60  			fmt.Sprintf("%v", key.String()))
    61  
    62  		// Add it
    63  		template.AddResource(resourceInvokerName, s3PublishResource)
    64  		return nil
    65  	}
    66  	return sparta.ServiceDecoratorHookFunc(artifactDecorator)
    67  }