Skip to content

Commit

Permalink
enable stylecheck for golangci lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeng committed Jul 28, 2023
1 parent 8e81d9b commit e8da9aa
Show file tree
Hide file tree
Showing 61 changed files with 909 additions and 877 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/

# Goreleaser
dist/
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ linters:
- staticcheck
- typecheck
- unused
- stylecheck
22 changes: 12 additions & 10 deletions cmd/ocm-backplane/cloud/assume.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"io"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/openshift/backplane-cli/pkg/awsUtil"
"github.com/spf13/cobra"

"github.com/openshift/backplane-cli/pkg/awsutil"
"github.com/openshift/backplane-cli/pkg/cli/config"
"github.com/openshift/backplane-cli/pkg/utils"
"github.com/spf13/cobra"
"io"
)

const (
Expand Down Expand Up @@ -70,16 +72,16 @@ func runAssume(_ *cobra.Command, args []string) error {
return fmt.Errorf("error retrieving backplane configuration: %w", err)
}

initialClient, err := awsUtil.StsClientWithProxy(bpConfig.ProxyURL)
initialClient, err := awsutil.StsClientWithProxy(bpConfig.ProxyURL)
if err != nil {
return fmt.Errorf("failed to create sts client: %w", err)
}
seedCredentials, err := awsUtil.AssumeRoleWithJWT(*ocmToken, assumeArgs.roleArn, initialClient)
seedCredentials, err := awsutil.AssumeRoleWithJWT(*ocmToken, assumeArgs.roleArn, initialClient)
if err != nil {
return fmt.Errorf("failed to assume role using JWT: %w", err)
}

clusterId, _, err := utils.DefaultOCMInterface.GetTargetCluster(args[0])
clusterID, _, err := utils.DefaultOCMInterface.GetTargetCluster(args[0])
if err != nil {
return fmt.Errorf("failed to get target cluster: %w", err)
}
Expand All @@ -89,7 +91,7 @@ func runAssume(_ *cobra.Command, args []string) error {
return fmt.Errorf("failed to create backplane client with access token: %w", err)
}

response, err := backplaneClient.GetAssumeRoleSequence(context.TODO(), clusterId)
response, err := backplaneClient.GetAssumeRoleSequence(context.TODO(), clusterID)
if err != nil {
return fmt.Errorf("failed to fetch arn sequence: %w", err)
}
Expand Down Expand Up @@ -119,13 +121,13 @@ func runAssume(_ *cobra.Command, args []string) error {
Region: "us-east-1",
Credentials: credentials.NewStaticCredentialsProvider(*seedCredentials.AccessKeyId, *seedCredentials.SecretAccessKey, *seedCredentials.SessionToken),
})
targetCredentials, err := awsUtil.AssumeRoleSequence(email, seedClient, roleAssumeSequence, bpConfig.ProxyURL, awsUtil.DefaultSTSClientProviderFunc)
targetCredentials, err := awsutil.AssumeRoleSequence(email, seedClient, roleAssumeSequence, bpConfig.ProxyURL, awsutil.DefaultSTSClientProviderFunc)
if err != nil {
return fmt.Errorf("failed to assume role sequence: %w", err)
}

