-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve.go
129 lines (102 loc) · 2.98 KB
/
retrieve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package domain
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
"github.com/gookit/color"
"github.com/sirupsen/logrus"
"github.com/skuid/domain/constants"
"github.com/skuid/domain/logging"
)
var (
RetrievePlanRoute = fmt.Sprintf("api/%v/metadata/retrieve/plan", DEFAULT_API_VERSION)
)
func GetRetrievePlan(auth *Authorization, filter *NlxPlanFilter) (duration time.Duration, result NlxPlanPayload, err error) {
planStart := time.Now()
defer func() { duration = time.Since(planStart) }()
var body []byte = make([]byte, 0)
if filter != nil {
if body, err = json.Marshal(filter); err != nil {
return
}
}
// this is a pliny request, so we provide the access token
headers := GenerateHeaders(auth.Host, auth.AccessToken)
// no matter what we want to pass application/json
// because the application/zip is discarded by pliny
// and warden will throw an error
headers[HeaderContentType] = JSON_CONTENT_TYPE
result, err = JsonBodyRequest[NlxPlanPayload](
fmt.Sprintf("%s/%s", auth.Host, RetrievePlanRoute),
http.MethodPost,
body,
headers,
)
return
}
type NlxRetrievalResult struct {
Plan NlxPlan
PlanName string
Url string
Data []byte
}
func ExecuteRetrieval(auth *Authorization, plans NlxPlanPayload) (duration time.Duration, results []NlxRetrievalResult, err error) {
logging.WithFields(logrus.Fields{
"func": "ExecuteRetrieval",
})
// for timing sake
start := time.Now()
defer func() { duration = time.Since(start) }()
var mu sync.Mutex
// this function generically handles a plan based on name / stuff
executePlan := func(name string, plan NlxPlan) error {
mu.Lock()
defer mu.Unlock()
logging.WithField("planName", name)
logging.Get().Debugf("Firing off %v", color.Magenta.Sprint(name))
headers := GeneratePlanHeaders(auth, plan)
headers[HeaderContentType] = JSON_CONTENT_TYPE
for k, header := range headers {
logging.Get().Tracef("header: (%v => %v)", color.Yellow.Sprint(k), color.Green.Sprint(header))
}
url := GenerateRoute(auth, plan)
logging.Get().Tracef("URL: %v", color.Blue.Sprint(url))
result, err := Request(
url, http.MethodPost, NewRetrievalRequestBody(plan.Metadata), headers,
)
if err != nil {
logging.Get().WithFields(logrus.Fields{
"plan": plan,
"planName": name,
"url": url,
})
logging.Get().WithError(err)
logging.Get().Errorf("error with %v request", color.Magenta.Sprint(name))
return err
}
results = append(results, NlxRetrievalResult{
Plan: plan,
PlanName: name,
Url: url,
Data: result,
})
return nil
}
// has to be pliny, then warden
if plans.MetadataService != nil {
if err = executePlan(constants.PLINY, *plans.MetadataService); err != nil {
return
}
}
if plans.CloudDataService != nil {
if err = executePlan(constants.WARDEN, *plans.CloudDataService); err != nil {
return
}
}
return
}
func (x NlxRetrievalResult) String() string {
return fmt.Sprintf("(%v => %v (size: %v))", x.PlanName, x.Url, len(x.Data))
}