github.com/zhaoxuat/libvirt-go-xml@v6.3.1-0.20200612053919-a025f1d30c41+incompatible/storage_pool.go (about)

     1  /*
     2   * This file is part of the libvirt-go-xml project
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    20   * THE SOFTWARE.
    21   *
    22   * Copyright (C) 2017 Red Hat, Inc.
    23   *
    24   */
    25  
    26  package libvirtxml
    27  
    28  import "encoding/xml"
    29  
    30  type StoragePoolSize struct {
    31  	Unit  string `xml:"unit,attr,omitempty"`
    32  	Value uint64 `xml:",chardata"`
    33  }
    34  
    35  type StoragePoolTargetPermissions struct {
    36  	Owner string `xml:"owner,omitempty"`
    37  	Group string `xml:"group,omitempty"`
    38  	Mode  string `xml:"mode,omitempty"`
    39  	Label string `xml:"label,omitempty"`
    40  }
    41  
    42  type StoragePoolTargetTimestamps struct {
    43  	Atime string `xml:"atime"`
    44  	Mtime string `xml:"mtime"`
    45  	Ctime string `xml:"ctime"`
    46  }
    47  
    48  type StoragePoolTarget struct {
    49  	Path        string                        `xml:"path,omitempty"`
    50  	Permissions *StoragePoolTargetPermissions `xml:"permissions"`
    51  	Timestamps  *StoragePoolTargetTimestamps  `xml:"timestamps"`
    52  	Encryption  *StorageEncryption            `xml:"encryption"`
    53  }
    54  
    55  type StoragePoolSourceFormat struct {
    56  	Type string `xml:"type,attr"`
    57  }
    58  
    59  type StoragePoolSourceProtocol struct {
    60  	Version string `xml:"ver,attr"`
    61  }
    62  
    63  type StoragePoolSourceHost struct {
    64  	Name string `xml:"name,attr"`
    65  	Port string `xml:"port,attr,omitempty"`
    66  }
    67  
    68  type StoragePoolSourceDevice struct {
    69  	Path          string                              `xml:"path,attr"`
    70  	PartSeparator string                              `xml:"part_separator,attr,omitempty"`
    71  	FreeExtents   []StoragePoolSourceDeviceFreeExtent `xml:"freeExtent"`
    72  }
    73  
    74  type StoragePoolSourceDeviceFreeExtent struct {
    75  	Start uint64 `xml:"start,attr"`
    76  	End   uint64 `xml:"end,attr"`
    77  }
    78  
    79  type StoragePoolSourceAuthSecret struct {
    80  	Usage string `xml:"usage,attr,omitempty"`
    81  	UUID  string `xml:"uuid,attr,omitempty"`
    82  }
    83  
    84  type StoragePoolSourceAuth struct {
    85  	Type     string                       `xml:"type,attr"`
    86  	Username string                       `xml:"username,attr"`
    87  	Secret   *StoragePoolSourceAuthSecret `xml:"secret"`
    88  }
    89  
    90  type StoragePoolSourceVendor struct {
    91  	Name string `xml:"name,attr"`
    92  }
    93  
    94  type StoragePoolSourceProduct struct {
    95  	Name string `xml:"name,attr"`
    96  }
    97  
    98  type StoragePoolPCIAddress struct {
    99  	Domain   *uint `xml:"domain,attr"`
   100  	Bus      *uint `xml:"bus,attr"`
   101  	Slot     *uint `xml:"slot,attr"`
   102  	Function *uint `xml:"function,attr"`
   103  }
   104  
   105  type StoragePoolSourceAdapterParentAddr struct {
   106  	UniqueID uint64                 `xml:"unique_id,attr"`
   107  	Address  *StoragePoolPCIAddress `xml:"address"`
   108  }
   109  
   110  type StoragePoolSourceAdapter struct {
   111  	Type       string                              `xml:"type,attr,omitempty"`
   112  	Name       string                              `xml:"name,attr,omitempty"`
   113  	Parent     string                              `xml:"parent,attr,omitempty"`
   114  	Managed    string                              `xml:"managed,attr,omitempty"`
   115  	WWNN       string                              `xml:"wwnn,attr,omitempty"`
   116  	WWPN       string                              `xml:"wwpn,attr,omitempty"`
   117  	ParentAddr *StoragePoolSourceAdapterParentAddr `xml:"parentaddr"`
   118  }
   119  
   120  type StoragePoolSourceDir struct {
   121  	Path string `xml:"path,attr"`
   122  }
   123  
   124  type StoragePoolSourceInitiator struct {
   125  	IQN StoragePoolSourceInitiatorIQN `xml:"iqn"`
   126  }
   127  
   128  type StoragePoolSourceInitiatorIQN struct {
   129  	Name string `xml:"name,attr,omitempty"`
   130  }
   131  
   132  type StoragePoolSource struct {
   133  	Name      string                      `xml:"name,omitempty"`
   134  	Dir       *StoragePoolSourceDir       `xml:"dir"`
   135  	Host      []StoragePoolSourceHost     `xml:"host"`
   136  	Device    []StoragePoolSourceDevice   `xml:"device"`
   137  	Auth      *StoragePoolSourceAuth      `xml:"auth"`
   138  	Vendor    *StoragePoolSourceVendor    `xml:"vendor"`
   139  	Product   *StoragePoolSourceProduct   `xml:"product"`
   140  	Format    *StoragePoolSourceFormat    `xml:"format"`
   141  	Protocol  *StoragePoolSourceProtocol  `xml:"protocol"`
   142  	Adapter   *StoragePoolSourceAdapter   `xml:"adapter"`
   143  	Initiator *StoragePoolSourceInitiator `xml:"initiator"`
   144  }
   145  
   146  type StoragePoolRefreshVol struct {
   147  	Allocation string `xml:"allocation,attr"`
   148  }
   149  
   150  type StoragePoolRefresh struct {
   151  	Volume StoragePoolRefreshVol `xml:"volume"`
   152  }
   153  
   154  type StoragePool struct {
   155  	XMLName    xml.Name            `xml:"pool"`
   156  	Type       string              `xml:"type,attr"`
   157  	Name       string              `xml:"name,omitempty"`
   158  	UUID       string              `xml:"uuid,omitempty"`
   159  	Allocation *StoragePoolSize    `xml:"allocation"`
   160  	Capacity   *StoragePoolSize    `xml:"capacity"`
   161  	Available  *StoragePoolSize    `xml:"available"`
   162  	Target     *StoragePoolTarget  `xml:"target"`
   163  	Source     *StoragePoolSource  `xml:"source"`
   164  	Refresh    *StoragePoolRefresh `xml:"refresh"`
   165  
   166  	/* Pool backend namespcaes must be last */
   167  	FSCommandline  *StoragePoolFSCommandline
   168  	RBDCommandline *StoragePoolRBDCommandline
   169  }
   170  
   171  type StoragePoolFSCommandlineOption struct {
   172  	Name string `xml:"name,attr"`
   173  }
   174  
   175  type StoragePoolFSCommandline struct {
   176  	XMLName xml.Name                         `xml:"http://libvirt.org/schemas/storagepool/fs/1.0 mount_opts"`
   177  	Options []StoragePoolFSCommandlineOption `xml:"option"`
   178  }
   179  
   180  type StoragePoolRBDCommandlineOption struct {
   181  	Name  string `xml:"name,attr"`
   182  	Value string `xml:"value,attr"`
   183  }
   184  
   185  type StoragePoolRBDCommandline struct {
   186  	XMLName xml.Name                          `xml:"http://libvirt.org/schemas/storagepool/rbd/1.0 config_opts"`
   187  	Options []StoragePoolRBDCommandlineOption `xml:"option"`
   188  }
   189  
   190  func (a *StoragePoolPCIAddress) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
   191  	marshalUintAttr(&start, "domain", a.Domain, "0x%04x")
   192  	marshalUintAttr(&start, "bus", a.Bus, "0x%02x")
   193  	marshalUintAttr(&start, "slot", a.Slot, "0x%02x")
   194  	marshalUintAttr(&start, "function", a.Function, "0x%x")
   195  	e.EncodeToken(start)
   196  	e.EncodeToken(start.End())
   197  	return nil
   198  }
   199  
   200  func (a *StoragePoolPCIAddress) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
   201  	for _, attr := range start.Attr {
   202  		if attr.Name.Local == "domain" {
   203  			if err := unmarshalUintAttr(attr.Value, &a.Domain, 0); err != nil {
   204  				return err
   205  			}
   206  		} else if attr.Name.Local == "bus" {
   207  			if err := unmarshalUintAttr(attr.Value, &a.Bus, 0); err != nil {
   208  				return err
   209  			}
   210  		} else if attr.Name.Local == "slot" {
   211  			if err := unmarshalUintAttr(attr.Value, &a.Slot, 0); err != nil {
   212  				return err
   213  			}
   214  		} else if attr.Name.Local == "function" {
   215  			if err := unmarshalUintAttr(attr.Value, &a.Function, 0); err != nil {
   216  				return err
   217  			}
   218  		}
   219  	}
   220  	d.Skip()
   221  	return nil
   222  }
   223  
   224  func (s *StoragePool) Unmarshal(doc string) error {
   225  	return xml.Unmarshal([]byte(doc), s)
   226  }
   227  
   228  func (s *StoragePool) Marshal() (string, error) {
   229  	doc, err := xml.MarshalIndent(s, "", "  ")
   230  	if err != nil {
   231  		return "", err
   232  	}
   233  	return string(doc), nil
   234  }