<commit_before>// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package apiserver

import (
	"github.com/juju/errors"
	"github.com/juju/utils/set"

	"github.com/juju/juju/rpc/rpcreflect"
	"github.com/juju/juju/state"
)

var inUpgradeError = errors.New("upgrade in progress - Juju functionality is limited")

type upgradingRoot struct {
	srvRoot
}

var _ apiRoot = (*upgradingRoot)(nil)

// newUpgradingRoot creates a root where all but a few "safe" API
// calls fail with inUpgradeError.
func newUpgradingRoot(root *initialRoot, entity state.Entity) *upgradingRoot {
	return &upgradingRoot{
		srvRoot: *newSrvRoot(root, entity),
	}
}

// FindMethod extended srvRoot.FindMethod. It returns inUpgradeError
// for most API calls except those that are deemed safe or important
// for use while Juju is upgrading.
func (r *upgradingRoot) FindMethod(rootName string, version int, methodName string) (rpcreflect.MethodCaller, error) {
	if _, _, err := r.lookupMethod(rootName, version, methodName); err != nil {
		return nil, err
	}
	if !isMethodAllowedDuringUpgrade(rootName, methodName) {
		return nil, inUpgradeError
	}
	return r.srvRoot.FindMethod(rootName, version, methodName)
}

var allowedMethodsDuringUpgrades = set.NewStrings(
	"Client.FullStatus",     // for "juju status"
	"Client.PrivateAddress", // for "juju ssh"
	"Client.PublicAddress",  // for "juju ssh"
	"Client.WatchDebugLog",  // for "juju debug-log"
)

func isMethodAllowedDuringUpgrade(rootName, methodName string) bool {
	fullName := rootName + "." + methodName
	return allowedMethodsDuringUpgrades.Contains(fullName)
}
<commit_msg>apiserver: allow an extra API method during upgrades<commit_after>// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package apiserver

import (
	"github.com/juju/errors"
	"github.com/juju/utils/set"

	"github.com/juju/juju/rpc/rpcreflect"
	"github.com/juju/juju/state"
)

var inUpgradeError = errors.New("upgrade in progress - Juju functionality is limited")

type upgradingRoot struct {
	srvRoot
}

var _ apiRoot = (*upgradingRoot)(nil)

// newUpgradingRoot creates a root where all but a few "safe" API
// calls fail with inUpgradeError.
func newUpgradingRoot(root *initialRoot, entity state.Entity) *upgradingRoot {
	return &upgradingRoot{
		srvRoot: *newSrvRoot(root, entity),
	}
}

// FindMethod extended srvRoot.FindMethod. It returns inUpgradeError
// for most API calls except those that are deemed safe or important
// for use while Juju is upgrading.
func (r *upgradingRoot) FindMethod(rootName string, version int, methodName string) (rpcreflect.MethodCaller, error) {
	if _, _, err := r.lookupMethod(rootName, version, methodName); err != nil {
		return nil, err
	}
	if !isMethodAllowedDuringUpgrade(rootName, methodName) {
		return nil, inUpgradeError
	}
	return r.srvRoot.FindMethod(rootName, version, methodName)
}

var allowedMethodsDuringUpgrades = set.NewStrings(
	"Client.FullStatus",     // for "juju status"
	"Client.EnvironmentGet", // for "juju ssh"
	"Client.PrivateAddress", // for "juju ssh"
	"Client.PublicAddress",  // for "juju ssh"
	"Client.WatchDebugLog",  // for "juju debug-log"
)

func isMethodAllowedDuringUpgrade(rootName, methodName string) bool {
	fullName := rootName + "." + methodName
	return allowedMethodsDuringUpgrades.Contains(fullName)
}

{'fraction_non_alphanumeric': 0.10768318006526253, 'fraction_numerical': 0.0029664787896766538, 'mean_word_length': 4.744463373083475, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5294986', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1093, 'n_words': 305}