github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/environs/context/callcontext.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package context
     5  
     6  import "github.com/juju/errors"
     7  
     8  // ProviderCallContext exposes useful capabilities when making calls
     9  // to an underlying cloud substrate.
    10  type ProviderCallContext interface {
    11  
    12  	// InvalidateCredential provides means to invalidate a credential
    13  	// that is used to make a call.
    14  	InvalidateCredential(string) error
    15  
    16  	// Dying returns the dying chan.
    17  	Dying() <-chan struct{}
    18  }
    19  
    20  // Dying returns the dying chan.
    21  type Dying func() <-chan struct{}
    22  
    23  func NewCloudCallContext() *CloudCallContext {
    24  	return &CloudCallContext{
    25  		InvalidateCredentialFunc: func(string) error {
    26  			return errors.NotImplementedf("InvalidateCredentialCallback")
    27  		},
    28  	}
    29  }
    30  
    31  // CloudCallContext is a context intended to provide behaviors that are necessary
    32  // to make a valid and lean call to an underlying substrate, for example cloud API.
    33  //
    34  // For instance, when Juju makes a call to cloud API with an expired credential,
    35  // we might not yet know that it is expired until cloud API rejects it. However,
    36  // we do know in advance, before making the call, that we want to mark this
    37  // credential as invalid if the cloud API rejects it.
    38  // How credential will be found, where it is stored in Juju data model,
    39  // what calls need to be done to mark it so,
    40  // will be the responsibility of internal functions that are passed in to this context
    41  // as this knowledge is specific to where the call was made *from* not on what object
    42  // it was made.
    43  type CloudCallContext struct {
    44  	// InvalidateCredentialFunc is the actual callback function
    45  	// that invalidates the credential used in the context of this call.
    46  	InvalidateCredentialFunc func(string) error
    47  
    48  	// DyingFunc returns the dying chan.
    49  	DyingFunc Dying
    50  }
    51  
    52  // InvalidateCredential implements context.InvalidateCredentialCallback.
    53  func (c *CloudCallContext) InvalidateCredential(reason string) error {
    54  	return c.InvalidateCredentialFunc(reason)
    55  }
    56  
    57  // Dying returns the dying chan.
    58  func (c *CloudCallContext) Dying() <-chan struct{} {
    59  	if c.DyingFunc == nil {
    60  		return nil
    61  	}
    62  	return c.DyingFunc()
    63  }