Class: Wavefront::User

Inherits:
CoreApi show all
Includes:
Mixin::User
Defined in:
lib/wavefront-sdk/user.rb

Overview

Manage and query Wavefront users

Instance Attribute Summary

Attributes inherited from CoreApi

#api, #creds, #logger, #opts

Instance Method Summary collapse

Methods included from Mixin::User

#validate_account_list, #validate_user_list, #validate_usergroup_list

Methods inherited from CoreApi

#api_base, #api_path, #hash_for_update, #initialize, #setup_api, #time_to_ms

Methods included from Mixins

#log, #parse_relative_time, #parse_time, #relative_time, #time_multiplier, #valid_relative_time?

Methods included from Validators

#uuid?, #wf_account_id?, #wf_alert_id?, #wf_alert_severity?, #wf_apitoken_id?, #wf_cloudintegration_id?, #wf_dashboard_id?, #wf_derivedmetric_id?, #wf_distribution?, #wf_distribution_count?, #wf_distribution_interval?, #wf_distribution_values?, #wf_epoch?, #wf_event_id?, #wf_granularity?, #wf_ingestionpolicy_id?, #wf_integration_id?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_monitoredcluster_id?, #wf_ms_ts?, #wf_name?, #wf_notificant_id?, #wf_permission?, #wf_point?, #wf_point_tag?, #wf_point_tags?, #wf_proxy_id?, #wf_sampling_value?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_serviceaccount_id?, #wf_source_id?, #wf_string?, #wf_tag?, #wf_ts?, #wf_user_id?, #wf_usergroup_id?, #wf_value?, #wf_version?, #wf_webhook_id?

Constructor Details

This class inherits a constructor from Wavefront::CoreApi

Instance Method Details

#add_groups_to_user(id, group_list = []) ⇒ Wavefront::Response

POST /api/v2/user/id/addUserGroups Adds specific user groups to the user

Parameters:

  • id (String)

    ID of the user

  • group_list (Array[String]) (defaults to: [])

    list of groups to add

Returns:



84
85
86
87
88
89
# File 'lib/wavefront-sdk/user.rb', line 84

def add_groups_to_user(id, group_list = [])
  wf_user_id?(id)
  validate_usergroup_list(group_list)
  api.post([id, 'addUserGroups'].uri_concat, group_list,
           'application/json')
end

#business_functions(id) ⇒ Wavefront::Response

GET /api/v2/user/id/businessFunctions Returns business functions of a specific user.

Parameters:

Returns:



210
211
212
213
# File 'lib/wavefront-sdk/user.rb', line 210

def business_functions(id)
  wf_user_id?(id)
  api.get([id, 'businessFunctions'].uri_concat)
end

#construct_response(body_obj, status) ⇒ Object

Construct a response almost from scratch. Used for ‘list’, among others.



251
252
253
254
255
256
257
258
259
260
# File 'lib/wavefront-sdk/user.rb', line 251

def construct_response(body_obj, status)
  { status: { result: status.to_s.start_with?('2') ? 'OK' : 'ERROR',
              message: extract_api_message(status, body_obj),
              code: status },
    response: { items: [body_obj].flatten,
                offset: 0,
                limit: body_obj.size,
                totalItems: body_obj.size,
                moreItems: false } }.to_json
end

#create(body, send_email = false) ⇒ Wavefront::Response

POST /api/v2/user Creates or updates a user

Parameters:

  • body (Hash)

    a hash of parameters describing the user. Please refer to the Wavefront Swagger docs for key:value information

Returns:

Raises:

  • (ArgumentError)


28
29
30
31
32
# File 'lib/wavefront-sdk/user.rb', line 28

def create(body, send_email = false)
  raise ArgumentError unless body.is_a?(Hash)

  api.post("?sendEmail=#{send_email}", body, 'application/json')
end

#delete(id) ⇒ Wavefront::Response

DELETE /api/v2/user/id Delete a specific user. See also #delete_users.

Parameters:

  • id (String)

    ID of the user

Returns:



40
41
42
43
# File 'lib/wavefront-sdk/user.rb', line 40

def delete(id)
  wf_user_id?(id)
  api.delete(id)
end

#delete_users(user_list) ⇒ Wavefront::Response

POST /api/v2/user/deleteUsers Deletes multiple users

Yep, a POST that DELETEs. Not to be confused with DELETE. I don’t make the API, I just cover it.

Parameters:

Returns:

Raises:

  • (ArgumentError)


151
152
153
154
155
156
# File 'lib/wavefront-sdk/user.rb', line 151

def delete_users(user_list)
  raise ArgumentError unless user_list.is_a?(Array)

  validate_user_list(user_list)
  api.post('deleteUsers', user_list, 'application/json')
end

#describe(id) ⇒ Wavefront::Response

GET /api/v2/user/id Retrieves a user by identifier (email addr).

Parameters:

  • id (String)

    ID of the user

Returns:



51
52
53
54
# File 'lib/wavefront-sdk/user.rb', line 51

def describe(id)
  wf_user_id?(id)
  api.get(id)
end

#everythingObject

the user API class does not support pagination. Be up-front about that.

Raises:

  • (NoMethodError)


265
266
267
# File 'lib/wavefront-sdk/user.rb', line 265

def everything
  raise NoMethodError
end

#grant(id, pgroup) ⇒ Wavefront::Response

PUT /api/v2/user/id/grant Grants a specific user permission.

Parameters:

  • id (String)

    ID of the user

  • pgroup (String)

    permission group to grant to user. We do not validate this so that changes to the API do not mandate changes to the SDK. At the time of writing, valid values are browse, agent_management, alerts_management, dashboard_management, embedded_charts, events_management, external_links_management, host_tag_management, metrics_management, user_management,

