@@ -4,8 +4,11 @@ import (
4
4
"bytes"
5
5
"context"
6
6
"encoding/json"
7
+ "errors"
7
8
"fmt"
9
+ "io"
8
10
"net/http"
11
+ "strings"
9
12
10
13
"github.com/grafana/grafana/pkg/infra/log"
11
14
"github.com/grafana/grafana/pkg/services/cloudmigration"
@@ -24,11 +27,11 @@ type gmsClientImpl struct {
24
27
log * log.ConcreteLogger
25
28
}
26
29
27
- func (c * gmsClientImpl ) ValidateKey (ctx context.Context , cm cloudmigration.CloudMigrationSession ) error {
30
+ func (c * gmsClientImpl ) ValidateKey (ctx context.Context , cm cloudmigration.CloudMigrationSession ) ( err error ) {
28
31
logger := c .log .FromContext (ctx )
29
32
30
33
// TODO update service url to gms
31
- path := fmt .Sprintf ("https://cms-%s.%s/cloud-migrations/ api/v1/validate-key" , cm . ClusterSlug , c . domain )
34
+ path := fmt .Sprintf ("%s/ api/v1/validate-key" , buildBasePath ( c . domain , cm . ClusterSlug ) )
32
35
33
36
// validation is an empty POST to GMS with the authorization header included
34
37
req , err := http .NewRequest ("POST" , path , bytes .NewReader (nil ))
@@ -45,30 +48,25 @@ func (c *gmsClientImpl) ValidateKey(ctx context.Context, cm cloudmigration.Cloud
45
48
logger .Error ("error sending http request for token validation" , "err" , err .Error ())
46
49
return fmt .Errorf ("http request error: %w" , err )
47
50
}
48
-
49
51
defer func () {
50
- if err := resp .Body .Close (); err != nil {
51
- logger . Error ( "closing request body" , "err" , err . Error ( ))
52
+ if closeErr := resp .Body .Close (); closeErr != nil {
53
+ err = errors . Join ( err , fmt . Errorf ( "closing response body: %w" , closeErr ))
52
54
}
53
55
}()
54
56
55
57
if resp .StatusCode != 200 {
56
- var errResp map [string ]any
57
- if err := json .NewDecoder (resp .Body ).Decode (& errResp ); err != nil {
58
- logger .Error ("decoding error response" , "err" , err .Error ())
59
- } else {
60
- return fmt .Errorf ("token validation failure: %v" , errResp )
61
- }
58
+ body , _ := io .ReadAll (resp .Body )
59
+ return fmt .Errorf ("token validation failure: %v" , body )
62
60
}
63
61
64
62
return nil
65
63
}
66
64
67
- func (c * gmsClientImpl ) MigrateData (ctx context.Context , cm cloudmigration.CloudMigrationSession , request cloudmigration.MigrateDataRequest ) (* cloudmigration.MigrateDataResponse , error ) {
65
+ func (c * gmsClientImpl ) MigrateData (ctx context.Context , cm cloudmigration.CloudMigrationSession , request cloudmigration.MigrateDataRequest ) (result * cloudmigration.MigrateDataResponse , err error ) {
68
66
logger := c .log .FromContext (ctx )
69
67
70
68
// TODO update service url to gms
71
- path := fmt .Sprintf ("https://cms-%s.%s/cloud-migrations/ api/v1/migrate-data" , cm . ClusterSlug , c . domain )
69
+ path := fmt .Sprintf ("%s/ api/v1/migrate-data" , buildBasePath ( c . domain , cm . ClusterSlug ) )
72
70
73
71
reqDTO := convertRequestToDTO (request )
74
72
body , err := json .Marshal (reqDTO )
@@ -90,31 +88,30 @@ func (c *gmsClientImpl) MigrateData(ctx context.Context, cm cloudmigration.Cloud
90
88
if err != nil {
91
89
c .log .Error ("error sending http request for cloud migration run" , "err" , err .Error ())
92
90
return nil , fmt .Errorf ("http request error: %w" , err )
93
- } else if resp .StatusCode >= 400 {
94
- c .log .Error ("received error response for cloud migration run" , "statusCode" , resp .StatusCode )
95
- return nil , fmt .Errorf ("http request error: %w" , err )
96
91
}
97
-
98
92
defer func () {
99
- if err := resp .Body .Close (); err != nil {
100
- logger . Error ( "closing request body: %w" , err )
93
+ if closeErr := resp .Body .Close (); closeErr != nil {
94
+ err = errors . Join ( err , fmt . Errorf ( "closing response body: %w" , closeErr ) )
101
95
}
102
96
}()
103
97
98
+ if resp .StatusCode >= 400 {
99
+ c .log .Error ("received error response for cloud migration run" , "statusCode" , resp .StatusCode )
100
+ return nil , fmt .Errorf ("http request error: %w" , err )
101
+ }
102
+
104
103
var respDTO MigrateDataResponseDTO
105
104
if err := json .NewDecoder (resp .Body ).Decode (& respDTO ); err != nil {
106
105
logger .Error ("unmarshalling response body: %w" , err )
107
106
return nil , fmt .Errorf ("unmarshalling migration run response: %w" , err )
108
107
}
109
108
110
- result := convertResponseFromDTO (respDTO )
111
- return & result , nil
109
+ res := convertResponseFromDTO (respDTO )
110
+ return & res , nil
112
111
}
113
112
114
- func (c * gmsClientImpl ) StartSnapshot (ctx context.Context , session cloudmigration.CloudMigrationSession ) (* cloudmigration.StartSnapshotResponse , error ) {
115
- logger := c .log .FromContext (ctx )
116
-
117
- path := fmt .Sprintf ("https://cms-%s.%s/cloud-migrations/api/v1/start-snapshot" , session .ClusterSlug , c .domain )
113
+ func (c * gmsClientImpl ) StartSnapshot (ctx context.Context , session cloudmigration.CloudMigrationSession ) (out * cloudmigration.StartSnapshotResponse , err error ) {
114
+ path := fmt .Sprintf ("%s/api/v1/start-snapshot" , buildBasePath (c .domain , session .ClusterSlug ))
118
115
119
116
// Send the request to cms with the associated auth token
120
117
req , err := http .NewRequest (http .MethodPost , path , nil )
@@ -130,20 +127,21 @@ func (c *gmsClientImpl) StartSnapshot(ctx context.Context, session cloudmigratio
130
127
if err != nil {
131
128
c .log .Error ("error sending http request to start snapshot" , "err" , err .Error ())
132
129
return nil , fmt .Errorf ("http request error: %w" , err )
133
- } else if resp .StatusCode >= 400 {
134
- c .log .Error ("received error response to start snapshot" , "statusCode" , resp .StatusCode )
135
- return nil , fmt .Errorf ("http request error: %w" , err )
136
130
}
137
-
138
131
defer func () {
139
- if err := resp .Body .Close (); err != nil {
140
- logger . Error ( "closing request body: %w" , err )
132
+ if closeErr := resp .Body .Close (); closeErr != nil {
133
+ err = errors . Join ( err , fmt . Errorf ( "closing response body: %w" , closeErr ) )
141
134
}
142
135
}()
143
136
137
+ if resp .StatusCode >= 400 {
138
+ body , _ := io .ReadAll (resp .Body )
139
+ c .log .Error ("received error response to start snapshot" , "statusCode" , resp .StatusCode )
140
+ return nil , fmt .Errorf ("http request error: body=%s %w" , string (body ), err )
141
+ }
142
+
144
143
var result cloudmigration.StartSnapshotResponse
145
144
if err := json .NewDecoder (resp .Body ).Decode (& result ); err != nil {
146
- logger .Error ("unmarshalling response body: %w" , err )
147
145
return nil , fmt .Errorf ("unmarshalling start snapshot response: %w" , err )
148
146
}
149
147
@@ -187,3 +185,10 @@ func convertResponseFromDTO(result MigrateDataResponseDTO) cloudmigration.Migrat
187
185
Items : items ,
188
186
}
189
187
}
188
+
189
+ func buildBasePath (domain , clusterSlug string ) string {
190
+ if strings .HasPrefix (domain , "http://localhost" ) {
191
+ return domain
192
+ }
193
+ return fmt .Sprintf ("https://cms-%s.%s/cloud-migrations" , clusterSlug , domain )
194
+ }
0 commit comments