<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package lxdclient

import (
	"github.com/juju/errors"
)

const (
	remoteLocalName   = "local"
	remoteDefaultName = remoteLocalName

	// TODO(ericsnow) This may be changing to "local"
	remoteIDForLocal = ""
)

// Local is LXD's default "remote". Essentially it is an unencrypted,
// unauthenticated connection to localhost over a unix socket.
var Local = Remote{
	Name: remoteLocalName,
	Host: "", // The LXD API turns this into the local unix socket.
	Cert: nil,
}

// Remote describes a LXD "remote" server for a client. In
// particular it holds the information needed for the client
// to connect to the remote.
type Remote struct {
	// Name is a label for this remote.
	Name string

	// Host identifies the host to which the client should connect.
	// An empty string is interpreted as:
	//   "localhost over a unix socket (unencrypted)".
	Host string

	// Cert holds the TLS certificate data for the client to use.
	Cert *Cert
}

// ID identifies the remote to the raw LXD client code.
func (r Remote) ID() string {
	if r.Host == Local.Host {
		return remoteIDForLocal
	}
	return r.Name
}

// SetDefaults updates a copy of the remote with default values
// where needed.
func (r Remote) SetDefaults() (Remote, error) {
	if r.Host == Local.Host {
		return r.setLocalDefaults(), nil
	}

	if r.Cert == nil {
		certPEM, keyPEM, err := genCertAndKey()
		if err != nil {
			return r, errors.Trace(err)
		}
		r.Cert = NewCert(certPEM, keyPEM)
	}

	return r, nil
}

func (r Remote) setLocalDefaults() Remote {
	if r.Name == "" {
		r.Name = remoteLocalName
	}

	// TODO(ericsnow) Set r.Cert to nil?

	return r
}

// Validate checks the Remote fields for invalid values.
func (r Remote) Validate() error {
	if r.Name == "" {
		return errors.NotValidf("remote missing name,")
	}

	if r.Host == Local.Host {
		if err := r.validateLocal(); err != nil {
			return errors.Trace(err)
		}
		return nil
	}

	// TODO(ericsnow) Ensure the host is a valid hostname or address?

	if r.Cert == nil {
		return errors.NotValidf("remote without cert")
	} else if err := r.Cert.Validate(); err != nil {
		return errors.Trace(err)
	}

	return nil
}
<commit_msg>Check "local-ness" via Remote.ID() instead of Remote.Host.<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package lxdclient

import (
	"github.com/juju/errors"
)

const (
	remoteLocalName   = "local"
	remoteDefaultName = remoteLocalName

	// TODO(ericsnow) This may be changing to "local"
	remoteIDForLocal = ""
)

// Local is LXD's default "remote". Essentially it is an unencrypted,
// unauthenticated connection to localhost over a unix socket.
var Local = Remote{
	Name: remoteLocalName,
	Host: "", // The LXD API turns this into the local unix socket.
	Cert: nil,
}

// Remote describes a LXD "remote" server for a client. In
// particular it holds the information needed for the client
// to connect to the remote.
type Remote struct {
	// Name is a label for this remote.
	Name string

	// Host identifies the host to which the client should connect.
	// An empty string is interpreted as:
	//   "localhost over a unix socket (unencrypted)".
	Host string

	// Cert holds the TLS certificate data for the client to use.
	Cert *Cert
}

// ID identifies the remote to the raw LXD client code.
func (r Remote) ID() string {
	if r.Host == Local.Host {
		return remoteIDForLocal
	}
	return r.Name
}

// SetDefaults updates a copy of the remote with default values
// where needed.
func (r Remote) SetDefaults() (Remote, error) {
	if r.ID() == Local.ID() {
		return r.setLocalDefaults(), nil
	}

	if r.Cert == nil {
		certPEM, keyPEM, err := genCertAndKey()
		if err != nil {
			return r, errors.Trace(err)
		}
		r.Cert = NewCert(certPEM, keyPEM)
	}

	return r, nil
}

func (r Remote) setLocalDefaults() Remote {
	if r.Name == "" {
		r.Name = remoteLocalName
	}

	// TODO(ericsnow) Set r.Cert to nil?

	return r
}

// Validate checks the Remote fields for invalid values.
func (r Remote) Validate() error {
	if r.Name == "" {
		return errors.NotValidf("remote missing name,")
	}

	if r.ID() == Local.ID() {
		if err := r.validateLocal(); err != nil {
			return errors.Trace(err)
		}
		return nil
	}

	// TODO(ericsnow) Ensure the host is a valid hostname or address?

	if r.Cert == nil {
		return errors.NotValidf("remote without cert")
	} else if err := r.Cert.Validate(); err != nil {
		return errors.Trace(err)
	}

	return nil
}

{'fraction_non_alphanumeric': 0.11103767349636484, 'fraction_numerical': 0.002203128442388191, 'mean_word_length': 3.246959775491113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4648237', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1471, 'n_words': 564}