func()

in internal/metadata/unmarshal.go [147:243]


func (a *attributes) UnmarshalJSON(b []byte) error {
	var mkbool = func(value bool) *bool {
		res := new(bool)
		*res = value
		return res
	}
	// Unmarshal to literal JSON types before doing anything else.
	type inner struct {
		CreatedBy             string      `json:"created-by"`
		BlockProjectKeys      string      `json:"block-project-ssh-keys"`
		Hostname              string      `json:"hostname"`
		Diagnostics           string      `json:"diagnostics"`
		DisableAccountManager string      `json:"disable-account-manager"`
		DisableAddressManager string      `json:"disable-address-manager"`
		EnableCorePlugin      string      `json:"enable-guest-agent-core-plugin"`
		EnableDiagnostics     string      `json:"enable-diagnostics"`
		EnableOSLogin         string      `json:"enable-oslogin"`
		EnableWindowsSSH      string      `json:"enable-windows-ssh"`
		EnableWSFC            string      `json:"enable-wsfc"`
		OldSSHKeys            string      `json:"sshKeys"`
		SSHKeys               string      `json:"ssh-keys"`
		TwoFactor             string      `json:"enable-oslogin-2fa"`
		SecurityKey           string      `json:"enable-oslogin-sk"`
		RequireCerts          string      `json:"enable-oslogin-certificates"`
		WindowsKeys           windowsKeys `json:"windows-keys"`
		WSFCAddresses         string      `json:"wsfc-addrs"`
		WSFCAgentPort         string      `json:"wsfc-agent-port"`
		DisableTelemetry      string      `json:"disable-guest-telemetry"`
	}
	var temp inner
	if err := json.Unmarshal(b, &temp); err != nil {
		return err
	}
	a.Hostname = temp.Hostname
	a.Diagnostics = temp.Diagnostics
	a.WSFCAddresses = temp.WSFCAddresses
	a.WSFCAgentPort = temp.WSFCAgentPort
	a.WindowsKeys = temp.WindowsKeys
	a.CreatedBy = temp.CreatedBy

	if value, err := strconv.ParseBool(temp.BlockProjectKeys); err == nil {
		a.BlockProjectKeys = value
	}

	if value, err := strconv.ParseBool(temp.EnableCorePlugin); err == nil {
		a.EnableCorePlugin = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.EnableDiagnostics); err == nil {
		a.EnableDiagnostics = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.DisableAccountManager); err == nil {
		a.DisableAccountManager = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.DisableAddressManager); err == nil {
		a.DisableAddressManager = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.EnableOSLogin); err == nil {
		a.EnableOSLogin = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.EnableWindowsSSH); err == nil {
		a.EnableWindowsSSH = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.EnableWSFC); err == nil {
		a.EnableWSFC = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.TwoFactor); err == nil {
		a.TwoFactor = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.SecurityKey); err == nil {
		a.SecurityKey = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.RequireCerts); err == nil {
		a.RequireCerts = mkbool(value)
	}

	if value, err := strconv.ParseBool(temp.DisableTelemetry); err == nil {
		a.DisableTelemetry = value
	}
	// So SSHKeys will be nil instead of []string{}
	if temp.SSHKeys != "" {
		a.SSHKeys = strings.Split(temp.SSHKeys, "\n")
	}
	if temp.OldSSHKeys != "" {
		a.BlockProjectKeys = true
		a.SSHKeys = append(a.SSHKeys, strings.Split(temp.OldSSHKeys, "\n")...)
	}
	return nil
}