Class: Bamboo::Rest
- Inherits:
-
Object
- Object
- Bamboo::Rest
- Defined in:
- lib/puppet_x/bamboo/rest.rb
Overview
The class to invoke bamboo REST endpoint to GET/PUT configurations
Constant Summary collapse
- DEFAULT_BAMBOO_HOST =
'localhost'
- DEFAULT_BAMBOO_PORT =
'8085'
- DEFAULT_BAMBOO_BASE_URL =
DEFAULT_BAMBOO_HOST + ":" + DEFAULT_BAMBOO_PORT
Instance Attribute Summary collapse
-
#bamboo_host ⇒ Object
readonly
Returns the value of attribute bamboo_host.
-
#bamboo_port ⇒ Object
readonly
Returns the value of attribute bamboo_port.
Class Method Summary collapse
-
.create_bamboo_resource(resource_name, config) ⇒ Object
Create a bamboo resource via REST end point Make sure bamboo is running before actually invoking the endpoint.
-
.delete_bamboo_resource(resource_name) ⇒ Object
Delete bamboo configurations via REST end point Make sure bamboo is running before actually invoking the endpoint.
-
.get_bamboo_settings(resource_name, content_type = 'application/json') ⇒ Object
Retrieve configurations of bamboo via REST endpoint Make sure bamboo is running before actually invoking the endpoint.
- .init_service ⇒ Object
- .service ⇒ Object
-
.update_bamboo_settings(resource_name, config, method = 'put', content_type = 'application/json') ⇒ Object
Update bamboo configurations via REST end point Make sure bamboo is running before actually invoking the endpoint.
- .with_client ⇒ Object
Instance Method Summary collapse
-
#initialize(options, bamboo_base_url = DEFAULT_BAMBOO_BASE_URL) ⇒ Rest
constructor
A new instance of Rest.
- #make_request(path, method) ⇒ Object
Constructor Details
#initialize(options, bamboo_base_url = DEFAULT_BAMBOO_BASE_URL) ⇒ Rest
Returns a new instance of Rest.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/puppet_x/bamboo/rest.rb', line 20 def initialize(, bamboo_base_url = DEFAULT_BAMBOO_BASE_URL) # https://docs.ruby-lang.org/en/2.1.0/URI. components = URI.split(bamboo_base_url) @bamboo_host = components[2] port = components[3] if port.nil? @bamboo_port = DEFAULT_BAMBOO_PORT else @bamboo_port = port end @retries = 10 @user = [:admin_username] @password = [:admin_password] @timeout = [:connection_timeout] @open_timeout = [:connection_open_timeout] end |
Instance Attribute Details
#bamboo_host ⇒ Object (readonly)
Returns the value of attribute bamboo_host.
13 14 15 |
# File 'lib/puppet_x/bamboo/rest.rb', line 13 def bamboo_host @bamboo_host end |
#bamboo_port ⇒ Object (readonly)
Returns the value of attribute bamboo_port.
14 15 16 |
# File 'lib/puppet_x/bamboo/rest.rb', line 14 def bamboo_port @bamboo_port end |
Class Method Details
.create_bamboo_resource(resource_name, config) ⇒ Object
Create a bamboo resource via REST end point Make sure bamboo is running before actually invoking the endpoint
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/puppet_x/bamboo/rest.rb', line 129 def self.create_bamboo_resource(resource_name, config) service.ensure_running with_client do |client| response = client.make_request(resource_name, 'post') do |req| req['Content-Type'] = 'application/json' req['Accept'] = 'application/json' req.body = JSON.generate(config) end if !response.is_a?(Net::HTTPSuccess) Bamboo::ExceptionHandler.process(response) { |msg| raise "Could not create #{resource_name} at #{client.bamboo_host}: #{msg}" } end end end |
.delete_bamboo_resource(resource_name) ⇒ Object
Delete bamboo configurations via REST end point Make sure bamboo is running before actually invoking the endpoint
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/puppet_x/bamboo/rest.rb', line 147 def self.delete_bamboo_resource(resource_name) service.ensure_running with_client { |client| response = client.make_request(resource_name, 'delete') { |req| req['Accept'] = 'application/json' } if response.is_a?(Net::HTTPNotFound) # Do nothing here, the resource has already been deleted elsif response.is_a?(Net::HTTPSuccess) # Good response else Bamboo::ExceptionHandler.process(response) { |msg| raise "Could not delete #{resource_name} from #{client.bamboo_host}: #{msg}" } end } end |
.get_bamboo_settings(resource_name, content_type = 'application/json') ⇒ Object
Retrieve configurations of bamboo via REST endpoint Make sure bamboo is running before actually invoking the endpoint
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/puppet_x/bamboo/rest.rb', line 84 def self.get_bamboo_settings(resource_name, content_type='application/json') service.ensure_running with_client do |client| response = client.make_request(resource_name, 'get') do |req| req['Accept'] = content_type end if response.is_a?(Net::HTTPSuccess) begin JSON.parse(response.body) rescue => e raise "Could not parse the JSON response from Bamboo (url: #{client.bamboo_host}, resource: #{resource_name}): #{e} (response: #{response})" end else yield response if block_given? Bamboo::ExceptionHandler.process(e) { |msg| raise "Could not get #{resource_name}, #{e} " } end end end |
.init_service ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/puppet_x/bamboo/rest.rb', line 74 def self.init_service Bamboo::Config.configure { |bamboo_base_url, | client = Bamboo::Service::HealthCheckClient.new() service = Bamboo::Service.new(client, ) Bamboo::CachingService.new(service) } end |
.service ⇒ Object
70 71 72 |
# File 'lib/puppet_x/bamboo/rest.rb', line 70 def self.service @service = init_service end |
.update_bamboo_settings(resource_name, config, method = 'put', content_type = 'application/json') ⇒ Object
Update bamboo configurations via REST end point Make sure bamboo is running before actually invoking the endpoint
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/puppet_x/bamboo/rest.rb', line 108 def self.update_bamboo_settings(resource_name, config, method = 'put', content_type='application/json') if method != 'put' && method != 'post' raise 'Invalid method given' end service.ensure_running with_client do |client| response = client.make_request(resource_name, method) do |req| req['Content-Type'] = content_type req['Accept'] = content_type req.body = JSON.generate(config) end if !response.is_a? Net::HTTPSuccess Bamboo::ExceptionHandler.process(response) do |msg| raise "Could not update #{resource_name} at #{client.bamboo_host}: #{msg}" end end end end |
Instance Method Details
#make_request(path, method) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/puppet_x/bamboo/rest.rb', line 43 def make_request(path, method) Net::HTTP.start(@bamboo_host, @bamboo_port) do |http| case method when 'get' req = Net::HTTP::Get.new(path) when 'put' req = Net::HTTP::Put.new(path) when 'post' req = Net::HTTP::Post.new(path) when 'delete' req = Net::HTTP::Delete.new(path) else raise "Unsupported HTTP method: #{method} for path #{path}" end req.basic_auth @user, @password http.read_timeout=@timeout http.open_timeout=@open_timeout # Required to avoid XSS alerts req['X-Atlassian-Token'] = 'no-check' # Fill the hole yield req http.request req end end |