Skip to content

Commit

Permalink
spaces environ: code to check whether cloud supports providerspaces +…
Browse files Browse the repository at this point in the history
… facade tests
  • Loading branch information
nam committed Jan 24, 2020
1 parent a7c7194 commit 5020480
Show file tree
Hide file tree
Showing 15 changed files with 434 additions and 97 deletions.
41 changes: 37 additions & 4 deletions apiserver/facades/client/spaces/mocks/package_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 12 additions & 49 deletions apiserver/facades/client/spaces/shims.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package spaces
import (
"github.com/juju/errors"
"github.com/juju/juju/apiserver/common/networkingcommon"
jujucontroller "github.com/juju/juju/controller"
"github.com/juju/juju/controller"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/network"
coresettings "github.com/juju/juju/core/settings"
"github.com/juju/juju/core/settings"
"github.com/juju/juju/state"
"github.com/juju/juju/state/stateenvirons"
"github.com/juju/loggo"
Expand Down Expand Up @@ -105,65 +105,28 @@ func (s *stateShim) SubnetByCIDR(cidr string) (networkingcommon.BackingSubnet, e
return networkingcommon.NewSubnetShim(result), nil
}

// TODO: nammn check whether we want to have ops and do transaction here? VS single transactions
// TODO: spaces collection, constraints collection and controllerSettings
func (s *stateShim) RenameSpace(fromSpaceName, toName string) error {
func (s *stateShim) RenameSpace(settingsChanges settings.ItemChanges, constraints constraints.Value, fromSpaceName, toName string) error {

constraints, err := getChangedConstraints(s.State, fromSpaceName, toName)
if err != nil {
logger.Errorf("constraints failed: %q", err)
return errors.Trace(err)
}

settingsChanges, err := getSettingsChanges(s.State, fromSpaceName, toName)
if err != nil {
logger.Errorf("settings failed: %q", err)
return errors.Trace(err)
}

err = s.State.RenameSpace(fromSpaceName, toName, settingsChanges, constraints)
err := s.State.RenameSpace(fromSpaceName, toName, settingsChanges, constraints)
if err != nil {
return errors.Trace(err)
}

return nil
}

func getSettingsChanges(st *state.State, fromSpaceName, toName string) (coresettings.ItemChanges, error) {
config, err := st.ControllerConfig()
func (s *stateShim) Constraints() (constraints.Value, error) {
result, err := s.State.ModelConstraints()
if err != nil {
logger.Errorf("failed getting conf: %q", err)
return nil, errors.Trace(err)
}
var deltas coresettings.ItemChanges

if mgmtSpace := config.JujuManagementSpace(); mgmtSpace == fromSpaceName {
change := coresettings.MakeModification(jujucontroller.JujuManagementSpace, fromSpaceName, toName)
deltas = append(deltas, change)
}
if haSpace := config.JujuHASpace(); haSpace == fromSpaceName {
change := coresettings.MakeModification(jujucontroller.JujuHASpace, fromSpaceName, toName)
deltas = append(deltas, change)
return constraints.Value{}, errors.Trace(err)
}
return deltas, nil
return result, nil
}

// getChangedConstraints will do nothing if there are no spaces constraints to update
func getChangedConstraints(st *state.State, fromSpaceName, toName string) (constraints.Value, error) {
modelConstraints, err := st.ModelConstraints()
func (s *stateShim) ControllerConfig() (controller.Config, error) {
result, err := s.State.ControllerConfig()
if err != nil {
return constraints.Value{}, errors.Trace(err)
}
if modelConstraints.HasSpaces() {
deref := *modelConstraints.Spaces
for i, space := range *modelConstraints.Spaces {
if space == fromSpaceName {
deref[i] = toName
modelConstraints.Spaces = &deref
break
}
}

return nil, errors.Trace(err)
}
return modelConstraints, nil
return result, nil
}
86 changes: 82 additions & 4 deletions apiserver/facades/client/spaces/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import (
"github.com/juju/juju/apiserver/common/networkingcommon"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
jujucontroller "github.com/juju/juju/controller"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/permission"
coresettings "github.com/juju/juju/core/settings"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/state"
Expand Down Expand Up @@ -68,7 +71,15 @@ type Backing interface {
// AllMachines loads all machines
AllMachines() ([]Machine, error)

RenameSpace(from, to string) error
// RenameSpace renames the given space `a` given name `b`.
// SettingsChanges and Constraints holds the changes to be done in the state.
RenameSpace(settingsChanges coresettings.ItemChanges, constraints constraints.Value, fromSpaceName, toName string) error

// ControllerConfig returns current ControllerConfig.
ControllerConfig() (jujucontroller.Config, error)

// Constraints returns current constraints.
Constraints() (constraints.Value, error)
}

// APIv2 provides the spaces API facade for versions < 3.
Expand Down Expand Up @@ -273,7 +284,6 @@ func (api *API) createOneSpace(args params.CreateSpaceParams) error {

// RenameSpace renames a space.
func (api *API) RenameSpace(args params.RenameSpacesParams) (params.ErrorResults, error) {
// TODO: don't allow MAAS
isAdmin, err := api.authorizer.HasPermission(permission.AdminAccess, api.backing.ModelTag())
if err != nil && !errors.IsNotFound(err) {
return params.ErrorResults{}, errors.Trace(err)
Expand All @@ -284,7 +294,7 @@ func (api *API) RenameSpace(args params.RenameSpacesParams) (params.ErrorResults
if err := api.check.ChangeAllowed(); err != nil {
return params.ErrorResults{}, errors.Trace(err)
}
if err = api.checkSupportsSpaces(); err != nil {
if err = api.checkSupportsProviderSpaces(); err != nil {
return params.ErrorResults{}, common.ServerError(errors.Trace(err))
}
results := params.ErrorResults{
Expand Down Expand Up @@ -313,7 +323,22 @@ func (api *API) RenameSpace(args params.RenameSpacesParams) (params.ErrorResults
results.Results[i].Error = common.ServerError(errors.Trace(newErr))
continue
}
if err := api.backing.RenameSpace(fromTag.Id(), toTag.Id()); err != nil {
// ControllerConfig
settingChanges, err := api.getSettingsChanges(fromTag.Id(), toTag.Id())
if err != nil {
newErr := errors.Annotatef(err, "retrieving setting changes")
results.Results[i].Error = common.ServerError(errors.Trace(newErr))
continue
}
// Constraints
constraintChanges, err := api.getConstraintsChanges(fromTag.Id(), toTag.Id())
if err != nil {
newErr := errors.Annotatef(err, "retrieving constraint changes")
results.Results[i].Error = common.ServerError(errors.Trace(newErr))
continue
}
// RenameSpace
if err := api.backing.RenameSpace(settingChanges, constraintChanges, fromTag.Id(), toTag.Id()); err != nil {
newErr := errors.Annotatef(err, "failed to rename space %q to name %q", fromTag.Id(), toTag.Id())
results.Results[i].Error = common.ServerError(errors.Trace(newErr))
continue
Expand Down Expand Up @@ -480,6 +505,19 @@ func (api *API) checkSupportsSpaces() error {
return nil
}

// checkSupportsSpaces checks if the environment implements NetworkingEnviron
// and also if it supports spaces.
func (api *API) checkSupportsProviderSpaces() error {
env, err := environs.GetEnviron(api.backing, environs.New)
if err != nil {
return errors.Annotate(err, "getting environ")
}
if !environs.SupportsProviderSpaces(api.context, env) {
return errors.NotSupportedf("provider spaces")
}
return nil
}

func (api *API) getMachineCountBySpaceID(spaceID string) (int, error) {
var count int
machines, err := api.backing.AllMachines()
Expand Down Expand Up @@ -514,3 +552,43 @@ func (api *API) getApplicationsBindSpace(givenSpaceID string) ([]string, error)
}
return applications.SortedValues(), nil
}

// getConstraintsChanges will do nothing if there are no spaces constraints to update
func (api *API) getConstraintsChanges(fromSpaceName, toName string) (constraints.Value, error) {
currentConstraints, err := api.backing.Constraints()
if err != nil {
return constraints.Value{}, errors.Trace(err)
}

if currentConstraints.HasSpaces() {
deref := *currentConstraints.Spaces
for i, space := range *currentConstraints.Spaces {
if space == fromSpaceName {
deref[i] = toName
currentConstraints.Spaces = &deref
break
}
}

}
return currentConstraints, nil
}

func (api *API) getSettingsChanges(fromSpaceName, toName string) (coresettings.ItemChanges, error) {
currentControllerConfig, err := api.backing.ControllerConfig()
if err != nil {
return nil, errors.Trace(err)
}

var deltas coresettings.ItemChanges

if mgmtSpace := currentControllerConfig.JujuManagementSpace(); mgmtSpace == fromSpaceName {
change := coresettings.MakeModification(jujucontroller.JujuManagementSpace, fromSpaceName, toName)
deltas = append(deltas, change)
}
if haSpace := currentControllerConfig.JujuHASpace(); haSpace == fromSpaceName {
change := coresettings.MakeModification(jujucontroller.JujuHASpace, fromSpaceName, toName)
deltas = append(deltas, change)
}
return deltas, nil
}

0 comments on commit 5020480

Please sign in to comment.