github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/api/systemmanager/systemmanager.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package systemmanager
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/loggo"
     9  	"github.com/juju/names"
    10  
    11  	"github.com/juju/juju/api"
    12  	"github.com/juju/juju/api/base"
    13  	"github.com/juju/juju/apiserver/params"
    14  )
    15  
    16  var logger = loggo.GetLogger("juju.api.systemmanager")
    17  
    18  // Client provides methods that the Juju client command uses to interact
    19  // with systems stored in the Juju Server.
    20  type Client struct {
    21  	base.ClientFacade
    22  	facade base.FacadeCaller
    23  }
    24  
    25  // NewClient creates a new `Client` based on an existing authenticated API
    26  // connection.
    27  func NewClient(st base.APICallCloser) *Client {
    28  	frontend, backend := base.NewClientFacade(st, "SystemManager")
    29  	logger.Tracef("%#v", frontend)
    30  	return &Client{ClientFacade: frontend, facade: backend}
    31  }
    32  
    33  // AllEnvironments allows system administrators to get the list of all the
    34  // environments in the system.
    35  func (c *Client) AllEnvironments() ([]base.UserEnvironment, error) {
    36  	var environments params.UserEnvironmentList
    37  	err := c.facade.FacadeCall("AllEnvironments", nil, &environments)
    38  	if err != nil {
    39  		return nil, errors.Trace(err)
    40  	}
    41  	result := make([]base.UserEnvironment, len(environments.UserEnvironments))
    42  	for i, env := range environments.UserEnvironments {
    43  		owner, err := names.ParseUserTag(env.OwnerTag)
    44  		if err != nil {
    45  			return nil, errors.Annotatef(err, "OwnerTag %q at position %d", env.OwnerTag, i)
    46  		}
    47  		result[i] = base.UserEnvironment{
    48  			Name:           env.Name,
    49  			UUID:           env.UUID,
    50  			Owner:          owner.Username(),
    51  			LastConnection: env.LastConnection,
    52  		}
    53  	}
    54  	return result, nil
    55  }
    56  
    57  // EnvironmentConfig returns all environment settings for the
    58  // system environment.
    59  func (c *Client) EnvironmentConfig() (map[string]interface{}, error) {
    60  	result := params.EnvironmentConfigResults{}
    61  	err := c.facade.FacadeCall("EnvironmentConfig", nil, &result)
    62  	return result.Config, err
    63  }
    64  
    65  // DestroySystem puts the system environment into a "dying" state,
    66  // and removes all non-manager machine instances. Underlying DestroyEnvironment
    67  // calls will fail if there are any manually-provisioned non-manager machines
    68  // in state.
    69  func (c *Client) DestroySystem(destroyEnvs bool, ignoreBlocks bool) error {
    70  	args := params.DestroySystemArgs{
    71  		DestroyEnvironments: destroyEnvs,
    72  		IgnoreBlocks:        ignoreBlocks,
    73  	}
    74  	return c.facade.FacadeCall("DestroySystem", args, nil)
    75  }
    76  
    77  // ListBlockedEnvironments returns a list of all environments within the system
    78  // which have at least one block in place.
    79  func (c *Client) ListBlockedEnvironments() ([]params.EnvironmentBlockInfo, error) {
    80  	result := params.EnvironmentBlockInfoList{}
    81  	err := c.facade.FacadeCall("ListBlockedEnvironments", nil, &result)
    82  	return result.Environments, err
    83  }
    84  
    85  // RemoveBlocks removes all the blocks in the system.
    86  func (c *Client) RemoveBlocks() error {
    87  	args := params.RemoveBlocksArgs{All: true}
    88  	return c.facade.FacadeCall("RemoveBlocks", args, nil)
    89  }
    90  
    91  // WatchAllEnv returns an AllEnvWatcher, from which you can request
    92  // the Next collection of Deltas (for all environments).
    93  func (c *Client) WatchAllEnvs() (*api.AllWatcher, error) {
    94  	info := new(api.WatchAll)
    95  	if err := c.facade.FacadeCall("WatchAllEnvs", nil, info); err != nil {
    96  		return nil, err
    97  	}
    98  	return api.NewAllEnvWatcher(c.facade.RawAPICaller(), &info.AllWatcherId), nil
    99  }