github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/state/networks.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/names"
    10  	"gopkg.in/mgo.v2/bson"
    11  
    12  	"github.com/juju/juju/network"
    13  )
    14  
    15  // Network represents the state of a network.
    16  type Network struct {
    17  	st  *State
    18  	doc networkDoc
    19  }
    20  
    21  // NetworkInfo describes a single network.
    22  type NetworkInfo struct {
    23  	// Name is juju-internal name of the network.
    24  	Name string
    25  
    26  	// ProviderId is a provider-specific network id.
    27  	ProviderId network.Id
    28  
    29  	// CIDR of the network, in 123.45.67.89/24 format.
    30  	CIDR string
    31  
    32  	// VLANTag needs to be between 1 and 4094 for VLANs and 0 for
    33  	// normal networks. It's defined by IEEE 802.1Q standard.
    34  	VLANTag int
    35  }
    36  
    37  // networkDoc represents a configured network that a machine can be a
    38  // part of.
    39  type networkDoc struct {
    40  	DocID string `bson:"_id"`
    41  	// Name is the network's name. It should be one of the machine's
    42  	// included networks.
    43  	Name    string `bson:"name"`
    44  	EnvUUID string `bson:"env-uuid"`
    45  
    46  	ProviderId network.Id
    47  	CIDR       string
    48  	VLANTag    int
    49  }
    50  
    51  func newNetwork(st *State, doc *networkDoc) *Network {
    52  	return &Network{st, *doc}
    53  }
    54  
    55  func (st *State) newNetworkDoc(args NetworkInfo) *networkDoc {
    56  	return &networkDoc{
    57  		DocID:      st.docID(args.Name),
    58  		EnvUUID:    st.EnvironUUID(),
    59  		Name:       args.Name,
    60  		ProviderId: args.ProviderId,
    61  		CIDR:       args.CIDR,
    62  		VLANTag:    args.VLANTag,
    63  	}
    64  }
    65  
    66  // GoString implements fmt.GoStringer.
    67  func (n *Network) GoString() string {
    68  	return fmt.Sprintf(
    69  		"&state.Network{name: %q, providerId: %q, cidr: %q, vlanTag: %v}",
    70  		n.Name(), n.ProviderId(), n.CIDR(), n.VLANTag())
    71  }
    72  
    73  // Name returns the network name.
    74  func (n *Network) Name() string {
    75  	return n.doc.Name
    76  }
    77  
    78  // ProviderId returns the provider-specific id of the network.
    79  func (n *Network) ProviderId() network.Id {
    80  	return n.doc.ProviderId
    81  }
    82  
    83  // Tag returns the network tag.
    84  func (n *Network) Tag() names.Tag {
    85  	return names.NewNetworkTag(n.doc.Name)
    86  }
    87  
    88  // CIDR returns the network CIDR (e.g. 192.168.50.0/24).
    89  func (n *Network) CIDR() string {
    90  	return n.doc.CIDR
    91  }
    92  
    93  // VLANTag returns the network VLAN tag. It's a number between 1 and
    94  // 4094 for VLANs and 0 if the network is not a VLAN.
    95  func (n *Network) VLANTag() int {
    96  	return n.doc.VLANTag
    97  }
    98  
    99  // IsVLAN returns whether the network is a VLAN (has tag > 0) or a
   100  // normal network.
   101  func (n *Network) IsVLAN() bool {
   102  	return n.doc.VLANTag > 0
   103  }
   104  
   105  // Interfaces returns all network interfaces on the network.
   106  func (n *Network) Interfaces() ([]*NetworkInterface, error) {
   107  	networkInterfaces, closer := n.st.getCollection(networkInterfacesC)
   108  	defer closer()
   109  
   110  	docs := []networkInterfaceDoc{}
   111  	sel := bson.D{{"networkname", n.doc.Name}}
   112  	err := networkInterfaces.Find(sel).All(&docs)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	ifaces := make([]*NetworkInterface, len(docs))
   117  	for i, doc := range docs {
   118  		ifaces[i] = newNetworkInterface(n.st, &doc)
   119  	}
   120  	return ifaces, nil
   121  }