-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
100 lines (88 loc) · 2.6 KB
/
app.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
package arpio
import (
"encoding/json"
"fmt"
"time"
)
// Enumeration of the types of Arpio applications.
const (
StandardAppType = "standard"
TerraformAppType = "terraform"
)
type App struct {
AccountID string `json:"accountId"`
AppID string `json:"appId,omitempty"`
AppType string `json:"type"`
CreatedAt time.Time `json:"createdAt,omitempty"`
Name string `json:"name"`
NotificationEmails []string `json:"notificationEmails,omitempty"`
RPO int `json:"rpo"`
SelectionRules []SelectionRule `json:"-"`
SourceAwsAccountID string `json:"sourceAwsAccountId"`
SourceRegion string `json:"sourceRegion"`
SyncPhase string `json:"syncPhase,omitempty"`
TargetAwsAccountID string `json:"targetAwsAccountId"`
TargetRegion string `json:"targetRegion"`
// JSON serialization helpers
RawSelectionRules []json.RawMessage `json:"selectionRules"`
}
func (a App) MarshalJSON() ([]byte, error) {
// Use a type alias to avoid invoking this function recursively
type AppDTO App
// Marshal SelectionRules through RawSelectionRules
a.RawSelectionRules = []json.RawMessage{}
if a.SelectionRules != nil {
for _, r := range a.SelectionRules {
b, err := json.Marshal(r)
if err != nil {
return nil, err
}
a.RawSelectionRules = append(a.RawSelectionRules, b)
}
}
return json.Marshal((AppDTO)(a))
}
func (a *App) UnmarshalJSON(b []byte) error {
// Use a type alias to avoid invoking this function recursively
type AppDTO App
err := json.Unmarshal(b, (*AppDTO)(a))
if err != nil {
return err
}
// Unmarshal RawSelectionRules into SelectionRules
a.SelectionRules = []SelectionRule{}
for _, rawSelectionRule := range a.RawSelectionRules {
var baseRule selectionRule
err = json.Unmarshal(rawSelectionRule, &baseRule)
if err != nil {
return err
}
var rule SelectionRule
switch baseRule.RuleType {
case ArnRuleType:
r := &ArnRule{}
err = json.Unmarshal(rawSelectionRule, r)
rule = *r
case TagRuleType:
r := &TagRule{}
err = json.Unmarshal(rawSelectionRule, r)
rule = *r
default:
panic(fmt.Sprintf("unhandled selection rule type: %s", baseRule.RuleType))
}
if err != nil {
return err
}
a.SelectionRules = append(a.SelectionRules, rule)
}
return nil
}
// SyncPair returns a SyncPair struct for the App's endpoint information.
func (a App) SyncPair() SyncPair {
return NewSyncPair(
a.SourceAwsAccountID,
a.SourceRegion,
a.TargetAwsAccountID,
a.TargetRegion,
)
}