Skip to content

Commit

Permalink
simplify iac provider registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Kanchwala committed Jul 31, 2020
1 parent 5f0ce1a commit 8e15ba2
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 27 deletions.
40 changes: 40 additions & 0 deletions pkg/iac-providers/register.go
@@ -0,0 +1,40 @@
/*
Copyright (C) 2020 Accurics, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package iacprovider

import (
"reflect"
)

// map of supported IaC providers
var supportedIacProviders = make(map[supportedIacType](map[supportedIacVersion]reflect.Type))

// RegisterIacProvider registers an IaC provider for terrascan
// if the Iac provider does not have a version, it can be kept empty
func RegisterIacProvider(iacType supportedIacType, iacVersion supportedIacVersion, iacProvider reflect.Type) {

if iacVersion == "" {
iacVersion = defaultIacVersion
}

// version support
supportedTerraformVersions := make(map[supportedIacVersion]reflect.Type)
supportedTerraformVersions[iacVersion] = iacProvider

// type support
supportedIacProviders[iacType] = supportedTerraformVersions
}
77 changes: 77 additions & 0 deletions pkg/iac-providers/register_test.go
@@ -0,0 +1,77 @@
package iacprovider

import (
"reflect"
"testing"
)

type MockIacProvider struct{}

func TestRegisterIacProvider(t *testing.T) {

/*
table := []struct {
name string
iacType supportedIacType
iacVersion supportedIacVersion
want reflect.Type
}{
{
name: "mock iac type and version",
iacType: supportedIacType("mockIacType"),
iacVersion: supportedIacVersion("mockIacVersion"),
want: reflect.TypeOf(MockIacProvider{}),
},
{
name: "mock iac type default version",
iacType: supportedIacType("mockIacType"),
iacVersion: supportedIacVersion(""),
want: reflect.TypeOf(MockIacProvider{}),
},
}
*/

t.Run("mock iac provider", func(t *testing.T) {

var (
iacType = supportedIacType("mockIacType")
iacVersion = supportedIacVersion("mockIacVersion")
want = reflect.TypeOf(MockIacProvider{})
)

RegisterIacProvider(iacType, iacVersion, want)

if _, present := supportedIacProviders[iacType]; !present {
t.Errorf("mockIacType not registered")
}
got, present := supportedIacProviders[iacType][iacVersion]
if !present {
t.Errorf("mockIacVersion not registered")
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: '%v', want: '%v'", got, want)
}
})

t.Run("mock iac default version", func(t *testing.T) {

var (
iacType = supportedIacType("mockIacType")
iacVersion = supportedIacVersion("")
want = reflect.TypeOf(MockIacProvider{})
)

RegisterIacProvider(iacType, iacVersion, want)

if _, present := supportedIacProviders[iacType]; !present {
t.Errorf("mockIacType not registered")
}
got, present := supportedIacProviders[iacType][defaultIacVersion]
if !present {
t.Errorf("defaultIacVersion not registered")
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: '%v', want: '%v'", got, want)
}
})
}
20 changes: 20 additions & 0 deletions pkg/iac-providers/terraform.go
@@ -0,0 +1,20 @@
package iacprovider

import (
"reflect"

tfv12 "github.com/accurics/terrascan/pkg/iac-providers/terraform/v12"
)

// terraform specific constants
const (
terraform supportedIacType = "terraform"
terraformV12 supportedIacVersion = "v12"
)

// register terraform as an IaC provider with terrascan
func init() {

// register iac provider
RegisterIacProvider(terraform, terraformV12, reflect.TypeOf(tfv12.TfV12{}))
}
29 changes: 2 additions & 27 deletions pkg/iac-providers/supported.go → pkg/iac-providers/types.go
Expand Up @@ -16,38 +16,13 @@

package iacprovider

import (
"reflect"

tfv12 "github.com/accurics/terrascan/pkg/iac-providers/terraform/v12"
)

// SupportedIacType data type for supported IaC provider
type supportedIacType string

// supported IaC providers
const (
terraform supportedIacType = "terraform"
)

// supportedIacVersion data type for supported Iac provider
type supportedIacVersion string

// supported Iac versions
// default Iac versions
const (
defaultVersion supportedIacVersion = "default"
terraformV12 supportedIacVersion = "v12"
defaultIacVersion supportedIacVersion = "default"
)

// map of supported IaC providers
var supportedIacProviders map[supportedIacType](map[supportedIacVersion]reflect.Type)

// initializes a map of supported IaC providers
func init() {
supportedIacProviders = make(map[supportedIacType](map[supportedIacVersion]reflect.Type))

// terraform support
supportedTerraformVersions := make(map[supportedIacVersion]reflect.Type)
supportedTerraformVersions[terraformV12] = reflect.TypeOf(tfv12.TfV12{})
supportedIacProviders[terraform] = supportedTerraformVersions
}

0 comments on commit 8e15ba2

Please sign in to comment.