forked from f5devcentral/go-bigip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathilx.go
216 lines (196 loc) · 5.57 KB
/
ilx.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
212
213
214
215
216
package bigip
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/davecgh/go-spew/spew"
)
type ILXWorkspace struct {
Name string `json:"name,omitempty"`
FullPath string `json:"fullPath,omitempty"`
Generation int `json:"generation,omitempty"`
SelfLink string `json:"selfLink,omitempty"`
NodeVersion string `json:"nodeVersion,omitempty"`
StagedDirectory string `json:"stagedDirectory,omitempty"`
Version string `json:"version,omitempty"`
Extensions []Extension `json:"extensions,omitempty"`
Rules []File `json:"rules,omitempty"`
}
type File struct {
Name string `json:"name,omitempty"`
}
type Extension struct {
Name string `json:"name,omitempty"`
Files []File `json:"files,omitempty"`
}
func (b *BigIP) GetWorkspace(ctx context.Context, path string) (*ILXWorkspace, error) {
spc := &ILXWorkspace{}
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
err, exists := b.getForEntity(spc, uriMgmt, uriTm, uriIlx, uriWorkspace, path)
if !exists {
return nil, fmt.Errorf("workspace does not exist: %w", err)
}
if err != nil {
return nil, fmt.Errorf("error getting ILX Workspace: %w", err)
}
}
return spc, nil
}
func (b *BigIP) CreateWorkspace(ctx context.Context, path string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
err := b.post(ILXWorkspace{Name: path}, uriMgmt, uriTm, uriIlx, uriWorkspace, "")
if err != nil {
return fmt.Errorf("error creating ILX Workspace: %w", err)
}
}
return nil
}
func (b *BigIP) DeleteWorkspace(ctx context.Context, name string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
err := b.delete(uriMgmt, uriTm, uriIlx, uriWorkspace, name)
if err != nil {
return fmt.Errorf("error deleting ILX Workspace: %w", err)
}
}
return nil
}
func (b *BigIP) PatchWorkspace(ctx context.Context, name string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
err := b.patch(ILXWorkspace{Name: name}, uriMgmt, uriTm, uriIlx, uriWorkspace, name)
if err != nil {
return fmt.Errorf("error patching ILX Workspace: %w", err)
}
}
return nil
}
type ExtensionConfig struct {
Name string `json:"name,omitempty"`
Partition string `json:"partition,omitempty"`
WorkspaceName string `json:"workspaceName,omitempty"`
}
func (b *BigIP) CreateExtension(ctx context.Context, opts ExtensionConfig) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
err := b.post(ILXWorkspace{Name: opts.WorkspaceName}, uriMgmt, uriTm, uriIlx, uriWorkspace+"?options=extension,"+opts.Name)
if err != nil {
return fmt.Errorf("error creating ILX Extension: %w", err)
}
}
return nil
}
// UploadExtensionFiles uploads the files in the given directory to the BIG-IP system
// Only index.js and package.json files are uploaded as they are the only mutable files.
func (b *BigIP) UploadExtensionFiles(ctx context.Context, opts ExtensionConfig, path string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
destination := fmt.Sprintf("%s/%s/%s/extensions/%s/", WORKSPACE_UPLOAD_PATH, opts.Partition, opts.WorkspaceName, opts.Name)
files, err := readFilesFromDirectory(path)
if err != nil {
return err
}
err = b.uploadFilesToDestination(files, destination)
if err != nil {
return err
}
}
return nil
}
func (b *BigIP) UploadRuleFiles(ctx context.Context, opts ExtensionConfig, path string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
destination := fmt.Sprintf("%s/%s/%s/rules/", WORKSPACE_UPLOAD_PATH, opts.Partition, opts.WorkspaceName)
files, err := readFilesFromDirectory(path)
if err != nil {
return err
}
if err = b.uploadFilesToDestination(files, destination); err != nil {
return err
}
}
return nil
}
func (b *BigIP) uploadFilesToDestination(files []*os.File, destination string) error {
uploadedFilePaths, err := b.uploadFiles(files)
if err != nil {
return err
}
for _, uploadedFilePath := range uploadedFilePaths {
err := b.runCatCommand(uploadedFilePath, destination)
if err != nil {
return err
}
}
return nil
}
func readFilesFromDirectory(path string) ([]*os.File, error) {
fileDirs, err := os.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("error reading directory: %w", err)
}
files := []*os.File{}
for _, fileDir := range fileDirs {
if fileDir.IsDir() {
continue
}
f, err := fileFromDirEntry(fileDir, path)
if err != nil {
return nil, fmt.Errorf("error getting file from directory entry: %w", err)
}
files = append(files, f)
}
return files, nil
}
func (b *BigIP) uploadFiles(files []*os.File) ([]string, error) {
uploadedFilePaths := []string{}
for _, file := range files {
if file.Name() == "index.js" || file.Name() == "package.json" {
res, err := b.UploadFile(file)
if err != nil {
return nil, fmt.Errorf("error uploading file: %w", err)
}
uploadedFilePaths = append(uploadedFilePaths, res.LocalFilePath)
}
}
return uploadedFilePaths, nil
}
func (b *BigIP) runCatCommand(uploadedFilePath, destination string) error {
fileName := filepath.Base(uploadedFilePath)
command := BigipCommand{
Command: "run",
UtilCmdArgs: fmt.Sprintf("-c 'cat %s > %s'", uploadedFilePath, destination+fileName),
}
output, err := b.RunCommand(&command)
if err != nil {
return fmt.Errorf("error running command: %w", err)
}
spew.Dump(output)
return nil
}
func fileFromDirEntry(entry fs.DirEntry, dir string) (*os.File, error) {
path := filepath.Join(dir, entry.Name())
file, err := os.Open(path)
if err != nil {
return nil, err
}
return file, nil
}