github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/azure/errors.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Azure/go-autorest/autorest"
     7  	"github.com/Azure/go-autorest/autorest/azure"
     8  )
     9  
    10  // SubscriptionIDNotFound is an error that occurs when the Azure Subscription ID could not be found or was not provided
    11  type SubscriptionIDNotFound struct{}
    12  
    13  func (err SubscriptionIDNotFound) Error() string {
    14  	return fmt.Sprintf("Could not find an Azure Subscription ID in expected environment variable %s and one was not provided for this test.", AzureSubscriptionID)
    15  }
    16  
    17  // ResourceGroupNameNotFound is an error that occurs when the target Azure Resource Group name could not be found or was not provided
    18  type ResourceGroupNameNotFound struct{}
    19  
    20  func (err ResourceGroupNameNotFound) Error() string {
    21  	return fmt.Sprintf("Could not find an Azure Resource Group name in expected environment variable %s and one was not provided for this test.", AzureResGroupName)
    22  }
    23  
    24  // FailedToParseError is returned when an object cannot be parsed
    25  type FailedToParseError struct {
    26  	objectType string
    27  	objectID   string
    28  }
    29  
    30  func (err FailedToParseError) Error() string {
    31  	return fmt.Sprintf("Failed to parse %s with ID %s", err.objectType, err.objectID)
    32  }
    33  
    34  // NewFailedToParseError creates a new not found error when an expected object is not found in the search space
    35  func NewFailedToParseError(objectType string, objectID string) FailedToParseError {
    36  	return FailedToParseError{objectType, objectID}
    37  }
    38  
    39  // NotFoundError is returned when an expected object is not found in the search spa
    40  type NotFoundError struct {
    41  	objectType  string
    42  	objectID    string
    43  	searchSpace string
    44  }
    45  
    46  func (err NotFoundError) Error() string {
    47  	var objIDMsg string
    48  
    49  	if err.objectID != "Any" {
    50  		objIDMsg = fmt.Sprintf(" with id %s", err.objectID)
    51  	}
    52  
    53  	return fmt.Sprintf("Object of type %s%s not found in %s", err.objectType, objIDMsg, err.searchSpace)
    54  }
    55  
    56  // NewNotFoundError creates a new not found error when an expected object is not found in the search space
    57  func NewNotFoundError(objectType string, objectID string, region string) NotFoundError {
    58  	return NotFoundError{objectType, objectID, region}
    59  }
    60  
    61  // ResourceNotFoundErrorExists checks the Service Error Code for the 'Resource Not Found' error
    62  func ResourceNotFoundErrorExists(err error) bool {
    63  	if err != nil {
    64  		if autorestError, ok := err.(autorest.DetailedError); ok {
    65  			if requestError, ok := autorestError.Original.(*azure.RequestError); ok {
    66  				return (requestError.ServiceError.Code == "ResourceNotFound")
    67  			}
    68  		}
    69  	}
    70  	return false
    71  }