github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/providers/factory.go (about)

     1  package providers
     2  
     3  // Factory is a function type that creates a new instance of a resource
     4  // provider, or returns an error if that is impossible.
     5  type Factory func() (Interface, error)
     6  
     7  // FactoryFixed is a helper that creates a Factory that just returns some given
     8  // single provider.
     9  //
    10  // Unlike usual factories, the exact same instance is returned for each call
    11  // to the factory and so this must be used in only specialized situations where
    12  // the caller can take care to either not mutate the given provider at all
    13  // or to mutate it in ways that will not cause unexpected behavior for others
    14  // holding the same reference.
    15  func FactoryFixed(p Interface) Factory {
    16  	return func() (Interface, error) {
    17  		return p, nil
    18  	}
    19  }
    20  
    21  // ProviderHasResource is a helper that requests schema from the given provider
    22  // and checks if it has a resource type of the given name.
    23  //
    24  // This function is more expensive than it may first appear since it must
    25  // retrieve the entire schema from the underlying provider, and so it should
    26  // be used sparingly and especially not in tight loops.
    27  //
    28  // Since retrieving the provider may fail (e.g. if the provider is accessed
    29  // over an RPC channel that has operational problems), this function will
    30  // return false if the schema cannot be retrieved, under the assumption that
    31  // a subsequent call to do anything with the resource type would fail
    32  // anyway.
    33  func ProviderHasResource(provider Interface, typeName string) bool {
    34  	resp := provider.GetProviderSchema()
    35  	if resp.Diagnostics.HasErrors() {
    36  		return false
    37  	}
    38  
    39  	_, exists := resp.ResourceTypes[typeName]
    40  	return exists
    41  }
    42  
    43  // ProviderHasDataSource is a helper that requests schema from the given
    44  // provider and checks if it has a data source of the given name.
    45  //
    46  // This function is more expensive than it may first appear since it must
    47  // retrieve the entire schema from the underlying provider, and so it should
    48  // be used sparingly and especially not in tight loops.
    49  //
    50  // Since retrieving the provider may fail (e.g. if the provider is accessed
    51  // over an RPC channel that has operational problems), this function will
    52  // return false if the schema cannot be retrieved, under the assumption that
    53  // a subsequent call to do anything with the data source would fail
    54  // anyway.
    55  func ProviderHasDataSource(provider Interface, dataSourceName string) bool {
    56  	resp := provider.GetProviderSchema()
    57  	if resp.Diagnostics.HasErrors() {
    58  		return false
    59  	}
    60  
    61  	_, exists := resp.DataSources[dataSourceName]
    62  	return exists
    63  }