Skip to content

Commit

Permalink
helper improve code reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoReboul committed Mar 27, 2020
1 parent 460d6c5 commit 26bcadd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
31 changes: 31 additions & 0 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"cloud.google.com/go/firestore"
"cloud.google.com/go/functions/metadata"
"cloud.google.com/go/pubsub"
"google.golang.org/api/cloudresourcemanager/v1"
cloudresourcemanagerv2 "google.golang.org/api/cloudresourcemanager/v2"
Expand All @@ -42,6 +43,11 @@ type PubSubMessage struct {
Data []byte `json:"data"`
}

// Window Cloud Asset Inventory feed message time window
type Window struct {
StartTime time.Time `json:"startTime" firestore:"startTime"`
}

// BuildAncestorsDisplayName build a slice of Ancestor friendly name fron a slice of ancestors
func BuildAncestorsDisplayName(ctx context.Context, ancestors []string, collectionID string, firestoreClient *firestore.Client, cloudresourcemanagerService *cloudresourcemanager.Service, cloudresourcemanagerServiceV2 *cloudresourcemanagerv2.Service) []string {
cnt := len(ancestors)
Expand Down Expand Up @@ -251,6 +257,31 @@ func GetTopicList(ctx context.Context, pubSubClient *pubsub.Client) ([]string, e
return topicList, nil
}

// IntialRetryCheck performs intitial controls
// 1) return true and metadata when controls are passed
// 2) return false when controls failed:
// - 2a) with an error to retry the cloud function entry point function
// - 2b) with nil to stop the cloud function entry point function
func IntialRetryCheck(ctxEvent context.Context, initFailed bool, retryTimeOutSeconds int64) (bool, *metadata.Metadata, error) {
metadata, err := metadata.FromContext(ctxEvent)
if err != nil {
// Assume an error on the function invoker and try again.
return false, metadata, fmt.Errorf("metadata.FromContext: %v", err) // RETRY
}
if initFailed {
log.Println("ERROR - init function failed")
return false, metadata, nil // NO RETRY
}

// Ignore events that are too old.
expiration := metadata.Timestamp.Add(time.Duration(retryTimeOutSeconds) * time.Second)
if time.Now().After(expiration) {
log.Printf("ERROR - too many retries for expired event '%q'", metadata.EventID)
return false, metadata, nil // NO MORE RETRY
}
return true, metadata, nil
}

// makeCompatible update a GCP asset ancestryPath to make it compatible with former Policy Library REGO rules
func makeCompatible(path string) string {
path = strings.Replace(path, "organizations", "organization", -1)
Expand Down
35 changes: 7 additions & 28 deletions publish2fs/publish2fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import (
"log"
"os"
"strconv"
"time"

"github.com/BrunoReboul/ram/helper"

"cloud.google.com/go/firestore"
"cloud.google.com/go/functions/metadata"
)

// Global structure for global variables to optimize the cloud function performances
Expand All @@ -41,15 +39,10 @@ type Global struct {

// FeedMessage Cloud Asset Inventory feed message
type FeedMessage struct {
Asset Asset `json:"asset" firestore:"asset"`
Window Window `json:"window" firestore:"window"`
Deleted bool `json:"deleted" firestore:"deleted"`
Origin string `json:"origin" firestore:"origin"`
}

// Window Cloud Asset Inventory feed message time window
type Window struct {
StartTime time.Time `json:"startTime" firestore:"startTime"`
Asset Asset `json:"asset" firestore:"asset"`
Window helper.Window `json:"window" firestore:"window"`
Deleted bool `json:"deleted" firestore:"deleted"`
Origin string `json:"origin" firestore:"origin"`
}

// Asset Cloud Asset Metadata
Expand Down Expand Up @@ -88,27 +81,13 @@ func Initialize(ctx context.Context, global *Global) {
// EntryPoint is the function to be executed for each cloud function occurence
func EntryPoint(ctxEvent context.Context, PubSubMessage helper.PubSubMessage, global *Global) error {
// log.Println(string(PubSubMessage.Data))
if global.initFailed {
log.Println("ERROR - init function failed")
return nil // NO RETRY
}

metadata, err := metadata.FromContext(ctxEvent)
if err != nil {
// Assume an error on the function invoker and try again.
return fmt.Errorf("metadata.FromContext: %v", err) // RETRY
}

// Ignore events that are too old.
expiration := metadata.Timestamp.Add(time.Duration(global.retryTimeOutSeconds) * time.Second)
if time.Now().After(expiration) {
log.Printf("ERROR - too many retries for expired event '%q'", metadata.EventID)
return nil // NO MORE RETRY
if ok, _, err := helper.IntialRetryCheck(ctxEvent, global.initFailed, global.retryTimeOutSeconds); !ok {
return err
}
// log.Printf("EventType %s EventID %s Resource %s Timestamp %v", metadata.EventType, metadata.EventID, metadata.Resource.Type, metadata.Timestamp)

var feedMessage FeedMessage
err = json.Unmarshal(PubSubMessage.Data, &feedMessage)
err := json.Unmarshal(PubSubMessage.Data, &feedMessage)
if err != nil {
log.Printf("ERROR - json.Unmarshal: %v", err)
return nil // NO RETRY
Expand Down

0 comments on commit 26bcadd

Please sign in to comment.