Skip to content

Commit a26b27f

Browse files
committed
Fix create and delete remediation commands
Switch to RawBackplaneApiClient Switch backplaneapi client calls and parse explicitly when needed
1 parent cd1b254 commit a26b27f

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

pkg/remediation/remediation.go

+15-37
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,40 @@ package remediation
22

33
import (
44
"context"
5-
"encoding/json"
6-
"errors"
75
"fmt"
86
"net/http"
97
"net/url"
10-
"strings"
118

129
ocmsdk "github.com/openshift-online/ocm-sdk-go"
1310
BackplaneApi "github.com/openshift/backplane-api/pkg/client"
1411
"github.com/openshift/backplane-cli/pkg/backplaneapi"
1512
"github.com/openshift/backplane-cli/pkg/cli/config"
1613
"github.com/openshift/backplane-cli/pkg/ocm"
17-
logger "github.com/sirupsen/logrus"
14+
"github.com/openshift/backplane-cli/pkg/utils"
1815
"k8s.io/client-go/rest"
1916
)
2017

21-
// TODO are we missusing the backplane api package? We have generated functions like backplaneapi.CreateRemediationWithResponse which already reads the body. All the utils functions do read the body again. Failing my calls here.
2218
func DoCreateRemediation(api string, clusterID string, accessToken string, remediationName string) (proxyURI string, err error) {
23-
client, err := backplaneapi.DefaultClientUtils.MakeBackplaneAPIClientWithAccessToken(api, accessToken)
19+
client, err := backplaneapi.DefaultClientUtils.MakeRawBackplaneAPIClientWithAccessToken(api, accessToken)
2420
if err != nil {
2521
return "", fmt.Errorf("unable to create backplane api client")
2622
}
2723

28-
logger.Debug("Sending request...")
29-
resp, err := client.CreateRemediationWithResponse(context.TODO(), clusterID, &BackplaneApi.CreateRemediationParams{Remediation: remediationName})
24+
resp, err := client.CreateRemediation(context.TODO(), clusterID, &BackplaneApi.CreateRemediationParams{Remediation: remediationName})
3025
if err != nil {
31-
logger.Debug("unexpected...")
3226
return "", err
3327
}
28+
if resp.StatusCode != http.StatusOK {
29+
return "", utils.TryPrintAPIError(resp, false)
30+
}
3431

35-
// TODO figure out the error handling here
36-
if resp.StatusCode() != http.StatusOK {
37-
// logger.Debugf("Unmarshal error resp body: %s", resp.Body)
38-
var dest BackplaneApi.Error
39-
if err := json.Unmarshal(resp.Body, &dest); err != nil {
40-
// Avoid squashing the HTTP response info with Unmarshal err...
41-
logger.Debugf("Unmarshaled %s", *dest.Message)
42-
43-
bodyStr := strings.ReplaceAll(string(resp.Body[:]), "\n", " ")
44-
err := fmt.Errorf("code:'%d'; failed to unmarshal response:'%s'; %w", resp.StatusCode(), bodyStr, err)
45-
return "", err
46-
}
47-
return "", errors.New(*dest.Message)
32+
remediationResponse, err := BackplaneApi.ParseCreateRemediationResponse(resp)
4833

34+
if err != nil {
35+
return "", fmt.Errorf("unable to parse response body from backplane: \n Status Code: %d", resp.StatusCode)
4936
}
50-
return api + *resp.JSON200.ProxyUri, nil
37+
38+
return api + *remediationResponse.JSON200.ProxyUri, nil
5139
}
5240

5341
// CreateRemediationWithConn can be used to programtically interact with backplaneapi
@@ -76,28 +64,18 @@ func CreateRemediationWithConn(bp config.BackplaneConfiguration, ocmConnection *
7664
}
7765

7866
func DoDeleteRemediation(api string, clusterID string, accessToken string, remediation string) error {
79-
client, err := backplaneapi.DefaultClientUtils.MakeBackplaneAPIClientWithAccessToken(api, accessToken)
67+
client, err := backplaneapi.DefaultClientUtils.MakeRawBackplaneAPIClientWithAccessToken(api, accessToken)
8068
if err != nil {
8169
return fmt.Errorf("unable to create backplane api client")
8270
}
8371

84-
resp, err := client.DeleteRemediationWithResponse(context.TODO(), clusterID, &BackplaneApi.DeleteRemediationParams{Remediation: &remediation})
72+
resp, err := client.DeleteRemediation(context.TODO(), clusterID, &BackplaneApi.DeleteRemediationParams{Remediation: &remediation})
8573
if err != nil {
8674
return err
8775
}
8876

89-
if resp.StatusCode() != http.StatusOK {
90-
// logger.Debugf("Unmarshal error resp body: %s", resp.Body)
91-
var dest BackplaneApi.Error
92-
if err := json.Unmarshal(resp.Body, &dest); err != nil {
93-
// Avoid squashing the HTTP response info with Unmarshal err...
94-
logger.Debugf("Unmarshaled %s", *dest.Message)
95-
96-
bodyStr := strings.ReplaceAll(string(resp.Body[:]), "\n", " ")
97-
err := fmt.Errorf("code:'%d'; failed to unmarshal response:'%s'; %w", resp.StatusCode(), bodyStr, err)
98-
return err
99-
}
100-
return errors.New(*dest.Message)
77+
if resp.StatusCode != http.StatusOK {
78+
return utils.TryPrintAPIError(resp, false)
10179
}
10280

10381
return nil

0 commit comments

Comments
 (0)