-
Notifications
You must be signed in to change notification settings - Fork 23
/
models.go
184 lines (146 loc) · 4.73 KB
/
models.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package models
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"fmt"
)
// DeploymentStatus is the enumeration of values which can be provided in a Deployment's status field
type DeploymentStatus string
// AppConfigFileName is the name of top-level config file describing the app
const AppConfigFileName string = "config.json"
// Default deployment settings
const (
DefaultLocation string = "US-VA"
DefaultDeploymentModel string = "GLOBAL"
// DeploymentStatusCreated indicates the app deployment has been created but is not in the job queue yet
DeploymentStatusCreated DeploymentStatus = "created"
// DeploymentStatusSuccessful indicates the app was successfully deployed
DeploymentStatusSuccessful DeploymentStatus = "successful"
// DeploymentStatusFailed indicates the app deployment failed
DeploymentStatusFailed DeploymentStatus = "failed"
// DeploymentStatusPending indicates the app deployment is in the job queue but has not yet started
DeploymentStatusPending DeploymentStatus = "pending"
)
// App config field identifiers
const (
AppIDField string = "app_id"
AppNameField string = "name"
AppLocationField string = "location"
AppDeploymentModelField string = "deployment_model"
)
const (
prettyPrintPrefix = ""
prettyPrintIndent = " "
)
// AppInstanceData defines data pertaining to a specific deployment of a Realm application
type AppInstanceData map[string]interface{}
// UnmarshalFile unmarshals data from a local config file into an AppInstanceData
func (aic *AppInstanceData) UnmarshalFile(path string) error {
return readAndUnmarshalInto(json.Unmarshal, filepath.Join(path, AppConfigFileName), &aic)
}
// readAndUnmarshalInto unmarshals data from the given path into an interface{} using the provided marshalFn
func readAndUnmarshalInto(marshalFn func(in []byte, out interface{}) error, path string, out interface{}) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if len(data) == 0 {
return nil
}
if err := marshalFn(data, out); err != nil {
return fmt.Errorf("failed to parse %s: %s", path, err)
}
return nil
}
// MarshalFile writes the AppInstanceData to the AppConfigFileName at the provided path
func (aic *AppInstanceData) MarshalFile(path string) error {
contents, err := json.MarshalIndent(aic, prettyPrintPrefix, prettyPrintIndent)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(path, AppConfigFileName), contents, 0600)
}
// AppID returns the app's Client App ID
func (aic AppInstanceData) AppID() string {
appID, ok := aic[AppIDField].(string)
if !ok {
return ""
}
return appID
}
// AppName returns the app's user-defined name
func (aic AppInstanceData) AppName() string {
appName, ok := aic[AppNameField].(string)
if !ok {
return ""
}
return appName
}
// AppLocation returns the app's target location
func (aic AppInstanceData) AppLocation() string {
appLocation, ok := aic[AppLocationField].(string)
if !ok {
return DefaultLocation
}
return appLocation
}
// AppDeploymentModel returns the app's deployment model
func (aic AppInstanceData) AppDeploymentModel() string {
appDeploymentModel, ok := aic[AppDeploymentModelField].(string)
if !ok {
return DefaultDeploymentModel
}
return appDeploymentModel
}
// UserProfile holds basic metadata for a given user
type UserProfile struct {
Roles []role `json:"roles"`
}
// AllGroupIDs returns all available group ids for a given user
func (pd *UserProfile) AllGroupIDs() []string {
groupIDs := []string{}
for _, role := range pd.Roles {
if role.GroupID != "" {
groupIDs = append(groupIDs, role.GroupID)
}
}
return groupIDs
}
type role struct {
GroupID string `json:"group_id"`
}
// App represents basic Realm App data
type App struct {
ID string `json:"_id"`
GroupID string `json:"group_id"`
ClientAppID string `json:"client_app_id"`
Name string `json:"name"`
}
// AppDraft represents a Realm App Draft
type AppDraft struct {
ID string `json:"_id"`
}
// Deployment represents a Realm Deployment
type Deployment struct {
ID string `json:"_id"`
Status DeploymentStatus `json:"status"`
}
// DraftDiff represents the diff of an AppDraft
type DraftDiff struct {
Diffs []string `json:"diffs"`
HostingFilesDiff HostingDiff `json:"hosting_files_diff"`
}
// HasChanges returns whether the DraftDiff contains any changes or not
func (d *DraftDiff) HasChanges() bool {
return len(d.Diffs) != 0 ||
len(d.HostingFilesDiff.Added) != 0 ||
len(d.HostingFilesDiff.Deleted) != 0 ||
len(d.HostingFilesDiff.Modified) != 0
}
// HostingDiff represents the hosting files section of a DraftDiff
type HostingDiff struct {
Added []string `json:"added"`
Deleted []string `json:"deleted"`
Modified []string `json:"modified"`
}