-
Notifications
You must be signed in to change notification settings - Fork 3
/
apicfg-impexp.go
181 lines (160 loc) · 4.79 KB
/
apicfg-impexp.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
package webui
import (
"bytes"
"encoding/json"
"io/ioutil"
"mime/multipart"
"os"
"github.com/adejoux/pSeriesCollector/pkg/data/impexp"
"github.com/go-macaron/binding"
"gopkg.in/macaron.v1"
)
// UploadForm form struct
type UploadForm struct {
AutoRename bool
OverWrite bool
ExportFile *multipart.FileHeader
}
// NewAPICfgImportExport Import/Export REST API creator
func NewAPICfgImportExport(m *macaron.Macaron) error {
bind := binding.Bind
m.Group("/api/cfg/export", func() {
m.Get("/:objtype/:id", reqSignedIn, ExportObject)
m.Post("/:objtype/:id", reqSignedIn, bind(impexp.ExportInfo{}), ExportObjectToFile)
})
m.Group("/api/cfg/bulkexport", func() {
m.Post("/", reqSignedIn, binding.Json(impexp.ExportData{}), BulkExportObjectToFile)
})
m.Group("/api/cfg/import", func() {
m.Post("/", reqSignedIn, binding.MultipartForm(UploadForm{}), ImportDataFile)
})
return nil
}
/****************/
/*IMPORT*/
/*****************/
// ImportCheck import check struct
type ImportCheck struct {
IsOk bool
Message string
Data *impexp.ExportData
}
// ImportDataFile import data from uploaded file
func ImportDataFile(ctx *Context, uf UploadForm) {
if (UploadForm{}) == uf {
log.Error("Error no data in expected struct")
ctx.JSON(404, "Error no data in expected struct")
return
}
log.Debugf("Uploaded data :%+v", uf)
if uf.ExportFile == nil {
ctx.JSON(404, "Error no file uploaded struct")
return
}
log.Debugf("Uploaded File : %+v", uf)
file, err := uf.ExportFile.Open()
if err != nil {
log.Warningf("Error on Open Uploaded File: %s", err)
ctx.JSON(404, err.Error())
return
}
buf := new(bytes.Buffer)
buf.ReadFrom(file)
s := buf.String()
log.Debug("FILE DATA: %s", s)
ImportedData := impexp.ExportData{}
if err := json.Unmarshal(buf.Bytes(), &ImportedData); err != nil {
log.Errorf("Error in data to struct (json-unmarshal) procces: %s", err)
ctx.JSON(404, err.Error())
return
}
log.Debugf("IMPORTED STRUCT %+v", ImportedData)
a, err := ImportedData.ImportCheck()
if err != nil && uf.AutoRename == false && uf.OverWrite == false {
ctx.JSON(200, &ImportCheck{IsOk: false, Message: err.Error(), Data: a})
return
}
err = ImportedData.Import(uf.OverWrite, uf.AutoRename)
if err != nil {
log.Errorf("Some Error happened on import data: %s", err)
ctx.JSON(200, &ImportCheck{IsOk: false, Message: err.Error(), Data: &ImportedData})
return
}
ctx.JSON(200, &ImportCheck{IsOk: true, Message: "All objects have been imported", Data: &ImportedData})
}
/****************/
/*EXPORT*/
/****************/
// ExportObject export object
func ExportObject(ctx *Context) {
id := ctx.Params(":id")
objtype := ctx.Params(":objtype")
exp := impexp.NewExport(&impexp.ExportInfo{FileName: "autogenerated.txt", Description: "autogenerated"})
err := exp.Export(objtype, id, true, 0)
if err != nil {
log.Warningf("Error on get object array for type %s with ID %s , error: %s", objtype, id, err)
ctx.JSON(404, err.Error())
} else {
ctx.JSON(200, &exp)
}
}
func generateFile(ctx *Context, exp *impexp.ExportData) {
outdata, err := json.Marshal(exp)
if err != nil {
log.Errorf("Error on Json data formatting: %s", err)
ctx.JSON(404, err.Error())
return
}
tmpfile, err := ioutil.TempFile("", "snmpcollector")
if err != nil {
log.Errorf("Error on create new temporal file: %s", err)
ctx.JSON(404, err.Error())
return
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(outdata); err != nil {
log.Errorf("Error on write data to temporal file: %s", tmpfile.Name())
ctx.JSON(404, err.Error())
return
}
if err := tmpfile.Close(); err != nil {
log.Errorf("Error on close temporal file: %s", tmpfile.Name())
ctx.JSON(404, err.Error())
return
}
ctx.ServeFile(tmpfile.Name())
}
// ExportObjectToFile export Object to file
func ExportObjectToFile(ctx *Context, info impexp.ExportInfo) {
id := ctx.Params(":id")
objtype := ctx.Params(":objtype")
exp := impexp.NewExport(&info)
err := exp.Export(objtype, id, true, 0)
if err != nil {
log.Errorf("Error on create Export Data with: %s", err)
ctx.JSON(404, err.Error())
return
}
generateFile(ctx, exp)
}
// BulkExportObjectToFile export object recursively to file
func BulkExportObjectToFile(ctx *Context, data impexp.ExportData) {
log.Debugf("DATA %#+v", data)
exp := impexp.NewExport(data.Info)
for _, obj := range data.Objects {
var recursive bool
if obj.Options != nil {
recursive = obj.Options.Recursive
} else {
recursive = true
}
err := exp.Export(obj.ObjectTypeID, obj.ObjectID, recursive, 0)
if err != nil {
log.Errorf("Error on create Export Data for object %s: %s with: %s", obj.ObjectTypeID, obj.ObjectID, err)
ctx.JSON(404, err.Error())
return
}
log.Infof("Object type %s| with ID: %s | exported Successfully %t", obj.ObjectTypeID, obj.ObjectID, recursive)
}
generateFile(ctx, exp)
}