Total Lines | 128 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package solr |
||
2 | |||
3 | import ( |
||
4 | "bytes" |
||
5 | "context" |
||
6 | "fmt" |
||
7 | "io" |
||
8 | "log" |
||
9 | "mime/multipart" |
||
10 | "net/http" |
||
11 | "os" |
||
12 | "path/filepath" |
||
13 | ) |
||
14 | |||
15 | const ( |
||
16 | ActionUpload = "UPLOAD" |
||
1 ignored issue
–
show
|
|||
17 | ) |
||
18 | |||
19 | type Config struct { |
||
1 ignored issue
–
show
|
|||
20 | Name string `url:"name,omitempty"` |
||
21 | } |
||
22 | |||
23 | type ConfigParameter struct { |
||
1 ignored issue
–
show
|
|||
24 | Action string `url:"action,omitempty"` |
||
25 | Name string `url:"name,omitempty"` |
||
26 | OmitHeader bool `url:"omitHeader,omitempty"` |
||
27 | } |
||
28 | |||
29 | type ConfigAPI struct { |
||
1 ignored issue
–
show
|
|||
30 | client *Client |
||
31 | } |
||
32 | |||
33 | type CreateConfig struct { |
||
1 ignored issue
–
show
|
|||
34 | Name string `json:"name,omitempty"` |
||
35 | BaseConfigSet string `json:"baseConfigSet,omitempty"` |
||
36 | ConfigSetPropImmutable bool `json:"configSetProp.immutable,omitempty"` |
||
37 | } |
||
38 | |||
39 | func (c *ConfigAPI) List(ctx context.Context) (*Response, error) { |
||
1 ignored issue
–
show
|
|||
40 | req, err := c.client.NewRequest(ctx, http.MethodGet, "/api/cluster/configs", nil, &ConfigParameter{ |
||
41 | OmitHeader: true, |
||
42 | }, nil) |
||
43 | if err != nil { |
||
44 | return nil, err |
||
45 | } |
||
46 | |||
47 | response, err := c.client.Do(ctx, req) |
||
48 | if err != nil { |
||
49 | return nil, err |
||
50 | } |
||
51 | |||
52 | return response, err |
||
53 | } |
||
54 | |||
55 | func (c *ConfigAPI) Upload(ctx context.Context, filename string, name string) (*Response, error) { |
||
1 ignored issue
–
show
|
|||
56 | file, err := os.Open(filename) |
||
57 | if err != nil { |
||
58 | log.Fatal(err) |
||
59 | } |
||
60 | defer file.Close() |
||
61 | |||
62 | body := &bytes.Buffer{} |
||
63 | writer := multipart.NewWriter(body) |
||
64 | part, err := writer.CreateFormFile("application/octet-stream", filepath.Base(file.Name())) |
||
65 | if err != nil { |
||
66 | log.Fatal(err) |
||
67 | } |
||
68 | |||
69 | _, err = io.Copy(part, file) |
||
70 | if err != nil { |
||
71 | return nil, err |
||
72 | } |
||
73 | |||
74 | err = writer.Close() |
||
75 | if err != nil { |
||
76 | return nil, err |
||
77 | } |
||
78 | |||
79 | req, err := c.client.NewRequest(ctx, http.MethodPost, "/solr/admin/configs", body, &ConfigParameter{ |
||
80 | Action: ActionUpload, |
||
81 | Name: name, |
||
82 | }, &map[string]string{ |
||
83 | "Content-Type": "application/octet-stream", |
||
84 | }) |
||
85 | if err != nil { |
||
86 | return nil, err |
||
87 | } |
||
88 | |||
89 | response, err := c.client.Do(ctx, req) |
||
90 | if err != nil { |
||
91 | return nil, err |
||
92 | } |
||
93 | |||
94 | return response, err |
||
95 | } |
||
96 | |||
97 | func (c *ConfigAPI) Create(ctx context.Context, config CreateConfig) (*Response, error) { |
||
1 ignored issue
–
show
|
|||
98 | req, err := c.client.NewRequest(ctx, http.MethodPost, "/api/cluster/configs", config, &ConfigParameter{ |
||
99 | OmitHeader: true, |
||
100 | }, nil) |
||
101 | if err != nil { |
||
102 | return nil, err |
||
103 | } |
||
104 | |||
105 | response, err := c.client.Do(ctx, req) |
||
106 | if err != nil { |
||
107 | return nil, err |
||
108 | } |
||
109 | |||
110 | return response, err |
||
111 | } |
||
112 | |||
113 | func (c *ConfigAPI) Delete(ctx context.Context, name string) (*Response, error) { |
||
1 ignored issue
–
show
|
|||
114 | path := fmt.Sprintf("/api/cluster/configs/%s", name) |
||
115 | |||
116 | req, err := c.client.NewRequest(ctx, http.MethodDelete, path, nil, &ConfigParameter{ |
||
117 | OmitHeader: true, |
||
118 | }, nil) |
||
119 | if err != nil { |
||
120 | return nil, err |
||
121 | } |
||
122 | |||
123 | response, err := c.client.Do(ctx, req) |
||
124 | if err != nil { |
||
125 | return nil, err |
||
126 | } |
||
127 | |||
128 | return response, err |
||
129 | } |