Skip to content

Commit 1699359

Browse files
OSD-24269: Add cluster login support via OHSS card (#478)
* Add JIRA client * Add Jira Client * OSD-24269:Enable login to cluster via OHSS card * OSD-24269: Refactor Jira client with issue service
1 parent 7ab551e commit 1699359

File tree

10 files changed

+277
-17
lines changed

10 files changed

+277
-17
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ mock-gen:
115115
mockgen -destination=./pkg/cli/session/mocks/sessionMock.go -package=mocks github.com/openshift/backplane-cli/pkg/cli/session BackplaneSessionInterface
116116
mockgen -destination=./pkg/utils/mocks/shellCheckerMock.go -package=mocks github.com/openshift/backplane-cli/pkg/utils ShellCheckerInterface
117117
mockgen -destination=./pkg/pagerduty/mocks/clientMock.go -package=mocks github.com/openshift/backplane-cli/pkg/pagerduty PagerDutyClient
118-
mockgen -destination=./pkg/utils/mocks/jiraMock.go -package=mocks github.com/openshift/backplane-cli/pkg/utils IssueServiceInterface
118+
mockgen -destination=./pkg/jira/mocks/jiraMock.go -package=mocks github.com/openshift/backplane-cli/pkg/jira IssueServiceInterface
119119

120120
.PHONY: build-image
121121
build-image:

cmd/ocm-backplane/login/login.go

+41-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/openshift/backplane-cli/pkg/backplaneapi"
2525
"github.com/openshift/backplane-cli/pkg/cli/config"
2626
"github.com/openshift/backplane-cli/pkg/cli/globalflags"
27+
"github.com/openshift/backplane-cli/pkg/jira"
2728
"github.com/openshift/backplane-cli/pkg/login"
2829
"github.com/openshift/backplane-cli/pkg/ocm"
2930
"github.com/openshift/backplane-cli/pkg/pagerduty"
@@ -36,6 +37,7 @@ const (
3637
LoginTypeClusterID = "cluster-id"
3738
LoginTypeExistingKubeConfig = "kube-config"
3839
LoginTypePagerduty = "pagerduty"
40+
LoginTypeJira = "jira"
3941
)
4042

4143
var (
@@ -44,6 +46,7 @@ var (
4446
kubeConfigPath string
4547
pd string
4648
defaultNamespace string
49+
ohss string
4750
}
4851

4952
// loginType derive the login type based on flags and args
@@ -62,7 +65,7 @@ var (
6265
run oc command later to operate the target cluster.`,
6366
Example: " backplane login <id>\n backplane login %test%\n backplane login <external_id>\n backplane login --pd <incident-id>",
6467
Args: func(cmd *cobra.Command, args []string) error {
65-
if cmd.Flags().Lookup("pd").Changed {
68+
if cmd.Flags().Lookup("pd").Changed || cmd.Flags().Lookup("ohss").Changed {
6669
if err := cobra.ExactArgs(0)(cmd, args); err != nil {
6770
return err
6871
}
@@ -77,6 +80,9 @@ var (
7780
RunE: runLogin,
7881
SilenceUsage: true,
7982
}
83+
84+
//ohss service
85+
ohssService *jira.OHSSService
8086
)
8187

8288
func init() {
@@ -112,6 +118,12 @@ func init() {
112118
"default",
113119
"The default namespace for a user to execute commands in",
114120
)
121+
flags.StringVar(
122+
&args.ohss,
123+
"ohss",
124+
"",
125+
"Login using JIRA Id",
126+
)
115127

116128
}
117129

@@ -140,7 +152,16 @@ func runLogin(cmd *cobra.Command, argv []string) (err error) {
140152
}
141153
clusterKey = info.ClusterID
142154
elevateReason = info.WebURL
143-
155+
case LoginTypeJira:
156+
ohssIssue, err := getClusterInfoFromJira()
157+
if err != nil {
158+
return err
159+
}
160+
if ohssIssue.ClusterID == "" {
161+
return fmt.Errorf("clusterID cannot be detected for JIRA issue:%s", args.ohss)
162+
}
163+
clusterKey = ohssIssue.ClusterID
164+
elevateReason = ohssIssue.WebURL
144165
case LoginTypeClusterID:
145166
logger.Debugf("Cluster Key is given in argument")
146167
clusterKey = argv[0]
@@ -571,9 +592,11 @@ func preLogin(cmd *cobra.Command, argv []string) (err error) {
571592
loginType = LoginTypeClusterID
572593

573594
case 0:
574-
if args.pd == "" {
595+
if args.pd == "" && args.ohss == "" {
575596
loginType = LoginTypeExistingKubeConfig
576-
} else {
597+
} else if args.ohss != "" {
598+
loginType = LoginTypeJira
599+
} else if args.pd != "" {
577600
loginType = LoginTypePagerduty
578601
}
579602
}
@@ -606,6 +629,20 @@ func getClusterInfoFromPagerduty(bpConfig config.BackplaneConfiguration) (alert
606629
return alert, nil
607630
}
608631

632+
// getClusterInfoFromJira returns a cluster info OHSS card
633+
func getClusterInfoFromJira() (ohss jira.OHSSIssue, err error) {
634+
if ohssService == nil {
635+
ohssService = jira.NewOHSSService(jira.DefaultIssueService)
636+
}
637+
638+
ohss, err = ohssService.GetIssue(args.ohss)
639+
if err != nil {
640+
return ohss, err
641+
}
642+
643+
return ohss, nil
644+
}
645+
609646
// getClusterIDFromExistingKubeConfig returns clusterId from kubeconfig
610647
func getClusterIDFromExistingKubeConfig() (string, error) {
611648
var clusterKey string

cmd/ocm-backplane/login/login_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ import (
1414
. "github.com/onsi/ginkgo"
1515
. "github.com/onsi/gomega"
1616
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
17+
"github.com/trivago/tgo/tcontainer"
1718
"k8s.io/client-go/tools/clientcmd"
1819
"k8s.io/client-go/tools/clientcmd/api"
1920

21+
"github.com/andygrunwald/go-jira"
2022
"github.com/openshift/backplane-cli/pkg/backplaneapi"
2123
backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks"
2224
"github.com/openshift/backplane-cli/pkg/cli/config"
2325
"github.com/openshift/backplane-cli/pkg/client/mocks"
26+
jiraClient "github.com/openshift/backplane-cli/pkg/jira"
27+
jiraMock "github.com/openshift/backplane-cli/pkg/jira/mocks"
2428
"github.com/openshift/backplane-cli/pkg/login"
2529
"github.com/openshift/backplane-cli/pkg/ocm"
2630
ocmMock "github.com/openshift/backplane-cli/pkg/ocm/mocks"
@@ -40,6 +44,7 @@ var _ = Describe("Login command", func() {
4044
mockClientWithResp *mocks.MockClientWithResponsesInterface
4145
mockOcmInterface *ocmMock.MockOCMInterface
4246
mockClientUtil *backplaneapiMock.MockClientUtils
47+
mockIssueService *jiraMock.MockIssueServiceInterface
4348

4449
testClusterID string
4550
testToken string
@@ -576,4 +581,62 @@ var _ = Describe("Login command", func() {
576581
})
577582

578583
})
584+
585+
Context("check JIRA OHSS login", func() {
586+
var (
587+
testOHSSID string
588+
testIssue jira.Issue
589+
issueFields *jira.IssueFields
590+
)
591+
BeforeEach(func() {
592+
mockIssueService = jiraMock.NewMockIssueServiceInterface(mockCtrl)
593+
ohssService = jiraClient.NewOHSSService(mockIssueService)
594+
testOHSSID = "OHSS-1000"
595+
})
596+
597+
It("should login to ohss card cluster", func() {
598+
599+
loginType = LoginTypeJira
600+
args.ohss = testOHSSID
601+
err := utils.CreateTempKubeConfig(nil)
602+
args.kubeConfigPath = ""
603+
Expect(err).To(BeNil())
604+
issueFields = &jira.IssueFields{
605+
Project: jira.Project{Key: jiraClient.JiraOHSSProjectKey},
606+
Unknowns: tcontainer.MarshalMap{jiraClient.CustomFieldClusterID: testClusterID},
607+
}
608+
testIssue = jira.Issue{ID: testOHSSID, Fields: issueFields}
609+
globalOpts.ProxyURL = "https://squid.myproxy.com"
610+
mockIssueService.EXPECT().Get(testOHSSID, nil).Return(&testIssue, nil, nil).Times(1)
611+
mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes()
612+
mockClientUtil.EXPECT().SetClientProxyURL(globalOpts.ProxyURL).Return(nil)
613+
mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(testClusterID, testClusterID, nil)
614+
mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(testClusterID)).Return(false, nil).AnyTimes()
615+
mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil)
616+
mockClientUtil.EXPECT().MakeRawBackplaneAPIClientWithAccessToken(backplaneAPIURI, testToken).Return(mockClient, nil)
617+
mockClient.EXPECT().LoginCluster(gomock.Any(), gomock.Eq(testClusterID)).Return(fakeResp, nil)
618+
619+
err = runLogin(nil, nil)
620+
621+
Expect(err).To(BeNil())
622+
})
623+
624+
It("should failed missing cluster id ohss cards", func() {
625+
626+
loginType = LoginTypeJira
627+
args.ohss = testOHSSID
628+
629+
issueFields = &jira.IssueFields{
630+
Project: jira.Project{Key: jiraClient.JiraOHSSProjectKey},
631+
}
632+
testIssue = jira.Issue{ID: testOHSSID, Fields: issueFields}
633+
mockIssueService.EXPECT().Get(testOHSSID, nil).Return(&testIssue, nil, nil).Times(1)
634+
mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes()
635+
636+
err := runLogin(nil, nil)
637+
638+
Expect(err).NotTo(BeNil())
639+
Expect(err.Error()).To(Equal("clusterID cannot be detected for JIRA issue:OHSS-1000"))
640+
})
641+
})
579642
})

pkg/accessrequest/accessRequest.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
ocmsdk "github.com/openshift-online/ocm-sdk-go"
1010
acctrspv1 "github.com/openshift-online/ocm-sdk-go/accesstransparency/v1"
1111
"github.com/openshift/backplane-cli/pkg/cli/config"
12+
jiraClient "github.com/openshift/backplane-cli/pkg/jira"
1213
"github.com/openshift/backplane-cli/pkg/ocm"
1314
"github.com/openshift/backplane-cli/pkg/utils"
1415
logger "github.com/sirupsen/logrus"
@@ -114,7 +115,7 @@ func verifyAndPossiblyRetrieveIssue(bpConfig *config.BackplaneConfiguration, isP
114115
return nil, nil
115116
}
116117

117-
issue, _, err := utils.DefaultIssueService.Get(issueID, nil)
118+
issue, _, err := jiraClient.DefaultIssueService.Get(issueID, nil)
118119
if err != nil {
119120
return nil, err
120121
}
@@ -145,7 +146,7 @@ func createNotificationIssue(bpConfig *config.BackplaneConfiguration, isProd boo
145146
},
146147
}
147148

148-
issue, _, err := utils.DefaultIssueService.Create(issue)
149+
issue, _, err := jiraClient.DefaultIssueService.Create(issue)
149150
if err != nil {
150151
return nil, err
151152
}
@@ -164,7 +165,7 @@ func getProjectFromIssueID(issueID string) string {
164165
}
165166

166167
func transitionIssue(issueID, newTransitionName string) {
167-
possibleTransitions, _, err := utils.DefaultIssueService.GetTransitions(issueID)
168+
possibleTransitions, _, err := jiraClient.DefaultIssueService.GetTransitions(issueID)
168169
if err != nil {
169170
logger.Warnf("won't transition the '%s' JIRA issue to '%s' as it was not possible to retrieve the possible transitions for the issue: %v", issueID, newTransitionName, err)
170171
} else {
@@ -180,7 +181,7 @@ func transitionIssue(issueID, newTransitionName string) {
180181
if transitionID == "" {
181182
logger.Warnf("won't transition the '%s' JIRA issue to '%s' as there is no transition named that way", issueID, newTransitionName)
182183
} else {
183-
_, err := utils.DefaultIssueService.DoTransition(issueID, transitionID)
184+
_, err := jiraClient.DefaultIssueService.DoTransition(issueID, transitionID)
184185

185186
if err != nil {
186187
logger.Warnf("failed to transition the '%s' JIRA issue to '%s': %v", issueID, newTransitionName, err)
@@ -196,7 +197,7 @@ func updateNotificationIssueDescription(issue *jira.Issue, onApprovalTransitionN
196197
onApprovalTransitionName, accessRequest.HREF()),
197198
}
198199

199-
_, _, err := utils.DefaultIssueService.Update(issue)
200+
_, _, err := jiraClient.DefaultIssueService.Update(issue)
200201
if err != nil {
201202
logger.Warnf("failed to update the description of the '%s' JIRA issue: %v", issue.Key, err)
202203
}

pkg/accessrequest/accessRequest_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import (
1717
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
1818
"github.com/openshift/backplane-cli/pkg/backplaneapi"
1919
backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks"
20+
jiraClient "github.com/openshift/backplane-cli/pkg/jira"
21+
jiraMocks "github.com/openshift/backplane-cli/pkg/jira/mocks"
2022
"github.com/openshift/backplane-cli/pkg/ocm"
2123
ocmMock "github.com/openshift/backplane-cli/pkg/ocm/mocks"
22-
"github.com/openshift/backplane-cli/pkg/utils"
23-
utilsMocks "github.com/openshift/backplane-cli/pkg/utils/mocks"
2424
)
2525

2626
const testDesc = "accessrequest package"
@@ -57,7 +57,7 @@ var _ = Describe(testDesc, func() {
5757
mockCtrl *gomock.Controller
5858
mockClientUtil *backplaneapiMock.MockClientUtils
5959
mockOcmInterface *ocmMock.MockOCMInterface
60-
mockIssueService *utilsMocks.MockIssueServiceInterface
60+
mockIssueService *jiraMocks.MockIssueServiceInterface
6161

6262
clusterID string
6363
ocmEnv *cmv1.Environment
@@ -81,8 +81,8 @@ var _ = Describe(testDesc, func() {
8181
mockOcmInterface = ocmMock.NewMockOCMInterface(mockCtrl)
8282
ocm.DefaultOCMInterface = mockOcmInterface
8383

84-
mockIssueService = utilsMocks.NewMockIssueServiceInterface(mockCtrl)
85-
utils.DefaultIssueService = mockIssueService
84+
mockIssueService = jiraMocks.NewMockIssueServiceInterface(mockCtrl)
85+
jiraClient.DefaultIssueService = mockIssueService
8686

8787
clusterID = "cluster-12345678"
8888

pkg/utils/jira.go pkg/jira/issueService.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package jira
22

33
import (
44
"errors"

pkg/jira/jira_suite_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package jira_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestJira(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Jira Service Suite")
13+
}

pkg/utils/mocks/jiraMock.go pkg/jira/mocks/jiraMock.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)