/
message.go
69 lines (56 loc) · 1.67 KB
/
message.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
package eve
import (
"context"
"encoding/json"
"fmt"
uuid "github.com/satori/go.uuid"
"github.com/unanet/eve/pkg/s3"
"github.com/unanet/go/pkg/errors"
)
type CloudUploader interface {
Upload(ctx context.Context, key string, body []byte) (*s3.Location, error)
}
type CloudDownloader interface {
Download(ctx context.Context, location *s3.Location) ([]byte, error)
}
type DeploymentCallbackMessage struct {
DeploymentID uuid.UUID `json:"deployment_id"`
Status DeploymentPlanStatus `json:"status"`
Type PlanType `json:"type"`
Messages []string `json:"messages"`
}
type CallbackMessage struct {
Messages []string `json:"messages"`
}
func UnMarshalNSDeploymentFromS3LocationBody(ctx context.Context, cd CloudDownloader, b []byte) (*NSDeploymentPlan, error) {
var location s3.Location
err := json.Unmarshal(b, &location)
if err != nil {
return nil, errors.Wrap(err)
}
planText, err := cd.Download(ctx, &location)
if err != nil {
return nil, errors.Wrap(err)
}
var nsDeploymentPlan NSDeploymentPlan
err = json.Unmarshal(planText, &nsDeploymentPlan)
if err != nil {
return nil, errors.Wrap(err)
}
return &nsDeploymentPlan, nil
}
func MarshalNSDeploymentPlanToS3LocationBody(ctx context.Context, cu CloudUploader, plan *NSDeploymentPlan) ([]byte, error) {
nsDeploymentJson, err := json.Marshal(plan)
if err != nil {
return nil, errors.Wrap(err)
}
location, err := cu.Upload(ctx, fmt.Sprintf("%s.json", plan.DeploymentID), nsDeploymentJson)
if err != nil {
return nil, errors.Wrap(err)
}
locationJson, err := json.Marshal(location)
if err != nil {
return nil, errors.Wrap(err)
}
return locationJson, nil
}