Skip to content

Commit cd33436

Browse files
author
Pritam Das
authored
Add resty client (#185)
Signed-off-by: pritamdas99 <pritam@appscode.com>
1 parent 332d74b commit cd33436

File tree

3 files changed

+132
-2
lines changed

3 files changed

+132
-2
lines changed

hazelcast/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package hazelcast
2+
3+
type HZClient interface {
4+
ChangeClusterState(state string) (string, error)
5+
GetClusterState() (string, error)
6+
}

hazelcast/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ limitations under the License.
1717
package hazelcast
1818

1919
import (
20+
"github.com/go-resty/resty/v2"
2021
hazelcast "github.com/hazelcast/hazelcast-go-client"
2122
)
2223

2324
type Client struct {
2425
*hazelcast.Client
2526
}
27+
28+
type HZRestyClient struct {
29+
Client *resty.Client
30+
Config *RestyConfig
31+
password string
32+
}

hazelcast/kubedb_client_builder.go

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ import (
2020
"context"
2121
"crypto/tls"
2222
"crypto/x509"
23+
"encoding/json"
2324
"fmt"
25+
"net"
26+
"net/http"
27+
"time"
28+
29+
"github.com/go-resty/resty/v2"
2430

2531
"github.com/go-logr/logr"
2632
hazelcast "github.com/hazelcast/hazelcast-go-client"
@@ -130,8 +136,9 @@ func (o *KubeDBClientBuilder) GetTLSConfig() (*tls.Config, error) {
130136
}
131137

132138
func (o *KubeDBClientBuilder) GetHazelcastClient() (*Client, error) {
133-
if o.podName == "" {
134-
o.url = o.ServiceURL()
139+
if o.podName != "" {
140+
o.url = fmt.Sprintf("%s.%s.%s.svc.cluster.local:%d", o.podName, o.db.GoverningServiceName(), o.db.GetNamespace(), kubedb.HazelcastRestPort)
141+
135142
}
136143

137144
if o.url == "" {
@@ -183,6 +190,116 @@ func (o *KubeDBClientBuilder) GetHazelcastClient() (*Client, error) {
183190
}, nil
184191
}
185192

193+
type RestyConfig struct {
194+
host string
195+
transport *http.Transport
196+
}
197+
198+
func (o *KubeDBClientBuilder) GetHazelcastRestyClient() (*HZRestyClient, error) {
199+
if o.url == "" {
200+
o.url = fmt.Sprintf("%s://%s.%s.svc:%d", o.db.GetConnectionScheme(), o.db.ServiceName(), o.db.GetNamespace(), kubedb.HazelcastRestPort)
201+
}
202+
203+
config := RestyConfig{
204+
host: o.url,
205+
transport: &http.Transport{
206+
IdleConnTimeout: time.Second * 3,
207+
DialContext: (&net.Dialer{
208+
Timeout: time.Second * 30,
209+
}).DialContext,
210+
},
211+
}
212+
213+
var username, password string
214+
if !o.db.Spec.DisableSecurity {
215+
user, pass, err := o.GetAuthCredentials()
216+
if err != nil {
217+
return nil, err
218+
}
219+
username = user
220+
password = pass
221+
}
222+
223+
defaultTlsConfig, err := o.GetTLSConfig()
224+
if err != nil {
225+
klog.Errorf("Failed to get default tls config: %v", err)
226+
}
227+
config.transport.TLSClientConfig = defaultTlsConfig
228+
newClient := resty.New()
229+
newClient.SetTransport(config.transport).SetScheme(o.db.GetConnectionScheme()).SetBaseURL(config.host)
230+
newClient.SetHeader("Accept", "application/json")
231+
newClient.SetBasicAuth(username, password)
232+
newClient.SetTimeout(time.Second * 30)
233+
234+
return &HZRestyClient{
235+
Client: newClient,
236+
Config: &config,
237+
password: password,
238+
}, nil
239+
}
240+
241+
func (client *HZRestyClient) ChangeClusterState(state string) (string, error) {
242+
req := client.Client.R().SetDoNotParseResponse(true)
243+
param := fmt.Sprintf("admin&%s&%s", client.password, state)
244+
req.SetHeader("Content-Type", "application/json")
245+
req.SetBody(param)
246+
res, err := req.Post("/hazelcast/rest/management/cluster/changeState")
247+
if err != nil {
248+
klog.Error(err, "Failed to send http request")
249+
return "", err
250+
}
251+
if res != nil {
252+
if res.IsError() {
253+
klog.Error(res.Error())
254+
return "", errors.New(fmt.Sprintf("HTTP request failed: %v, StatusCode: %v", res.Error(), res.StatusCode()))
255+
}
256+
} else {
257+
return "", errors.New("response can not be nil")
258+
}
259+
body := res.RawBody()
260+
responseBody := make(map[string]interface{})
261+
if err := json.NewDecoder(body).Decode(&responseBody); err != nil {
262+
return "", fmt.Errorf("failed to deserialize the response: %v", err)
263+
}
264+
if val, ok := responseBody["status"]; ok {
265+
if strValue, ok := val.(string); ok {
266+
return strValue, nil
267+
}
268+
return "", errors.New("failed to convert response to string")
269+
}
270+
return "", errors.New("status is missing")
271+
}
272+
273+
func (client *HZRestyClient) GetClusterState() (string, error) {
274+
req := client.Client.R().SetDoNotParseResponse(true)
275+
276+
res, err := req.Get("/hazelcast/health")
277+
if err != nil {
278+
klog.Error(err, "Failed to send http request")
279+
return "", err
280+
}
281+
if res != nil {
282+
if res.IsError() {
283+
klog.Error(res.Error())
284+
return "", errors.New(fmt.Sprintf("HTTP request failed: %v, StatusCode: %v", res.Error(), res.StatusCode()))
285+
}
286+
} else {
287+
return "", errors.New("response can not be nil")
288+
}
289+
body := res.RawBody()
290+
responseBody := make(map[string]interface{})
291+
if err := json.NewDecoder(body).Decode(&responseBody); err != nil {
292+
return "", fmt.Errorf("failed to deserialize the response: %v", err)
293+
}
294+
if val, ok := responseBody["clusterState"]; ok {
295+
if strValue, ok := val.(string); ok {
296+
return strValue, nil
297+
}
298+
return "", errors.New("failed to convert response to string")
299+
}
300+
return "", errors.New("status is missing")
301+
}
302+
186303
func (o *KubeDBClientBuilder) ServiceURL() string {
187304
return fmt.Sprintf("%s.%s.svc:%d", o.db.ServiceName(), o.db.GetNamespace(), kubedb.HazelcastRestPort)
188305
}

0 commit comments

Comments
 (0)