Class: Airwatch::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(host_name, api_key, email: nil, password: nil, authorization: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • host (String)

    make sure you get this from Settings > System > Advanced > Site URLs



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/airwatch/client.rb', line 8

def initialize(host_name, api_key, email: nil, password: nil, authorization: nil)
	@host_name = host_name
	@api_key = api_key

	if email && password
		@authorization = "Basic #{Base64.strict_encode64("#{email}:#{password}")}"
	elsif authorization
		@authorization = authorization
	else
		raise 'must provide (email & password) or authorization'
	end
end

Instance Method Details

#add_smart_group_assignment_to_app(smart_group_id, app_id) ⇒ Object



146
147
148
# File 'lib/airwatch/client.rb', line 146

def add_smart_group_assignment_to_app(smart_group_id, app_id)
	post("mam/apps/internal/#{app_id}/smartgroups/#{smart_group_id}")
end

#app_devices(app_id) ⇒ Object

returns a list of device IDs that the app is installed on



34
35
36
# File 'lib/airwatch/client.rb', line 34

def app_devices(app_id)
	get("mam/apps/internal/#{app_id}/devices")
end

#apps_search(**args) ⇒ Object

Parameters:

  • (optional) (String)

    bundleid the app’s bundle identifier, or nil

  • (optional) (String)

    type ‘app’ or nill

  • (optional) (String)

    applicationtype ‘internal’ or nil



28
29
30
31
# File 'lib/airwatch/client.rb', line 28

def apps_search(**args)
	params = args.to_h
	get('mam/apps/search', params)
end

#begin_install(blob_id, app_name, org_group_id) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/airwatch/client.rb', line 71

def begin_install(blob_id, app_name, org_group_id)
	supportedmodels = {'Model' => [{'ModelId' => 1,'ModelName' => 'iPhone'},{'ModelId' => 2,'ModelName' => 'iPad'},{'ModelId' => 3,'ModelName' => 'iPod Touch'}]}
	params = { 
		'BlobId' => blob_id,
		'DeviceType' => 'Apple', # 'Apple', 
		'ApplicationName' => app_name, 
		'AppVersion' => nil, 
		'SupportedModels' => supportedmodels, 
		'LocationGroupId' => org_group_id
	}
	url = build_url('mam/apps/internal/begininstall')
	post('mam/apps/internal/begininstall', params.to_json)
end

#build_url(resource, query_params = nil) ⇒ Object



227
228
229
230
231
232
# File 'lib/airwatch/client.rb', line 227

def build_url(resource, query_params = nil)
	url = "https://#{@host_name}/api/#{resource}"
	return url if query_params.nil?
	template = Addressable::Template.new("#{url}{?query*}")
	template.expand(query: query_params).to_s
end

#device_details(device_id) ⇒ Object

Devices #



158
159
160
# File 'lib/airwatch/client.rb', line 158

def device_details(device_id)
	get("mdm/devices/#{device_id}")
end

#device_profile_details(device_id: nil, serial_number: nil) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/airwatch/client.rb', line 162

def device_profile_details(device_id: nil, serial_number: nil)
	params = {}

	unless device_id.nil?
		params[:searchby] = 'deviceid'
		params[:id] = device_id
	end

	unless serial_number.nil?
		params[:searchby] = 'serialnumber'
		params[:id] = serial_number
	end

	get('mdm/devices/profiles', params)
end

#get(resource, params = {}) ⇒ Object

HTTP request stuff #



192
193
194
195
# File 'lib/airwatch/client.rb', line 192

def get(resource, params = {})
	url = build_url(resource, params)
	HTTParty.get(url, { headers: request_headers })
end

#install_internal_app(app_id, device_id) ⇒ Object



38
39
40
# File 'lib/airwatch/client.rb', line 38

def install_internal_app(app_id, device_id)
	post("mam/apps/internal/#{app_id}/install", { deviceid: device_id })
end

#post(resource, params = {}) ⇒ Object



202
203
204
205
# File 'lib/airwatch/client.rb', line 202

def post(resource, params = {})
	url = build_url(resource)
	HTTParty.post(url, { headers: request_headers, body: params })
end

#pretty_get(resource, params = {}) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'lib/airwatch/client.rb', line 207

def pretty_get(resource, params = {})
	response = get(resource, params)
	unless response.code == 200
		puts response
		puts "⚠️  HTTP #{response.code}"
		return
	end
	puts JSON.pretty_generate(response.parsed_response)
end

#profiles_install(profile_id, device_id: nil, serial_number: nil) ⇒ Object

This method is idempotent.

Parameters:

  • profile_id (String)

    the AirWatch profile ID

  • device_id (Integer) (defaults to: nil)

    the AirWatch device ID

See Also:

  • 1034 of AirWatch REST API docs


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/airwatch/client.rb', line 98

def profiles_install(profile_id, device_id: nil, serial_number: nil)
	params = {}

	unless device_id.nil?
		params[:deviceid] = device_id
	end

	unless serial_number.nil?
		params[:serialnumber] = serial_number
	end

	post("mdm/profiles/#{profile_id}/install", params)
end

#profiles_platforms_apple_createObject

See Also:

  • 722 of AirWatch REST API docs


90
91
# File 'lib/airwatch/client.rb', line 90

def profiles_platforms_apple_create
end

#profiles_remove(profile_id, device_id) ⇒ Object

This method is NOT idempotent; after the first successful call, subsequent calls will return HTTP 400.

Parameters:

  • profile_id (String)

    the AirWatch profile ID

  • device_id (Integer)

    the AirWatch device ID

See Also:

  • 1034 of AirWatch REST API docs


117
118
119
# File 'lib/airwatch/client.rb', line 117

def profiles_remove(profile_id, device_id)
	post("mdm/profiles/#{profile_id}/remove", { deviceid: device_id })
end

#put(resource, params = {}) ⇒ Object



197
198
199
200
# File 'lib/airwatch/client.rb', line 197

def put(resource, params = {})
	url = build_url(resource)
	HTTParty.put(url, { headers: request_headers, body: params })
end

#remove_smart_group_asignment_from_app(smart_group_id, app_id) ⇒ Object



150
151
152
# File 'lib/airwatch/client.rb', line 150

def remove_smart_group_asignment_from_app(smart_group_id, app_id)
	delete("mam/apps/internal/#{app_id}/smartgroups/#{smart_group_id}")
end

#request_headersObject

private



218
219
220
221
222
223
224
225
# File 'lib/airwatch/client.rb', line 218

def request_headers
	{
		'aw-tenant-code': @api_key,
		'Accept': 'application/json',
		'Content-Type': 'application/json',
		'Authorization': @authorization
	}
end

#search_devices(serial_number: nil) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/airwatch/client.rb', line 178

def search_devices(serial_number: nil)
	params = {}

	unless serial_number.nil?
		params[:serialnumber] = serial_number
	end

	get('mdm/devices', params)
end

#smart_group_apps(smart_group_id) ⇒ Object

returns the list of apps assigned to Smart Group.

See Also:

  • 1173 of AirWatch REST API docs


132
133
134
# File 'lib/airwatch/client.rb', line 132

def smart_group_apps(smart_group_id)
	get("mdm/smartgroups/#{smart_group_id}/apps")
end

#smart_group_details(smart_group_id) ⇒ Object



136
137
138
# File 'lib/airwatch/client.rb', line 136

def smart_group_details(smart_group_id)
	get("mdm/smartgroups/#{smart_group_id}")
end

#smart_group_update(smart_group_id, device_addition_ids:) ⇒ Object



140
141
142
143
144
# File 'lib/airwatch/client.rb', line 140

def smart_group_update(smart_group_id, device_addition_ids:)
	raise 'need to assign to at least 1 device' if device_addition_ids.empty?
	device_additions = device_addition_ids.map { |id| { id: id } }
	put("mdm/smartgroups/#{smart_group_id}", { criteriatype: 'UserDevice', deviceadditions: device_additions }.to_json)
end

#smart_groups_searchObject

Smart groups #



125
126
127
# File 'lib/airwatch/client.rb', line 125

def smart_groups_search
	get('mdm/smartgroups/search')
end

#upload_blob(file_path, org_group_id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/airwatch/client.rb', line 42

def upload_blob(file_path, org_group_id)
	raise "No IPA found at path: #{file_path}" unless File.file?(file_path)
	filename = File.basename(file_path)
	url = build_url('mam/blobs/uploadblob', { filename: filename, organizationgroupid: org_group_id.to_s })
	payload = File.open(file_path, 'rb')
	headers = request_headers
	headers[:'Content-Type'] = 'application/octet-stream'
	headers[:'Expect'] = '100-continue'
	response = nil
	begin
		response = RestClient::Request.execute(
			:url => url,
			:method => :post,
			:headers => headers,
			:payload => File.open(file_path, 'rb')
		)
	rescue RestClient::BadRequest => e
		puts "Error uploading blob"
		puts "Exception message: ---------"
		puts e.message
		puts "HTTP status code: ---------"
		puts e.http_code
		puts "HTTP body: ---------"
		puts e.http_body
		return nil
	end
	JSON.parse(response)
end