github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/environs/context/cloud.go (about)

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