Module: Grafana::Organizations
- Included in:
- Client
- Defined in:
- lib/grafana/organizations.rb
Overview
Instance Method Summary collapse
-
#add_user_to_organization(params) ⇒ Hash
Add User in Organisation.
-
#create_organisation(params) ⇒ Hash
Create Organisation.
-
#delete_organisation(organisation_id) ⇒ Hash
Delete Organisation.
-
#delete_user_from_organization(params) ⇒ Hash
Delete User in Organisation.
-
#organization(organisation_id) ⇒ Hash
Get a single data sources by Id or Name.
-
#organization_users(organization_id) ⇒ Hash
Get Users in Organisation.
-
#organizations ⇒ Object
Search all Organisations GET /api/orgs.
-
#update_organization(params) ⇒ Hash
Update Organisation.
-
#update_organization_user(params) ⇒ Hash
Update Users in Organisation.
Instance Method Details
#add_user_to_organization(params) ⇒ Hash
Add User in Organisation
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/grafana/organizations.rb', line 125 def add_user_to_organization( params ) data = validate_organisation_user( params ) status = data.dig('status') return data if( status.nil? || status.to_i == 404 ) org = data.dig('organisation') usr = data.dig('user') organization_id = org.dig('id') organization = org.dig('name') login_or_email = usr.dig('email') role = data.dig('role') endpoint = format( '/api/orgs/%d/users', organization_id ) payload = { loginOrEmail: login_or_email, role: role } @logger.debug("Adding user '#{login_or_email}' to organisation '#{organization}' (POST #{endpoint})") if @debug post( endpoint, payload.to_json ) end |
#create_organisation(params) ⇒ Hash
Create Organisation
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/grafana/organizations.rb', line 244 def create_organisation( 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 ) org = organization( name ) return { 'status' => 409, 'message' => format('Organisation \'%s\' already exists', name ) } if( org.nil? || org.dig('status').to_i == 200 ) endpoint = '/api/orgs' payload = { name: name } @logger.debug("Create Organisation (POST #{endpoint})") if @debug post( endpoint, payload.to_json ) end |
#delete_organisation(organisation_id) ⇒ Hash
Delete Organisation
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/grafana/organizations.rb', line 273 def delete_organisation( organisation_id ) if( organisation_id.is_a?(String) && organisation_id.is_a?(Integer) ) raise ArgumentError.new(format('wrong type. \'organisation_id\' must be an String (for an Organisation name) ' \ 'or an Integer (for an Organisation Id), given \'%s\'', organisation_id.class.to_s)) end raise ArgumentError.new('missing \'organisation_id\'') if( organisation_id.size.zero? ) if(organisation_id.is_a?(String)) data = organizations.dig('message') organisation_map = {} data.each do |d| organisation_map[d.dig('id')] = d.dig('name') end organisation_id = organisation_map.select { |_,y| y == organisation_id }.keys.first if( organisation_map ) end return { 'status' => 404, 'message' => format( 'No Organisation \'%s\' found', organisation_id) } if( organisation_id.nil? ) endpoint = format( '/api/orgs/%d', organisation_id ) @logger.debug("Deleting organization #{organisation_id} (DELETE #{endpoint})") if @debug delete(endpoint) end |
#delete_user_from_organization(params) ⇒ Hash
Delete User in Organisation
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/grafana/organizations.rb', line 208 def delete_user_from_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? ) organization = validate( params, required: true, var: 'organization', type: String ) login_or_email = validate( params, required: true, var: 'login_or_email', type: String ) org = organization( organization ) usr = user( login_or_email ) return { 'status' => 404, 'message' => format('Organization \'%s\' not found', organization) } if( org.nil? || org.dig('status').to_i != 200 ) return { 'status' => 404, 'message' => format('User \'%s\' not found', login_or_email) } if( usr.nil? || usr.dig('status').to_i != 200 ) organization_id = org.dig('id') usr_id = usr.dig('id') endpoint = format( '/api/orgs/%d/users/%d', organization_id, usr_id ) @logger.debug("Deleting user '#{login_or_email}' in organization '#{organization}' (DELETE #{endpoint})") if @debug delete(endpoint) end |
#organization(organisation_id) ⇒ Hash
Get a single data sources by Id or Name
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/grafana/organizations.rb', line 24 def organization( organisation_id ) if( organisation_id.is_a?(String) && organisation_id.is_a?(Integer)) raise ArgumentError.new(format('wrong type. \'organisation_id\' must be an String (for an Datasource name) ' \ 'or an Integer (for an Datasource Id), given \'%s\'', organisation_id.class.to_s)) end raise ArgumentError.new('missing \'organisation_id\'') if( organisation_id.size.zero? ) endpoint = format( '/api/orgs/%d', organisation_id ) if(organisation_id.is_a?(Integer)) endpoint = format( '/api/orgs/name/%s', URI.escape( organisation_id ) ) if(organisation_id.is_a?(String)) @logger.debug("Attempting to get existing data source Id #{organisation_id} (GET #{endpoint})") if @debug get(endpoint) end |
#organization_users(organization_id) ⇒ Hash
Get Users in Organisation
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/grafana/organizations.rb', line 87 def organization_users( organization_id ) if( organization_id.is_a?(String) && organization_id.is_a?(Integer)) raise ArgumentError.new(format('wrong type. \'organization_id\' must be an String (for an Organisation name) '\ 'or an Integer (for an Organisation Id), given \'%s\'', organization_id.class.to_s)) end raise ArgumentError.new('missing \'organization_id\'') if( organization_id.size.zero? ) if(organization_id.is_a?(String)) org = organization(organization_id) return { 'status' => 404, 'message' => format('Organization \'%s\' not found', organization) } if( org.nil? || org.dig('status').to_i != 200 ) organization_id = org.dig('id') end endpoint = format( '/api/orgs/%s/users', organization_id ) @logger.debug("Getting users in Organisation id #{organization_id} (GET #{endpoint})") if @debug get(endpoint) end |
#organizations ⇒ Object
Search all Organisations GET /api/orgs
10 11 12 13 14 |
# File 'lib/grafana/organizations.rb', line 10 def organizations endpoint = '/api/orgs' @logger.debug("Getting all organizations (GET #{endpoint})") if @debug get( endpoint ) end |
#update_organization(params) ⇒ Hash
Update Organisation
fields Adress 1, Adress 2, City are not implemented yet.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/grafana/organizations.rb', line 56 def update_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? ) organization = validate( params, required: true, var: 'organization', type: String ) name = validate( params, required: true, var: 'name', type: String ) org = organization( organization ) return { 'status' => 404, 'message' => format('Organization \'%s\' not found', organization) } if( org.nil? || org.dig('status').to_i != 200 ) organization_id = org.dig('id') endpoint = format( '/api/orgs/%s', organization_id ) payload = { name: name } @logger.debug("Update Organisation id #{organization_id} (PUT #{endpoint})") if @debug put( endpoint, payload.to_json ) end |
#update_organization_user(params) ⇒ Hash
Update Users in Organisation
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/grafana/organizations.rb', line 168 def update_organization_user( params ) data = validate_organisation_user( params ) status = data.dig('status') return data if( status.nil? || status.to_i == 404 ) org = data.dig('organisation') usr = data.dig('user') organization_id = org.dig('id') organization = org.dig('name') usr_id = usr.dig('id') login_or_email = usr.dig('name') role = data.dig(:role) endpoint = format( '/api/orgs/%d/users/%d', organization_id, usr_id ) payload = { role: role } @logger.debug("Updating user '#{login_or_email}' in organization '#{organization}' (PATCH #{endpoint})") if @debug patch( endpoint, payload.to_json ) end |