-
Notifications
You must be signed in to change notification settings - Fork 582
/
integrationArtifactDeploy.go
236 lines (204 loc) · 9.72 KB
/
integrationArtifactDeploy.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package cmd
import (
"fmt"
"io"
"net/http"
"time"
"github.com/Jeffail/gabs/v2"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/cpi"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)
const retryCount = 14
type integrationArtifactDeployUtils interface {
command.ExecRunner
// Add more methods here, or embed additional interfaces, or remove/replace as required.
// The integrationArtifactDeployUtils interface should be descriptive of your runtime dependencies,
// i.e. include everything you need to be able to mock in tests.
// Unit tests shall be executable in parallel (not depend on global state), and don't (re-)test dependencies.
}
type integrationArtifactDeployUtilsBundle struct {
*command.Command
// Embed more structs as necessary to implement methods or interfaces you add to integrationArtifactDeployUtils.
// Structs embedded in this way must each have a unique set of methods attached.
// If there is no struct which implements the method you need, attach the method to
// integrationArtifactDeployUtilsBundle and forward to the implementation of the dependency.
}
func newIntegrationArtifactDeployUtils() integrationArtifactDeployUtils {
utils := integrationArtifactDeployUtilsBundle{
Command: &command.Command{},
}
// Reroute command output to logging framework
utils.Stdout(log.Writer())
utils.Stderr(log.Writer())
return &utils
}
func integrationArtifactDeploy(config integrationArtifactDeployOptions, telemetryData *telemetry.CustomData) {
// Utils can be used wherever the command.ExecRunner interface is expected.
// It can also be used for example as a mavenExecRunner.
utils := newIntegrationArtifactDeployUtils()
utils.Stdout(log.Writer())
httpClient := &piperhttp.Client{}
// For HTTP calls import piperhttp "github.com/SAP/jenkins-library/pkg/http"
// and use a &piperhttp.Client{} in a custom system
// Example: step checkmarxExecuteScan.go
// Error situations should be bubbled up until they reach the line below which will then stop execution
// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
err := runIntegrationArtifactDeploy(&config, telemetryData, httpClient)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runIntegrationArtifactDeploy(config *integrationArtifactDeployOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
clientOptions := piperhttp.ClientOptions{}
header := make(http.Header)
header.Add("Accept", "application/json")
serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey)
if err != nil {
return err
}
deployURL := fmt.Sprintf("%s/api/v1/DeployIntegrationDesigntimeArtifact?Id='%s'&Version='%s'", serviceKey.OAuth.Host, config.IntegrationFlowID, "Active")
tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, Client: httpClient}
token, err := cpi.CommonUtils.GetBearerToken(tokenParameters)
if err != nil {
return errors.Wrap(err, "failed to fetch Bearer Token")
}
clientOptions.Token = fmt.Sprintf("Bearer %s", token)
httpClient.SetOptions(clientOptions)
httpMethod := "POST"
deployResp, httpErr := httpClient.SendRequest(httpMethod, deployURL, nil, header, nil)
if httpErr != nil {
return errors.Wrapf(httpErr, "HTTP %v request to %v failed with error", httpMethod, deployURL)
}
if deployResp != nil && deployResp.Body != nil {
defer deployResp.Body.Close()
}
if deployResp == nil {
return errors.Errorf("did not retrieve a HTTP response")
}
if deployResp.StatusCode == http.StatusAccepted {
log.Entry().
WithField("IntegrationFlowID", config.IntegrationFlowID).
Info("successfully deployed into CPI runtime")
taskId, readErr := io.ReadAll(deployResp.Body)
if readErr != nil {
return errors.Wrap(readErr, "Task Id not found. HTTP response body could not be read.")
}
deploymentError := pollIFlowDeploymentStatus(string(taskId), retryCount, config, httpClient, serviceKey.OAuth.Host)
return deploymentError
}
responseBody, readErr := io.ReadAll(deployResp.Body)
if readErr != nil {
return errors.Wrapf(readErr, "HTTP response body could not be read, response status code: %v", deployResp.StatusCode)
}
log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code : %v", string(responseBody), deployResp.StatusCode)
return errors.Errorf("integration flow deployment failed, response Status code: %v", deployResp.StatusCode)
}
// pollIFlowDeploymentStatus - Poll the integration flow deployment status, return status or error details
func pollIFlowDeploymentStatus(taskId string, retryCount int, config *integrationArtifactDeployOptions, httpClient piperhttp.Sender, apiHost string) error {
if retryCount <= 0 {
return errors.New("failed to start integration artifact after retrying several times")
}
deployStatus, err := getIntegrationArtifactDeployStatus(config, httpClient, apiHost, taskId)
if err != nil {
return err
}
//if artifact starting, then retry based on provided retry count
//with specific delay between each retry
if deployStatus == "DEPLOYING" {
// Calling Sleep method
sleepTime := int(retryCount * 3)
time.Sleep(time.Duration(sleepTime) * time.Second)
retryCount--
return pollIFlowDeploymentStatus(taskId, retryCount, config, httpClient, apiHost)
}
//if artifact started, then just return
if deployStatus == "SUCCESS" {
return nil
}
//if error return immediately with error details
if deployStatus == "FAIL" || deployStatus == "FAIL_ON_LICENSE_ERROR" {
resp, err := getIntegrationArtifactDeployError(config, httpClient, apiHost)
if err != nil {
return err
}
return errors.New(resp)
}
return nil
}
// GetHTTPErrorMessage - Return HTTP failure message
func getHTTPErrorMessage(httpErr error, response *http.Response, httpMethod, statusURL string) (string, error) {
responseBody, readErr := io.ReadAll(response.Body)
if readErr != nil {
return "", errors.Wrapf(readErr, "HTTP response body could not be read, response status code: %v", response.StatusCode)
}
log.Entry().Errorf("a HTTP error occurred! Response body: %v, response status code: %v", string(responseBody), response.StatusCode)
return "", errors.Wrapf(httpErr, "HTTP %v request to %v failed with error: %v", httpMethod, statusURL, responseBody)
}
// getIntegrationArtifactDeployStatus - Get integration artifact Deploy Status
func getIntegrationArtifactDeployStatus(config *integrationArtifactDeployOptions, httpClient piperhttp.Sender, apiHost string, taskId string) (string, error) {
httpMethod := "GET"
header := make(http.Header)
header.Add("content-type", "application/json")
header.Add("Accept", "application/json")
deployStatusURL := fmt.Sprintf("%s/api/v1/BuildAndDeployStatus(TaskId='%s')", apiHost, taskId)
deployStatusResp, httpErr := httpClient.SendRequest(httpMethod, deployStatusURL, nil, header, nil)
if deployStatusResp != nil && deployStatusResp.Body != nil {
defer deployStatusResp.Body.Close()
}
if deployStatusResp == nil {
return "", errors.Errorf("did not retrieve a HTTP response: %v", httpErr)
}
if deployStatusResp.StatusCode == http.StatusOK {
log.Entry().
WithField("IntegrationFlowID", config.IntegrationFlowID).
Info("Successfully started integration flow artefact in CPI runtime")
bodyText, readErr := io.ReadAll(deployStatusResp.Body)
if readErr != nil {
return "", errors.Wrapf(readErr, "HTTP response body could not be read, response status code: %v", deployStatusResp.StatusCode)
}
jsonResponse, parsingErr := gabs.ParseJSON([]byte(bodyText))
if parsingErr != nil {
return "", errors.Wrapf(parsingErr, "HTTP response body could not be parsed as JSON: %v", string(bodyText))
}
deployStatus := jsonResponse.Path("d.Status").Data().(string)
return deployStatus, nil
}
if httpErr != nil {
return getHTTPErrorMessage(httpErr, deployStatusResp, httpMethod, deployStatusURL)
}
return "", errors.Errorf("failed to get Integration Flow artefact runtime status, response Status code: %v", deployStatusResp.StatusCode)
}
// getIntegrationArtifactDeployError - Get integration artifact deploy error details
func getIntegrationArtifactDeployError(config *integrationArtifactDeployOptions, httpClient piperhttp.Sender, apiHost string) (string, error) {
httpMethod := "GET"
header := make(http.Header)
header.Add("content-type", "application/json")
errorStatusURL := fmt.Sprintf("%s/api/v1/IntegrationRuntimeArtifacts('%s')/ErrorInformation/$value", apiHost, config.IntegrationFlowID)
errorStatusResp, httpErr := httpClient.SendRequest(httpMethod, errorStatusURL, nil, header, nil)
if errorStatusResp != nil && errorStatusResp.Body != nil {
defer errorStatusResp.Body.Close()
}
if errorStatusResp == nil {
return "", errors.Errorf("did not retrieve a HTTP response: %v", httpErr)
}
if errorStatusResp.StatusCode == http.StatusOK {
log.Entry().
WithField("IntegrationFlowID", config.IntegrationFlowID).
Info("Successfully retrieved Integration Flow artefact deploy error details")
responseBody, readErr := io.ReadAll(errorStatusResp.Body)
if readErr != nil {
return "", errors.Wrapf(readErr, "HTTP response body could not be read, response status code: %v", errorStatusResp.StatusCode)
}
log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code: %v", string(responseBody), errorStatusResp.StatusCode)
errorDetails := string(responseBody)
return errorDetails, nil
}
if httpErr != nil {
return getHTTPErrorMessage(httpErr, errorStatusResp, httpMethod, errorStatusURL)
}
return "", errors.Errorf("failed to get Integration Flow artefact deploy error details, response Status code: %v", errorStatusResp.StatusCode)
}