github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/addresser/watcher.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package addresser
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/state"
    10  )
    11  
    12  // ipAddressesWatcher implements an entity watcher with the transformation
    13  // of the received document ids of IP addresses into their according tags.
    14  type ipAddressesWatcher struct {
    15  	state.StringsWatcher
    16  
    17  	st StateInterface
    18  }
    19  
    20  // MapChanges converts IP address values to tags.
    21  func (w *ipAddressesWatcher) MapChanges(in []string) ([]string, error) {
    22  	if len(in) == 0 {
    23  		return in, nil
    24  	}
    25  	mapped := make([]string, len(in))
    26  	for i, v := range in {
    27  		ipAddr, err := w.st.IPAddress(v)
    28  		if err != nil {
    29  			return nil, errors.Annotate(err, "cannot fetch address")
    30  		}
    31  		mapped[i] = ipAddr.Tag().String()
    32  	}
    33  	return mapped, nil
    34  }