diff --git a/openapi/provider_configuration.go b/openapi/provider_configuration.go index 11b3aea68..f897f0376 100644 --- a/openapi/provider_configuration.go +++ b/openapi/provider_configuration.go @@ -1,6 +1,7 @@ package openapi import ( + "fmt" "github.com/hashicorp/terraform/helper/schema" ) @@ -17,7 +18,7 @@ type providerConfiguration struct { // createProviderConfig returns a providerConfiguration populated with the values provided by the user in the provider's terraform // configuration mapped to the corresponding -func newProviderConfiguration(headers SpecHeaderParameters, securitySchemaDefinitions *SpecSecurityDefinitions, data *schema.ResourceData) *providerConfiguration { +func newProviderConfiguration(headers SpecHeaderParameters, securitySchemaDefinitions *SpecSecurityDefinitions, data *schema.ResourceData) (*providerConfiguration, error) { providerConfiguration := &providerConfiguration{} providerConfiguration.Headers = map[string]string{} providerConfiguration.SecuritySchemaDefinitions = map[string]specAPIKeyAuthenticator{} @@ -27,6 +28,8 @@ func newProviderConfiguration(headers SpecHeaderParameters, securitySchemaDefini secDefTerraformCompliantName := secDef.getTerraformConfigurationName() if value, exists := data.GetOkExists(secDefTerraformCompliantName); exists { providerConfiguration.SecuritySchemaDefinitions[secDefTerraformCompliantName] = createAPIKeyAuthenticator(secDef, value.(string)) + } else { + return nil, fmt.Errorf("security schema definition '%s' is missing the value, please make sure this value is provided in the terraform configuration", secDefTerraformCompliantName) } } } @@ -36,10 +39,12 @@ func newProviderConfiguration(headers SpecHeaderParameters, securitySchemaDefini headerTerraformCompliantName := headerParam.GetHeaderTerraformConfigurationName() if value, exists := data.GetOkExists(headerTerraformCompliantName); exists { providerConfiguration.Headers[headerTerraformCompliantName] = value.(string) + } else { + return nil, fmt.Errorf("header parameter '%s' is missing the value, please make sure this value is provided in the terraform configuration", headerTerraformCompliantName) } } } - return providerConfiguration + return providerConfiguration, nil } func (p *providerConfiguration) getAuthenticatorFor(s SpecSecurityScheme) specAPIKeyAuthenticator { diff --git a/openapi/provider_configuration_test.go b/openapi/provider_configuration_test.go index d90f75ae6..0163973fa 100644 --- a/openapi/provider_configuration_test.go +++ b/openapi/provider_configuration_test.go @@ -19,7 +19,10 @@ func TestNewProviderConfiguration(t *testing.T) { } data := newTestSchema(stringProperty, stringWithPreferredNameProperty, headerProperty).getResourceData(t) Convey("When newProviderConfiguration method is called", func() { - providerConfiguration := newProviderConfiguration(headers, securitySchemaDefinitions, data) + providerConfiguration, err := newProviderConfiguration(headers, securitySchemaDefinitions, data) + Convey("Then the error returned should be nil", func() { + So(err, ShouldBeNil) + }) Convey("Then the providerConfiguration headers should contain the configured header with the right value", func() { So(providerConfiguration.Headers, ShouldContainKey, headerProperty.getTerraformCompliantPropertyName()) So(providerConfiguration.Headers[headerProperty.getTerraformCompliantPropertyName()], ShouldEqual, headerProperty.Default) @@ -34,6 +37,43 @@ func TestNewProviderConfiguration(t *testing.T) { }) }) }) + + Convey("Given securitySchemaDefinitions and a schema ResourceData not containing values for the security definitions", t, func() { + headers := SpecHeaderParameters{} + securitySchemaDefinitions := &SpecSecurityDefinitions{ + newAPIKeyHeaderSecurityDefinition(stringProperty.getTerraformCompliantPropertyName(), "someHeaderSecDefName"), + } + data := newTestSchema().getResourceData(t) + Convey("When newProviderConfiguration method is called", func() { + _, err := newProviderConfiguration(headers, securitySchemaDefinitions, data) + Convey("Then the error returned should NOT be nil", func() { + So(err, ShouldNotBeNil) + }) + Convey("Adn the error message returned should be equal to", func() { + So(err.Error(), ShouldEqual, "security schema definition 'string_property' is missing the value, please make sure this value is provided in the terraform configuration") + }) + }) + }) + + Convey("Given a headers a SpecHeaderParameters and a schema ResourceData not containing values for the security definitions", t, func() { + headerProperty := newStringSchemaDefinitionPropertyWithDefaults("headerProperty", "header_property", true, false, "updatedValue") + headers := SpecHeaderParameters{ + SpecHeaderParam{ + Name: headerProperty.getTerraformCompliantPropertyName(), + }, + } + securitySchemaDefinitions := &SpecSecurityDefinitions{} + data := newTestSchema().getResourceData(t) + Convey("When newProviderConfiguration method is called", func() { + _, err := newProviderConfiguration(headers, securitySchemaDefinitions, data) + Convey("Then the error returned should NOT be nil", func() { + So(err, ShouldNotBeNil) + }) + Convey("Adn the error message returned should be equal to", func() { + So(err.Error(), ShouldEqual, "header parameter 'header_property' is missing the value, please make sure this value is provided in the terraform configuration") + }) + }) + }) } func TestGetAuthenticatorFor(t *testing.T) { diff --git a/openapi/provider_factory.go b/openapi/provider_factory.go index c1bd5e24c..177e362d7 100644 --- a/openapi/provider_factory.go +++ b/openapi/provider_factory.go @@ -67,6 +67,7 @@ func (p providerFactory) createTerraformProviderSchema() (map[string]*schema.Sch Type: schema.TypeString, Optional: true, } + log.Printf("[DEBUG] registered optional security definition '%s' into provider schema", securityDefinition.getTerraformConfigurationName()) } // Override security definitions to required if they are global security schemes @@ -79,6 +80,7 @@ func (p providerFactory) createTerraformProviderSchema() (map[string]*schema.Sch Type: schema.TypeString, Required: true, } + log.Printf("[DEBUG] registered required security scheme '%s' into provider schema", securityScheme.getTerraformConfigurationName()) } headers, err := p.specAnalyser.GetAllHeaderParameters() if err != nil { @@ -158,7 +160,10 @@ func (p providerFactory) createProviderConfig(data *schema.ResourceData) (*provi if err != nil { return nil, err } - providerConfiguration := newProviderConfiguration(headers, securityDefinitions, data) + providerConfiguration, err := newProviderConfiguration(headers, securityDefinitions, data) + if err != nil { + return nil, err + } return providerConfiguration, nil }