Skip to content

Commit

Permalink
Add sync for jenkins target
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Jun 8, 2019
1 parent 2a8de07 commit 21097d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func syncCredentials(target targets.Target, credentialsList []credentials.Creden
credChannel <- true
go func(cred credentials.Credentials) {
defer func() { <-credChannel }()
log.Infof("[%s] Syncing %s", target.GetName(), cred.GetID())
if err := target.UpdateCredentials(cred); err != nil {
message := fmt.Sprintf("Failed to send credential %s to %s: %v", cred.GetID(), target.GetName(), err)
if panicOnError {
Expand Down
29 changes: 27 additions & 2 deletions targets/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ func (jenkins *JenkinsTarget) UpdateListOfCredentials(listOfCredentials []creden
}

func (jenkins *JenkinsTarget) UpdateCredentials(cred credentials.Credentials) error {
jenkinsCred := toJenkinsCredential(cred)
if jenkinsCred == nil {
return fmt.Errorf("Unable to create jenkins credentials from %s", cred.GetID())
}
if jenkins.HasCredentials(cred) {
return jenkins.credentialsManager.Update(credentialsDomain, cred.GetID(), cred)
return jenkins.credentialsManager.Update(credentialsDomain, cred.GetID(), jenkinsCred)
}
return jenkins.credentialsManager.Add(credentialsDomain, cred)
return jenkins.credentialsManager.Add(credentialsDomain, jenkinsCred)
}

func (jenkins *JenkinsTarget) ValidateConfiguration() bool {
Expand All @@ -102,3 +106,24 @@ func (jenkins *JenkinsTarget) ValidateConfiguration() bool {
}
return true
}

func toJenkinsCredential(creds credentials.Credentials) interface{} {
switch creds.(type) {
case *credentials.SecretTextCredentials:
castCreds := creds.(*credentials.SecretTextCredentials)
return &gojenkins.StringCredentials{
ID: creds.GetID(),
Description: castCreds.Description,
Secret: castCreds.Secret,
}
case *credentials.UsernamePasswordCredentials:
castCreds := creds.(*credentials.UsernamePasswordCredentials)
return &gojenkins.UsernameCredentials{
ID: castCreds.GetID(),
Description: castCreds.Description,
Username: castCreds.Username,
Password: castCreds.Password,
}
}
return nil
}

0 comments on commit 21097d9

Please sign in to comment.