Change signature of CreateOrUpdateJob function

This commit is contained in:
Tomasz Sęk 2019-01-17 21:42:46 +01:00
parent 88d4f82e48
commit d318bea4a1
No known key found for this signature in database
GPG Key ID: DC356D23F6A644D0
5 changed files with 26 additions and 30 deletions

View File

@ -23,7 +23,7 @@ type Jenkins interface {
CreateFolder(name string, parents ...string) (*gojenkins.Folder, error) CreateFolder(name string, parents ...string) (*gojenkins.Folder, error)
CreateJobInFolder(config string, jobName string, parentIDs ...string) (*gojenkins.Job, error) CreateJobInFolder(config string, jobName string, parentIDs ...string) (*gojenkins.Job, error)
CreateJob(config string, options ...interface{}) (*gojenkins.Job, error) CreateJob(config string, options ...interface{}) (*gojenkins.Job, error)
CreateOrUpdateJob(config string, options ...interface{}) (*gojenkins.Job, error) CreateOrUpdateJob(config, jobName string) (*gojenkins.Job, bool, error)
RenameJob(job string, name string) *gojenkins.Job RenameJob(job string, name string) *gojenkins.Job
CopyJob(copyFrom string, newName string) (*gojenkins.Job, error) CopyJob(copyFrom string, newName string) (*gojenkins.Job, error)
DeleteJob(name string) (bool, error) DeleteJob(name string) (bool, error)
@ -58,25 +58,19 @@ type jenkins struct {
} }
// CreateOrUpdateJob creates or updates a job from config // CreateOrUpdateJob creates or updates a job from config
func (jenkins *jenkins) CreateOrUpdateJob(config string, options ...interface{}) (*gojenkins.Job, error) { func (jenkins *jenkins) CreateOrUpdateJob(config, jobName string) (job *gojenkins.Job, created bool, err error) {
// taken from gojenkins.CreateJob
qr := make(map[string]string)
if len(options) > 0 {
qr["name"] = options[0].(string)
} else {
return nil, errors.New("error creating job, job name is missing")
}
// create or update // create or update
job, err := jenkins.GetJob(qr["name"]) job, err = jenkins.GetJob(jobName)
if isNotFoundError(err) { if isNotFoundError(err) {
return jenkins.CreateJob(config, options...) job, err = jenkins.CreateJob(config, jobName)
created = true
return
} else if err != nil { } else if err != nil {
return nil, err return
} }
err = job.UpdateConfig(config) err = job.UpdateConfig(config)
return job, err return
} }
func isNotFoundError(err error) bool { func isNotFoundError(err error) bool {

View File

@ -1,5 +1,5 @@
// Code generated by MockGen. DO NOT EDIT. // Code generated by MockGen. DO NOT EDIT.
// Source: /home/bartek/go/src/github.com/VirtusLab/jenkins-operator/pkg/controller/jenkins/client/jenkins.go // Source: pkg/controller/jenkins/client/jenkins.go
// Package mock_client is a generated GoMock package. // Package mock_client is a generated GoMock package.
package client package client
@ -173,23 +173,19 @@ func (mr *MockJenkinsMockRecorder) CreateJob(config interface{}, options ...inte
} }
// CreateOrUpdateJob mocks base method // CreateOrUpdateJob mocks base method
func (m *MockJenkins) CreateOrUpdateJob(config string, options ...interface{}) (*gojenkins.Job, error) { func (m *MockJenkins) CreateOrUpdateJob(config, jobName string) (*gojenkins.Job, bool, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
varargs := []interface{}{config} ret := m.ctrl.Call(m, "CreateOrUpdateJob", config, jobName)
for _, a := range options {
varargs = append(varargs, a)
}
ret := m.ctrl.Call(m, "CreateOrUpdateJob", varargs...)
ret0, _ := ret[0].(*gojenkins.Job) ret0, _ := ret[0].(*gojenkins.Job)
ret1, _ := ret[1].(error) ret1, _ := ret[1].(bool)
return ret0, ret1 ret2, _ := ret[2].(error)
return ret0, ret1, ret2
} }
// CreateOrUpdateJob indicates an expected call of CreateOrUpdateJob // CreateOrUpdateJob indicates an expected call of CreateOrUpdateJob
func (mr *MockJenkinsMockRecorder) CreateOrUpdateJob(config interface{}, options ...interface{}) *gomock.Call { func (mr *MockJenkinsMockRecorder) CreateOrUpdateJob(config, jobName interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper() mr.mock.ctrl.T.Helper()
varargs := append([]interface{}{config}, options...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateOrUpdateJob", reflect.TypeOf((*MockJenkins)(nil).CreateOrUpdateJob), config, jobName)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateOrUpdateJob", reflect.TypeOf((*MockJenkins)(nil).CreateOrUpdateJob), varargs...)
} }
// RenameJob mocks base method // RenameJob mocks base method

View File

@ -63,10 +63,13 @@ func (s *SeedJobs) EnsureSeedJobs(jenkins *virtuslabv1alpha1.Jenkins) (done bool
// createJob is responsible for creating jenkins job which configures jenkins seed jobs and deploy keys // createJob is responsible for creating jenkins job which configures jenkins seed jobs and deploy keys
func (s *SeedJobs) createJob() error { func (s *SeedJobs) createJob() error {
_, err := s.jenkinsClient.CreateOrUpdateJob(seedJobConfigXML, ConfigureSeedJobsName) _, created, err := s.jenkinsClient.CreateOrUpdateJob(seedJobConfigXML, ConfigureSeedJobsName)
if err != nil { if err != nil {
return err return err
} }
if created {
s.logger.Info(fmt.Sprintf("'%s' job has been created", ConfigureSeedJobsName))
}
return nil return nil
} }

View File

@ -47,7 +47,7 @@ func TestEnsureSeedJobs(t *testing.T) {
jenkinsClient. jenkinsClient.
EXPECT(). EXPECT().
CreateOrUpdateJob(seedJobConfigXML, ConfigureSeedJobsName). CreateOrUpdateJob(seedJobConfigXML, ConfigureSeedJobsName).
Return(nil, nil) Return(nil, true, nil)
jenkinsClient. jenkinsClient.
EXPECT(). EXPECT().
@ -69,7 +69,7 @@ func TestEnsureSeedJobs(t *testing.T) {
jenkinsClient. jenkinsClient.
EXPECT(). EXPECT().
CreateOrUpdateJob(seedJobConfigXML, ConfigureSeedJobsName). CreateOrUpdateJob(seedJobConfigXML, ConfigureSeedJobsName).
Return(nil, nil) Return(nil, false, nil)
jenkinsClient. jenkinsClient.
EXPECT(). EXPECT().

View File

@ -40,10 +40,13 @@ func New(jenkinsClient jenkinsclient.Jenkins, k8sClient k8s.Client, logger logr.
// ConfigureGroovyJob configures jenkins job for executing groovy scripts // ConfigureGroovyJob configures jenkins job for executing groovy scripts
func (g *Groovy) ConfigureGroovyJob() error { func (g *Groovy) ConfigureGroovyJob() error {
_, err := g.jenkinsClient.CreateOrUpdateJob(fmt.Sprintf(configurationJobXMLFmt, g.scriptsPath), g.jobName) _, created, err := g.jenkinsClient.CreateOrUpdateJob(fmt.Sprintf(configurationJobXMLFmt, g.scriptsPath), g.jobName)
if err != nil { if err != nil {
return err return err
} }
if created {
g.logger.Info(fmt.Sprintf("'%s' job has been created", g.jobName))
}
return nil return nil
} }