github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/api/diskformatter/diskformatter.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package diskformatter
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  
    10  	"github.com/juju/juju/api/base"
    11  	"github.com/juju/juju/api/watcher"
    12  	"github.com/juju/juju/apiserver/params"
    13  )
    14  
    15  const diskFormatterFacade = "DiskFormatter"
    16  
    17  // State provides access to a diskformatter worker's view of the state.
    18  type State struct {
    19  	facade base.FacadeCaller
    20  	tag    names.UnitTag
    21  }
    22  
    23  // NewState creates a new client-side DiskFormatter facade.
    24  func NewState(caller base.APICaller, authTag names.UnitTag) *State {
    25  	return &State{
    26  		base.NewFacadeCaller(caller, diskFormatterFacade),
    27  		authTag,
    28  	}
    29  }
    30  
    31  // WatchBlockDevices watches the block devices attached to the machine
    32  // that hosts the authenticated unit agent.
    33  func (st *State) WatchBlockDevices() (watcher.StringsWatcher, error) {
    34  	var results params.StringsWatchResults
    35  	args := params.Entities{
    36  		Entities: []params.Entity{{Tag: st.tag.String()}},
    37  	}
    38  	err := st.facade.FacadeCall("WatchBlockDevices", args, &results)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	if len(results.Results) != 1 {
    43  		panic(errors.Errorf("expected 1 result, got %d", len(results.Results)))
    44  	}
    45  	result := results.Results[0]
    46  	if result.Error != nil {
    47  		return nil, result.Error
    48  	}
    49  	w := watcher.NewStringsWatcher(st.facade.RawAPICaller(), result)
    50  	return w, nil
    51  }
    52  
    53  // BlockDevices returns details of attached block devices with the specified tags.
    54  func (st *State) BlockDevices(tags []names.DiskTag) (params.BlockDeviceResults, error) {
    55  	var result params.BlockDeviceResults
    56  	args := params.Entities{
    57  		Entities: make([]params.Entity, len(tags)),
    58  	}
    59  	for i, tag := range tags {
    60  		args.Entities[i].Tag = tag.String()
    61  	}
    62  	err := st.facade.FacadeCall("BlockDevices", args, &result)
    63  	if err != nil {
    64  		return params.BlockDeviceResults{}, err
    65  	}
    66  	if len(result.Results) != len(tags) {
    67  		panic(errors.Errorf("expected %d results, got %d", len(tags), len(result.Results)))
    68  	}
    69  	return result, nil
    70  }
    71  
    72  // BlockDeviceStorageInstances returns the details of storage instance that
    73  // each named block device is assigned to.
    74  func (st *State) BlockDeviceStorageInstances(tags []names.DiskTag) (params.StorageInstanceResults, error) {
    75  	var results params.StorageInstanceResults
    76  	args := params.Entities{
    77  		Entities: make([]params.Entity, len(tags)),
    78  	}
    79  	for i, tag := range tags {
    80  		args.Entities[i].Tag = tag.String()
    81  	}
    82  	err := st.facade.FacadeCall("BlockDeviceStorageInstances", args, &results)
    83  	if err != nil {
    84  		return params.StorageInstanceResults{}, err
    85  	}
    86  	if len(results.Results) != len(tags) {
    87  		panic(errors.Errorf("expected %d results, got %d", len(tags), len(results.Results)))
    88  	}
    89  	return results, nil
    90  }