forked from thecodeteam/goisilon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_v2_exports.go
180 lines (146 loc) · 3.25 KB
/
api_v2_exports.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
package v2
import (
"errors"
"strconv"
"context"
"github.com/thecodeteam/goisilon/api"
"github.com/thecodeteam/goisilon/api/json"
)
// Export is an Isilon Export.
type Export struct {
ID int `json:"id,omitmarshal"`
Paths *[]string `json:"paths,omitempty"`
Clients *[]string `json:"clients,omitempty"`
RootClients *[]string `json:"root_clients,omitempty"`
MapAll *UserMapping `json:"map_all,omitempty"`
MapRoot *UserMapping `json:"map_root,omitempty"`
MapNonRoot *UserMapping `json:"map_non_root,omitempty"`
MapFailure *UserMapping `json:"map_failure,omitempty"`
}
// ExportList is a list of Isilon Exports.
type ExportList []*Export
// MarshalJSON marshals an ExportList to JSON.
func (l ExportList) MarshalJSON() ([]byte, error) {
exports := struct {
Exports []*Export `json:"exports,omitempty"`
}{l}
return json.Marshal(exports)
}
// UnmarshalJSON unmarshals an ExportList from JSON.
func (l *ExportList) UnmarshalJSON(text []byte) error {
exports := struct {
Exports []*Export `json:"exports,omitempty"`
}{}
if err := json.Unmarshal(text, &exports); err != nil {
return err
}
*l = exports.Exports
return nil
}
// ExportList GETs all exports.
func ExportsList(
ctx context.Context,
client api.Client) ([]*Export, error) {
var resp ExportList
if err := client.Get(
ctx,
exportsPath,
"",
nil,
nil,
&resp); err != nil {
return nil, err
}
return resp, nil
}
// ExportInspect GETs an export.
func ExportInspect(
ctx context.Context,
client api.Client,
id int) (*Export, error) {
var resp ExportList
if err := client.Get(
ctx,
exportsPath,
strconv.Itoa(id),
nil,
nil,
&resp); err != nil {
return nil, err
}
if len(resp) == 0 {
return nil, nil
}
return resp[0], nil
}
// ExportCreate POSTs an Export object to the Isilon server.
func ExportCreate(
ctx context.Context,
client api.Client,
export *Export) (int, error) {
if export.Paths != nil && len(*export.Paths) == 0 {
return 0, errors.New("no path set")
}
var resp Export
if err := client.Post(
ctx,
exportsPath,
"",
nil,
nil,
export,
&resp); err != nil {
return 0, err
}
return resp.ID, nil
}
// ExportUpdate PUTs an Export object to the Isilon server.
func ExportUpdate(
ctx context.Context,
client api.Client,
export *Export) error {
return client.Put(
ctx,
exportsPath,
strconv.Itoa(export.ID),
nil,
nil,
export,
nil)
}
// ExportDelete DELETEs an Export object on the Isilon server.
func ExportDelete(
ctx context.Context,
client api.Client,
id int) error {
return client.Delete(
ctx,
exportsPath,
strconv.Itoa(id),
nil,
nil,
nil)
}
// SetExportClients sets an Export's clients property.
func SetExportClients(
ctx context.Context,
client api.Client,
id int,
addrs ...string) error {
return ExportUpdate(ctx, client, &Export{ID: id, Clients: &addrs})
}
// SetExportRootClients sets an Export's root_clients property.
func SetExportRootClients(
ctx context.Context,
client api.Client,
id int,
addrs ...string) error {
return ExportUpdate(ctx, client, &Export{ID: id, RootClients: &addrs})
}
// Unexport is an alias for ExportDelete.
func Unexport(
ctx context.Context,
client api.Client,
id int) error {
return ExportDelete(ctx, client, id)
}