github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/names/v5"
     9  	"gopkg.in/macaroon.v2"
    10  
    11  	"github.com/juju/juju/core/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  	// ControllerAlias holds an optional alias for the target controller.
    26  	ControllerAlias string
    27  
    28  	// Addrs holds the addresses and ports of the target controller's
    29  	// API servers.
    30  	Addrs []string
    31  
    32  	// CACert holds the CA certificate that will be used to validate
    33  	// the target API server's certificate, in PEM format.
    34  	CACert string
    35  
    36  	// AuthTag holds the user tag to authenticate with to the target
    37  	// controller.
    38  	AuthTag names.UserTag
    39  
    40  	// Password holds the password to use with AuthTag.
    41  	Password string
    42  
    43  	// Macaroons holds macaroons to use with AuthTag. At least one of
    44  	// Password or Macaroons must be set.
    45  	Macaroons []macaroon.Slice
    46  }
    47  
    48  // Validate returns an error if the TargetInfo contains bad data. Nil
    49  // is returned otherwise.
    50  func (info *TargetInfo) Validate() error {
    51  	if !names.IsValidModel(info.ControllerTag.Id()) {
    52  		return errors.NotValidf("ControllerTag")
    53  	}
    54  
    55  	if len(info.Addrs) < 1 {
    56  		return errors.NotValidf("empty Addrs")
    57  	}
    58  	for _, addr := range info.Addrs {
    59  		_, err := network.ParseMachineHostPort(addr)
    60  		if err != nil {
    61  			return errors.NotValidf("%q in Addrs", addr)
    62  		}
    63  	}
    64  
    65  	if info.AuthTag.Id() == "" && len(info.Macaroons) == 0 {
    66  		return errors.NotValidf("empty AuthTag")
    67  	}
    68  
    69  	if info.Password == "" && len(info.Macaroons) == 0 {
    70  		return errors.NotValidf("missing Password & Macaroons")
    71  	}
    72  
    73  	return nil
    74  }