github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/core/migration/targetinfo.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migration
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  	"gopkg.in/macaroon.v1"
    10  
    11  	"github.com/juju/juju/network"
    12  )
    13  
    14  // TargetInfo holds the details required to connect to a
    15  // migration's target controller.
    16  //
    17  // TODO(mjs) - Note the similarity to api.Info. It would be nice to be
    18  // able to use api.Info here but state can't import api and moving
    19  // api.Info to live under the core package is too big a project to be
    20  // done right now.
    21  type TargetInfo struct {
    22  	// ControllerTag holds tag for the target controller.
    23  	ControllerTag names.ControllerTag
    24  
    25  	// Addrs holds the addresses and ports of the target controller's
    26  	// API servers.
    27  	Addrs []string
    28  
    29  	// CACert holds the CA certificate that will be used to validate
    30  	// the target API server's certificate, in PEM format.
    31  	CACert string
    32  
    33  	// AuthTag holds the user tag to authenticate with to the target
    34  	// controller.
    35  	AuthTag names.UserTag
    36  
    37  	// Password holds the password to use with AuthTag.
    38  	Password string
    39  
    40  	// Macaroons holds macaroons to use with AuthTag. At least one of
    41  	// Password or Macaroons must be set.
    42  	Macaroons []macaroon.Slice
    43  }
    44  
    45  // Validate returns an error if the TargetInfo contains bad data. Nil
    46  // is returned otherwise.
    47  func (info *TargetInfo) Validate() error {
    48  	if !names.IsValidModel(info.ControllerTag.Id()) {
    49  		return errors.NotValidf("ControllerTag")
    50  	}
    51  
    52  	if len(info.Addrs) < 1 {
    53  		return errors.NotValidf("empty Addrs")
    54  	}
    55  	for _, addr := range info.Addrs {
    56  		_, err := network.ParseHostPort(addr)
    57  		if err != nil {
    58  			return errors.NotValidf("%q in Addrs", addr)
    59  		}
    60  	}
    61  
    62  	if info.CACert == "" {
    63  		return errors.NotValidf("empty CACert")
    64  	}
    65  
    66  	if info.AuthTag.Id() == "" {
    67  		return errors.NotValidf("empty AuthTag")
    68  	}
    69  
    70  	if info.Password == "" && len(info.Macaroons) == 0 {
    71  		return errors.NotValidf("missing Password & Macaroons")
    72  	}
    73  
    74  	return nil
    75  }