github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/credentialcommon/backend.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package credentialcommon
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/cloud"
    11  	"github.com/juju/juju/core/instance"
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/environs/context"
    14  	"github.com/juju/juju/environs/instances"
    15  	"github.com/juju/juju/state"
    16  )
    17  
    18  // PersistentBackend defines persisted entities that are accessed
    19  // during credential validity check.
    20  type PersistentBackend interface {
    21  	// Model returns the model entity.
    22  	Model() (Model, error)
    23  
    24  	// Cloud returns the controller's cloud definition.
    25  	Cloud(name string) (cloud.Cloud, error)
    26  
    27  	// CloudCredential returns the cloud credential for the given tag.
    28  	CloudCredential(tag names.CloudCredentialTag) (state.Credential, error)
    29  
    30  	// AllMachines returns all machines in the model.
    31  	AllMachines() ([]Machine, error)
    32  }
    33  
    34  // Model defines model methods needed for the check.
    35  type Model interface {
    36  	// Cloud returns the name of the cloud to which the model is deployed.
    37  	Cloud() string
    38  
    39  	// CloudRegion returns the name of the cloud region to which the model is deployed.
    40  	CloudRegion() string
    41  
    42  	// Config returns the config for the model.
    43  	Config() (*config.Config, error)
    44  
    45  	// ValidateCloudCredential validates new cloud credential for this model.
    46  	ValidateCloudCredential(tag names.CloudCredentialTag, credential cloud.Credential) error
    47  
    48  	// Type returns the type of the model.
    49  	Type() state.ModelType
    50  
    51  	// CloudCredential returns the tag of the cloud credential used for managing the
    52  	// model's cloud resources, and a boolean indicating whether a credential is set.
    53  	CloudCredential() (names.CloudCredentialTag, bool)
    54  }
    55  
    56  // Machine defines machine methods needed for the check.
    57  type Machine interface {
    58  	// IsManual returns true if the machine was manually provisioned.
    59  	IsManual() (bool, error)
    60  
    61  	// IsContainer returns true if the machine is a container.
    62  	IsContainer() bool
    63  
    64  	// InstanceId returns the provider specific instance id for this
    65  	// machine, or a NotProvisionedError, if not set.
    66  	InstanceId() (instance.Id, error)
    67  
    68  	// Id returns the machine id.
    69  	Id() string
    70  }
    71  
    72  // CloudProvider defines methods needed from the cloud provider to perform the check.
    73  type CloudProvider interface {
    74  	// AllInstances returns all instances currently known to the cloud provider.
    75  	AllInstances(ctx context.ProviderCallContext) ([]instances.Instance, error)
    76  }
    77  
    78  type stateShim struct {
    79  	*state.State
    80  }
    81  
    82  // NewPersistentBackend creates a credential validity backend to use, based on state.State.
    83  func NewPersistentBackend(p *state.State) PersistentBackend {
    84  	return stateShim{p}
    85  }
    86  
    87  // AllMachines implements PersistentBackend.AllMachines.
    88  func (st stateShim) AllMachines() ([]Machine, error) {
    89  	machines, err := st.State.AllMachines()
    90  	if err != nil {
    91  		return nil, errors.Trace(err)
    92  	}
    93  	result := make([]Machine, len(machines))
    94  	for i, m := range machines {
    95  		result[i] = m
    96  	}
    97  	return result, nil
    98  }
    99  
   100  // Model implements PersistentBackend.Model.
   101  func (st stateShim) Model() (Model, error) {
   102  	m, err := st.State.Model()
   103  	return m, err
   104  }