github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/common/upgradeseries.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/names.v2" 9 10 "github.com/juju/juju/api/base" 11 apiwatcher "github.com/juju/juju/api/watcher" 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/core/model" 14 "github.com/juju/juju/core/watcher" 15 ) 16 17 // UpgradeSeriesAPI provides common agent-side API functions to 18 // call into apiserver.common/UpgradeSeries 19 type UpgradeSeriesAPI struct { 20 facade base.FacadeCaller 21 tag names.Tag 22 } 23 24 // NewUpgradeSeriesAPI creates a UpgradeSeriesAPI on the specified facade, 25 // and uses this name when calling through the caller. 26 func NewUpgradeSeriesAPI(facade base.FacadeCaller, tag names.Tag) *UpgradeSeriesAPI { 27 return &UpgradeSeriesAPI{facade: facade, tag: tag} 28 } 29 30 // WatchUpgradeSeriesNotifications returns a NotifyWatcher for observing the state of 31 // a series upgrade. 32 func (u *UpgradeSeriesAPI) WatchUpgradeSeriesNotifications() (watcher.NotifyWatcher, error) { 33 var results params.NotifyWatchResults 34 args := params.Entities{ 35 Entities: []params.Entity{{Tag: u.tag.String()}}, 36 } 37 err := u.facade.FacadeCall("WatchUpgradeSeriesNotifications", args, &results) 38 if err != nil { 39 return nil, err 40 } 41 if len(results.Results) != 1 { 42 return nil, errors.Errorf("expected 1 result, got %d", len(results.Results)) 43 } 44 result := results.Results[0] 45 if result.Error != nil { 46 return nil, result.Error 47 } 48 w := apiwatcher.NewNotifyWatcher(u.facade.RawAPICaller(), result) 49 return w, nil 50 } 51 52 // UpgradeSeriesUnitStatus returns the upgrade series status of a 53 // unit from remote state. 54 func (u *UpgradeSeriesAPI) UpgradeSeriesUnitStatus() ([]model.UpgradeSeriesStatus, error) { 55 var results params.UpgradeSeriesStatusResults 56 args := params.Entities{ 57 Entities: []params.Entity{{Tag: u.tag.String()}}, 58 } 59 60 err := u.facade.FacadeCall("UpgradeSeriesUnitStatus", args, &results) 61 if err != nil { 62 return nil, err 63 } 64 65 statuses := make([]model.UpgradeSeriesStatus, len(results.Results)) 66 for i, res := range results.Results { 67 if res.Error != nil { 68 // TODO (externalreality) The code to convert api errors (with 69 // error codes) back to normal Go errors is in bad spot and 70 // causes import cycles which is why we don't use it here and may 71 // be the reason why it has few uses despite being useful. 72 if params.IsCodeNotFound(res.Error) { 73 return nil, errors.NewNotFound(res.Error, "") 74 } 75 return nil, res.Error 76 } 77 statuses[i] = res.Status 78 } 79 return statuses, nil 80 } 81 82 // SetUpgradeSeriesUnitStatus sets the upgrade series status of the 83 // unit in the remote state. 84 func (u *UpgradeSeriesAPI) SetUpgradeSeriesUnitStatus(status model.UpgradeSeriesStatus, reason string) error { 85 var results params.ErrorResults 86 args := params.UpgradeSeriesStatusParams{ 87 Params: []params.UpgradeSeriesStatusParam{{ 88 Entity: params.Entity{Tag: u.tag.String()}, 89 Status: status, 90 Message: reason, 91 }}, 92 } 93 err := u.facade.FacadeCall("SetUpgradeSeriesUnitStatus", args, &results) 94 if err != nil { 95 return err 96 } 97 if len(results.Results) != 1 { 98 return errors.Errorf("expected 1 result, got %d", len(results.Results)) 99 } 100 result := results.Results[0] 101 if result.Error != nil { 102 return result.Error 103 } 104 return nil 105 }