github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/virt/domain_interface.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package virt
    18  
    19  import (
    20  	"errors"
    21  
    22  	libvirtxml "github.com/libvirt/libvirt-go-xml"
    23  )
    24  
    25  const (
    26  	// DomainStateNoState means "no state", i.e. that the domain state is undefined
    27  	DomainStateNoState DomainState = iota
    28  	// DomainStateRunning means that the domain is running
    29  	DomainStateRunning
    30  	// DomainStateBlocked means that the domain is blocked on resource
    31  	DomainStateBlocked
    32  	// DomainStatePaused means that the domain is paused by user
    33  	DomainStatePaused
    34  	// DomainStateShutdown means that the domain is being shut down
    35  	DomainStateShutdown
    36  	// DomainStateCrashed means that the domain is crashed
    37  	DomainStateCrashed
    38  	// DomainStatePMSuspended means that the domain is suspended
    39  	DomainStatePMSuspended
    40  	// DomainStateShutoff means that the domain is shut off
    41  	DomainStateShutoff
    42  )
    43  
    44  // DomainState represents a state of a domain
    45  type DomainState int
    46  
    47  // ErrDomainNotFound error is returned by DomainConnection's
    48  // Lookup*() methods when the domain in question cannot be found
    49  var ErrDomainNotFound = errors.New("domain not found")
    50  
    51  // ErrSecretNotFound error is returned by DomainConnection's
    52  // Lookup*() methods when the domain in question cannot be found
    53  var ErrSecretNotFound = errors.New("secret not found")
    54  
    55  // DomainConnection provides operations on domains that correspond to VMs
    56  type DomainConnection interface {
    57  	// Define creates and returns a new domain based on the specified definition
    58  	DefineDomain(def *libvirtxml.Domain) (Domain, error)
    59  	// ListDomains lists all the domains available on the system
    60  	ListDomains() ([]Domain, error)
    61  	// LookupByName tries to locate the domain by name. In case if the
    62  	// domain cannot be found but no other error occurred, it returns
    63  	// ErrDomainNotFound
    64  	LookupDomainByName(name string) (Domain, error)
    65  	// LookupDomainByUUIDString tries to locate the domain by its UUID. In case if the
    66  	// domain cannot be found but no other error occurred, it returns
    67  	// ErrDomainNotFound
    68  	LookupDomainByUUIDString(uuid string) (Domain, error)
    69  	// DefineSecret defines a Secret with the specified value
    70  	DefineSecret(def *libvirtxml.Secret) (Secret, error)
    71  	// LookupSecretByUUIDString tries to locate the secret by its UUID. In case if the
    72  	// secret cannot be found but no other error occurred, it returns
    73  	// ErrSecretNotFound
    74  	LookupSecretByUUIDString(uuid string) (Secret, error)
    75  	// LookupSecretByUsageName tries to locate the secret by its Usage name. In case if the
    76  	// secret cannot be found but no other error occurred, it returns
    77  	// ErrSecretNotFound
    78  	LookupSecretByUsageName(usageType string, usageName string) (Secret, error)
    79  }
    80  
    81  // Secret represents a secret that's used by the domain
    82  type Secret interface {
    83  	// SetValue sets the value of the secret
    84  	SetValue(value []byte) error
    85  	// Remove removes the secret
    86  	Remove() error
    87  }
    88  
    89  // Domain represents a domain which corresponds to a VM
    90  type Domain interface {
    91  	// Create boots the domain
    92  	Create() error
    93  	// Destroy destroys the domain
    94  	Destroy() error
    95  	// Undefine removes the domain so it will no longer be possible
    96  	// to locate it using LookupByName() or LookupByUUIDString()
    97  	Undefine() error
    98  	// Shutdown shuts down the domain
    99  	Shutdown() error
   100  	// State obtains the current state of the domain
   101  	State() (DomainState, error)
   102  	// UUIDString returns UUID string for this domain
   103  	UUIDString() (string, error)
   104  	// Name returns the name of this domain
   105  	Name() (string, error)
   106  	// XML retrieves xml definition of the domain
   107  	XML() (*libvirtxml.Domain, error)
   108  	// GetRSS returns RSS used by VM in bytes
   109  	GetRSS() (uint64, error)
   110  	// GetCPUTime returns cpu time used by VM in nanoseconds per core
   111  	GetCPUTime() (uint64, error)
   112  }