github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/subnets/subnets.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package subnets 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 10 "github.com/juju/juju/apiserver/common" 11 "github.com/juju/juju/apiserver/common/networkingcommon" 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/state" 14 ) 15 16 func init() { 17 common.RegisterStandardFacade("Subnets", 2, NewAPI) 18 } 19 20 // SubnetsAPI defines the methods the Subnets API facade implements. 21 type SubnetsAPI interface { 22 // AllZones returns all availability zones known to Juju. If a 23 // zone is unusable, unavailable, or deprecated the Available 24 // field will be false. 25 AllZones() (params.ZoneResults, error) 26 27 // AllSpaces returns the tags of all network spaces known to Juju. 28 AllSpaces() (params.SpaceResults, error) 29 30 // AddSubnets adds existing subnets to Juju. 31 AddSubnets(args params.AddSubnetsParams) (params.ErrorResults, error) 32 33 // ListSubnets returns the matching subnets after applying 34 // optional filters. 35 ListSubnets(args params.SubnetsFilters) (params.ListSubnetsResults, error) 36 } 37 38 // subnetsAPI implements the SubnetsAPI interface. 39 type subnetsAPI struct { 40 backing networkingcommon.NetworkBacking 41 resources *common.Resources 42 authorizer common.Authorizer 43 } 44 45 // NewAPI creates a new Subnets API server-side facade with a 46 // state.State backing. 47 func NewAPI(st *state.State, res *common.Resources, auth common.Authorizer) (SubnetsAPI, error) { 48 return newAPIWithBacking(networkingcommon.NewStateShim(st), res, auth) 49 } 50 51 // newAPIWithBacking creates a new server-side Subnets API facade with 52 // a common.NetworkBacking 53 func newAPIWithBacking(backing networkingcommon.NetworkBacking, resources *common.Resources, authorizer common.Authorizer) (SubnetsAPI, error) { 54 // Only clients can access the Subnets facade. 55 if !authorizer.AuthClient() { 56 return nil, common.ErrPerm 57 } 58 return &subnetsAPI{ 59 backing: backing, 60 resources: resources, 61 authorizer: authorizer, 62 }, nil 63 } 64 65 // AllZones is defined on the API interface. 66 func (api *subnetsAPI) AllZones() (params.ZoneResults, error) { 67 return networkingcommon.AllZones(api.backing) 68 } 69 70 // AllSpaces is defined on the API interface. 71 func (api *subnetsAPI) AllSpaces() (params.SpaceResults, error) { 72 var results params.SpaceResults 73 74 spaces, err := api.backing.AllSpaces() 75 if err != nil { 76 return results, errors.Trace(err) 77 } 78 79 results.Results = make([]params.SpaceResult, len(spaces)) 80 for i, space := range spaces { 81 // TODO(dimitern): Add a Tag() a method and use it here. Too 82 // early to do it now as it will just complicate the tests. 83 tag := names.NewSpaceTag(space.Name()) 84 results.Results[i].Tag = tag.String() 85 } 86 return results, nil 87 } 88 89 // AddSubnets is defined on the API interface. 90 func (api *subnetsAPI) AddSubnets(args params.AddSubnetsParams) (params.ErrorResults, error) { 91 return networkingcommon.AddSubnets(api.backing, args) 92 } 93 94 // ListSubnets lists all the available subnets or only those matching 95 // all given optional filters. 96 func (api *subnetsAPI) ListSubnets(args params.SubnetsFilters) (results params.ListSubnetsResults, err error) { 97 return networkingcommon.ListSubnets(api.backing, args) 98 }