I am trying to change the status of the chromeos device.
My "get" request works with getting the device ID from the serial number.
However, I am not able to figure out how to pass the payload
in golang google sdk/api, since it is a "post" request.
The payload in this case is the Action. (deprovision, disable, reenable, pre_provisioned_disable, pre_provisioned_reenable) and deprovisionReason if action is deprovision.
package main
import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
)
func readCsvFile(filePath string) [][]string {
f, err := os.Open(filePath)
if err != nil {
log.Fatal("Unable to read input file "+filePath, err)
}
defer f.Close()
csvReader := csv.NewReader(f)
records, err := csvReader.ReadAll()
if err != nil {
log.Fatal("Unable to parse file as CSV for "+filePath, err)
}
return records
}
// Retrieve a token, saves the token, then returns the generated client.
func getClient(config *oauth2.Config) *http.Client {
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
tokFile := "token.json"
tok, err := tokenFromFile(tokFile)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(tokFile, tok)
}
return config.Client(context.Background(), tok)
}
// Request a token from the web, then returns the retrieved token.
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: \n%v\n", authURL)
var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
log.Fatalf("Unable to read authorization code: %v", err)
}
tok, err := config.Exchange(context.TODO(), authCode)
if err != nil {
log.Fatalf("Unable to retrieve token from web: %v", err)
}
return tok
}
// Retrieves a token from a local file.
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}
// Saves a token to a file path.
func saveToken(path string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %s\n", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
}
func findDeviceId(srv *admin.Service, deviceSerial string) (deviceId string) {
deviceId = ""
r, err := srv.Chromeosdevices.List("aaa").Query(deviceSerial).Do()
if err != nil {
log.Printf("Unable to retrieve device with serial %s in domain: %v", deviceSerial, err)
} else {
for _, u := range r.Chromeosdevices {
deviceId = u.DeviceId
fmt.Printf("%s %s (%s)\n", u.DeviceId, u.SerialNumber, u.Status)
}
}
return deviceId
}
func main() {
ctx := context.Background()
b, err := os.ReadFile("credentials.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
config, err := google.ConfigFromJSON(b, admin.AdminDirectoryDeviceChromeosScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
srv, err := admin.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve directory Client %v", err)
}
deviceId := findDeviceId(srv, "xxx")
deviceAction := make(map[string]string)
deviceAction["action"] = "disable"
deviceAction["deprovisionReason"] = "retiring_device"
r, err := srv.Chromeosdevices.Action("aaa", deviceId, &deviceAction).Do()
fmt.Println(r)
fmt.Println(err)
}
Getting error
cannot use deviceAction (variable of type map[string]string) as *admin.ChromeOsDeviceAction value in argument to srv.Chromeosdevices.ActioncompilerIncompatibleAssign
The method ChromeosdevicesService.Action
takes ChromeOsDeviceAction
:
chromeosdeviceaction := &admin.ChromeOsDeviceAction{
Action: "disable",
DeprovisionReason: "retiring_device",
}
Your code would be simpler and more portable using Application Default Credentials. This approach is shown and referenced in the library's documentation: Creating a client