github.com/libvirt/libvirt-go-xml@v7.4.0+incompatible/interface.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 Lian Duan <blazeblue@gmail.com> 23 * 24 */ 25 26 package libvirtxml 27 28 import ( 29 "encoding/xml" 30 ) 31 32 type Interface struct { 33 XMLName xml.Name `xml:"interface"` 34 Name string `xml:"name,attr,omitempty"` 35 Start *InterfaceStart `xml:"start"` 36 MTU *InterfaceMTU `xml:"mtu"` 37 Protocol []InterfaceProtocol `xml:"protocol"` 38 Link *InterfaceLink `xml:"link"` 39 MAC *InterfaceMAC `xml:"mac"` 40 Bond *InterfaceBond `xml:"bond"` 41 Bridge *InterfaceBridge `xml:"bridge"` 42 VLAN *InterfaceVLAN `xml:"vlan"` 43 } 44 45 type InterfaceStart struct { 46 Mode string `xml:"mode,attr"` 47 } 48 49 type InterfaceMTU struct { 50 Size uint `xml:"size,attr"` 51 } 52 53 type InterfaceProtocol struct { 54 Family string `xml:"family,attr,omitempty"` 55 AutoConf *InterfaceAutoConf `xml:"autoconf"` 56 DHCP *InterfaceDHCP `xml:"dhcp"` 57 IPs []InterfaceIP `xml:"ip"` 58 Route []InterfaceRoute `xml:"route"` 59 } 60 61 type InterfaceAutoConf struct { 62 } 63 64 type InterfaceDHCP struct { 65 PeerDNS string `xml:"peerdns,attr,omitempty"` 66 } 67 68 type InterfaceIP struct { 69 Address string `xml:"address,attr"` 70 Prefix uint `xml:"prefix,attr,omitempty"` 71 } 72 73 type InterfaceRoute struct { 74 Gateway string `xml:"gateway,attr"` 75 } 76 77 type InterfaceLink struct { 78 Speed uint `xml:"speed,attr,omitempty"` 79 State string `xml:"state,attr,omitempty"` 80 } 81 82 type InterfaceMAC struct { 83 Address string `xml:"address,attr"` 84 } 85 86 type InterfaceBond struct { 87 Mode string `xml:"mode,attr,omitempty"` 88 ARPMon *InterfaceBondARPMon `xml:"arpmon"` 89 MIIMon *InterfaceBondMIIMon `xml:"miimon"` 90 Interfaces []Interface `xml:"interface"` 91 } 92 93 type InterfaceBondARPMon struct { 94 Interval uint `xml:"interval,attr,omitempty"` 95 Target string `xml:"target,attr,omitempty"` 96 Validate string `xml:"validate,attr,omitempty"` 97 } 98 99 type InterfaceBondMIIMon struct { 100 Freq uint `xml:"freq,attr,omitempty"` 101 UpDelay uint `xml:"updelay,attr,omitempty"` 102 Carrier string `xml:"carrier,attr,omitempty"` 103 } 104 105 type InterfaceBridge struct { 106 STP string `xml:"stp,attr,omitempty"` 107 Delay *float64 `xml:"delay,attr"` 108 Interfaces []Interface `xml:"interface"` 109 } 110 111 type InterfaceVLAN struct { 112 Tag *uint `xml:"tag,attr"` 113 Interface *Interface `xml:"interface"` 114 } 115 116 type interfaceDup Interface 117 118 func (s *Interface) MarshalXML(e *xml.Encoder, start xml.StartElement) error { 119 start.Name.Local = "interface" 120 121 typ := "ethernet" 122 if s.Bond != nil { 123 typ = "bond" 124 } else if s.Bridge != nil { 125 typ = "bridge" 126 } else if s.VLAN != nil { 127 typ = "vlan" 128 } 129 130 start.Attr = append(start.Attr, xml.Attr{ 131 Name: xml.Name{Local: "type"}, 132 Value: typ, 133 }) 134 135 i := interfaceDup(*s) 136 137 return e.EncodeElement(i, start) 138 } 139 140 func (s *Interface) Unmarshal(doc string) error { 141 return xml.Unmarshal([]byte(doc), s) 142 } 143 144 func (s *Interface) Marshal() (string, error) { 145 doc, err := xml.MarshalIndent(s, "", " ") 146 if err != nil { 147 return "", err 148 } 149 return string(doc), nil 150 }