Skip to content

Commit

Permalink
helper
Browse files Browse the repository at this point in the history
- add func InitialRetryCheck
- add type Key
  • Loading branch information
BrunoReboul committed Mar 27, 2020
1 parent 435aaa4 commit 3c6b537
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 68 deletions.
14 changes: 14 additions & 0 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ import (
"google.golang.org/api/iterator"
)

// Key Service account json key
type Key struct {
Type string `json:"type"`
ProjectID string `json:"project_id"`
PrivateKeyID string `json:"private_key_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
ClientID string `json:"client_id"`
AuthURI string `json:"auth_uri"`
TokenURI string `json:"token_uri"`
AuthProviderX509CertURL string `json:"auth_provider_x509_cert_url"`
ClientX509CertURL string `json:"client_x509_cert_url"`
}

// PublishRequest Pub/sub
type PublishRequest struct {
Topic string `json:"topic"`
Expand Down
36 changes: 4 additions & 32 deletions listgroups/listgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"google.golang.org/api/iam/v1"
"google.golang.org/api/option"

"cloud.google.com/go/functions/metadata"
"cloud.google.com/go/pubsub"
admin "google.golang.org/api/admin/directory/v1"
)
Expand Down Expand Up @@ -64,20 +63,6 @@ type Global struct {
retryTimeOutSeconds int64
}

// Key Service account json key
type Key struct {
Type string `json:"type"`
ProjectID string `json:"project_id"`
PrivateKeyID string `json:"private_key_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
ClientID string `json:"client_id"`
AuthURI string `json:"auth_uri"`
TokenURI string `json:"token_uri"`
AuthProviderX509CertURL string `json:"auth_provider_x509_cert_url"`
ClientX509CertURL string `json:"client_x509_cert_url"`
}

// FeedMessage Cloud Asset Inventory feed message
type FeedMessage struct {
Asset Asset `json:"asset"`
Expand Down Expand Up @@ -163,7 +148,7 @@ func Initialize(ctx context.Context, global *Global) {
global.initFailed = true
return
}
var key Key
var key helper.Key
err = json.Unmarshal(keyJSONdata, &key)
if err != nil {
log.Printf("ERROR - json.Unmarshal(keyJSONdata, &key): %v", err)
Expand Down Expand Up @@ -230,22 +215,9 @@ 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
ok, metadata, err := helper.IntialRetryCheck(ctxEvent, global.initFailed, global.retryTimeOutSeconds)
if !ok {
return err
}
// log.Printf("EventType %s EventID %s Resource %s Timestamp %v", metadata.EventType, metadata.EventID, metadata.Resource.Type, metadata.Timestamp)

Expand Down
20 changes: 3 additions & 17 deletions stream2bq/stream2bq.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"cloud.google.com/go/bigquery"
"cloud.google.com/go/firestore"
"cloud.google.com/go/functions/metadata"
cloudresourcemanagerv2 "google.golang.org/api/cloudresourcemanager/v2"
)

Expand Down Expand Up @@ -299,25 +298,12 @@ 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 err error
switch global.tableName {
case "complianceStatus":
err = persistComplianceStatus(PubSubMessage.Data, global)
Expand Down
22 changes: 3 additions & 19 deletions upload2gcs/upload2gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
"os"
"strconv"
"strings"
"time"

"github.com/BrunoReboul/ram/helper"
"google.golang.org/api/cloudresourcemanager/v1"

"cloud.google.com/go/firestore"
"cloud.google.com/go/functions/metadata"
"cloud.google.com/go/storage"
cloudresourcemanagerv2 "google.golang.org/api/cloudresourcemanager/v2"
)
Expand Down Expand Up @@ -127,27 +125,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 3c6b537

Please sign in to comment.