Module: Grafana::Organizations

Included in:
Client
Defined in:
lib/grafana/organizations.rb

Overview

Instance Method Summary collapse

Instance Method Details

#add_user_to_organization(params) ⇒ Hash

Add User in Organisation

Examples:

params = {
  organization: 'Foo',
  login_or_email: '[email protected]',
  role: 'Viewer'
}
add_user_to_organization( params )

Parameters:

Options Hash (params):

  • organization (String)

    Organisation name

  • login_or_email (String)

    Login or email

  • role (String)

    Name of the Role - only ‘Viewer’, ‘Editor’, ‘Read Only Editor’ or ‘Admin’ allowed

Returns:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/grafana/organizations.rb', line 119

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')
   = usr.dig('email')
  role = data.dig('role')

  endpoint = format( '/api/orgs/%d/users', organization_id )
  payload = {
    loginOrEmail: ,
    role: role
  }

  @logger.debug("Adding user '#{}' to organisation '#{organization}' (POST #{endpoint})") if @debug

  post( endpoint, payload.to_json )
end

#create_organisation(params) ⇒ Hash

Create Organisation

Examples:

params = {
  name: 'Foo'
}
create_organisation( params )

Parameters:

Options Hash (params):

  • organization (String)

    Organisation name

Returns:

Raises:

  • (ArgumentError)


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/grafana/organizations.rb', line 238

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

Examples:

delete_organisation( 1 )
delete_organisation( 'Foo' )

Parameters:

  • organisation_id (Mixed)

    Organisation Name (String) or Organisation Id (Integer) for delete Organisation

Returns:

Raises:

  • (ArgumentError)


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/grafana/organizations.rb', line 267

def delete_organisation( organisation_id )

  raise ArgumentError.new(format('wrong type. user \'organisation_id\' must be an String (for an Datasource name) or an Integer (for an Datasource Id), given \'%s\'', organisation_id.class.to_s)) if( organisation_id.is_a?(String) && organisation_id.is_a?(Integer) )
  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 { |x,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

Examples:

params = {
  organization: 'Foo',
  login_or_email: '[email protected]'
}
delete_user_from_organization( params )

Parameters:

Options Hash (params):

  • organization (String)

    Organisation name

  • login_or_email (String)

    Login or email

Returns:

Raises:

  • (ArgumentError)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/grafana/organizations.rb', line 202

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 )
   = validate( params, required: true, var: 'login_or_email', type: String )

  org = organization( organization )
  usr = user(  )

  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', ) } 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 '#{}' in organization '#{organization}' (DELETE #{endpoint})") if @debug
  delete(endpoint)
end

#organization(organisation_id) ⇒ Hash

Get a single data sources by Id or Name

Examples:

organisation( 1 )
organisation( 'foo' )

Returns:

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/grafana/organizations.rb', line 24

def organization( organisation_id )

  raise ArgumentError.new(format('wrong type. user \'organisation_id\' must be an String (for an Datasource name) or an Integer (for an Datasource Id), given \'%s\'', organisation_id.class.to_s)) if( organisation_id.is_a?(String) && organisation_id.is_a?(Integer) )
  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

Examples:

organization_users( 1 )
organization_users( 'Foo Bar' )

Parameters:

  • organization_id (Mixed)

    Organistaion Name (String) or Organistaion Id (Integer)

Returns:

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/grafana/organizations.rb', line 84

def organization_users( organization_id )

  raise ArgumentError.new(format('wrong type. user \'organization_id\' must be an String (for an Organisation name) or an Integer (for an Organisation Id), given \'%s\'', organization_id.class.to_s)) if( organization_id.is_a?(String) && organization_id.is_a?(Integer) )
  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

#organizationsObject

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.

Examples:

update_organization( organization: 'Main. Org', name: 'Foo+Bar' )

Parameters:

Options Hash (params):

  • organization (String)

    name of the Organisation

  • name (String)

    new name

  • adress_1 (String)
  • adress_2 (String)
  • city (String)

Returns:

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/grafana/organizations.rb', line 53

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

Examples:

params = {
  organization: 'Foo',
  login_or_email: '[email protected]',
  role: 'Viewer'
}
update_organization_user( params )

Parameters:

Options Hash (params):

  • organization (String)

    Organisation name

  • login_or_email (String)

    Login or email

  • role (String)

    Name of the Role - only ‘Viewer’, ‘Editor’, ‘Read Only Editor’ or ‘Admin’ allowed

Returns:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/grafana/organizations.rb', line 162

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')
   = 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 '#{}' in organization '#{organization}' (PATCH #{endpoint})") if @debug
  patch( endpoint, payload.to_json )
end