Skip to content

Commit cc75e7f

Browse files
Support test and (re)submit submissions by ID
1 parent 46a4235 commit cc75e7f

5 files changed

Lines changed: 74 additions & 23 deletions

File tree

pkg/cmd/agent/submit.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func NewSubmitCmd(logger *log.Logger) *cobra.Command {
4343
Long: `This takes a docker image or submission manifest and submits it for evaluation.`,
4444
Args: cobra.MaximumNArgs(1),
4545
Run: func(cmd *cobra.Command, args []string) {
46+
var manifest *client.Manifest
4647
if len(args) > 0 {
4748
submissionConfig.Image = args[0]
4849
} else if submissionConfig.ManifestPath == "" {
@@ -53,8 +54,10 @@ func NewSubmitCmd(logger *log.Logger) *cobra.Command {
5354
level.Error(logger).Log("msg", err.Error())
5455
os.Exit(1)
5556
}
56-
57-
submission, err := submissionConfig.Submission()
57+
if submissionConfig.ManifestPath != "" {
58+
manifest, err = client.ManifestFromPath(submissionConfig.ManifestPath)
59+
}
60+
submission, err := submissionConfig.Submission(manifest)
5861
if err != nil {
5962
level.Error(logger).Log("msg", "failed to configure manifest", "err", err.Error())
6063
os.Exit(1)

pkg/cmd/agent/test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,42 @@ func NewTestCmd(logger *log.Logger) *cobra.Command {
3434
to DIAMBRA. This is useful for testing your agent before submitting it. Optionally, you can pass in commands to run instead of the configured entrypoint.`,
3535
Args: cobra.MaximumNArgs(2),
3636
Run: func(cmd *cobra.Command, args []string) {
37-
nargs := len(args)
37+
var (
38+
nargs = len(args)
39+
manifest *client.Manifest
40+
)
3841
if nargs > 0 {
3942
submissionConfig.Image = args[0]
4043
}
4144
if nargs > 1 {
4245
submissionConfig.Command = args[1:]
4346
}
44-
submission, err := submissionConfig.Submission()
47+
switch {
48+
case submissionConfig.SubmissionID != 0:
49+
cl, err := client.NewClient(logger, c.CredPath)
50+
if err != nil {
51+
level.Error(logger).Log("msg", "failed to create client", "err", err.Error())
52+
os.Exit(1)
53+
}
54+
s, err := cl.Submission(submissionConfig.SubmissionID)
55+
if err != nil {
56+
level.Error(logger).Log("msg", "failed to get submission", "err", err.Error())
57+
os.Exit(1)
58+
}
59+
manifest = &s.Manifest
60+
case submissionConfig.ManifestPath != "":
61+
manifest, err = client.ManifestFromPath(submissionConfig.ManifestPath)
62+
if err != nil {
63+
level.Error(logger).Log("msg", "failed to read manifest", "err", err.Error())
64+
os.Exit(1)
65+
}
66+
default:
67+
if submissionConfig.Image == "" {
68+
level.Error(logger).Log("msg", "either image, manifest path or submission id must be provided")
69+
os.Exit(1)
70+
}
71+
}
72+
submission, err := submissionConfig.Submission(manifest)
4573
if err != nil {
4674
level.Error(logger).Log("msg", "failed to configure manifest", "err", err.Error())
4775
os.Exit(1)

pkg/diambra/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (c *Client) Request(method string, path string, body io.Reader, authenticat
5454
if err != nil {
5555
return nil, err
5656
}
57-
level.Debug(c.logger).Log("msg", "Request", "method", method, "path", path, "body", body, "authenticated", authenticated, "apiURL", apiURL)
57+
level.Debug(c.logger).Log("msg", "Request", "method", method, "url", surl, "authenticated", authenticated)
5858

5959
req, err := http.NewRequest(
6060
method,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"os"
910

1011
"github.com/go-kit/log/level"
12+
"gopkg.in/yaml.v2"
1113
)
1214

1315
// Mode Enum
@@ -62,3 +64,33 @@ func (c *Client) Submit(submission *Submission) (int, error) {
6264
}
6365
return s.ID, nil
6466
}
67+
68+
func (c *Client) Submission(id int) (*Submission, error) {
69+
resp, err := c.Request("GET", fmt.Sprintf("submissions/%d", id), nil, true)
70+
if err != nil {
71+
return nil, err
72+
}
73+
defer resp.Body.Close()
74+
if resp.StatusCode != http.StatusOK {
75+
errResp, err := io.ReadAll(resp.Body)
76+
if err != nil {
77+
errResp = []byte(fmt.Sprintf("failed to read error response: %s", err))
78+
}
79+
return nil, fmt.Errorf("failed to get submission: %s: %s", resp.Status, errResp)
80+
}
81+
var s Submission
82+
if err := json.NewDecoder(resp.Body).Decode(&s); err != nil {
83+
return nil, err
84+
}
85+
return &s, nil
86+
}
87+
88+
func ManifestFromPath(path string) (*Manifest, error) {
89+
manifest := &Manifest{}
90+
f, err := os.Open(path)
91+
if err != nil {
92+
return nil, fmt.Errorf("failed to open manifest: %w", err)
93+
}
94+
defer f.Close()
95+
return manifest, yaml.NewDecoder(f).Decode(manifest)
96+
}

pkg/diambra/config.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/go-kit/log"
3030
"github.com/go-kit/log/level"
3131
"github.com/spf13/pflag"
32-
"gopkg.in/yaml.v2"
3332
)
3433

3534
const (
@@ -238,6 +237,7 @@ type SubmissionConfig struct {
238237
Secrets map[string]string
239238
Command []string
240239
ManifestPath string
240+
SubmissionID int
241241
}
242242

243243
func NewSubmissionConfig(logger log.Logger) *SubmissionConfig {
@@ -253,25 +253,13 @@ func (c *SubmissionConfig) AddFlags(flags *pflag.FlagSet) {
253253
flags.StringToStringVarP(&c.Sources, "submission.source", "u", nil, "Source urls to pass to the agent")
254254
flags.StringToStringVar(&c.Secrets, "submission.secret", nil, "Secrets to pass to the agent")
255255
flags.StringVar(&c.ManifestPath, "submission.manifest", "", "Path to manifest file.")
256+
flags.IntVar(&c.SubmissionID, "submission.id", 0, "Submission ID to retrieve manifest from")
256257
}
257258

258-
func (c *SubmissionConfig) Submission() (*client.Submission, error) {
259-
if c.Image == "" && c.ManifestPath == "" {
260-
return nil, fmt.Errorf("either image or manifest path must be provided")
259+
func (c *SubmissionConfig) Submission(manifest *client.Manifest) (*client.Submission, error) {
260+
if manifest == nil {
261+
manifest = &client.Manifest{}
261262
}
262-
// Decode manifestPath
263-
var manifest client.Manifest
264-
if c.ManifestPath != "" {
265-
f, err := os.Open(c.ManifestPath)
266-
if err != nil {
267-
return nil, fmt.Errorf("failed to open manifest: %w", err)
268-
}
269-
defer f.Close()
270-
if err := yaml.NewDecoder(f).Decode(&manifest); err != nil {
271-
return nil, fmt.Errorf("failed to decode manifest: %w", err)
272-
}
273-
}
274-
275263
if c.Image != "" {
276264
manifest.Image = c.Image
277265
}
@@ -304,7 +292,7 @@ func (c *SubmissionConfig) Submission() (*client.Submission, error) {
304292
}
305293

306294
return &client.Submission{
307-
Manifest: manifest,
295+
Manifest: *manifest,
308296
Secrets: c.Secrets,
309297
}, nil
310298
}

0 commit comments

Comments
 (0)