Module: Grafana::Organization
- Included in:
- Client
- Defined in:
- lib/grafana/organization.rb
Overview
Instance Method Summary collapse
-
#add_user_to_current_organization(params) ⇒ Hash
Add a new user to the actual organisation.
-
#current_organization ⇒ Hash
Get current Organisation.
-
#current_organization_users ⇒ Hash
Get all users within the actual organisation.
-
#update_current_organization(params) ⇒ Hash
Update current Organisation.
Instance Method Details
#add_user_to_current_organization(params) ⇒ Hash
Add a new user to the actual organisation
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/grafana/organization.rb', line 78 def add_user_to_current_organization( params ) raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) ) raise ArgumentError.new('missing \'params\'') if( params.size.zero? ) login_or_email = validate( params, required: true, var: 'login_or_email', type: String ) role = validate( params, required: true, var: 'role', type: String ) valid_roles = ['Viewer', 'Editor', 'Read Only Editor', 'Admin'] # https://stackoverflow.com/questions/9333952/case-insensitive-arrayinclude?answertab=votes#tab-top # Do this once, or each time the array changes downcased = Set.new valid_roles.map(&:downcase) unless( downcased.include?( role.downcase ) ) return { 'status' => 404, 'login_or_email' => login_or_email, 'role' => role, 'message' => format( 'wrong role. Role must be one of %s, given \'%s\'', valid_roles.join(', '), role ) } end org = current_organization_users usr = user( login_or_email ) return { 'status' => 404, 'message' => format('User \'%s\' not found', login_or_email) } if( usr.nil? || usr.dig('status').to_i != 200 ) if( org.is_a?(Hash) && org.dig('status').to_i == 200 ) org = org.dig('message') return { 'status' => 404, 'message' => format('User \'%s\' are already in the organisation', login_or_email) } if( org.select { |x| x.dig('email') == login_or_email || x.dig('login') == login_or_email }.count >= 1 ) end endpoint = '/api/org/users' payload = { loginOrEmail: login_or_email, role: role } @logger.debug("Adding user to current organization (POST #{endpoint})") if @debug post(endpoint, payload.to_json) end |
#current_organization ⇒ Hash
Get current Organisation
15 16 17 18 19 |
# File 'lib/grafana/organization.rb', line 15 def current_organization endpoint = '/api/org' @logger.debug("Get current Organisation (GET #{endpoint})") if @debug get(endpoint) end |
#current_organization_users ⇒ Hash
Get all users within the actual organisation
57 58 59 60 61 |
# File 'lib/grafana/organization.rb', line 57 def current_organization_users endpoint = '/api/org/users' @logger.debug("Getting organization users (GET #{endpoint})") if @debug get(endpoint) end |
#update_current_organization(params) ⇒ Hash
Update current Organisation
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/grafana/organization.rb', line 34 def update_current_organization( params ) raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) ) raise ArgumentError.new('missing \'params\'') if( params.size.zero? ) name = validate( params, required: true, var: 'name', type: String ) endpoint = '/api/org' payload = { name: name } @logger.debug("Updating current organization (PUT #{endpoint})") if @debug put(endpoint, payload.to_json) end |