github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/modelupgrader/modelupgrader.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package modelupgrader
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/loggo"
     9  	"gopkg.in/juju/names.v2"
    10  
    11  	"github.com/juju/juju/api/base"
    12  	"github.com/juju/juju/api/common"
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/core/status"
    15  	"github.com/juju/juju/core/watcher"
    16  )
    17  
    18  var logger = loggo.GetLogger("juju.api.modelupgrader")
    19  
    20  // Client provides methods that the Juju client command uses to interact
    21  // with models stored in the Juju Server.
    22  type Client struct {
    23  	facade base.FacadeCaller
    24  }
    25  
    26  // NewClient creates a new `Client` based on an existing authenticated API
    27  // connection.
    28  func NewClient(caller base.APICaller) *Client {
    29  	return &Client{base.NewFacadeCaller(caller, "ModelUpgrader")}
    30  }
    31  
    32  // ModelEnvironVersion returns the current version of the environ corresponding
    33  // to the specified model.
    34  func (c *Client) ModelEnvironVersion(tag names.ModelTag) (int, error) {
    35  	args := params.Entities{
    36  		Entities: []params.Entity{{Tag: tag.String()}},
    37  	}
    38  	var results params.IntResults
    39  	err := c.facade.FacadeCall("ModelEnvironVersion", &args, &results)
    40  	if err != nil {
    41  		return -1, errors.Trace(err)
    42  	}
    43  	if len(results.Results) != 1 {
    44  		return -1, errors.Errorf("expected 1 result, got %d", len(results.Results))
    45  	}
    46  	if err := results.Results[0].Error; err != nil {
    47  		return -1, err
    48  	}
    49  	return results.Results[0].Result, nil
    50  }
    51  
    52  // ModelTargetEnvironVersion returns the target version of the environ
    53  // corresponding to the specified model.
    54  func (c *Client) ModelTargetEnvironVersion(tag names.ModelTag) (int, error) {
    55  	args := params.Entities{
    56  		Entities: []params.Entity{{Tag: tag.String()}},
    57  	}
    58  	var results params.IntResults
    59  	err := c.facade.FacadeCall("ModelTargetEnvironVersion", &args, &results)
    60  	if err != nil {
    61  		return -1, errors.Trace(err)
    62  	}
    63  	if len(results.Results) != 1 {
    64  		return -1, errors.Errorf("expected 1 result, got %d", len(results.Results))
    65  	}
    66  	if err := results.Results[0].Error; err != nil {
    67  		return -1, err
    68  	}
    69  	return results.Results[0].Result, nil
    70  }
    71  
    72  // SetModelEnvironVersion sets the current version of the environ corresponding
    73  // to the specified model.
    74  func (c *Client) SetModelEnvironVersion(tag names.ModelTag, v int) error {
    75  	args := params.SetModelEnvironVersions{
    76  		Models: []params.SetModelEnvironVersion{{
    77  			ModelTag: tag.String(),
    78  			Version:  v,
    79  		}},
    80  	}
    81  	var results params.ErrorResults
    82  	err := c.facade.FacadeCall("SetModelEnvironVersion", &args, &results)
    83  	if err != nil {
    84  		return errors.Trace(err)
    85  	}
    86  	return results.OneError()
    87  }
    88  
    89  // WatchModelEnvironVersion starts a NotifyWatcher that notifies the caller upon
    90  // changes to the environ version of the model with the specified tag.
    91  func (c *Client) WatchModelEnvironVersion(tag names.ModelTag) (watcher.NotifyWatcher, error) {
    92  	return common.Watch(c.facade, "WatchModelEnvironVersion", tag)
    93  }
    94  
    95  // SetModelStatus sets the status of a model.
    96  func (c *Client) SetModelStatus(tag names.ModelTag, status status.Status, info string, data map[string]interface{}) error {
    97  	var result params.ErrorResults
    98  	args := params.SetStatus{
    99  		Entities: []params.EntityStatusArgs{
   100  			{Tag: tag.String(), Status: status.String(), Info: info, Data: data},
   101  		},
   102  	}
   103  	if err := c.facade.FacadeCall("SetModelStatus", args, &result); err != nil {
   104  		return errors.Trace(err)
   105  	}
   106  	return result.OneError()
   107  }