Skip to content

Instantly share code, notes, and snippets.

@mehmetg
Last active December 12, 2016 23:32
Show Gist options
  • Save mehmetg/e645337867f5862a25d4488508546ba1 to your computer and use it in GitHub Desktop.
Save mehmetg/e645337867f5862a25d4488508546ba1 to your computer and use it in GitHub Desktop.
Chef library to set credentials for Sauce OnDemand plugin.
#
# Cookbook Name:: nw_jenkins
# HWRP:: jenkins_saucelabs_password_credentials
#
# Author:: Mehmet Gerceker <mehmetg@msn.com>
class Chef
class Resource::JenkinsSauceLabsPasswordCredentials < Resource::JenkinsPasswordCredentials
resource_name :jenkins_saucelabs_password_credentials
# Attributes
attribute :username,
kind_of: String,
name_attribute: true
attribute :password,
kind_of: String,
required: true
end
end
class Chef
class Provider::JenkinsSauceLabsPasswordCredentials < Provider::JenkinsPasswordCredentials
use_inline_resources
provides :jenkins_saucelabs_password_credentials
def load_current_resource
@current_resource ||= Resource::JenkinsSauceLabsPasswordCredentials.new(new_resource.name)
super
if current_credentials
@current_resource.password(current_credentials[:password])
end
@current_credentials
end
private
#
# @see Chef::Resource::JenkinsPasswordCredentials#credentials_groovy
# @see https://github.com/jenkinsci/sauce-ondemand-plugin/blob/master/src/main/java/hudson/plugins/sauce_ondemand/credentials/SauceCredentials.java
#
def credentials_groovy
<<-EOH.gsub(/ ^{8}/, '')
import com.cloudbees.plugins.credentials.CredentialsScope
import hudson.plugins.sauce_ondemand.credentials.SauceCredentials
credentials = new SauceCredentials(
CredentialsScope.GLOBAL,
#{convert_to_groovy(new_resource.id)},
#{convert_to_groovy(new_resource.username)},
#{convert_to_groovy(new_resource.password)},
#{convert_to_groovy(new_resource.description)}
)
EOH
end
#
# @see Chef::Resource::JenkinsCredentials#attribute_to_property_map
#
def attribute_to_property_map
{ password: 'credentials.password.plainText' }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment