Skip to content

Commit

Permalink
v0.0.17
Browse files Browse the repository at this point in the history
  • Loading branch information
frikky committed Mar 27, 2021
1 parent c935184 commit 9bf0f07
Show file tree
Hide file tree
Showing 6 changed files with 740 additions and 80 deletions.
56 changes: 56 additions & 0 deletions cloudSync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package shuffle

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)

func executeCloudAction(action CloudSyncJob, apikey string) error {
data, err := json.Marshal(action)
if err != nil {
log.Printf("Failed cloud webhook action marshalling: %s", err)
return err
}

client := &http.Client{}
syncUrl := fmt.Sprintf("https://shuffler.io/api/v1/cloud/sync/handle_action")
req, err := http.NewRequest(
"POST",
syncUrl,
bytes.NewBuffer(data),
)

req.Header.Add("Authorization", fmt.Sprintf(`Bearer %s`, apikey))
newresp, err := client.Do(req)
if err != nil {
return err
}

respBody, err := ioutil.ReadAll(newresp.Body)
if err != nil {
return err
}

type Result struct {
Success bool `json:"success"`
Reason string `json:"reason"`
}

//log.Printf("Data: %s", string(respBody))
responseData := Result{}
err = json.Unmarshal(respBody, &responseData)
if err != nil {
return err
}

if !responseData.Success {
return errors.New(fmt.Sprintf("Cloud error from Shuffler: %s", responseData.Reason))
}

return nil
}
146 changes: 128 additions & 18 deletions db-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ func SetWorkflowExecution(ctx context.Context, workflowExecution WorkflowExecuti
return errors.New("ExecutionId can't be empty.")
}

