github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/environs/open.go (about)

     1  // Copyright 2011, 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package environs
     5  
     6  import (
     7  	stdcontext "context"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/environs/context"
    12  	"github.com/juju/juju/jujuclient"
    13  )
    14  
    15  // AdminUser is the initial admin user created for all controllers.
    16  const AdminUser = "admin"
    17  
    18  // New returns a new environment based on the provided configuration.
    19  func New(ctx stdcontext.Context, args OpenParams) (Environ, error) {
    20  	p, err := Provider(args.Cloud.Type)
    21  	if err != nil {
    22  		return nil, errors.Trace(err)
    23  	}
    24  	return Open(ctx, p, args)
    25  }
    26  
    27  // Open creates an Environ instance and errors if the provider is not for a cloud.
    28  func Open(ctx stdcontext.Context, p EnvironProvider, args OpenParams) (Environ, error) {
    29  	if envProvider, ok := p.(CloudEnvironProvider); !ok {
    30  		return nil, errors.NotValidf("cloud environ provider %T", p)
    31  	} else {
    32  		return envProvider.Open(ctx, args)
    33  	}
    34  }
    35  
    36  // Destroy destroys the controller and, if successful,
    37  // its associated configuration data from the given store.
    38  func Destroy(
    39  	controllerName string,
    40  	env ControllerDestroyer,
    41  	ctx context.ProviderCallContext,
    42  	store jujuclient.ControllerStore,
    43  ) error {
    44  	details, err := store.ControllerByName(controllerName)
    45  	if errors.IsNotFound(err) {
    46  		// No controller details, nothing to do.
    47  		return nil
    48  	} else if err != nil {
    49  		return errors.Trace(err)
    50  	}
    51  	if err := env.DestroyController(ctx, details.ControllerUUID); err != nil {
    52  		return errors.Trace(err)
    53  	}
    54  	err = store.RemoveController(controllerName)
    55  	if err != nil && !errors.IsNotFound(err) {
    56  		return errors.Trace(err)
    57  	}
    58  	return nil
    59  }