Skip to content

Commit 72a0e3d

Browse files
Move helper functions to common package (#806)
Follow-up to #805 These are all helpers that may be used by any "section" of the provider and only depends on external packages
1 parent f3c8b22 commit 72a0e3d

21 files changed

+127
-126
lines changed

provider/adapters.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

provider/common/adapters.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package common
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5+
)
6+
7+
func ListToStringSlice(src []interface{}) []string {
8+
dst := make([]string, 0, len(src))
9+
for _, s := range src {
10+
val, ok := s.(string)
11+
if !ok {
12+
val = ""
13+
}
14+
dst = append(dst, val)
15+
}
16+
return dst
17+
}
18+
19+
func SetToStringSlice(src *schema.Set) []string {
20+
return ListToStringSlice(src.List())
21+
}
22+
23+
func ListToIntSlice(src []interface{}) []int {
24+
dst := make([]int, 0, len(src))
25+
for _, s := range src {
26+
val, ok := s.(int)
27+
if !ok {
28+
val = 0
29+
}
30+
dst = append(dst, val)
31+
}
32+
return dst
33+
}
34+
35+
func SetToIntSlice(src *schema.Set) []int {
36+
return ListToIntSlice(src.List())
37+
}
38+
39+
func StringSliceToList(list []string) []interface{} {
40+
vs := make([]interface{}, 0, len(list))
41+
for _, v := range list {
42+
vs = append(vs, v)
43+
}
44+
return vs
45+
}
46+
47+
func StringSliceToSet(src []string) *schema.Set {
48+
return schema.NewSet(schema.HashString, StringSliceToList(src))
49+
}
50+
51+
func Int32SliceToIntList(list []int32) []interface{} {
52+
vs := make([]interface{}, 0, len(list))
53+
for _, v := range list {
54+
vs = append(vs, int(v))
55+
}
56+
return vs
57+
}
58+
59+
func Int32SliceToSet(src []int32) *schema.Set {
60+
return schema.NewSet(schema.HashInt, Int32SliceToIntList(src))
61+
}
62+
63+
func ListOfSetsToStringSlice(listSet []interface{}) [][]string {
64+
ret := make([][]string, 0, len(listSet))
65+
for _, set := range listSet {
66+
ret = append(ret, SetToStringSlice(set.(*schema.Set)))
67+
}
68+
return ret
69+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package provider
1+
package common
22

33
import (
44
"bytes"
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package provider
1+
package common
22

33
import (
44
"fmt"
@@ -8,21 +8,21 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
)
1010

11-
// schemaDiffFloat32 is a SchemaDiffSuppressFunc for diffing float32 values.
11+
// SchemaDiffFloat32 is a SchemaDiffSuppressFunc for diffing float32 values.
1212
// schema.TypeFloat uses float64, which is a problem for API types that use
1313
// float32. Terraform automatically converts float32 to float64 which changes
1414
// the precision and causes incorrect diffs.
1515
//
1616
// For example, synthetic_monitoring.Probe.Latitude is float32. Attempting to
1717
// set grafanacloud_synthetic_monitoring_probe.latitude to 27.98606 results in
1818
// 27.986059188842773. The solution is to diff old and new values as float32.
19-
func schemaDiffFloat32(k, old string, nw string, d *schema.ResourceData) bool {
19+
func SchemaDiffFloat32(k, old string, nw string, d *schema.ResourceData) bool {
2020
old32, _ := strconv.ParseFloat(old, 32)
2121
nw32, _ := strconv.ParseFloat(nw, 32)
2222
return old32 == nw32
2323
}
2424

25-
func cloneResourceSchemaForDatasource(r *schema.Resource, updates map[string]*schema.Schema) map[string]*schema.Schema {
25+
func CloneResourceSchemaForDatasource(r *schema.Resource, updates map[string]*schema.Schema) map[string]*schema.Schema {
2626
resourceSchema := r.Schema
2727
clone := make(map[string]*schema.Schema)
2828
for k, v := range resourceSchema {
@@ -47,6 +47,6 @@ func cloneResourceSchemaForDatasource(r *schema.Resource, updates map[string]*sc
4747
return clone
4848
}
4949

50-
func allowedValuesDescription(description string, allowedValues []string) string {
50+
func AllowedValuesDescription(description string, allowedValues []string) string {
5151
return fmt.Sprintf("%s. Allowed values: `%s`.", description, strings.Join(allowedValues, "`, `"))
5252
}

provider/data_source_cloud_stack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func DatasourceCloudStack() *schema.Resource {
1212
return &schema.Resource{
1313
Description: "Data source for Grafana Stack",
1414
ReadContext: datasourceCloudStackRead,
15-
Schema: cloneResourceSchemaForDatasource(ResourceCloudStack(), map[string]*schema.Schema{
15+
Schema: common.CloneResourceSchemaForDatasource(ResourceCloudStack(), map[string]*schema.Schema{
1616
"slug": {
1717
Type: schema.TypeString,
1818
Required: true,

provider/data_source_data_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func DatasourceDatasource() *schema.Resource {
1414
return &schema.Resource{
1515
Description: "Get details about a Grafana Datasource querying by either name, uid or ID",
1616
ReadContext: datasourceDatasourceRead,
17-
Schema: cloneResourceSchemaForDatasource(ResourceDataSource(), map[string]*schema.Schema{
17+
Schema: common.CloneResourceSchemaForDatasource(ResourceDataSource(), map[string]*schema.Schema{
1818
"id": {
1919
Type: schema.TypeString,
2020
Optional: true,

provider/data_source_library_panel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func DatasourceLibraryPanel() *schema.Resource {
1212
return &schema.Resource{
1313
Description: "Data source for retrieving a single library panel by name or uid.",
1414
ReadContext: dataSourceLibraryPanelRead,
15-
Schema: cloneResourceSchemaForDatasource(ResourceLibraryPanel(), map[string]*schema.Schema{
15+
Schema: common.CloneResourceSchemaForDatasource(ResourceLibraryPanel(), map[string]*schema.Schema{
1616
"name": {
1717
Type: schema.TypeString,
1818
Optional: true,

provider/data_source_organization_preferences.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func DatasourceOrganizationPreferences() *schema.Resource {
1515
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/preferences/#get-current-org-prefs)
1616
`,
1717
ReadContext: dataSourceOrganizationPreferencesRead,
18-
Schema: cloneResourceSchemaForDatasource(ResourceOrganizationPreferences(), map[string]*schema.Schema{}),
18+
Schema: common.CloneResourceSchemaForDatasource(ResourceOrganizationPreferences(), map[string]*schema.Schema{}),
1919
}
2020
}
2121

provider/data_source_synthetic_monitoring_probe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func DatasourceSyntheticMonitoringProbe() *schema.Resource {
1515
return &schema.Resource{
1616
Description: "Data source for retrieving a single probe by name.",
1717
ReadContext: dataSourceSyntheticMonitoringProbeRead,
18-
Schema: cloneResourceSchemaForDatasource(ResourceSyntheticMonitoringProbe(), map[string]*schema.Schema{
18+
Schema: common.CloneResourceSchemaForDatasource(ResourceSyntheticMonitoringProbe(), map[string]*schema.Schema{
1919
"name": {
2020
Description: "Name of the probe.",
2121
Type: schema.TypeString,

provider/resource_alerting_contact_point_notifiers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
gapi "github.com/grafana/grafana-api-golang-client"
9+
"github.com/grafana/terraform-provider-grafana/provider/common"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1112
)
@@ -338,7 +339,7 @@ func packAddrs(addrs string) []string {
338339
}
339340

340341
func unpackAddrs(addrs []interface{}) string {
341-
strs := listToStringSlice(addrs)
342+
strs := common.ListToStringSlice(addrs)
342343
return strings.Join(strs, addrSeparator)
343344
}
344345

0 commit comments

Comments
 (0)