Skip to content

Commit

Permalink
acept providerConfigurationEndPoints as input
Browse files Browse the repository at this point in the history
- refactoring needed to remove specAnalyser dependency from newProviderConfigurationEndPoints
and also ensure that providerConfigurationEndPoints is not treated as a dependency
of the method
  • Loading branch information
dikhan committed Oct 30, 2019
1 parent dbdd80f commit fcbe235
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
21 changes: 7 additions & 14 deletions openapi/provider_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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(specAnalyser SpecAnalyser, data *schema.ResourceData) (*providerConfiguration, error) {
func newProviderConfiguration(specAnalyser SpecAnalyser, data *schema.ResourceData, providerConfigurationEndPoints *providerConfigurationEndPoints) (*providerConfiguration, error) {
providerConfiguration := &providerConfiguration{}
providerConfiguration.Headers = map[string]string{}
providerConfiguration.Endpoints = map[string]string{}
Expand Down Expand Up @@ -66,17 +66,8 @@ func newProviderConfiguration(specAnalyser SpecAnalyser, data *schema.ResourceDa
providerConfiguration.Region = region.(string)
}

providerConfigurationEndPoints, err := newProviderConfigurationEndPoints(specAnalyser)
if err != nil {
return nil, err
}

endpoints, err := providerConfigurationEndPoints.configureEndpoints(data)
if err != nil {
return nil, err
}
if endpoints != nil {
providerConfiguration.Endpoints = endpoints
if providerConfigurationEndPoints != nil {
providerConfiguration.Endpoints = providerConfigurationEndPoints.configureEndpoints(data)
}

return providerConfiguration, nil
Expand All @@ -99,8 +90,10 @@ func (p *providerConfiguration) getRegion() string {

// getEndPoint resolves the endpoint value for a given resource name
func (p *providerConfiguration) getEndPoint(resourceName string) string {
if endpoint, ok := p.Endpoints[resourceName]; ok {
return endpoint
if p.Endpoints != nil {
if endpoint, ok := p.Endpoints[resourceName]; ok {
return endpoint
}
}
return ""
}
23 changes: 19 additions & 4 deletions openapi/provider_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestNewProviderConfiguration(t *testing.T) {
Convey("Given a headers a SpecHeaderParameters, securitySchemaDefinitions and a schema ResourceData", t, func() {
Convey("Given a headers a SpecHeaderParameters, securitySchemaDefinitions, a schema ResourceData and a providerConfigurationEndPoints", t, func() {
headerProperty := newStringSchemaDefinitionPropertyWithDefaults("headerProperty", "header_property", true, false, "updatedValue")

specAnalyser := &specAnalyserStub{
Expand All @@ -24,9 +24,11 @@ func TestNewProviderConfiguration(t *testing.T) {
},
}

providerConfigurationEndPoints := &providerConfigurationEndPoints{}

data := newTestSchema(stringProperty, stringWithPreferredNameProperty, headerProperty).getResourceData(t)
Convey("When newProviderConfiguration method is called", func() {
providerConfiguration, err := newProviderConfiguration(specAnalyser, data)
providerConfiguration, err := newProviderConfiguration(specAnalyser, data, providerConfigurationEndPoints)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Expand Down Expand Up @@ -56,8 +58,9 @@ func TestNewProviderConfiguration(t *testing.T) {
globalSecuritySchemes: createSecuritySchemes([]map[string][]string{}),
},
}
providerConfigurationEndPoints := &providerConfigurationEndPoints{}
Convey("When newProviderConfiguration method is called", func() {
_, err := newProviderConfiguration(specAnalyser, data)
_, err := newProviderConfiguration(specAnalyser, data, providerConfigurationEndPoints)
Convey("Then the error returned should NOT be nil", func() {
So(err, ShouldNotBeNil)
})
Expand All @@ -80,9 +83,10 @@ func TestNewProviderConfiguration(t *testing.T) {
globalSecuritySchemes: createSecuritySchemes([]map[string][]string{}),
},
}
providerConfigurationEndPoints := &providerConfigurationEndPoints{}
data := newTestSchema().getResourceData(t)
Convey("When newProviderConfiguration method is called", func() {
_, err := newProviderConfiguration(specAnalyser, data)
_, err := newProviderConfiguration(specAnalyser, data, providerConfigurationEndPoints)
Convey("Then the error returned should NOT be nil", func() {
So(err, ShouldNotBeNil)
})
Expand Down Expand Up @@ -187,4 +191,15 @@ func TestGetEndPoint(t *testing.T) {
})
})
})
Convey("Given a providerConfiguration configured with nil endpoints", t, func() {
providerConfiguration := providerConfiguration{
Endpoints: nil,
}
Convey("When getEndPoint method is called with an existing resource name", func() {
value := providerConfiguration.getEndPoint("cdn_v1")
Convey("Then the value returned should be empty", func() {
So(value, ShouldBeEmpty)
})
})
})
}

0 comments on commit fcbe235

Please sign in to comment.