@@ -4,9 +4,11 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"net/http"
7
+ "os"
7
8
"strings"
8
9
9
- "github.com/openshift-online/ocm-cli/pkg/ocm"
10
+ ocmocm "github.com/openshift-online/ocm-cli/pkg/ocm"
11
+ ocmurls "github.com/openshift-online/ocm-cli/pkg/urls"
10
12
ocmsdk "github.com/openshift-online/ocm-sdk-go"
11
13
acctrspv1 "github.com/openshift-online/ocm-sdk-go/accesstransparency/v1"
12
14
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
@@ -35,19 +37,51 @@ type OCMInterface interface {
35
37
GetClusterActiveAccessRequest (ocmConnection * ocmsdk.Connection , clusterID string ) (* acctrspv1.AccessRequest , error )
36
38
CreateClusterAccessRequest (ocmConnection * ocmsdk.Connection , clusterID , reason , jiraIssueID , approvalDuration string ) (* acctrspv1.AccessRequest , error )
37
39
CreateAccessRequestDecision (ocmConnection * ocmsdk.Connection , accessRequest * acctrspv1.AccessRequest , decision acctrspv1.DecisionDecision , justification string ) (* acctrspv1.Decision , error )
40
+ SetupOCMConnection () (* ocmsdk.Connection , error )
38
41
}
39
42
40
43
const (
41
- ClustersPageSize = 50
44
+ ClustersPageSize = 50
45
+ ocmNotLoggedInMessage = "Not logged in"
42
46
)
43
47
44
- type DefaultOCMInterfaceImpl struct {}
48
+ type DefaultOCMInterfaceImpl struct {
49
+ // connection *ocmsdk.Connection
50
+ }
45
51
46
52
var DefaultOCMInterface OCMInterface = & DefaultOCMInterfaceImpl {}
47
53
54
+ // SetupOCMConnection setups the ocm connection for all the other ocm requests
55
+ func (o * DefaultOCMInterfaceImpl ) SetupOCMConnection () (* ocmsdk.Connection , error ) {
56
+
57
+ envURL := os .Getenv ("OCM_URL" )
58
+ if envURL != "" {
59
+ // Fetch the real ocm url from the alias and set it back to the ENV
60
+ ocmURL , err := ocmurls .ResolveGatewayURL (envURL , nil )
61
+ if err != nil {
62
+ return nil , err
63
+ }
64
+ os .Setenv ("OCM_URL" , ocmURL )
65
+ logger .Debugf ("reset the OCM_URL to %s" , ocmURL )
66
+ }
67
+
68
+ // Setup connection at the first try
69
+ connection , err := ocmocm .NewConnection ().Build ()
70
+ if err != nil {
71
+ if strings .Contains (err .Error (), ocmNotLoggedInMessage ) {
72
+ return nil , fmt .Errorf ("please ensure you are logged into OCM by using the command " +
73
+ "\" ocm login --url $ENV\" " )
74
+ } else {
75
+ return nil , err
76
+ }
77
+ }
78
+
79
+ return connection , nil
80
+ }
81
+
48
82
// IsClusterHibernating returns a boolean to indicate whether the cluster is hibernating
49
83
func (o * DefaultOCMInterfaceImpl ) IsClusterHibernating (clusterID string ) (bool , error ) {
50
- connection , err := ocm . NewConnection (). Build ()
84
+ connection , err := o . SetupOCMConnection ()
51
85
if err != nil {
52
86
return false , fmt .Errorf ("failed to create OCM connection: %v" , err )
53
87
}
@@ -64,7 +98,7 @@ func (o *DefaultOCMInterfaceImpl) IsClusterHibernating(clusterID string) (bool,
64
98
// GetTargetCluster returns one single cluster based on the search key and survery.
65
99
func (o * DefaultOCMInterfaceImpl ) GetTargetCluster (clusterKey string ) (clusterID , clusterName string , err error ) {
66
100
// Create the client for the OCM API:
67
- connection , err := ocm . NewConnection (). Build ()
101
+ connection , err := o . SetupOCMConnection ()
68
102
if err != nil {
69
103
return "" , "" , fmt .Errorf ("failed to create OCM connection: %v" , err )
70
104
}
@@ -96,7 +130,7 @@ func (o *DefaultOCMInterfaceImpl) GetTargetCluster(clusterKey string) (clusterID
96
130
// for the given clusterID
97
131
func (o * DefaultOCMInterfaceImpl ) GetManagingCluster (targetClusterID string ) (clusterID , clusterName string , isHostedControlPlane bool , err error ) {
98
132
// Create the client for the OCM API:
99
- connection , err := ocm . NewConnection (). Build ()
133
+ connection , err := o . SetupOCMConnection ()
100
134
if err != nil {
101
135
return "" , "" , false , fmt .Errorf ("failed to create OCM connection: %v" , err )
102
136
}
@@ -153,7 +187,7 @@ func (o *DefaultOCMInterfaceImpl) GetManagingCluster(targetClusterID string) (cl
153
187
// GetServiceCluster gets the service cluster for a given hpyershift hosted cluster
154
188
func (o * DefaultOCMInterfaceImpl ) GetServiceCluster (targetClusterID string ) (clusterID , clusterName string , err error ) {
155
189
// Create the client for the OCM API
156
- connection , err := ocm . NewConnection (). Build ()
190
+ connection , err := o . SetupOCMConnection ()
157
191
if err != nil {
158
192
return "" , "" , fmt .Errorf ("failed to create OCM connection: %v" , err )
159
193
}
@@ -207,7 +241,7 @@ func (o *DefaultOCMInterfaceImpl) GetServiceCluster(targetClusterID string) (clu
207
241
// GetOCMAccessToken initiates the OCM connection and returns the access token
208
242
func (o * DefaultOCMInterfaceImpl ) GetOCMAccessToken () (* string , error ) {
209
243
// Get ocm access token
210
- connection , err := ocm . NewConnection (). Build ()
244
+ connection , err := o . SetupOCMConnection ()
211
245
if err != nil {
212
246
return nil , fmt .Errorf ("failed to create OCM connection: %v" , err )
213
247
}
@@ -236,7 +270,7 @@ func (o *DefaultOCMInterfaceImpl) GetPullSecret() (string, error) {
236
270
237
271
// Get ocm access token
238
272
logger .Debugln ("Finding ocm token" )
239
- connection , err := ocm . NewConnection (). Build ()
273
+ connection , err := o . SetupOCMConnection ()
240
274
if err != nil {
241
275
return "" , fmt .Errorf ("failed to create OCM connection: %v" , err )
242
276
}
@@ -256,7 +290,7 @@ func (o *DefaultOCMInterfaceImpl) GetPullSecret() (string, error) {
256
290
// for a given internal cluster id.
257
291
func (o * DefaultOCMInterfaceImpl ) GetClusterInfoByID (clusterID string ) (* cmv1.Cluster , error ) {
258
292
// Create the client for the OCM API:
259
- connection , err := ocm . NewConnection (). Build ()
293
+ connection , err := o . SetupOCMConnection ()
260
294
if err != nil {
261
295
return nil , fmt .Errorf ("failed to create OCM connection: %v" , err )
262
296
}
@@ -282,7 +316,7 @@ func (o *DefaultOCMInterfaceImpl) GetClusterInfoByIDWithConn(ocmConnection *ocms
282
316
// IsProduction checks if OCM is currently in production env
283
317
func (o * DefaultOCMInterfaceImpl ) IsProduction () (bool , error ) {
284
318
// Create the client for the OCM API:
285
- connection , err := ocm . NewConnection (). Build ()
319
+ connection , err := o . SetupOCMConnection ()
286
320
if err != nil {
287
321
return false , fmt .Errorf ("failed to create OCM connection: %v" , err )
288
322
}
@@ -299,10 +333,10 @@ func (o *DefaultOCMInterfaceImpl) GetStsSupportJumpRoleARN(ocmConnection *ocmsdk
299
333
return response .Body ().RoleArn (), nil
300
334
}
301
335
302
- // GetBackplaneURL returns the Backplane API URL based on the OCM env
336
+ // GetOCMEnvironment returns the Backplane API URL based on the OCM env
303
337
func (o * DefaultOCMInterfaceImpl ) GetOCMEnvironment () (* cmv1.Environment , error ) {
304
338
// Create the client for the OCM API
305
- connection , err := ocm . NewConnection (). Build ()
339
+ connection , err := o . SetupOCMConnection ()
306
340
if err != nil {
307
341
return nil , fmt .Errorf ("failed to create OCM connection: %v" , err )
308
342
}
0 commit comments