Skip to content

Commit

Permalink
Move ocmwraper to ocm package and ClientUtils to backplane-api package (
Browse files Browse the repository at this point in the history
  • Loading branch information
samanthajayasinghe authored Dec 5, 2023
1 parent 3edb853 commit 1963e3e
Show file tree
Hide file tree
Showing 41 changed files with 397 additions and 176 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ mock-gen:
mockgen -destination=./pkg/client/mocks/ClientMock.go -package=mocks github.com/openshift/backplane-api/pkg/client ClientInterface
mockgen -destination=./pkg/client/mocks/ClientWithResponsesMock.go -package=mocks github.com/openshift/backplane-api/pkg/client ClientWithResponsesInterface
mockgen -destination=./pkg/utils/mocks/ClusterMock.go -package=mocks github.com/openshift/backplane-cli/pkg/utils ClusterUtils
mockgen -destination=./pkg/utils/mocks/ocmWrapperMock.go -package=mocks github.com/openshift/backplane-cli/pkg/utils OCMInterface
mockgen -destination=./pkg/utils/mocks/clientUtilsMock.go -package=mocks github.com/openshift/backplane-cli/pkg/utils ClientUtils
mockgen -destination=./pkg/ocm/mocks/ocmWrapperMock.go -package=mocks github.com/openshift/backplane-cli/pkg/ocm OCMInterface
mockgen -destination=./pkg/backplaneapi/mocks/clientUtilsMock.go -package=mocks github.com/openshift/backplane-cli/pkg/backplaneapi ClientUtils
mockgen -destination=./pkg/cli/session/mocks/sessionMock.go -package=mocks github.com/openshift/backplane-cli/pkg/cli/session BackplaneSessionInterface
mockgen -destination=./pkg/utils/mocks/shellCheckerMock.go -package=mocks github.com/openshift/backplane-cli/pkg/utils ShellCheckerInterface

Expand Down
10 changes: 6 additions & 4 deletions cmd/ocm-backplane/cloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
BackplaneApi "github.com/openshift/backplane-api/pkg/client"
"github.com/openshift/backplane-cli/pkg/awsutil"
"github.com/openshift/backplane-cli/pkg/backplaneapi"
"github.com/openshift/backplane-cli/pkg/cli/config"
bpCredentials "github.com/openshift/backplane-cli/pkg/credentials"
"github.com/openshift/backplane-cli/pkg/ocm"
"github.com/openshift/backplane-cli/pkg/utils"
logger "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -98,7 +100,7 @@ func (cfg *QueryConfig) GetCloudConsole() (*ConsoleResponse, error) {
func (cfg *QueryConfig) getCloudConsoleFromPublicAPI(ocmToken string) (*ConsoleResponse, error) {
logger.Debugln("Getting Cloud Console")

client, err := utils.DefaultClientUtils.GetBackplaneClient(cfg.BackplaneConfiguration.URL, ocmToken, cfg.BackplaneConfiguration.ProxyURL)
client, err := backplaneapi.DefaultClientUtils.GetBackplaneClient(cfg.BackplaneConfiguration.URL, ocmToken, cfg.BackplaneConfiguration.ProxyURL)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -164,7 +166,7 @@ func (cfg *QueryConfig) GetCloudCredentials() (bpCredentials.Response, error) {
}

func (cfg *QueryConfig) getCloudCredentialsFromBackplaneAPI(ocmToken string) (bpCredentials.Response, error) {
client, err := utils.DefaultClientUtils.GetBackplaneClient(cfg.BackplaneConfiguration.URL, ocmToken, cfg.BackplaneConfiguration.ProxyURL)
client, err := backplaneapi.DefaultClientUtils.GetBackplaneClient(cfg.BackplaneConfiguration.URL, ocmToken, cfg.BackplaneConfiguration.ProxyURL)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -237,7 +239,7 @@ func (cfg *QueryConfig) getIsolatedCredentials(ocmToken string) (aws.Credentials
return aws.Credentials{}, fmt.Errorf("failed to assume role using JWT: %w", err)
}

backplaneClient, err := utils.DefaultClientUtils.GetBackplaneClient(cfg.BackplaneConfiguration.URL, ocmToken, cfg.BackplaneConfiguration.ProxyURL)
backplaneClient, err := backplaneapi.DefaultClientUtils.GetBackplaneClient(cfg.BackplaneConfiguration.URL, ocmToken, cfg.BackplaneConfiguration.ProxyURL)
if err != nil {
return aws.Credentials{}, fmt.Errorf("failed to create backplane client with access token: %w", err)
}
Expand Down Expand Up @@ -280,7 +282,7 @@ func (cfg *QueryConfig) getIsolatedCredentials(ocmToken string) (aws.Credentials

func isIsolatedBackplaneAccess(cluster *cmv1.Cluster, ocmConnection *ocmsdk.Connection) (bool, error) {
if cluster.AWS().STS().Enabled() {
stsSupportJumpRole, err := utils.DefaultOCMInterface.GetStsSupportJumpRoleARN(ocmConnection, cluster.ID())
stsSupportJumpRole, err := ocm.DefaultOCMInterface.GetStsSupportJumpRoleARN(ocmConnection, cluster.ID())
if err != nil {
return false, fmt.Errorf("failed to get sts support jump role ARN for cluster %v: %w", cluster.ID(), err)
}
Expand Down
24 changes: 13 additions & 11 deletions cmd/ocm-backplane/cloud/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import (
. "github.com/onsi/gomega"
sdk "github.com/openshift-online/ocm-sdk-go"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift/backplane-cli/pkg/backplaneapi"
backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks"
"github.com/openshift/backplane-cli/pkg/cli/config"
"github.com/openshift/backplane-cli/pkg/utils"
mocks2 "github.com/openshift/backplane-cli/pkg/utils/mocks"
"github.com/openshift/backplane-cli/pkg/ocm"
ocmMock "github.com/openshift/backplane-cli/pkg/ocm/mocks"
)

//nolint:gosec
var _ = Describe("getIsolatedCredentials", func() {
var (
mockCtrl *gomock.Controller
mockOcmInterface *mocks2.MockOCMInterface
mockClientUtil *mocks2.MockClientUtils
mockOcmInterface *ocmMock.MockOCMInterface
mockClientUtil *backplaneapiMock.MockClientUtils

testOcmToken string
testClusterID string
Expand All @@ -37,11 +39,11 @@ var _ = Describe("getIsolatedCredentials", func() {
BeforeEach(func() {
mockCtrl = gomock.NewController(GinkgoT())

mockOcmInterface = mocks2.NewMockOCMInterface(mockCtrl)
utils.DefaultOCMInterface = mockOcmInterface
mockOcmInterface = ocmMock.NewMockOCMInterface(mockCtrl)
ocm.DefaultOCMInterface = mockOcmInterface

mockClientUtil = mocks2.NewMockClientUtils(mockCtrl)
utils.DefaultClientUtils = mockClientUtil
mockClientUtil = backplaneapiMock.NewMockClientUtils(mockCtrl)
backplaneapi.DefaultClientUtils = mockClientUtil

testOcmToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZW1haWwiOiJ0ZXN0QGZvby5jb20iLCJpYXQiOjE1MTYyMzkwMjJ9.5NG4wFhitEKZyzftSwU67kx4JVTEWcEoKhCl_AFp8T4"
testClusterID = "test123"
Expand Down Expand Up @@ -181,16 +183,16 @@ func TestIsIsolatedBackplaneAccess(t *testing.T) {
var _ = Describe("isIsolatedBackplaneAccess", func() {
var (
mockCtrl *gomock.Controller
mockOcmInterface *mocks2.MockOCMInterface
mockOcmInterface *ocmMock.MockOCMInterface

testClusterID string
)

BeforeEach(func() {
mockCtrl = gomock.NewController(GinkgoT())

mockOcmInterface = mocks2.NewMockOCMInterface(mockCtrl)
utils.DefaultOCMInterface = mockOcmInterface
mockOcmInterface = ocmMock.NewMockOCMInterface(mockCtrl)
ocm.DefaultOCMInterface = mockOcmInterface

testClusterID = "test123"
})
Expand Down
9 changes: 5 additions & 4 deletions cmd/ocm-backplane/cloud/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"os"
"strconv"

"github.com/openshift-online/ocm-cli/pkg/ocm"
ocmsdk "github.com/openshift-online/ocm-cli/pkg/ocm"
"github.com/openshift/backplane-cli/pkg/ocm"

"github.com/pkg/browser"
logger "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -102,12 +103,12 @@ func runConsole(cmd *cobra.Command, argv []string) (err error) {
clusterKey = clusterInfo.ClusterID
}

clusterID, clusterName, err := utils.DefaultOCMInterface.GetTargetCluster(clusterKey)
clusterID, clusterName, err := ocm.DefaultOCMInterface.GetTargetCluster(clusterKey)
if err != nil {
return err
}

cluster, err := utils.DefaultOCMInterface.GetClusterInfoByID(clusterID)
cluster, err := ocm.DefaultOCMInterface.GetClusterInfoByID(clusterID)
if err != nil {
return fmt.Errorf("failed to get cluster info for %s: %w", clusterID, err)
}
Expand All @@ -133,7 +134,7 @@ func runConsole(cmd *cobra.Command, argv []string) (err error) {
logger.Infof("Using backplane URL: %s\n", backplaneConfiguration.URL)

// Initialize OCM connection
ocmConnection, err := ocm.NewConnection().Build()
ocmConnection, err := ocmsdk.NewConnection().Build()
if err != nil {
return fmt.Errorf("unable to build ocm sdk: %w", err)
}
Expand Down
18 changes: 10 additions & 8 deletions cmd/ocm-backplane/cloud/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ import (
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"

"github.com/openshift/backplane-cli/pkg/backplaneapi"
backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks"
"github.com/openshift/backplane-cli/pkg/client/mocks"
"github.com/openshift/backplane-cli/pkg/info"
"github.com/openshift/backplane-cli/pkg/utils"
mocks2 "github.com/openshift/backplane-cli/pkg/utils/mocks"
"github.com/openshift/backplane-cli/pkg/ocm"
ocmMock "github.com/openshift/backplane-cli/pkg/ocm/mocks"
)

var _ = Describe("Cloud console command", func() {

var (
mockCtrl *gomock.Controller
mockClientWithResp *mocks.MockClientWithResponsesInterface
mockOcmInterface *mocks2.MockOCMInterface
mockClientUtil *mocks2.MockClientUtils
mockOcmInterface *ocmMock.MockOCMInterface
mockClientUtil *backplaneapiMock.MockClientUtils

proxyURI string
consoleAWSURL string
Expand All @@ -38,11 +40,11 @@ var _ = Describe("Cloud console command", func() {
mockCtrl = gomock.NewController(GinkgoT())
mockClientWithResp = mocks.NewMockClientWithResponsesInterface(mockCtrl)

mockOcmInterface = mocks2.NewMockOCMInterface(mockCtrl)
utils.DefaultOCMInterface = mockOcmInterface
mockOcmInterface = ocmMock.NewMockOCMInterface(mockCtrl)
ocm.DefaultOCMInterface = mockOcmInterface

mockClientUtil = mocks2.NewMockClientUtils(mockCtrl)
utils.DefaultClientUtils = mockClientUtil
mockClientUtil = backplaneapiMock.NewMockClientUtils(mockCtrl)
backplaneapi.DefaultClientUtils = mockClientUtil

proxyURI = "https://shard.apps"
consoleAWSURL = "https://signin.aws.amazon.com/federation?Action=login"
Expand Down
9 changes: 5 additions & 4 deletions cmd/ocm-backplane/cloud/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"errors"
"fmt"

"github.com/openshift-online/ocm-cli/pkg/ocm"
ocmsdk "github.com/openshift-online/ocm-cli/pkg/ocm"
"github.com/openshift/backplane-cli/pkg/cli/config"
bpCredentials "github.com/openshift/backplane-cli/pkg/credentials"
"github.com/openshift/backplane-cli/pkg/ocm"
"github.com/openshift/backplane-cli/pkg/utils"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -70,12 +71,12 @@ func runCredentials(cmd *cobra.Command, argv []string) error {
return fmt.Errorf("expected exactly one cluster")
}

clusterID, clusterName, err := utils.DefaultOCMInterface.GetTargetCluster(clusterKey)
clusterID, clusterName, err := ocm.DefaultOCMInterface.GetTargetCluster(clusterKey)
if err != nil {
return err
}

cluster, err := utils.DefaultOCMInterface.GetClusterInfoByID(clusterID)
cluster, err := ocm.DefaultOCMInterface.GetClusterInfoByID(clusterID)
if err != nil {
return fmt.Errorf("failed to get cluster info for %s: %w", clusterID, err)
}
Expand All @@ -101,7 +102,7 @@ func runCredentials(cmd *cobra.Command, argv []string) error {
logger.Infof("Using backplane URL: %s\n", backplaneConfiguration.URL)

// Initialize OCM connection
ocmConnection, err := ocm.NewConnection().Build()
ocmConnection, err := ocmsdk.NewConnection().Build()
if err != nil {
return fmt.Errorf("unable to build ocm sdk: %w", err)
}
Expand Down
18 changes: 11 additions & 7 deletions cmd/ocm-backplane/cloud/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import (
. "github.com/onsi/gomega"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift/backplane-cli/pkg/backplaneapi"
backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks"
"github.com/openshift/backplane-cli/pkg/client/mocks"
bpCredentials "github.com/openshift/backplane-cli/pkg/credentials"
"github.com/openshift/backplane-cli/pkg/info"
"github.com/openshift/backplane-cli/pkg/ocm"
ocmMock "github.com/openshift/backplane-cli/pkg/ocm/mocks"
"github.com/openshift/backplane-cli/pkg/utils"
mocks2 "github.com/openshift/backplane-cli/pkg/utils/mocks"
log "github.com/sirupsen/logrus"
Expand All @@ -32,8 +36,8 @@ var _ = Describe("Cloud console command", func() {
var (
mockCtrl *gomock.Controller
mockClientWithResp *mocks.MockClientWithResponsesInterface
mockOcmInterface *mocks2.MockOCMInterface
mockClientUtil *mocks2.MockClientUtils
mockOcmInterface *ocmMock.MockOCMInterface
mockClientUtil *backplaneapiMock.MockClientUtils
mockClusterUtils *mocks2.MockClusterUtils

proxyURI string
Expand All @@ -51,15 +55,15 @@ var _ = Describe("Cloud console command", func() {
mockCtrl = gomock.NewController(GinkgoT())
mockClientWithResp = mocks.NewMockClientWithResponsesInterface(mockCtrl)

mockOcmInterface = mocks2.NewMockOCMInterface(mockCtrl)
utils.DefaultOCMInterface = mockOcmInterface
mockOcmInterface = ocmMock.NewMockOCMInterface(mockCtrl)
ocm.DefaultOCMInterface = mockOcmInterface

mockClientUtil = mocks2.NewMockClientUtils(mockCtrl)
utils.DefaultClientUtils = mockClientUtil
mockClientUtil = backplaneapiMock.NewMockClientUtils(mockCtrl)
backplaneapi.DefaultClientUtils = mockClientUtil

mockClusterUtils = mocks2.NewMockClusterUtils(mockCtrl)
utils.DefaultClusterUtils = mockClusterUtils
utils.DefaultClientUtils = mockClientUtil
backplaneapi.DefaultClientUtils = mockClientUtil

proxyURI = "https://shard.apps"
credentialAWS = "fake aws credential"
Expand Down
9 changes: 5 additions & 4 deletions cmd/ocm-backplane/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"k8s.io/client-go/rest"

"github.com/openshift/backplane-cli/pkg/cli/config"
"github.com/openshift/backplane-cli/pkg/ocm"
"github.com/openshift/backplane-cli/pkg/utils"
)

Expand Down Expand Up @@ -318,7 +319,7 @@ func runConsole(cmd *cobra.Command, argv []string) (err error) {

// Get ocm access token
logger.Debugln("Finding ocm token")
ocmToken, err := utils.DefaultOCMInterface.GetOCMAccessToken()
ocmToken, err := ocm.DefaultOCMInterface.GetOCMAccessToken()
if err != nil {
return err
}
Expand Down Expand Up @@ -391,7 +392,7 @@ func runConsole(cmd *cobra.Command, argv []string) (err error) {
consoleArgs.image,
)

c, err := utils.DefaultOCMInterface.GetClusterInfoByID(clusterID)
c, err := ocm.DefaultOCMInterface.GetClusterInfoByID(clusterID)
if err != nil {
return err
}
Expand Down Expand Up @@ -561,7 +562,7 @@ func fetchPullSecretIfNotExist() (string, string, error) {
return "", "", err
}

response, err := utils.DefaultOCMInterface.GetPullSecret()
response, err := ocm.DefaultOCMInterface.GetPullSecret()
if err != nil {
return "", "", fmt.Errorf("failed to get pull secret from ocm: %v", err)
}
Expand Down Expand Up @@ -656,7 +657,7 @@ func isRunningHigherThan411() bool {
if err != nil {
return false
}
currentCluster, err := utils.DefaultOCMInterface.GetClusterInfoByID(currentClusterInfo.ClusterID)
currentCluster, err := ocm.DefaultOCMInterface.GetClusterInfoByID(currentClusterInfo.ClusterID)

if err != nil {
return false
Expand Down
9 changes: 5 additions & 4 deletions cmd/ocm-backplane/console/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
"k8s.io/client-go/tools/clientcmd/api"

"github.com/openshift/backplane-cli/pkg/info"
"github.com/openshift/backplane-cli/pkg/ocm"
ocmMock "github.com/openshift/backplane-cli/pkg/ocm/mocks"
"github.com/openshift/backplane-cli/pkg/utils"
mocks "github.com/openshift/backplane-cli/pkg/utils/mocks"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"

Expand All @@ -28,7 +29,7 @@ import (
var _ = Describe("console command", func() {
var (
mockCtrl *gomock.Controller
mockOcmInterface *mocks.MockOCMInterface
mockOcmInterface *ocmMock.MockOCMInterface

capturedCommands [][]string

Expand All @@ -42,8 +43,8 @@ var _ = Describe("console command", func() {

BeforeEach(func() {
mockCtrl = gomock.NewController(GinkgoT())
mockOcmInterface = mocks.NewMockOCMInterface(mockCtrl)
utils.DefaultOCMInterface = mockOcmInterface
mockOcmInterface = ocmMock.NewMockOCMInterface(mockCtrl)
ocm.DefaultOCMInterface = mockOcmInterface

os.Setenv("CONTAINER_ENGINE", PODMAN)

Expand Down
Loading

0 comments on commit 1963e3e

Please sign in to comment.