cacheKey := fmt.Sprintf("workflowexecution-%s", workflowExecution.ExecutionId)
nameKey := "workflowexecution"
cacheKey := fmt.Sprintf("%s_%s", nameKey, workflowExecution.ExecutionId)
executionData, err := json.Marshal(workflowExecution)
if err != nil {
log.Printf("[WARNING] Failed marshalling execution for cache: %s", err)
Expand All @@ -213,7 +214,7 @@ func SetWorkflowExecution(ctx context.Context, workflowExecution WorkflowExecuti
}

// New struct, to not add body, author etc
key := datastore.NameKey("workflowexecution", workflowExecution.ExecutionId, nil)
key := datastore.NameKey(nameKey, workflowExecution.ExecutionId, nil)
if _, err := project.Dbclient.Put(ctx, key, &workflowExecution); err != nil {
log.Printf("Error adding workflow_execution: %s", err)
return err
Expand Down Expand Up @@ -310,13 +311,13 @@ func SetInitExecutionVariables(ctx context.Context, workflowExecution WorkflowEx
if sourceFound {
parents[branch.DestinationID] = append(parents[branch.DestinationID], branch.SourceID)
} else {
log.Printf("[INFO] ID %s was not found in actions! Skipping parent. (TRIGGER?)", branch.SourceID)
log.Printf("[INFO] Action ID %s was not found in actions! Skipping parent. (TRIGGER?)", branch.SourceID)
}

if destinationFound {
children[branch.SourceID] = append(children[branch.SourceID], branch.DestinationID)
} else {
log.Printf("[INFO] ID %s was not found in actions! Skipping child. (TRIGGER?)", branch.SourceID)
log.Printf("[INFO] Action ID %s was not found in actions! Skipping child. (TRIGGER?)", branch.SourceID)
}
}

Expand Down Expand Up @@ -385,7 +386,8 @@ func GetExecutionVariables(ctx context.Context, executionId string) (string, int
}

func GetWorkflowExecution(ctx context.Context, id string) (*WorkflowExecution, error) {
cacheKey := fmt.Sprintf("workflowexecution-%s", id)
nameKey := "workflowexecution"
cacheKey := fmt.Sprintf("%s_%s", nameKey, id)
workflowExecution := &WorkflowExecution{}
if project.CacheDb {
cache, err := GetCache(ctx, cacheKey)
Expand All @@ -401,7 +403,7 @@ func GetWorkflowExecution(ctx context.Context, id string) (*WorkflowExecution, e
}
}

key := datastore.NameKey("workflowexecution", strings.ToLower(id), nil)
key := datastore.NameKey(nameKey, strings.ToLower(id), nil)
if err := project.Dbclient.Get(ctx, key, workflowExecution); err != nil {
return &WorkflowExecution{}, err
}
Expand Down Expand Up @@ -439,13 +441,42 @@ func GetApp(ctx context.Context, id string, user User) (*WorkflowApp, error) {
}

func GetWorkflow(ctx context.Context, id string) (*Workflow, error) {

key := datastore.NameKey("workflow", strings.ToLower(id), nil)
workflow := &Workflow{}
nameKey := "workflow"

cacheKey := fmt.Sprintf("%s_%s", nameKey, id)
if project.CacheDb {
cache, err := GetCache(ctx, cacheKey)
if err == nil {
cacheData := []byte(cache.([]uint8))
//log.Printf("CACHEDATA: %#v", cacheData)
err = json.Unmarshal(cacheData, &workflow)
if err == nil {
return workflow, nil
}
} else {
log.Printf("[INFO] Failed getting cache for workflow: %s", err)
}
}

key := datastore.NameKey(nameKey, strings.ToLower(id), nil)
if err := project.Dbclient.Get(ctx, key, workflow); err != nil {
return &Workflow{}, err
}

if project.CacheDb {
data, err := json.Marshal(workflow)
if err != nil {
log.Printf("[WARNING] Failed marshalling in getworkflow: %s", err)
return workflow, nil
}

err = SetCache(ctx, cacheKey, data)
if err != nil {
log.Printf("[WARNING] Failed setting cache for getworkflow: %s", err)
}
}

return workflow, nil
}

Expand Down Expand Up @@ -483,6 +514,7 @@ func GetOrg(ctx context.Context, id string) (*Org, error) {
return &Org{}, err
}

newUsers := []User{}
for _, user := range curOrg.Users {
user.Password = ""
user.Session = ""
Expand All @@ -491,8 +523,10 @@ func GetOrg(ctx context.Context, id string) (*Org, error) {
user.VerificationToken = ""
user.ApiKey = ""
user.Executions = ExecutionInfo{}
newUsers = append(newUsers, user)
}

curOrg.Users = newUsers
if project.CacheDb {
neworg, err := json.Marshal(curOrg)
if err != nil {
Expand Down Expand Up @@ -525,15 +559,29 @@ func SetOrg(ctx context.Context, data Org, id string) error {
}

if project.CacheDb {
newUsers := []User{}
for _, user := range data.Users {
user.Password = ""
user.Session = ""
user.ResetReference = ""
user.PrivateApps = []WorkflowApp{}
user.VerificationToken = ""
user.ApiKey = ""
user.Executions = ExecutionInfo{}
newUsers = append(newUsers, user)
}

data.Users = newUsers

neworg, err := json.Marshal(data)
if err != nil {
log.Printf("[WARNING] Failed marshalling in setorg: %s", err)
return nil
}

err = SetCache(ctx, id, neworg)
if err != nil {
log.Printf("Failed setting cache: %s", err)
//DeleteCache(neworg)
log.Printf("[WARNING] Failed setting cache for org: %s", err)
}
}

Expand Down Expand Up @@ -578,6 +626,8 @@ func GetSession(ctx context.Context, thissession string) (*Session, error) {
// Index = Username
func DeleteKey(ctx context.Context, entity string, value string) error {
// Non indexed User data
DeleteCache(ctx, fmt.Sprintf("%s_%s", entity, value))

key1 := datastore.NameKey(entity, value, nil)

err = project.Dbclient.Delete(ctx, key1)
Expand Down Expand Up @@ -806,9 +856,10 @@ func fixUserOrg(ctx context.Context, user *User) *User {
return user
}

func GetAllWorkflowAppAuth(ctx context.Context, OrgId string) ([]AppAuthenticationStorage, error) {
func GetAllWorkflowAppAuth(ctx context.Context, orgId string) ([]AppAuthenticationStorage, error) {
//log.Printf("\n\nGetting ALL workflow app auth for org %s", orgId)
var allworkflowapps []AppAuthenticationStorage
q := datastore.NewQuery("workflowappauth").Filter("org_id = ", OrgId)
q := datastore.NewQuery("workflowappauth").Filter("org_id = ", orgId)

_, err = project.Dbclient.GetAll(ctx, q, &allworkflowapps)
if err != nil {
Expand All @@ -818,9 +869,9 @@ func GetAllWorkflowAppAuth(ctx context.Context, OrgId string) ([]AppAuthenticati
return allworkflowapps, nil
}

func GetEnvironments(ctx context.Context, OrgId string) ([]Environment, error) {
func GetEnvironments(ctx context.Context, orgId string) ([]Environment, error) {
var environments []Environment
q := datastore.NewQuery("Environments").Filter("org_id =", OrgId)
q := datastore.NewQuery("Environments").Filter("org_id =", orgId)

_, err = project.Dbclient.GetAll(ctx, q, &environments)
if err != nil {
Expand Down Expand Up @@ -1147,19 +1198,33 @@ func GetWorkflowQueue(ctx context.Context, id string) (ExecutionRequestWrapper,
}

func SetWorkflow(ctx context.Context, workflow Workflow, id string, optionalEditedSecondsOffset ...int) error {
nameKey := "workflow"
key := datastore.NameKey(nameKey, id, nil)
workflow.Edited = int64(time.Now().Unix())
if len(optionalEditedSecondsOffset) > 0 {
workflow.Edited += int64(optionalEditedSecondsOffset[0])
}

key := datastore.NameKey("workflow", id, nil)

// New struct, to not add body, author etc
if _, err := project.Dbclient.Put(ctx, key, &workflow); err != nil {
log.Printf("Error adding workflow: %s", err)
return err
}

if project.CacheDb {
data, err := json.Marshal(workflow)
if err != nil {
log.Printf("[WARNING] Failed marshalling in getworkflow: %s", err)
return nil
}

cacheKey := fmt.Sprintf("%s_%s", nameKey, id)
err = SetCache(ctx, cacheKey, data)
if err != nil {
log.Printf("[WARNING] Failed setting cache for getworkflow: %s", err)
}
}

return nil
}

Expand Down Expand Up @@ -1218,24 +1283,69 @@ func GetApikey(ctx context.Context, apikey string) (User, error) {
}

func GetHook(ctx context.Context, hookId string) (*Hook, error) {
key := datastore.NameKey("hooks", strings.ToLower(hookId), nil)
nameKey := "hooks"
cacheKey := fmt.Sprintf("%s_%s", nameKey, hookId)

hook := &Hook{}
if project.CacheDb {
cache, err := GetCache(ctx, cacheKey)
if err == nil {
cacheData := []byte(cache.([]uint8))
//log.Printf("CACHEDATA: %#v", cacheData)
err = json.Unmarshal(cacheData, &hook)
if err == nil {
return hook, nil
}
} else {
log.Printf("[INFO] Failed getting cache for hook: %s", err)
}
}

key := datastore.NameKey(nameKey, strings.ToLower(hookId), nil)
if err := project.Dbclient.Get(ctx, key, hook); err != nil {
return &Hook{}, err
}

if project.CacheDb {
hookData, err := json.Marshal(hook)
if err != nil {
log.Printf("[WARNING] Failed marshalling in gethook: %s", err)
return hook, nil
}

err = SetCache(ctx, cacheKey, hookData)
if err != nil {
log.Printf("[WARNING] Failed setting cache for gethook: %s", err)
}
}

return hook, nil
}

func SetHook(ctx context.Context, hook Hook) error {
key1 := datastore.NameKey("hooks", strings.ToLower(hook.Id), nil)
nameKey := "hooks"
key1 := datastore.NameKey(nameKey, strings.ToLower(hook.Id), nil)

// New struct, to not add body, author etc
if _, err := project.Dbclient.Put(ctx, key1, &hook); err != nil {
log.Printf("Error adding hook: %s", err)
return err
}

if project.CacheDb {
hookData, err := json.Marshal(hook)
if err != nil {
log.Printf("[WARNING] Failed marshalling in setHook: %s", err)
return nil
}

cacheKey := fmt.Sprintf("%s_%s", nameKey, hook.Id)
err = SetCache(ctx, cacheKey, hookData)
if err != nil {
log.Printf("[WARNING] Failed setting cache for hook: %s", err)
}
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ require (
cloud.google.com/go/datastore v1.4.0
cloud.google.com/go/storage v1.12.0
github.com/frikky/kin-openapi v0.38.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/satori/go.uuid v1.2.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
google.golang.org/api v0.36.0
google.golang.org/appengine v1.6.7
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/patrickmn/go-cache v1.0.0 h1:3gD5McaYs9CxjyK5AXGcq8gdeCARtd/9gJDUvVeaZ0Y=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down Expand Up @@ -169,6 +172,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down
Loading

0 comments on commit 9bf0f07

Please sign in to comment.