Returns:

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
# File 'lib/wavefront-sdk/user.rb', line 118

def grant(id, pgroup)
  wf_user_id?(id)
  raise ArgumentError unless pgroup.is_a?(String)

  api.post([id, 'grant'].uri_concat, "group=#{pgroup}",
           'application/x-www-form-urlencoded')
end

#grant_permission(permission, user_list) ⇒ Wavefront::Response

POST /api/v2/user/grant/permission Grants a specific user permission to multiple users See #grant for possible permissions. This method operates on multiple users.

Parameters:

  • permission (String)

    permission to grant

  • user_list (Array[String])

    users who should receive the permission

Returns:

Raises:

  • (ArgumentError)


167
168
169
170
171
172
173
# File 'lib/wavefront-sdk/user.rb', line 167

def grant_permission(permission, user_list)
  raise ArgumentError unless user_list.is_a?(Array)

  validate_user_list(user_list)
  api.post(['grant', permission].uri_concat, user_list,
           'application/json')
end

#invite(body) ⇒ Wavefront::Response

POST /api/v2/user/invite Invite users with given user groups and permissions.

Parameters:

  • body (Array[Hash])

    array of hashes describing a user. See API docs for more details.

Returns:

Raises:

  • (ArgumentError)


198
199
200
201
202
203
# File 'lib/wavefront-sdk/user.rb', line 198

def invite(body)
  raise ArgumentError unless body.is_a?(Array)
  raise ArgumentError unless body.first.is_a?(Hash)

  api.post('invite', body, 'application/json')
end

#itemize_response(body_obj) ⇒ Object



244
245
246
247
# File 'lib/wavefront-sdk/user.rb', line 244

def itemize_response(body_obj)
  { status: body_obj[:status],
    response: { items: [body_obj[:response]].flatten } }.to_json
end

#listObject

GET /api/v2/user Get all users.



16
17
18
# File 'lib/wavefront-sdk/user.rb', line 16

def list
  api.get('')
end

#remove_groups_from_user(id, group_list = []) ⇒ Wavefront::Response

POST /api/v2/user/id/removeUserGroups Removes specific user groups from the user

Parameters:

  • id (String)

    ID of the user

  • group_list (Array[String]) (defaults to: [])

    list of groups to remove

Returns:



97
98
99
100
101
102
# File 'lib/wavefront-sdk/user.rb', line 97

def remove_groups_from_user(id, group_list = [])
  wf_user_id?(id)
  validate_usergroup_list(group_list)
  api.post([id, 'removeUserGroups'].uri_concat, group_list,
           'application/json')
end

#response_shim(body, status) ⇒ String

Fake a response which looks like we get from all the other paths. I’m expecting the user response model to be made consistent with others in the future.

Returns:



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/wavefront-sdk/user.rb', line 230

def response_shim(body, status)
  body_obj = JSON.parse(body, symbolize_names: true)

  if body_obj.is_a?(Hash) && body_obj.key?(:status)
    if response_looks_right?(body_obj)
      body
    else
      itemize_response(body_obj)
    end
  else
    construct_response(body_obj, status)
  end
end

#revoke(id, pgroup) ⇒ Wavefront::Response

PUT /api/v2/user/id/revoke Revokes a specific user permission.

Parameters:

  • id (String)

    ID of the user

  • pgroup (String)

    permission group to revoke from the user. We do not validate this so that changes to the API do not mandate changes to the SDK. See #update for valid values.

Returns:

Raises:

  • (ArgumentError)


135
136
137
138
139
140
141
# File 'lib/wavefront-sdk/user.rb', line 135

def revoke(id, pgroup)
  wf_user_id?(id)
  raise ArgumentError unless pgroup.is_a?(String)

  api.post([id, 'revoke'].uri_concat, "group=#{pgroup}",
           'application/x-www-form-urlencoded')
end

#revoke_permission(permission, user_list) ⇒ Wavefront::Response

POST /api/v2/user/revoke/permission Revokes a specific user permission from multiple users See #grant for possible permissions. This method operates on multiple users.

Parameters:

  • permission (String)

    permission to revoke

  • user_list (Array[String])

    users who should lose the permission

Returns:

Raises:

  • (ArgumentError)


184
185
186
187
188
189
190
# File 'lib/wavefront-sdk/user.rb', line 184

def revoke_permission(permission, user_list)
  raise ArgumentError unless user_list.is_a?(Array)

  validate_user_list(user_list)
  api.post(['revoke', permission].uri_concat, user_list,
           'application/json')
end

#update(id, body, modify = true) ⇒ Wavefront::Response

PUT /api/v2/user/id Update a specific user definition.

Parameters:

  • id (String)

    a Wavefront user ID

  • body (Hash)

    key-value hash of the parameters you wish to change

  • modify (true, false) (defaults to: true)

    if true, use #describe() to get a hash describing the existing object, and modify that with the new body. If false, pass the new body straight through.

Returns:

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
# File 'lib/wavefront-sdk/user.rb', line 67

def update(id, body, modify = true)
  wf_user_id?(id)
  raise ArgumentError unless body.is_a?(Hash)

  return api.put(id, body, 'application/json') unless modify

  api.put(id, hash_for_update(describe(id).response, body),
          'application/json')
end

#validate_users(id_list) ⇒ Wavefront::Response

POST /api/v2/user/validateUsers Returns valid users and service accounts, also invalid identifiers from the given list

Parameters:

Returns:



221
222
223
# File 'lib/wavefront-sdk/user.rb', line 221

def validate_users(id_list)
  api.post('validateUsers', id_list, 'application/json')
end