github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	"github.com/juju/juju/network"
     9  	"github.com/juju/names"
    10  )
    11  
    12  // TargetInfo holds the details required to connect to a
    13  // migration's target controller.
    14  //
    15  // TODO(mjs) - Note the similarity to api.Info. It would be nice
    16  // to be able to use api.Info here but state can't import api and
    17  // moving api.Info to live under the core package is too big a project
    18  // to be done right now.
    19  type TargetInfo struct {
    20  	// ControllerTag holds tag for the target controller.
    21  	ControllerTag names.ModelTag
    22  
    23  	// Addrs holds the addresses and ports of the target controller's
    24  	// API servers.
    25  	Addrs []string
    26  
    27  	// CACert holds the CA certificate that will be used to validate
    28  	// the target API server's certificate, in PEM format.
    29  	CACert string
    30  
    31  	// AuthTag holds the user tag to authenticate with to the target
    32  	// controller.
    33  	AuthTag names.UserTag
    34  
    35  	// Password holds the password to use with AuthTag.
    36  	Password string
    37  }
    38  
    39  // Validate returns an error if the TargetInfo contains bad data. Nil
    40  // is returned otherwise.
    41  func (info *TargetInfo) Validate() error {
    42  	if !names.IsValidModel(info.ControllerTag.Id()) {
    43  		return errors.NotValidf("ControllerTag")
    44  	}
    45  
    46  	if len(info.Addrs) < 1 {
    47  		return errors.NotValidf("empty Addrs")
    48  	}
    49  	for _, addr := range info.Addrs {
    50  		_, err := network.ParseHostPort(addr)
    51  		if err != nil {
    52  			return errors.NotValidf("%q in Addrs", addr)
    53  		}
    54  	}
    55  
    56  	if info.CACert == "" {
    57  		return errors.NotValidf("empty CACert")
    58  	}
    59  
    60  	if info.AuthTag.Id() == "" {
    61  		return errors.NotValidf("empty AuthTag")
    62  	}
    63  
    64  	if info.Password == "" {
    65  		return errors.NotValidf("empty Password")
    66  	}
    67  
    68  	return nil
    69  }