-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathstorage_client.go
More file actions
127 lines (105 loc) · 3.35 KB
/
storage_client.go
File metadata and controls
127 lines (105 loc) · 3.35 KB
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
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package common
import (
"errors"
"fmt"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/parnurzeal/gorequest"
)
// StorageOptions storage options
type StorageOptions struct {
Host string
Token string
Debug bool
}
// StorageClient storage client
type StorageClient struct {
host string
token string
serverDebug bool
}
// StorageCli global storage client
var StorageCli *StorageClient
// SetStorageClient set storage client
func SetStorageClient(options StorageOptions) error {
cli, err := NewStorageClient(options)
if err != nil {
return err
}
StorageCli = cli
return nil
}
// NewStorageClient create bcs storage client
func NewStorageClient(options StorageOptions) (*StorageClient, error) {
c := &StorageClient{
host: options.Host,
token: options.Token,
serverDebug: options.Debug,
}
return c, nil
}
// SyncClusterData sync cluster data to storage
func (s *StorageClient) SyncClusterData(clusterID string, data map[string]interface{}) error {
if s == nil {
return ErrServerNotInit
}
reqUrl := fmt.Sprintf("%s/bcsapi/v4/storage/clusters/%s", s.host, clusterID)
respData := &StorageResponse{}
_, _, errs := gorequest.New().
Timeout(defaultTimeOut).
Put(reqUrl).
Set("Content-Type", "application/json").
Set("Accept", "application/json").
Set("Authorization", fmt.Sprintf("Bearer %s", s.token)).
SetDebug(s.serverDebug).
Send(SyncClusterDataRequest{Data: data}).
EndStruct(&respData)
if len(errs) > 0 {
blog.Errorf("call api SyncClusterData failed: %v", errs[0])
return errs[0]
}
if !respData.Result {
blog.Errorf("call api SyncClusterData failed: %v", respData.Message)
return errors.New(respData.Message)
}
// successfully request
blog.Infof("call api SyncClusterData with url(%s) successfully", reqUrl)
return nil
}
// DelClusterData del storage cluster data
func (s *StorageClient) DelClusterData(clusterID string) error {
if s == nil {
return ErrServerNotInit
}
reqUrl := fmt.Sprintf("%s/bcsapi/v4/storage/clusters/%s", s.host, clusterID)
respData := &StorageResponse{}
_, _, errs := gorequest.New().
Timeout(defaultTimeOut).
Delete(reqUrl).
Set("Content-Type", "application/json").
Set("Accept", "application/json").
Set("Authorization", fmt.Sprintf("Bearer %s", s.token)).
SetDebug(s.serverDebug).
EndStruct(&respData)
if len(errs) > 0 {
blog.Errorf("call api DelClusterData failed: %v", errs[0])
return errs[0]
}
if !respData.Result {
blog.Errorf("call api DelClusterData failed: %v", respData.Message)
return errors.New(respData.Message)
}
// successfully request
blog.Infof("call api DelClusterData with url(%s) successfully", reqUrl)
return nil
}