This repository has been archived by the owner on May 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
applicationregistryclient.go
121 lines (97 loc) · 3.59 KB
/
applicationregistryclient.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
package registration
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/kyma-incubator/hack-showcase/github-connector/internal/apperrors"
log "github.com/sirupsen/logrus"
)
//PayloadBuilder is an interface containing all necessary functions required to build an ServiceDetails structure
type PayloadBuilder interface {
Build() (ServiceDetails, error)
GetApplicationRegistryURL() string
}
//ApplicationRegistryClient is an interface containing all necessary functions required to register service in Kyma's application registry
type ApplicationRegistryClient interface {
RegisterService() (string, apperrors.AppError)
}
type applicationRegistryClient struct {
builder PayloadBuilder
register ApplicationRegistryClient
retryDelay int
retriesCount int
}
//NewApplicationRegistryClient creates a applicationRegistryClient instance with the passed in interface
func NewApplicationRegistryClient(b PayloadBuilder, retryTime int, retries int) applicationRegistryClient {
return applicationRegistryClient{
builder: b,
retryDelay: retryTime * int(time.Second),
retriesCount: retries,
}
}
//RegisterService - register service in Kyma and get a response
func (r applicationRegistryClient) RegisterService() (string, apperrors.AppError) {
jsonBody, err := r.builder.Build()
if err != nil {
return "", apperrors.Internal("While building service details json: %s", err)
}
id, err := jsonBody.requestWithRetries(r.builder.GetApplicationRegistryURL(), r.retryDelay, r.retriesCount)
if err != nil {
return "", apperrors.Internal("While trying to register service: %s", err.Error())
}
return id, nil
}
func (jsonBody *ServiceDetails) requestWithRetries(url string, retryDelay int, retriesCount int) (string, error) {
var id string
var err error
for i := 0; i < retriesCount; i++ {
time.Sleep(time.Duration(retryDelay))
id, err = sendRequest(*jsonBody, url)
if err == nil {
break
}
log.Warn(err.Error())
}
if err != nil {
return "", apperrors.UpstreamServerCallFailed("While sending service registration request: %s", err)
}
return id, nil
}
//RegisterResponse contain structure of response json
type RegisterResponse struct {
ID string
}
//Do - create request and send it to kyma application registry
func sendRequest(JSONBody ServiceDetails, url string) (string, error) {
// parse json to io.Reader
requestByte, err := json.Marshal(JSONBody)
if err != nil {
return "", apperrors.Internal("Failed to parse application registry request JSON body: %s", err.Error())
}
requestReader := bytes.NewReader(requestByte)
httpRequest, err := http.NewRequest(http.MethodPost, url, requestReader)
if err != nil {
return "", apperrors.Internal("Failed to create JSON request: %s", err.Error())
}
httpRequest.Header.Set("Content-Type", "application/json")
client := &http.Client{}
httpResponse, err := client.Do(httpRequest)
if err != nil {
return "", apperrors.UpstreamServerCallFailed("Failed to make request to '%s': %s", url, err.Error())
}
if httpResponse.StatusCode != http.StatusOK {
return "", apperrors.UpstreamServerCallFailed("Incorrect response code '%d' while sending JSON request from %s", httpResponse.StatusCode, url)
}
bodyBytes, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return "", apperrors.Internal("Failed to read service ID from application registry JSON response: %s", err)
}
var jsonResponse RegisterResponse
err = json.Unmarshal(bodyBytes, &jsonResponse)
if err != nil {
return "", apperrors.Internal("Failed while unmarshalling JSON response from application registry: %s", err)
}
return jsonResponse.ID, nil
}