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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage
     5  
     6  import (
     7  	"github.com/juju/names"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/juju/apiserver/common"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/feature"
    13  	"github.com/juju/juju/state"
    14  )
    15  
    16  func init() {
    17  	common.RegisterStandardFacadeForFeature("Storage", 1, NewAPI, feature.Storage)
    18  }
    19  
    20  var getState = func(st *state.State) storageAccess {
    21  	return stateShim{st}
    22  }
    23  
    24  type StorageAPI interface {
    25  	Show(entities params.Entities) (params.StorageShowResults, error)
    26  }
    27  
    28  // API implements the storage interface and is the concrete
    29  // implementation of the api end point.
    30  type API struct {
    31  	storage    storageAccess
    32  	authorizer common.Authorizer
    33  }
    34  
    35  // NewAPI returns a new storage API facade.
    36  func NewAPI(
    37  	st *state.State,
    38  	resources *common.Resources,
    39  	authorizer common.Authorizer,
    40  ) (*API, error) {
    41  	if !authorizer.AuthClient() {
    42  		return nil, common.ErrPerm
    43  	}
    44  
    45  	return &API{
    46  		storage:    getState(st),
    47  		authorizer: authorizer,
    48  	}, nil
    49  }
    50  
    51  func (api *API) Show(entities params.Entities) (params.StorageShowResults, error) {
    52  	all := make([]params.StorageShowResult, len(entities.Entities))
    53  	for i, entity := range entities.Entities {
    54  		all[i] = api.createStorageInstanceResult(entity.Tag)
    55  	}
    56  	return params.StorageShowResults{Results: all}, nil
    57  }
    58  
    59  func (api *API) createStorageInstanceResult(tag string) params.StorageShowResult {
    60  	aTag, err := names.ParseTag(tag)
    61  	if err != nil {
    62  		return params.StorageShowResult{
    63  			Error: params.ErrorResult{
    64  				Error: common.ServerError(errors.Annotatef(common.ErrPerm, "getting %v", tag))},
    65  		}
    66  	}
    67  	stateInstance, err := api.storage.StorageInstance(aTag.Id())
    68  	if err != nil {
    69  		return params.StorageShowResult{
    70  			Error: params.ErrorResult{
    71  				Error: common.ServerError(errors.Annotatef(common.ErrPerm, "getting %v", tag))},
    72  		}
    73  	}
    74  	return params.StorageShowResult{Result: api.getStorageInstance(stateInstance)}
    75  }
    76  
    77  func (api *API) getStorageInstance(si state.StorageInstance) params.StorageInstance {
    78  	return params.StorageInstance{
    79  		OwnerTag:    si.Owner().String(),
    80  		StorageTag:  si.Tag().String(),
    81  		StorageName: si.StorageName(),
    82  		Location:    "",
    83  		TotalSize:   0,
    84  	}
    85  }