/
response_conversion.go
217 lines (184 loc) · 6.98 KB
/
response_conversion.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package grpcserver
import (
"encoding/json"
"strings"
"github.com/chef/automate/api/interservice/cfgmgmt/response"
"github.com/chef/automate/components/config-mgmt-service/backend"
ingestBackend "github.com/chef/automate/components/ingest-service/backend"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
google_protobuf "github.com/golang/protobuf/ptypes/struct"
)
func toResponseRun(run backend.Run, associatedNode backend.Node) (*response.Run, error) {
chefError, err := toResponseError(run.Error)
startTime, _ := ptypes.TimestampProto(run.StartTime.UTC())
endTime, _ := ptypes.TimestampProto(run.EndTime.UTC())
return &response.Run{
Id: run.RunID,
NodeId: run.EntityUuid,
NodeName: run.NodeName,
Organization: run.OrganizationName,
StartTime: startTime,
EndTime: endTime,
Source: run.Source,
Status: run.Status,
TotalResourceCount: int32(run.TotalResourceCount),
UpdatedResourceCount: int32(run.UpdatedResourceCount),
ChefVersion: run.ChefVersion,
UptimeSeconds: int32(run.UptimeSeconds),
Environment: run.Environment,
Fqdn: run.Fqdn,
SourceFqdn: run.SourceFqdn,
Ipaddress: toString(run.Ipaddress),
RunList: run.RunList,
Tags: run.Tags,
ResourceNames: run.ResourceNames,
Recipes: run.Recipes,
ChefTags: run.ChefTags,
Cookbooks: run.Cookbooks,
Platform: run.Platform,
PlatformFamily: run.PlatformFamily,
PlatformVersion: run.PlatformVersion,
Roles: run.Roles,
PolicyName: run.PolicyName,
PolicyGroup: run.PolicyGroup,
PolicyRevision: run.PolicyRevision,
ExpandedRunList: toResponseExpandedRunList(run.ExpandedRunList),
Resources: toResponseResources(run.Resources),
Deprecations: toResponseDeprecations(run.Deprecations),
Error: chefError,
Projects: associatedNode.Projects,
VersionedCookbooks: toVersionsCookbooks(run.VersionedCookbooks),
Timezone: run.Timezone,
CloudProvider: run.CloudProvider,
KernelRelease: run.KernelRelease,
KernelVersion: run.KernelVersion,
VirtualizationSystem: run.VirtualizationSystem,
VirtualizationRole: run.VirtualizationRole,
DmiSystemManufacturer: run.DmiSystemManufacturer,
DmiSystemSerialNumber: run.DmiSystemSerialNumber,
Domain: run.Domain,
Hostname: run.Hostname,
Macaddress: run.Macaddress,
MemoryTotal: run.MemoryTotal,
Ip6Address: toString(run.Ip6address),
}, err
}
func toString(o interface{}) string {
s, ok := o.(string)
if ok {
return s
}
return ""
}
func toResponseError(chefError ingestBackend.ChefError) (*response.ChefError, error) {
chefErrorResponse := &response.ChefError{
Class: chefError.Class,
Message: chefError.Message,
Backtrace: chefError.Backtrace,
}
if chefError.Description.Title != "" {
sections, err := toStructCollection(chefError.Description.Sections)
if err != nil {
return chefErrorResponse, err
}
chefErrorResponse.Description = &response.Description{
Title: chefError.Description.Title,
Sections: sections,
}
}
return chefErrorResponse, nil
}
func toStructCollection(sections []map[string]interface{}) ([]*google_protobuf.Struct, error) {
structCollection := make([]*google_protobuf.Struct, len(sections))
for index, section := range sections {
protoStruct, err := toStruct(section)
if err != nil {
return nil, err
}
structCollection[index] = protoStruct
}
return structCollection, nil
}
func toStruct(section map[string]interface{}) (*google_protobuf.Struct, error) {
protoStruct := new(google_protobuf.Struct)
jsonBytes, err := json.Marshal(section)
if err != nil {
return protoStruct, err
}
err = protoFromJSON(string(jsonBytes), protoStruct)
return protoStruct, err
}
func protoFromJSON(content string, pb proto.Message) error {
unmarshaler := &jsonpb.Unmarshaler{AllowUnknownFields: true}
return unmarshaler.Unmarshal(strings.NewReader(content), pb)
}
func getMessageRawJSON(message proto.Message) (string, error) {
return (&jsonpb.Marshaler{OrigName: true}).MarshalToString(message)
}
func toResponseDeprecations(deprecations []ingestBackend.Deprecation) []*response.Deprecation {
responseDeprecations := make([]*response.Deprecation, len(deprecations))
for index, deprecation := range deprecations {
responseDeprecations[index] = &response.Deprecation{
Message: deprecation.Message,
Url: deprecation.URL,
Location: deprecation.Location,
}
}
return responseDeprecations
}
func toResponseExpandedRunList(expandedRunList ingestBackend.ExpandedRunList) *response.ExpandedRunList {
var responseExpandedRunList response.ExpandedRunList
responseExpandedRunList.Id = expandedRunList.ID
responseExpandedRunList.RunList = make([]*response.RunList, len(expandedRunList.RunList))
for index, runList := range expandedRunList.RunList {
responseExpandedRunList.RunList[index] = toResponseRunList(runList)
}
return &responseExpandedRunList
}
func toResponseRunList(runListItem ingestBackend.ExpandedRunListRunList) *response.RunList {
children := make([]*response.RunList, len(runListItem.Children))
for index, runListChild := range runListItem.Children {
children[index] = toResponseRunList(runListChild)
}
return &response.RunList{
Type: runListItem.Type,
Name: runListItem.Name,
Version: toString(runListItem.Version),
Skipped: runListItem.Skipped,
Children: children,
}
}
func toResponseResources(resources []ingestBackend.Resource) []*response.Resource {
responseResources := make([]*response.Resource, len(resources))
for index, resource := range resources {
chefError, _ := toResponseError(resource.Error)
responseResources[index] = &response.Resource{
Type: resource.Type,
Name: resource.Name,
Id: resource.ID,
Duration: resource.Duration,
Delta: resource.Delta,
CookbookName: resource.CookbookName,
CookbookVersion: resource.CookbookVersion,
Status: resource.Status,
RecipeName: resource.RecipeName,
Result: resource.Result,
Conditional: resource.Conditional, // might be empty
IgnoreFailure: resource.IgnoreFailure, // might be empty
Error: chefError,
}
}
return responseResources
}
func toVersionsCookbooks(versionedCookbooks []ingestBackend.VersionedCookbook) []*response.VersionedCookbook {
responseExpandedCookbooks := make([]*response.VersionedCookbook, len(versionedCookbooks))
for index, cookbook := range versionedCookbooks {
responseExpandedCookbooks[index] = &response.VersionedCookbook{
Name: cookbook.Name,
Version: cookbook.Version,
}
}
return responseExpandedCookbooks
}