83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
package host
|
|
|
|
import (
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
// URI is a string type, accessed via the pre-defined variables, and represent
|
|
// the URI pattern used to connect to a host.
|
|
// Example:
|
|
// Driver[+Transport]://<host or empty for local>[:PORT]/<path>[?Options&in=uri&format]
|
|
type URI struct {
|
|
Driver string
|
|
Transport string
|
|
Path string
|
|
Options []string
|
|
}
|
|
|
|
// CustomURI create and return a custom URI method, following RFC2396,
|
|
// keeping in mind that the hostname will be inserted between the transport and path
|
|
func CustomURI(driver, transport, path string, options ...string) *URI {
|
|
return &URI{
|
|
Driver: driver,
|
|
Transport: transport,
|
|
Path: path,
|
|
Options: options,
|
|
}
|
|
}
|
|
|
|
// URIs available to build connections to a libvirt host
|
|
var (
|
|
// URI for connecting to a remote QEMU system over SSH
|
|
URI_QEMU_SSH_SYSTEM = &URI{Driver: "qemu", Transport: "ssh", Path: "system"}
|
|
// URI for connecting to a remote QEMU system over TLS
|
|
// Builds the URI qemu://<host:port>/system
|
|
URI_QEMU_TLS_SYSTEM = &URI{Driver: "qemu", Transport: "", Path: "system"}
|
|
// URI for connecting to a remote QEMU session over SSH
|
|
URI_QEMU_SSH_SESSION = &URI{Driver: "qemu", Transport: "ssh", Path: "session"}
|
|
// URI for connecting to a local QEMU system over a UNIX socket
|
|
URI_QEMU_UNIX_SYSTEM = &URI{Driver: "qemu", Transport: "unix", Path: "system"}
|
|
// URI for connecting to a remote QEMU system over unsecured TCP
|
|
URI_QEMU_TCP_SYSTEM = &URI{Driver: "qemu", Transport: "tcp", Path: "system"}
|
|
|
|
// URI for connecting to a remote XEN system with SSH
|
|
URI_XEN_SSH_SYSTEM = &URI{Driver: "xen", Transport: "ssh", Path: "system"}
|
|
// URI for connecting to a remote XEN system over TLS
|
|
URI_XEN_TLS_SYSTEM = &URI{Driver: "xen", Transport: "", Path: "system"}
|
|
)
|
|
|
|
// ConnectionString takes a host name to interpolate into a URI and returns the string
|
|
func (u *URI) ConnectionString(h string) string {
|
|
var sb strings.Builder
|
|
optlen := len(u.Options)
|
|
sb.WriteString(u.Driver)
|
|
if u.Transport != "" {
|
|
sb.WriteRune('+')
|
|
sb.WriteString(u.Transport)
|
|
}
|
|
sb.WriteString("://")
|
|
if h != "" {
|
|
sb.WriteString(h)
|
|
}
|
|
sb.WriteRune('/')
|
|
sb.WriteString(u.Path)
|
|
|
|
if optlen > 0 {
|
|
sb.WriteRune('?')
|
|
for i, o := range u.Options {
|
|
sb.WriteString(o)
|
|
if optlen != i+1 {
|
|
sb.WriteRune('&')
|
|
}
|
|
}
|
|
}
|
|
log.Printf("Connection URI: %s", sb.String())
|
|
return sb.String()
|
|
}
|
|
|
|
// AddOpt adds more options to the option list
|
|
func (u *URI) AddOpt(opt string) {
|
|
u.Options = append(u.Options, opt)
|
|
}
|