Repository: chanzuckerberg/fogg
Pull request #533 (2021-02-17 23:26:45)

[feature] support for grafana provider versions
Support just for required_versions for now.

### Test Plan
* CI

### References
* https://registry.terraform.io/providers/grafana/grafana/latest

codecov[bot]: # [Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/533?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`main@0323d3f`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/chanzuckerberg/fogg/pull/533/graphs/tree.svg?width=650&height=150&src=pr&token=57DhtZ7g2A)](https://codecov.io/gh/chanzuckerberg/fogg/pull/533?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##             main     #533   +/-   ##
=======================================
  Coverage        ?   56.32%           
=======================================
  Files           ?       21           
  Lines           ?     1992           
  Branches        ?        0           
=======================================
  Hits            ?     1122           
  Misses          ?      715           
  Partials        ?      155           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/533?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/533?src=pr&el=footer). Last update [0323d3f...4977298](https://codecov.io/gh/chanzuckerberg/fogg/pull/533?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/config/v2/config.go b/config/v2/config.go
index 557df6c7..9b4c8ef1 100644
--- a/config/v2/config.go
+++ b/config/v2/config.go
@@ -121,6 +121,7 @@ type Providers struct {
 	Bless      *BlessProvider      `yaml:"bless,omitempty"`
 	Datadog    *DatadogProvider    `yaml:"datadog,omitempty"`
 	Github     *GithubProvider     `yaml:"github,omitempty"`
+	Grafana    *GrafanaProvider    `yaml:"grafana,omitempty"`
 	Heroku     *HerokuProvider     `yaml:"heroku,omitempty"`
 	Kubernetes *KubernetesProvider `yaml:"kubernetes,omitempty"`
 	Okta       *OktaProvider       `yaml:"okta,omitempty"`
@@ -174,6 +175,10 @@ type GithubProvider struct {
 	Version      *string `yaml:"version,omitempty"`
 }
 
+type GrafanaProvider struct {
+	CommonProvider `yaml:",inline"`
+}
+
 type SnowflakeProvider struct {
 	Account *string `yaml:"account,omitempty"`
 	Role    *string `yaml:"role,omitempty"`
@@ -450,6 +455,13 @@ func (c *Config) Generate(r *rand.Rand, size int) reflect.Value {
 		return nil
 	}
 
+	randGrafanaProvider := func(r *rand.Rand) *GrafanaProvider {
+		if r.Float32() < 0.5 {
+			return &GrafanaProvider{}
+		}
+		return nil
+	}
+
 	randSentryProvider := func(r *rand.Rand) *SentryProvider {
 		if r.Float32() < 0.5 {
 			return &SentryProvider{}
@@ -482,6 +494,7 @@ func (c *Config) Generate(r *rand.Rand, size int) reflect.Value {
 				AWS:        randAWSProvider(r, s),
 				Bless:      randBlessProvider(r, s),
 				Datadog:    randDatadogProvider(r),
+				Grafana:    randGrafanaProvider(r),
 				Heroku:     randHerokuProvider(r),
 				Kubernetes: randKubernetesProvider(r),
 				Okta:       randOktaProvider(r, s),
diff --git a/config/v2/resolvers.go b/config/v2/resolvers.go
index cd343b5b..adbf9257 100644
--- a/config/v2/resolvers.go
+++ b/config/v2/resolvers.go
@@ -388,6 +388,32 @@ func ResolveKubernetesProvider(commons ...Common) *KubernetesProvider {
 	}
 }
 
+func ResolveGrafanaProvider(commons ...Common) *GrafanaProvider {
+	var version *string
+	var enabled *bool
+
+	for _, c := range commons {
+		if c.Providers != nil && c.Providers.Grafana != nil {
+			t := c.Providers.Grafana
+
+			if t.Enabled != nil {
+				enabled = t.Enabled
+			}
+
+			if t.Version != nil {
+				version = t.Version
+			}
+		}
+	}
+
+	return &GrafanaProvider{
+		CommonProvider: CommonProvider{
+			Enabled: enabled,
+			Version: version,
+		},
+	}
+}
+
 func ResolveTfLint(commons ...Common) TfLint {
 	enabled := false
 	for _, c := range commons {
diff --git a/plan/plan.go b/plan/plan.go
index 07ff4ae0..81e9f341 100644
--- a/plan/plan.go
+++ b/plan/plan.go
@@ -125,6 +125,7 @@ type ProviderConfiguration struct {
 	Bless                  *BlessProvider      `yaml:"bless"`
 	Datadog                *DatadogProvider    `yaml:"datadog"`
 	Github                 *GithubProvider     `yaml:"github"`
+	Grafana                *GrafanaProvider    `yaml:"grafana"`
 	Heroku                 *HerokuProvider     `yaml:"heroku"`
 	Kubernetes             *KubernetesProvider `yaml:"kubernetes"`
 	Okta                   *OktaProvider       `yaml:"okta"`
@@ -220,6 +221,9 @@ type TfeProvider struct {
 type KubernetesProvider struct {
 }
 
+type GrafanaProvider struct {
+}
+
 // BackendKind is a enum of backends we support
 type BackendKind string
 
@@ -679,6 +683,18 @@ func resolveComponentCommon(commons ...v2.Common) ComponentCommon {
 		}
 	}
 
+	var grafanaPlan *GrafanaProvider
+
+	grafanaConfig := v2.ResolveGrafanaProvider(commons...)
+	if grafanaConfig.Enabled != nil && *grafanaConfig.Enabled {
+		grafanaPlan = &GrafanaProvider{}
+
+		providerVersions["grafana"] = ProviderVersion{
+			Source:  "grafana/grafana",
+			Version: grafanaConfig.Version,
+		}
+	}
+
 	tflintConfig := v2.ResolveTfLint(commons...)
 
 	tfLintPlan := TfLint{
@@ -767,6 +783,7 @@ func resolveComponentCommon(commons ...v2.Common) ComponentCommon {
 			Bless:                  blessPlan,
 			Datadog:                datadogPlan,
 			Github:                 githubPlan,
+			Grafana:                grafanaPlan,
 			Heroku:                 herokuPlan,
 			Kubernetes:             k8sPlan,
 			Okta:                   oktaPlan,


Test patch:
diff --git a/plan/plan_test.go b/plan/plan_test.go
index 306b7b6f..cab3f473 100644
--- a/plan/plan_test.go
+++ b/plan/plan_test.go
@@ -248,3 +248,24 @@ func TestOktaProvider(t *testing.T) {
 	disabled(plan.Accounts["foo"].ComponentCommon)
 	enabled(plan.Envs["prod"].Components["okta"].ComponentCommon)
 }
+
+func TestGrafanaProvider(t *testing.T) {
+	r := require.New(t)
+
+	plan := buildPlan(t, "v2_full_yaml")
+
+	enabled := func(c ComponentCommon) {
+		r.NotNil(c)
+		r.NotNil(c.ProviderConfiguration.Grafana)
+		r.NotNil(c.ProviderVersions["grafana"])
+	}
+
+	// disabled := func(c ComponentCommon) {
+	// 	r.NotNil(c)
+	// 	r.Nil(c.ProviderConfiguration.Sentry)
+	// }
+
+	// disabled(plan.Global.ComponentCommon)
+	// disabled(plan.Accounts["foo"].ComponentCommon)
+	enabled(plan.Envs["prod"].Components["hero"].ComponentCommon)
+}
diff --git a/testdata/tfe_provider_yaml/terraform/accounts/foo/fogg.tf b/testdata/tfe_provider_yaml/terraform/accounts/foo/fogg.tf
index 94317d0d..29fa956b 100644
--- a/testdata/tfe_provider_yaml/terraform/accounts/foo/fogg.tf
+++ b/testdata/tfe_provider_yaml/terraform/accounts/foo/fogg.tf
@@ -26,13 +26,6 @@ terraform {
 
     }
 
-    kubernetes = {
-      source = "hashicorp/kubernetes"
-
-      version = "2.2.2"
-
-    }
-
     local = {
       source = "hashicorp/local"
 
diff --git a/testdata/tfe_provider_yaml/terraform/global/fogg.tf b/testdata/tfe_provider_yaml/terraform/global/fogg.tf
index e1b48212..5bb0f8e1 100644
--- a/testdata/tfe_provider_yaml/terraform/global/fogg.tf
+++ b/testdata/tfe_provider_yaml/terraform/global/fogg.tf
@@ -26,13 +26,6 @@ terraform {
 
     }
 
-    kubernetes = {
-      source = "hashicorp/kubernetes"
-
-      version = "1.1.1"
-
-    }
-
     local = {
       source = "hashicorp/local"
 
diff --git a/testdata/v2_full_yaml/fogg.yml b/testdata/v2_full_yaml/fogg.yml
index 2ccd76d5..541c29bc 100644
--- a/testdata/v2_full_yaml/fogg.yml
+++ b/testdata/v2_full_yaml/fogg.yml
@@ -52,6 +52,9 @@ envs:
       hero:
         providers:
           heroku: {}
+          grafana:
+            enabled: true
+            version: 1.1.1
         depends_on:
           accounts: []
           components: []
diff --git a/testdata/v2_full_yaml/terraform/envs/prod/hero/fogg.tf b/testdata/v2_full_yaml/terraform/envs/prod/hero/fogg.tf
index 4f1abdc5..5880e107 100644
--- a/testdata/v2_full_yaml/terraform/envs/prod/hero/fogg.tf
+++ b/testdata/v2_full_yaml/terraform/envs/prod/hero/fogg.tf
@@ -42,6 +42,13 @@ terraform {
 
     }
 
+    grafana = {
+      source = "grafana/grafana"
+
+      version = "1.1.1"
+
+    }
+
     herok = {
       source = "heroku/heroku"
 
diff --git a/testdata/v2_full_yaml/terraform/envs/prod/sentry/fogg.tf b/testdata/v2_full_yaml/terraform/envs/prod/sentry/fogg.tf
index 6971fa46..b45aaec0 100644
--- a/testdata/v2_full_yaml/terraform/envs/prod/sentry/fogg.tf
+++ b/testdata/v2_full_yaml/terraform/envs/prod/sentry/fogg.tf
@@ -64,7 +64,7 @@ terraform {
     }
 
     sentry = {
-      source = "jianyuan/tfe"
+      source = "jianyuan/sentry"
 
       version = "1.2.3"