Managing loaner chromebooks for students and teachers in the HUUSD school district.
at main 113 lines 3.7 kB view raw
1# API Endpoint docs that might be useful: 2# 3# - https://developers.google.com/admin-sdk/directory/reference/rest/v1/chromeosdevices/update 4# - https://developers.google.com/admin-sdk/directory/v1/guides/manage-chrome-devices 5# - https://developers.google.com/admin-sdk/directory/reference/rest/v1/customer.devices.chromeos/batchChangeStatus 6# 7 8 9class GoogleService 10 include Singleton 11 12 OOB_URI = "urn:ietf:wg:oauth:2.0:oob" 13 SCOPE = [ 14 Google::Apis::AdminDirectoryV1::AUTH_ADMIN_DIRECTORY_DEVICE_CHROMEOS, 15 ].freeze 16 17 # this is a hack to work with the google library's requirement that tokens must be in files 18 TOKEN_FILE = Tempfile.new("token") 19 TOKEN_FILE << Rails.application.credentials.dig(:gsuite, :token) 20 TOKEN_FILE.rewind 21 TOKEN_FILE.close 22 23 def authorize 24 credentials = authorizer.get_credentials user_id 25 if credentials.nil? 26 url = authorizer.get_authorization_url base_url: OOB_URI 27 puts "Open the following URL in the browser and enter the " \ 28 "resulting code after authorization:\n" + url 29 code = gets 30 credentials = authorizer.get_and_store_credentials_from_code( 31 user_id:, code:, base_url: OOB_URI 32 ) 33 end 34 credentials 35 end 36 37 def client 38 service = Google::Apis::AdminDirectoryV1::DirectoryService.new 39 service.client_options.application_name = "IT Tool" 40 service.client_options.log_http_requests = false 41 service.authorization = authorize # calling the above method for authorization 42 43 service 44 end 45 46 def list_chrome_devices 47 client.list_chromeosdevices(customer: "my_customer") 48 49 # This will return a list of ChromeOS devices 50 # https://developers.google.com/admin-sdk/directory/v1/reference/chromeosdevices/list 51 end 52 53 def get_chrome_device(serialNumber) 54 client.get_chromeosdevice(customer: "my_customer", serialNumber: serialNumber) 55 56 # This will return a single ChromeOS device 57 # https://developers.google.com/admin-sdk/directory/v1/reference/chromeosdevices/get 58 end 59 60 def update_chrome_device(serialNumber, device) 61 client.update_chromeosdevice(customer: "my_customer", serialNumber: serialNumber, chrome_os_device_object: device) 62 63 # This will update a ChromeOS device 64 # https://developers.google.com/admin-sdk/directory/v1/reference/chromeosdevices/update 65 end 66 67 def disable_chrome_device(serialNumber) 68 client.update_chromeosdevice(customer: "my_customer", serialNumber: serialNumber, chrome_os_device_object: {status: "DISABLED"}) 69 70 # This will disable a ChromeOS device 71 # https://developers.google.com/admin-sdk/directory/v1/reference/chromeosdevices/update 72 end 73 74 def enable_chrome_device(serialNumber) 75 client.update_chromeosdevice(customer: "my_customer", serialNumber: serialNumber, chrome_os_device_object: {status: "ACTIVE"}) 76 77 # This will enable a ChromeOS device 78 # https://developers.google.com/admin-sdk/directory/v1/reference/chromeosdevices/update 79 end 80 81def toggle_chrome_device_status(serialNumber) 82 device = get_chrome_device(serialNumber) 83 if device.status == "ACTIVE" 84 disable_chrome_device(serialNumber) 85 else 86 enable_chrome_device(serialNumber) 87 end 88end 89 90def device_status(serialNumber) 91 device = get_chrome_device(serialNumber) 92 device.status 93end 94 95 private 96 97 def client_id 98 @client_id ||= Google::Auth::ClientId.from_hash JSON.parse(Rails.application.credentials.gsuite[:client_id_json]) 99 end 100 101 def token_store 102 @token_store ||= Google::Auth::Stores::FileTokenStore.new file: TOKEN_FILE.path 103 end 104 105 def authorizer 106 @authorizer ||= Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store 107 end 108 109 def user_id 110 "default" 111 end 112 113end