This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
133 lines (110 loc) · 3.1 KB
/
index.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
130
131
132
133
package boshuaV2
import (
"fmt"
"net/http"
"reflect"
"strings"
boshuaV2 "github.com/dpb587/boshua/artifact/datastore/datastoreutil/boshua.v2"
"github.com/dpb587/boshua/osversion"
osversiongraphql "github.com/dpb587/boshua/osversion/graphql"
"github.com/dpb587/boshua/releaseversion"
"github.com/dpb587/boshua/releaseversion/compilation"
"github.com/dpb587/boshua/releaseversion/compilation/datastore"
releaseversiongraphql "github.com/dpb587/boshua/releaseversion/graphql"
"github.com/machinebox/graphql"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type index struct {
name string
logger logrus.FieldLogger
config Config
client *boshuaV2.Client
}
var _ datastore.Index = &index{}
func New(name string, config Config, logger logrus.FieldLogger) datastore.Index {
return &index{
name: name,
logger: logger.WithField("build.package", reflect.TypeOf(index{}).PkgPath()),
config: config,
client: boshuaV2.NewClient(http.DefaultClient, config.BoshuaConfig, logger),
}
}
func (i *index) GetName() string {
return i.name
}
func (i *index) GetCompilationArtifacts(f datastore.FilterParams) ([]compilation.Artifact, error) {
// TODO this should be using "compilations", not singular compilation
fReleaseQueryFilter, fReleaseQueryVarsTypes, fReleaseQueryVars := releaseversiongraphql.BuildListQueryArgs(f.Release)
if len(fReleaseQueryFilter) > 0 {
fReleaseQueryFilter = fmt.Sprintf(`(%s)`, fReleaseQueryFilter)
}
fOSQueryFilter, fOSQueryVarsTypes, fOSQueryVars := osversiongraphql.BuildListQueryArgs(f.OS)
if len(fOSQueryFilter) > 0 {
fOSQueryFilter = fmt.Sprintf(`(%s)`, fOSQueryFilter)
}
fQueryVarsTypes := strings.Join([]string{fReleaseQueryVarsTypes, fOSQueryVarsTypes}, ", ")
if len(fQueryVarsTypes) > 0 {
fQueryVarsTypes = fmt.Sprintf(`(%s)`, fQueryVarsTypes)
}
// TODO weird singular vs multiple queries
cmd := fmt.Sprintf(`query %s {
release %s {
name
version
labels
compilations %s {
os
version
labels
tarball {
name
size
hashes {
type
hash
}
urls {
url
}
metaurls {
url
mediatype
}
}
}
}
}`, fQueryVarsTypes, fReleaseQueryFilter, fOSQueryFilter)
req := graphql.NewRequest(cmd)
for k, v := range fReleaseQueryVars {
req.Var(k, v)
}
for k, v := range fOSQueryVars {
req.Var(k, v)
}
var resp filterResponse
err := i.client.Execute(req, &resp)
if err != nil {
return nil, errors.Wrap(err, "executing remote request")
}
var results []compilation.Artifact
for _, compl := range resp.Release.Compilations {
results = append(results, compilation.Artifact{
Datastore: i.name,
OS: osversion.Reference{Name: compl.OS, Version: compl.Version},
Release: releaseversion.Reference{
Name: resp.Release.Name,
Version: resp.Release.Version,
},
Tarball: compl.Tarball,
Labels: compl.Labels,
})
}
return results, nil
}
func (i *index) StoreCompilationArtifact(_ compilation.Artifact) error {
return datastore.UnsupportedOperationErr
}
func (i *index) FlushCompilationCache() error {
return nil
}