-
Notifications
You must be signed in to change notification settings - Fork 24
/
workspaces.go
147 lines (133 loc) · 4.33 KB
/
workspaces.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
package geoserver
import (
"bytes"
"strconv"
)
// WorkspaceService define all geoserver workspace operations
type WorkspaceService interface {
// WorkspaceExists check if workspace in geoserver or not else return error
WorkspaceExists(workspaceName string) (exists bool, err error)
// GetWorkspaces get geoserver workspaces else return error
GetWorkspaces() (workspaces []*Resource, err error)
// GetWorkspace get geoserver workspaces else return error
GetWorkspace(workspaceName string) (workspace Workspace, err error)
// CreateWorkspace creates a workspace else return error
CreateWorkspace(workspaceName string) (created bool, err error)
//DeleteWorkspace delete geoserver workspace and its reources else return error
DeleteWorkspace(workspaceName string, recurse bool) (deleted bool, err error)
}
//Workspace is the Workspace Object
type Workspace struct {
Name string `json:"name,omitempty"`
Isolated bool `json:"isolated,omitempty"`
DataStores string `json:"dataStores,omitempty"`
CoverageStores string `json:"coverageStores,omitempty"`
WmsStores string `json:"wmsStores,omitempty"`
WmtsStores string `json:"wmtsStores,omitempty"`
}
//WorkspaceRequestBody is the api body
type WorkspaceRequestBody struct {
Workspace *Workspace `json:"workspace,omitempty"`
}
// CreateWorkspace creates a workspace and return if created or not else return error
func (g *GeoServer) CreateWorkspace(workspaceName string) (created bool, err error) {
//TODO: check if workspace exist before creating it
var workspace = Workspace{Name: workspaceName}
serializedWorkspace, _ := g.SerializeStruct(WorkspaceRequestBody{Workspace: &workspace})
targetURL := g.ParseURL("rest", "workspaces")
data := bytes.NewBuffer(serializedWorkspace)
httpRequest := HTTPRequest{
Method: postMethod,
Accept: jsonType,
Data: data,
DataType: jsonType + "; charset=utf-8",
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
}
// WorkspaceExists check if workspace in geoserver or not else return error
func (g *GeoServer) WorkspaceExists(workspaceName string) (exists bool, err error) {
_, workspaceErr := g.GetWorkspace(workspaceName)
if workspaceErr != nil {
exists = false
err = workspaceErr
return
}
exists = true
return
}
//DeleteWorkspace delete geoserver workspace and its reources else return error
func (g *GeoServer) DeleteWorkspace(workspaceName string, recurse bool) (deleted bool, err error) {
url := g.ParseURL("rest", "workspaces", workspaceName)
httpRequest := HTTPRequest{
Method: deleteMethod,
Accept: jsonType,
URL: url,
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
}
// GetWorkspaces get geoserver workspaces else return error
func (g *GeoServer) GetWorkspaces() (workspaces []*Resource, err error) {
url := g.ParseURL("rest", "workspaces")
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: url,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Warn(string(response))
workspaces = nil
err = g.GetError(responseCode, response)
return
}
var workspaceResponse struct {
Workspaces struct {
Workspace []*Resource
}
}
g.DeSerializeJSON(response, &workspaceResponse)
workspaces = workspaceResponse.Workspaces.Workspace
return
}
// GetWorkspace get geoserver workspace else return error
func (g *GeoServer) GetWorkspace(workspaceName string) (workspace Workspace, err error) {
url := g.ParseURL("rest", "workspaces", workspaceName)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: url,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
err = g.GetError(responseCode, response)
return
}
workspaceResponse := WorkspaceRequestBody{
Workspace: &Workspace{},
}
g.DeSerializeJSON(response, &workspaceResponse)
workspace = *workspaceResponse.Workspace
return
}