Class: Maze::Client::BitBarApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/client/bb_api_client.rb

Constant Summary collapse

BASE_URI =
'https://cloud.bitbar.com/api'
USER_SPECIFIC_URI =
"#{BASE_URI}/v2/me"

Instance Method Summary collapse

Constructor Details

#initialize(access_key) ⇒ BitBarApiClient

Returns a new instance of BitBarApiClient.



8
9
10
# File 'lib/maze/client/bb_api_client.rb', line 8

def initialize(access_key)
  @access_key = access_key
end

Instance Method Details

#delete_project(id) ⇒ Object



112
113
114
# File 'lib/maze/client/bb_api_client.rb', line 112

def delete_project (id)
  response = query_api "projects/#{id}", nil, USER_SPECIFIC_URI, "delete"
end

#find_device(device_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/maze/client/bb_api_client.rb', line 58

def find_device(device_name)
  path = "devices"
  query = {
    'filter': "displayName_eq_#{device_name};online_eq_true",
  }
  all_devices = query_api(path, query, BASE_URI)['data']
  if all_devices.size == 0
    Maze::Helper.error_exit "No devices found with name '#{device_name}'"
  else
    $logger.trace "All available devices with name #{device_name}: #{JSON.pretty_generate(all_devices)}"
    filtered_devices = all_devices.reject { |device| device['locked'] }
    if filtered_devices.size == 0
      Maze::Helper.error_exit "None of the #{all_devices.size} devices with name '#{device_name}' are currently available"
    else
      $logger.info "#{filtered_devices.size} of #{all_devices.size} devices with name '#{device_name}' are available"
    end

    return filtered_devices.sample
  end
end

#find_device_in_groups(device_group_ids) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/maze/client/bb_api_client.rb', line 37

def find_device_in_groups(device_group_ids)
  all_devices = []
  device_group_ids.each do |group_id|
    path = "device-groups/#{group_id}/devices"
    query = {
      'filter': "online_eq_true"
    }
    all_devices += query_api(path, query)['data']
  end

  $logger.trace "All available devices in group(s) #{device_group_ids}: #{JSON.pretty_generate(all_devices)}"
  filtered_devices = all_devices.reject { |device| device['locked'] }

  # Only send gauges to DataDog for single device groups
  if device_group_ids.size == 1
    Maze::Plugins::DatadogMetricsPlugin.send_gauge('bitbar.device.available', all_devices.size, [Maze.config.device])
    Maze::Plugins::DatadogMetricsPlugin.send_gauge('bitbar.device.unlocked', filtered_devices.size, [Maze.config.device])
  end
  return filtered_devices.size, filtered_devices.sample
end

#get_device_group_ids(device_group_names) ⇒ Object

Get the id(s) of a one or more device groups given their names. Multiple device group names should be separated by a pipe (which is directly supported by the BitBar API)



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/maze/client/bb_api_client.rb', line 25

def get_device_group_ids(device_group_names)
  query = {
    'filter': "displayName_in_#{device_group_names}"
  }
  device_groups = query_api('device-groups', query)
  if device_groups['data'].nil? || device_groups['data'].size == 0
    nil
  else
    device_groups['data'].map { |group| group['id'] }
  end
end

#get_device_group_listObject

Get a list of all available device groups



13
14
15
# File 'lib/maze/client/bb_api_client.rb', line 13

def get_device_group_list
  query_api('device-groups')
end

#get_device_list_for_group(device_group_id) ⇒ Object

Get a list of all available devices in a device group



18
19
20
21
# File 'lib/maze/client/bb_api_client.rb', line 18

def get_device_list_for_group(device_group_id)
  path = "device-groups/#{device_group_id}/devices"
  query_api(path)
end


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/maze/client/bb_api_client.rb', line 79

def get_device_session_ui_link(session_id)
  query = {
    'filter': "clientSideId_eq_#{session_id}"
  }
  data = query_api('device-sessions', query)['data']
  if data.size == 1
    data[0]['uiLink']
  else
    $logger.warn "Failed to get UI link for session #{session_id}.  Expected exactly 1 device-session, found #{data.size}"
  end
end

#get_projects(pattern) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/maze/client/bb_api_client.rb', line 91

def get_projects(pattern)
  query = {
    'limit': "1000"
  }

  project_data = query_api('projects', query)

  begin
    projects = Hash.new
    project_data['data'].each do |child|
      if child['name'].match(pattern)
        projects[child['name']] = child['id']
      end
    end
  rescue StandardError => e
    $logger.error "Error getting projects from BitBar: #{e}"
    raise
  end
  projects
end