-
Notifications
You must be signed in to change notification settings - Fork 24
/
coverage_stores.go
152 lines (143 loc) · 5.23 KB
/
coverage_stores.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
package geoserver
import (
"bytes"
"strconv"
)
// CoverageStoresService define all geoserver CoverageStores operations
type CoverageStoresService interface {
GetCoverageStores(workspaceName string) (coverageStores []*Resource, err error)
GetCoverageStore(workspaceName string, gridName string) (coverageStore *CoverageStore, err error)
CreateCoverageStore(workspaceName string, coverageStore CoverageStore) (created bool, err error)
UpdateCoverageStore(workspaceName string, coverageStore CoverageStore) (modified bool, err error)
DeleteCoverageStore(workspaceName string, coverageStore string, recurse bool) (deleted bool, err error)
}
//CoverageStore geoserver coverage store
type CoverageStore struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Workspace *Resource `json:"workspace,omitempty"`
Default bool `json:"_default,omitempty"`
Coverages string `json:"coverages,omitempty"`
}
//CoverageStoreRequestBody geoserver coverage store to send to api
type CoverageStoreRequestBody struct {
CoverageStore *CoverageStore `json:"coverageStore,omitempty"`
}
// GetCoverageStores return all coverage store as resources,
// err is an error if error occurred else err is nil
func (g *GeoServer) GetCoverageStores(workspaceName string) (coverageStores []*Resource, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores")
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
coverageStores = nil
err = g.GetError(responseCode, response)
return
}
var coverageStoresResponse struct {
CoverageStores struct {
CoverageStore []*Resource `json:"coverageStore,omitempty"`
} `json:"coverageStores,omitempty"`
}
g.DeSerializeJSON(response, &coverageStoresResponse)
coverageStores = coverageStoresResponse.CoverageStores.CoverageStore
return
}
// GetCoverageStore return coverage store from a workspace,
// err is an error if error occurred else err is nil
func (g *GeoServer) GetCoverageStore(workspaceName string, gridName string) (coverageStore *CoverageStore, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores", gridName)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
coverageStore = nil
err = g.GetError(responseCode, response)
return
}
var coverageStoreResponse struct {
CoverageStore *CoverageStore `json:"coverageStore,omitempty"`
}
g.DeSerializeJSON(response, &coverageStoreResponse)
coverageStore = coverageStoreResponse.CoverageStore
return
}
// CreateCoverageStore create coverage store in geoserver and return created one else return error
func (g *GeoServer) CreateCoverageStore(workspaceName string, coverageStore CoverageStore) (created bool, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores")
data := CoverageStoreRequestBody{
CoverageStore: &coverageStore,
}
serializedData, _ := g.SerializeStruct(data)
httpRequest := HTTPRequest{
Method: postMethod,
Data: bytes.NewBuffer(serializedData),
DataType: jsonType + "; charset=utf-8",
Accept: jsonType,
URL: targetURL,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusCreated {
g.logger.Error(string(response))
created = false
err = g.GetError(responseCode, response)
return
}
created = true
return
}
// UpdateCoverageStore parital update coverage store in geoserver else return error
func (g *GeoServer) UpdateCoverageStore(workspaceName string, coverageStore CoverageStore) (modified bool, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores", coverageStore.Name)
data := CoverageStoreRequestBody{CoverageStore: &coverageStore}
serializedData, _ := g.SerializeStruct(data)
httpRequest := HTTPRequest{
Method: putMethod,
Data: bytes.NewBuffer(serializedData),
DataType: jsonType,
Accept: jsonType,
URL: targetURL,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
modified = false
err = g.GetError(responseCode, response)
return
}
modified = true
return
}
// DeleteCoverageStore delete coverage store from geoserver else return error
func (g *GeoServer) DeleteCoverageStore(workspaceName string, coverageStore string, recurse bool) (deleted bool, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores", coverageStore)
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.Error(string(response))
deleted = false
err = g.GetError(responseCode, response)
return
}
deleted = true
return
}