@@ -2,52 +2,40 @@ package remediation
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
- "errors"
7
5
"fmt"
8
6
"net/http"
9
7
"net/url"
10
- "strings"
11
8
12
9
ocmsdk "github.com/openshift-online/ocm-sdk-go"
13
10
BackplaneApi "github.com/openshift/backplane-api/pkg/client"
14
11
"github.com/openshift/backplane-cli/pkg/backplaneapi"
15
12
"github.com/openshift/backplane-cli/pkg/cli/config"
16
13
"github.com/openshift/backplane-cli/pkg/ocm"
17
- logger "github.com/sirupsen/logrus "
14
+ "github.com/openshift/backplane-cli/pkg/utils "
18
15
"k8s.io/client-go/rest"
19
16
)
20
17
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.
22
18
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 )
24
20
if err != nil {
25
21
return "" , fmt .Errorf ("unable to create backplane api client" )
26
22
}
27
23
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 })
30
25
if err != nil {
31
- logger .Debug ("unexpected..." )
32
26
return "" , err
33
27
}
28
+ if resp .StatusCode != http .StatusOK {
29
+ return "" , utils .TryPrintAPIError (resp , false )
30
+ }
34
31
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 )
48
33
34
+ if err != nil {
35
+ return "" , fmt .Errorf ("unable to parse response body from backplane: \n Status Code: %d" , resp .StatusCode )
49
36
}
50
- return api + * resp .JSON200 .ProxyUri , nil
37
+
38
+ return api + * remediationResponse .JSON200 .ProxyUri , nil
51
39
}
52
40
53
41
// CreateRemediationWithConn can be used to programtically interact with backplaneapi
@@ -76,28 +64,18 @@ func CreateRemediationWithConn(bp config.BackplaneConfiguration, ocmConnection *
76
64
}
77
65
78
66
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 )
80
68
if err != nil {
81
69
return fmt .Errorf ("unable to create backplane api client" )
82
70
}
83
71
84
- resp , err := client .DeleteRemediationWithResponse (context .TODO (), clusterID , & BackplaneApi.DeleteRemediationParams {Remediation : & remediation })
72
+ resp , err := client .DeleteRemediation (context .TODO (), clusterID , & BackplaneApi.DeleteRemediationParams {Remediation : & remediation })
85
73
if err != nil {
86
74
return err
87
75
}
88
76
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 )
101
79
}
102
80
103
81
return nil
0 commit comments