github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/common/networkingcommon/spaces.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package networkingcommon
     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/params"
    12  	"github.com/juju/juju/environs"
    13  	"github.com/juju/juju/network"
    14  	"github.com/juju/juju/worker/environ"
    15  )
    16  
    17  // SupportsSpaces checks if the environment implements NetworkingEnviron
    18  // and also if it supports spaces.
    19  func SupportsSpaces(backing environ.ConfigGetter) error {
    20  	config, err := backing.ModelConfig()
    21  	if err != nil {
    22  		return errors.Annotate(err, "getting model config")
    23  	}
    24  	env, err := environs.New(config)
    25  	if err != nil {
    26  		return errors.Annotate(err, "validating model config")
    27  	}
    28  	netEnv, ok := environs.SupportsNetworking(env)
    29  	if !ok {
    30  		return errors.NotSupportedf("networking")
    31  	}
    32  	ok, err = netEnv.SupportsSpaces()
    33  	if !ok {
    34  		if err != nil && !errors.IsNotSupported(err) {
    35  			logger.Warningf("checking model spaces support failed with: %v", err)
    36  		}
    37  		return errors.NotSupportedf("spaces")
    38  	}
    39  	return nil
    40  }
    41  
    42  // CreateSpaces creates a new Juju network space, associating the
    43  // specified subnets with it (optional; can be empty).
    44  func CreateSpaces(backing NetworkBacking, args params.CreateSpacesParams) (results params.ErrorResults, err error) {
    45  	err = SupportsSpaces(backing)
    46  	if err != nil {
    47  		return results, common.ServerError(errors.Trace(err))
    48  	}
    49  
    50  	results.Results = make([]params.ErrorResult, len(args.Spaces))
    51  
    52  	for i, space := range args.Spaces {
    53  		err := createOneSpace(backing, space)
    54  		if err == nil {
    55  			continue
    56  		}
    57  		results.Results[i].Error = common.ServerError(errors.Trace(err))
    58  	}
    59  
    60  	return results, nil
    61  }
    62  
    63  func createOneSpace(backing NetworkBacking, args params.CreateSpaceParams) error {
    64  	// Validate the args, assemble information for api.backing.AddSpaces
    65  	var subnets []string
    66  
    67  	spaceTag, err := names.ParseSpaceTag(args.SpaceTag)
    68  	if err != nil {
    69  		return errors.Trace(err)
    70  	}
    71  
    72  	for _, tag := range args.SubnetTags {
    73  		subnetTag, err := names.ParseSubnetTag(tag)
    74  		if err != nil {
    75  			return errors.Trace(err)
    76  		}
    77  		subnets = append(subnets, subnetTag.Id())
    78  	}
    79  
    80  	// Add the validated space.
    81  	err = backing.AddSpace(spaceTag.Id(), network.Id(args.ProviderId), subnets, args.Public)
    82  	if err != nil {
    83  		return errors.Trace(err)
    84  	}
    85  	return nil
    86  }