forked from hishamkaram/geoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastores.go
211 lines (191 loc) · 6.32 KB
/
datastores.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
package geoserver
import (
"bytes"
"strconv"
)
// DatastoreService define all geoserver datastore operations
type DatastoreService interface {
// DatastoreExists checks if a datastore exists in a workspace else return error
DatastoreExists(workspaceName string, datastoreName string, quietOnNotFound bool) (exists bool, err error)
// GetDatastores return datastores in a workspace else return error
GetDatastores(workspaceName string) (datastores []*Resource, err error)
// GetDatastoreDetails get specific datastore from geoserver else return error
GetDatastoreDetails(workspaceName string, datastoreName string) (datastore *Datastore, err error)
//CreateDatastore create a datastore under provided workspace
CreateDatastore(datastoreConnection DatastoreConnection, workspaceName string) (created bool, err error)
// DeleteDatastore deletes a datastore from geoserver else return error
DeleteDatastore(workspaceName string, datastoreName string, recurse bool) (deleted bool, err error)
}
// Datastore holds geoserver store information
type Datastore struct {
Name string `json:"name,omitempty"`
Href string `json:"href,omitempty"`
Type string `json:"type,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Workspace *Workspace `json:"workspace,omitempty"`
Default bool `json:"_default,omitempty"`
FeatureTypes string `json:"featureTypes,omitempty"`
ConnectionParameters DatastoreConnectionParams `json:"connectionParameters,omitempty"`
}
//DatastoreDetails this struct to send and accept json data from/to geoserver
type DatastoreDetails struct {
Datastore *Datastore `json:"dataStore"`
}
// DatastoreConnection holds parameters to create new datastore in geoserver
type DatastoreConnection struct {
Name string
Host string
Port int
DBName string
DBUser string
DBPass string
Type string
}
// DatastoreConnectionParams in datastore json
type DatastoreConnectionParams struct {
Entry []*Entry `json:"entry,omitempty"`
}
//GetDatastoreObj return datastore Object to send to geoserver rest
func (connection *DatastoreConnection) GetDatastoreObj() (datastore Datastore) {
datastore = Datastore{
Name: connection.Name,
ConnectionParameters: DatastoreConnectionParams{
Entry: []*Entry{
{
Key: "host",
Value: connection.Host,
},
{
Key: "port",
Value: strconv.Itoa(connection.Port),
},
{
Key: "database",
Value: connection.DBName,
},
{
Key: "user",
Value: connection.DBUser,
},
{
Key: "passwd",
Value: connection.DBPass,
},
{
Key: "dbtype",
Value: connection.Type,
},
},
},
}
return
}
// DatastoreExists checks if a datastore exists in a workspace else return error
func (g *GeoServer) DatastoreExists(workspaceName string, datastoreName string, quietOnNotFound bool) (exists bool, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores", datastoreName)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"quietOnNotFound": strconv.FormatBool(quietOnNotFound)},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
exists = false
err = g.GetError(responseCode, response)
return
}
exists = true
return
}
// GetDatastores return datastores in a workspace else return error
func (g *GeoServer) GetDatastores(workspaceName string) (datastores []*Resource, err error) {
//TODO: check if workspace exist before creating it
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores")
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
datastores = nil
err = g.GetError(responseCode, response)
return
}
var query struct {
DataStores struct {
DataStore []*Resource
}
}
g.DeSerializeJSON(response, &query)
datastores = query.DataStores.DataStore
return
}
// GetDatastoreDetails get specific datastore from geoserver else return error
func (g *GeoServer) GetDatastoreDetails(workspaceName string, datastoreName string) (datastore *Datastore, err error) {
//TODO: check if workspace exist before creating it
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores", datastoreName)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
datastore = &Datastore{}
err = g.GetError(responseCode, response)
return
}
var query DatastoreDetails
g.DeSerializeJSON(response, &query)
datastore = query.Datastore
return
}
//CreateDatastore create a datastore under provided workspace
func (g *GeoServer) CreateDatastore(datastoreConnection DatastoreConnection, workspaceName string) (created bool, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores")
store := datastoreConnection.GetDatastoreObj()
datastore := DatastoreDetails{
Datastore: &store,
}
data, _ := g.SerializeStruct(datastore)
httpRequest := HTTPRequest{
Method: postMethod,
Accept: jsonType,
Data: bytes.NewBuffer(data),
DataType: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusCreated {
g.logger.Warn(string(response))
created = false
err = g.GetError(responseCode, response)
return
}
created = true
return
}
// DeleteDatastore deletes a datastore from geoserver else return error
func (g *GeoServer) DeleteDatastore(workspaceName string, datastoreName string, recurse bool) (deleted bool, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores", datastoreName)
httpRequest := HTTPRequest{
Method: deleteMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"recurse": strconv.FormatBool(recurse)},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Warn(string(response))
deleted = false
err = g.GetError(responseCode, response)
return
}
deleted = true
return
}