Skip to content

Commit

Permalink
Replace site_configs with site_yamls & output set of IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
atavakoliyext committed Jun 18, 2024
1 parent 088b0e0 commit 07b8ef4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 53 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ $ go build

### Inputs

#### `site_configs`
#### `site_yamls`

- Type: `map(map(any))`
- Type: `map(string)`
- Required

A map of site config maps, keyed by site ID.
A map of site config YAMLs, keyed by site ID.

#### `filter`

Expand All @@ -49,6 +49,6 @@ A single-character separator for the glob.

#### `sites`

- Type: `map(map(any))`
- Type: `set(string)`

A subset of `site_configs` whose FQNs match `filter`.
A set of site IDs from the keys of `site_yamls`, whose FQNs match `filter`.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/gobwas/glob v0.2.3
github.com/hashicorp/terraform v0.14.11
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,9 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
93 changes: 46 additions & 47 deletions site/data_sitefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/gobwas/glob"
"github.com/hashicorp/terraform/helper/schema"

"gopkg.in/yaml.v2"
)

func dataSourceSitefilter() *schema.Resource {
Expand All @@ -22,14 +24,17 @@ func dataSourceSitefilter() *schema.Resource {
Default: ".",
ValidateFunc: validateSeparator,
},
"site_configs": &schema.Schema{
"site_yamls": &schema.Schema{
Type: schema.TypeMap,
Required: true,
ValidateFunc: validateSiteConfigs,
},
"sites": &schema.Schema{
Type: schema.TypeMap,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
Expand All @@ -51,13 +56,8 @@ func validateSeparator(val interface{}, key string) ([]string, []error) {

func validateSiteConfigs(val interface{}, key string) ([]string, []error) {
var errs []error
for id, rawSiteConfig := range val.(map[string]any) {
asMap, ok := rawSiteConfig.(map[string]any)
if !ok {
errs = append(errs, fmt.Errorf("invalid %q[%q]: not a map", key, id))
continue
}
if _, err := NewSiteMetadata(asMap); err != nil {
for id, siteYAML := range val.(map[string]any) {
if _, err := NewSiteMetadata(siteYAML.(string)); err != nil {
errs = append(errs, fmt.Errorf("invalid %q[%d]: %w", key, id, err))
}
}
Expand All @@ -68,7 +68,7 @@ func dataSourceSitefilterRead(d *schema.ResourceData, _ any) error {
var (
filter = d.Get("filter").(string)
separator = d.Get("separator").(string)
rawSiteConfigs = d.Get("site_configs").(map[string]any)
rawSiteConfigs = d.Get("site_yamls").(map[string]any)
)

filterGlob, err := glob.Compile(filter, []rune(separator)[0])
Expand All @@ -78,56 +78,65 @@ func dataSourceSitefilterRead(d *schema.ResourceData, _ any) error {

siteMetadata, err := unmarshalSiteMetadata(rawSiteConfigs)
if err != nil {
return fmt.Errorf("invalid site_configs: %w", err)
return fmt.Errorf("invalid site_yamls: %w", err)
}

filteredSites := map[string]any{}
var matchedSites []string

for key, meta := range siteMetadata {
for id, meta := range siteMetadata {
if filterGlob.Match(meta.FQN()) {
filteredSites[key] = rawSiteConfigs[key]
matchedSites = append(matchedSites, id)
}
}

d.SetId(filter)
d.Set("sites", filteredSites)
d.Set("sites", matchedSites)
return nil
}

func unmarshalSiteMetadata(rawSiteConfigs map[string]any) (map[string]SiteMetadata, error) {
metas := map[string]SiteMetadata{}

for key, raw := range rawSiteConfigs {
asMap, ok := raw.(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid site_configs[%d]: must be a map", key)
}
meta, err := NewSiteMetadata(asMap)
for id, siteYAML := range rawSiteConfigs {
meta, err := NewSiteMetadata(siteYAML.(string))
if err != nil {
return nil, fmt.Errorf("invalid site_configs[%d]: %w", key, err)
return nil, fmt.Errorf("invalid site_yamls[%d]: %w", id, err)
}
metas[key] = meta
metas[id] = meta
}

return metas, nil
}

// SiteMetadata contains the metadata of a site required to construct its
// fully qualified name (FQN).
type SiteMetadata map[string]string
type SiteMetadata struct {
Env string `yaml:"env"`
Partition string `yaml:"partition"`
ServingRole string `yaml:"servingRole"`
DataCenter string `yaml:"dataCenter"`
}

// NewSiteMetadata returns a SiteMetadata constructed from the specified raw config map.
func NewSiteMetadata(rawConfig map[string]any) (SiteMetadata, error) {
m := map[string]string{}
func NewSiteMetadata(configYAML string) (SiteMetadata, error) {
var m SiteMetadata
err := yaml.Unmarshal([]byte(configYAML), &m)
if err != nil {
return m, fmt.Errorf("failed to unmarshal YAML: %w", err)
}

for _, k := range siteConfigRequiredKeys {
v, err := configAsString(rawConfig, k)
if err != nil {
return SiteMetadata(m), fmt.Errorf("invalid key %q: %w", k, err)
}
m[k] = v
switch {
case m.Env == "":
return m, fmt.Errorf("missing key: env")
case m.Partition == "":
return m, fmt.Errorf("missing key: partition")
case m.ServingRole == "":
return m, fmt.Errorf("missing key: servingRole")
case m.DataCenter == "":
return m, fmt.Errorf("missing key: dataCenter")
}
return SiteMetadata(m), nil

return m, nil
}

var siteConfigRequiredKeys = []string{
Expand All @@ -141,19 +150,9 @@ var siteConfigRequiredKeys = []string{
func (m SiteMetadata) FQN() string {
return fmt.Sprintf(
"%s.%s.%s.%s",
m["env"],
m["partition"],
m["servingRole"],
m["dataCenter"],
m.Env,
m.Partition,
m.ServingRole,
m.DataCenter,
)
}

func configAsString(rawSiteConfig map[string]any, key string) (string, error) {
if v, ok := rawSiteConfig[key]; !ok {
return "", fmt.Errorf("%q is missing", key)
} else if vStr, ok := v.(string); !ok {
return "", fmt.Errorf("%q is not a string", key)
} else {
return vStr, nil
}
}

0 comments on commit 07b8ef4

Please sign in to comment.