-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource.go
111 lines (92 loc) · 2.92 KB
/
resource.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
package clients
import (
"context"
"net/url"
"fmt"
"time"
"git.containerum.net/ch/permissions/pkg/errors"
"github.com/containerum/cherry"
"github.com/containerum/cherry/adaptors/cherrylog"
"github.com/containerum/utils/httputil"
"github.com/json-iterator/go"
"github.com/sirupsen/logrus"
"gopkg.in/resty.v1"
)
type ResourceServiceClient interface {
DeleteNamespaceResources(ctx context.Context, namespaceID string) error
DeleteAllUserNamespaces(ctx context.Context) error
}
type ResourceServiceHTTPClient struct {
log *cherrylog.LogrusAdapter
client *resty.Client
}
func NewResourceServiceHTTPClient(url *url.URL) *ResourceServiceHTTPClient {
log := logrus.WithField("component", "resource_service_client")
client := resty.New().
SetLogger(log.WriterLevel(logrus.DebugLevel)).
SetHostURL(url.String()).
SetDebug(true).
SetError(cherry.Err{}).
SetTimeout(10*time.Second).
SetHeader("Content-Type", "application/json").
SetHeader("Accept", "application/json")
client.JSONMarshal = jsoniter.Marshal
client.JSONUnmarshal = jsoniter.Unmarshal
return &ResourceServiceHTTPClient{
log: cherrylog.NewLogrusAdapter(log),
client: client,
}
}
func (r *ResourceServiceHTTPClient) DeleteNamespaceResources(ctx context.Context, namespaceID string) error {
r.log.WithField("namespace_id", namespaceID).Debugf("delete namespace resources")
resp, err := r.client.R().
SetHeaders(httputil.RequestXHeadersMap(ctx)).
SetPathParams(map[string]string{
"namespace": namespaceID,
}).
Delete("/namespaces/{namespace}")
if err != nil {
return errors.ErrInternal().Log(err, r.log)
}
if resp.Error() != nil {
return resp.Error().(*cherry.Err)
}
return nil
}
func (r *ResourceServiceHTTPClient) DeleteAllUserNamespaces(ctx context.Context) error {
r.log.Debugf("delete all user namespaces")
resp, err := r.client.R().
SetHeaders(httputil.RequestXHeadersMap(ctx)).
Delete("/namespaces")
if err != nil {
fmt.Println("ERROR HERE?", err)
return errors.ErrInternal().Log(err, r.log)
}
if resp.Error() != nil {
fmt.Println("ERROR HERE??", resp.Error())
return resp.Error().(*cherry.Err)
}
return nil
}
func (r *ResourceServiceHTTPClient) String() string {
return fmt.Sprintf("resource-service http client: url=%s", r.client.HostURL)
}
type ResourceServiceDummyClient struct {
log *logrus.Entry
}
func NewResourceServiceDummyClient() *ResourceServiceDummyClient {
return &ResourceServiceDummyClient{
log: logrus.WithField("component", "resource_service_stub"),
}
}
func (r *ResourceServiceDummyClient) DeleteNamespaceResources(ctx context.Context, namespaceID string) error {
r.log.WithField("namespace_id", namespaceID).Debugf("delete namespace resources")
return nil
}
func (r *ResourceServiceDummyClient) DeleteAllUserNamespaces(ctx context.Context) error {
r.log.Debugf("delete all user namespaces")
return nil
}
func (r *ResourceServiceDummyClient) String() string {
return "resource-service dummy client"
}