credsResponse := awsUtil.AWSCredentialsResponse{
AccessKeyId: *targetCredentials.AccessKeyId,
credsResponse := awsutil.AWSCredentialsResponse{
AccessKeyID: *targetCredentials.AccessKeyId,
SecretAccessKey: *targetCredentials.SecretAccessKey,
SessionToken: *targetCredentials.SessionToken,
Expiration: targetCredentials.Expiration.String(),
Expand Down
13 changes: 7 additions & 6 deletions cmd/ocm-backplane/cloud/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sigs.k8s.io/yaml"

BackplaneApi "github.com/openshift/backplane-api/pkg/client"

"github.com/openshift/backplane-cli/pkg/cli/config"
"github.com/openshift/backplane-cli/pkg/utils"
)
Expand Down Expand Up @@ -103,13 +104,13 @@ func runConsole(cmd *cobra.Command, argv []string) (err error) {
clusterKey = clusterInfo.ClusterID
}

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

logger.WithFields(logger.Fields{
"ID": clusterId,
"ID": clusterID,
"Name": clusterName}).Infoln("Target cluster")

// ============Get Backplane URl ==========================
Expand All @@ -130,8 +131,8 @@ func runConsole(cmd *cobra.Command, argv []string) (err error) {
logger.Infof("Using backplane URL: %s\n", bpURL)
}

// ======== Get cloudconsole from backplane API ============
response, err := getCloudConsole(bpURL, clusterId)
// ======== Get cloud console from backplane API ============
response, err := getCloudConsole(bpURL, clusterID)
if err != nil {
return err
}
Expand Down Expand Up @@ -160,13 +161,13 @@ func validateParams(argv []string) (err error) {
}

// getCloudConsole returns console response calling to public Backplane API
func getCloudConsole(backplaneURL string, clusterId string) (*ConsoleResponse, error) {
func getCloudConsole(backplaneURL string, clusterID string) (*ConsoleResponse, error) {
logger.Debugln("Getting Cloud Console")
client, err := utils.DefaultClientUtils.GetBackplaneClient(backplaneURL)
if err != nil {
return nil, err
}
resp, err := client.GetCloudConsole(context.TODO(), clusterId)
resp, err := client.GetCloudConsole(context.TODO(), clusterID)
if err != nil {
return nil, err
}
Expand Down
85 changes: 43 additions & 42 deletions cmd/ocm-backplane/cloud/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"

"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"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)

var _ = Describe("Cloud console command", func() {
Expand All @@ -27,11 +28,11 @@ var _ = Describe("Cloud console command", func() {
mockOcmInterface *mocks2.MockOCMInterface
mockClientUtil *mocks2.MockClientUtils

testClusterId string
trueClusterId string
proxyUri string
consoleAWSUrl string
consoleGcloudUrl string
testClusterID string
trueClusterID string
proxyURI string
consoleAWSURL string
consoleGcloudURL string

fakeAWSResp *http.Response
fakeGCloudResp *http.Response
Expand All @@ -48,18 +49,18 @@ var _ = Describe("Cloud console command", func() {
mockClientUtil = mocks2.NewMockClientUtils(mockCtrl)
utils.DefaultClientUtils = mockClientUtil

testClusterId = "test123"
trueClusterId = "trueID123"
proxyUri = "https://shard.apps"
consoleAWSUrl = "https://signin.aws.amazon.com/federation?Action=login"
consoleGcloudUrl = "https://cloud.google.com/"
testClusterID = "test123"
trueClusterID = "trueID123"
proxyURI = "https://shard.apps"
consoleAWSURL = "https://signin.aws.amazon.com/federation?Action=login"
consoleGcloudURL = "https://cloud.google.com/"

mockClientWithResp.EXPECT().LoginClusterWithResponse(gomock.Any(), gomock.Any()).Return(nil, nil).Times(0)

// Define fake AWS response
fakeAWSResp = &http.Response{
Body: MakeIoReader(
fmt.Sprintf(`{"proxy_uri":"proxy", "message":"msg", "ConsoleLink":"%s"}`, consoleAWSUrl),
fmt.Sprintf(`{"proxy_uri":"proxy", "message":"msg", "ConsoleLink":"%s"}`, consoleAWSURL),
),
Header: map[string][]string{},
StatusCode: http.StatusOK,
Expand All @@ -69,7 +70,7 @@ var _ = Describe("Cloud console command", func() {
// Define fake AWS response
fakeGCloudResp = &http.Response{
Body: MakeIoReader(
fmt.Sprintf(`{"proxy_uri":"proxy", "message":"msg", "ConsoleLink":"%s"}`, consoleGcloudUrl),
fmt.Sprintf(`{"proxy_uri":"proxy", "message":"msg", "ConsoleLink":"%s"}`, consoleGcloudURL),
),
Header: map[string][]string{},
StatusCode: http.StatusOK,
Expand All @@ -82,30 +83,30 @@ var _ = Describe("Cloud console command", func() {

// Disabled log output
log.SetOutput(io.Discard)
os.Setenv(info.BACKPLANE_URL_ENV_NAME, proxyUri)
os.Setenv(info.BackplaneURLEnvName, proxyURI)
})

AfterEach(func() {
os.Setenv(info.BACKPLANE_URL_ENV_NAME, "")
os.Setenv(info.BackplaneURLEnvName, "")
mockCtrl.Finish()
})

Context("Execute cloud console command", func() {
It("should return AWS cloud console", func() {
mockOcmInterface.EXPECT().GetTargetCluster(testClusterId).Return(trueClusterId, testClusterId, nil)
mockClientUtil.EXPECT().GetBackplaneClient(proxyUri).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterId).Return(fakeAWSResp, nil)
mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil)
mockClientUtil.EXPECT().GetBackplaneClient(proxyURI).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterID).Return(fakeAWSResp, nil)

err := runConsole(nil, []string{testClusterId})
err := runConsole(nil, []string{testClusterID})

Expect(err).To(BeNil())
})

It("should return GCP cloud console", func() {
mockOcmInterface.EXPECT().GetTargetCluster(testClusterId).Return(trueClusterId, testClusterId, nil)
mockClientUtil.EXPECT().GetBackplaneClient(proxyUri).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterId).Return(fakeGCloudResp, nil)
err := runConsole(nil, []string{testClusterId})
mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil)
mockClientUtil.EXPECT().GetBackplaneClient(proxyURI).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterID).Return(fakeGCloudResp, nil)
err := runConsole(nil, []string{testClusterID})

Expect(err).To(BeNil())
})
Expand All @@ -114,32 +115,32 @@ var _ = Describe("Cloud console command", func() {

Context("get Cloud Console", func() {
It("should return AWS cloud URL", func() {
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterId).Return(fakeAWSResp, nil)
mockClientUtil.EXPECT().GetBackplaneClient(proxyUri).Return(mockClient, nil).AnyTimes()
cloudResponse, err := getCloudConsole(proxyUri, trueClusterId)
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterID).Return(fakeAWSResp, nil)
mockClientUtil.EXPECT().GetBackplaneClient(proxyURI).Return(mockClient, nil).AnyTimes()
cloudResponse, err := getCloudConsole(proxyURI, trueClusterID)
Expect(err).To(BeNil())

Expect(cloudResponse.ConsoleLink).To(Equal(consoleAWSUrl))
Expect(cloudResponse.ConsoleLink).To(Equal(consoleAWSURL))

})

It("should return Gcloud cloud URL", func() {

mockClientUtil.EXPECT().GetBackplaneClient(proxyUri).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterId).Return(fakeGCloudResp, nil)
cloudResponse, err := getCloudConsole(proxyUri, trueClusterId)
mockClientUtil.EXPECT().GetBackplaneClient(proxyURI).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterID).Return(fakeGCloudResp, nil)
cloudResponse, err := getCloudConsole(proxyURI, trueClusterID)
Expect(err).To(BeNil())

Expect(cloudResponse.ConsoleLink).To(Equal(consoleGcloudUrl))
Expect(cloudResponse.ConsoleLink).To(Equal(consoleGcloudURL))

})

It("should fail when AWS Unavailable", func() {
fakeAWSResp.StatusCode = http.StatusInternalServerError

mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterId).Return(fakeAWSResp, nil)
mockClientUtil.EXPECT().GetBackplaneClient(proxyUri).Return(mockClient, nil).AnyTimes()
_, err := getCloudConsole(proxyUri, trueClusterId)
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterID).Return(fakeAWSResp, nil)
mockClientUtil.EXPECT().GetBackplaneClient(proxyURI).Return(mockClient, nil).AnyTimes()
_, err := getCloudConsole(proxyURI, trueClusterID)
Expect(err).NotTo(BeNil())

Expect(err.Error()).Should(ContainSubstring("error from backplane: \n Status Code: 500\n"))
Expand All @@ -148,9 +149,9 @@ var _ = Describe("Cloud console command", func() {

It("should fail when GCP Unavailable", func() {
fakeGCloudResp.StatusCode = http.StatusInternalServerError
mockClientUtil.EXPECT().GetBackplaneClient(proxyUri).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterId).Return(fakeGCloudResp, nil)
_, err := getCloudConsole(proxyUri, trueClusterId)
mockClientUtil.EXPECT().GetBackplaneClient(proxyURI).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterID).Return(fakeGCloudResp, nil)
_, err := getCloudConsole(proxyURI, trueClusterID)
Expect(err).NotTo(BeNil())

Expect(err.Error()).Should(ContainSubstring("error from backplane: \n Status Code: 500\n"))
Expand All @@ -159,9 +160,9 @@ var _ = Describe("Cloud console command", func() {

It("should fail for unauthorized BP-API", func() {
fakeAWSResp.StatusCode = http.StatusUnauthorized
mockClientUtil.EXPECT().GetBackplaneClient(proxyUri).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterId).Return(fakeAWSResp, nil)
_, err := getCloudConsole(proxyUri, trueClusterId)
mockClientUtil.EXPECT().GetBackplaneClient(proxyURI).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().GetCloudConsole(gomock.Any(), trueClusterID).Return(fakeAWSResp, nil)
_, err := getCloudConsole(proxyURI, trueClusterID)
Expect(err).NotTo(BeNil())

Expect(err.Error()).Should(ContainSubstring("error from backplane: \n Status Code: 401\n"))
Expand Down
Loading

0 comments on commit e8da9aa

Please sign in to comment.