-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0309b0
commit 1b1dc51
Showing
14 changed files
with
183 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package targets | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/bndr/gojenkins" | ||
"github.com/coveo/credentials-sync/credentials" | ||
) | ||
|
||
type JenkinsTarget struct { | ||
Base `mapstructure:",squash"` | ||
|
||
LoginCredentials *credentials.Credentials | ||
URL string | ||
SecureConnection bool | ||
|
||
client *gojenkins.Jenkins | ||
existingCredentials []credentials.Credentials | ||
} | ||
|
||
func (jenkins *JenkinsTarget) Initialize() error { | ||
auth := (*jenkins.LoginCredentials).(*credentials.UsernamePasswordCredentials) | ||
jenkins.client = gojenkins.CreateJenkins(jenkins.URL, auth.Username, auth.Password) | ||
jenkins.client.Requester.SslVerify = jenkins.SecureConnection | ||
jenkins.client.Init() | ||
var err error | ||
defer func() { | ||
if r := recover(); r != nil { | ||
log.Error("Recovered the following error while initializing Jenkins: ", r) | ||
switch x := r.(type) { | ||
case string: | ||
err = errors.New(x) | ||
case error: | ||
err = x | ||
default: | ||
err = errors.New("unknown panic") | ||
} | ||
} | ||
}() | ||
return err | ||
} | ||
|
||
func (jenkins *JenkinsTarget) ToString() string { | ||
return fmt.Sprintf("%s (Jenkins) - %s", jenkins.BaseToString(), jenkins.URL) | ||
} | ||
|
||
func (jenkins *JenkinsTarget) UpdateListOfCredentials(listOfCredentials []*credentials.Credentials) error { | ||
for _, credentials := range listOfCredentials { | ||
if err := jenkins.UpdateCredentials(credentials); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (jenkins *JenkinsTarget) UpdateCredentials(credentials *credentials.Credentials) error { | ||
return nil | ||
} | ||
|
||
func (jenkins *JenkinsTarget) ValidateConfiguration() bool { | ||
if _, err := url.ParseRequestURI(jenkins.URL); err != nil { | ||
log.Errorf("The Jenkins target `%s` has an invalid URL: %s", jenkins.Name, jenkins.URL) | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,62 @@ | ||
package targets | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/coveo/credentials-sync/credentials" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Target interface { | ||
BaseValidateConfiguration() bool | ||
Initialize() error | ||
ToString() string | ||
UpdateListOfCredentials([]*credentials.Credentials) error | ||
UpdateCredentials(*credentials.Credentials) error | ||
ValidateConfiguration() bool | ||
} | ||
|
||
type Base struct { | ||
Name string | ||
Tags map[string]string | ||
} | ||
|
||
// BaseToString prints out the target fields common to all types of targets | ||
func (targetBase *Base) BaseToString() string { | ||
tagString := "" | ||
for tagKey, tagValue := range targetBase.Tags { | ||
tagString = fmt.Sprintf("%s %s=%s", tagString, tagKey, tagValue) | ||
} | ||
return fmt.Sprintf("%s [Tags: %s]", targetBase.Name, strings.TrimSpace(tagString)) | ||
} | ||
|
||
func (targetBase *Base) BaseValidateConfiguration() bool { | ||
if targetBase.Name == "" { | ||
log.Errorf("Every target need to define a name") | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
type Configuration struct { | ||
JenkinsTargets []*JenkinsTarget `mapstructure:"jenkins"` | ||
} | ||
|
||
func (config *Configuration) AllTargets() []Target { | ||
targets := []Target{} | ||
for _, target := range config.JenkinsTargets { | ||
targets = append(targets, target) | ||
} | ||
return targets | ||
} | ||
|
||
func (config *Configuration) ValidateConfiguration() bool { | ||
configIsOK := true | ||
for _, target := range config.AllTargets() { | ||
if !target.ValidateConfiguration() || !target.BaseValidateConfiguration() { | ||
configIsOK = false | ||
} | ||
} | ||
return configIsOK | ||
} |