Skip to content

Commit

Permalink
Cleanup policy definition templates
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Jul 24, 2023
1 parent bd04fa7 commit 666c076
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 1,571 deletions.
41 changes: 3 additions & 38 deletions gen/doc_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ import (
)

const (
featureTemplateDefinitionsPath = "./gen/definitions/feature_templates/"
policyObjectDefinitionsPath = "./gen/definitions/policy_objects/"
policyDefinitionDefinitionsPath = "./gen/definitions/policy_definitions/"
genericDefinitionsPath = "./gen/definitions/generic/"
featureTemplateDefinitionsPath = "./gen/definitions/feature_templates/"
policyObjectDefinitionsPath = "./gen/definitions/policy_objects/"
genericDefinitionsPath = "./gen/definitions/generic/"
)

type YamlConfig struct {
Expand Down Expand Up @@ -65,8 +64,6 @@ func main() {
featureTemplateConfigs := make([]YamlConfig, len(featureTemplateFiles))
policyObjectFiles, _ := ioutil.ReadDir(policyObjectDefinitionsPath)
policyObjectConfigs := make([]YamlConfig, len(policyObjectFiles))
policyDefinitionFiles, _ := ioutil.ReadDir(policyDefinitionDefinitionsPath)
policyDefinitionConfigs := make([]YamlConfig, len(policyDefinitionFiles))
genericFiles, _ := ioutil.ReadDir(genericDefinitionsPath)
genericConfigs := make([]YamlConfig, len(genericFiles))

Expand Down Expand Up @@ -132,38 +129,6 @@ func main() {
}
}

// Load policy definition configs
for i, filename := range policyDefinitionFiles {
yamlFile, err := ioutil.ReadFile(filepath.Join(policyDefinitionDefinitionsPath, filename.Name()))
if err != nil {
log.Fatalf("Error reading file: %v", err)
}

config := YamlConfig{}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Fatalf("Error parsing yaml: %v", err)
}
policyDefinitionConfigs[i] = config
}

// Update policy definition doc category
for i := range policyDefinitionConfigs {
for _, path := range docPaths {
filename := path + SnakeCase(policyDefinitionConfigs[i].Name) + "_policy_definition.md"
content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("Error opening documentation: %v", err)
}

cat := policyDefinitionConfigs[i].DocCategory
s := string(content)
s = strings.ReplaceAll(s, `subcategory: ""`, `subcategory: "`+cat+`"`)

ioutil.WriteFile(filename, []byte(s), 0644)
}
}

// Load generic configs
for i, filename := range genericFiles {
yamlFile, err := ioutil.ReadFile(filepath.Join(genericDefinitionsPath, filename.Name()))
Expand Down
92 changes: 9 additions & 83 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ import (
)

const (
featureTemplateDefinitionsPath = "./gen/definitions/feature_templates/"
featureTemplateModelsPath = "./gen/models/feature_templates/"
policyObjectDefinitionsPath = "./gen/definitions/policy_objects/"
policyDefinitionDefinitionsPath = "./gen/definitions/policy_definitions/"
genericDefinitionsPath = "./gen/definitions/generic/"
providerTemplate = "./gen/templates/provider.go"
providerLocation = "./internal/provider/provider.go"
changelogTemplate = "./gen/templates/changelog.md.tmpl"
changelogLocation = "./templates/guides/changelog.md.tmpl"
changelogOriginal = "./CHANGELOG.md"
featureTemplateDefinitionsPath = "./gen/definitions/feature_templates/"
featureTemplateModelsPath = "./gen/models/feature_templates/"
policyObjectDefinitionsPath = "./gen/definitions/policy_objects/"
genericDefinitionsPath = "./gen/definitions/generic/"
providerTemplate = "./gen/templates/provider.go"
providerLocation = "./internal/provider/provider.go"
changelogTemplate = "./gen/templates/changelog.md.tmpl"
changelogLocation = "./templates/guides/changelog.md.tmpl"
changelogOriginal = "./CHANGELOG.md"
)

type t struct {
Expand Down Expand Up @@ -140,49 +139,6 @@ var policyObjectTemplates = []t{
},
}

var policyDefinitionTemplates = []t{
{
path: "./gen/templates/policy_definitions/model.go",
prefix: "./internal/provider/model_sdwan_",
suffix: "_policy_definition.go",
},
{
path: "./gen/templates/policy_definitions/data_source.go",
prefix: "./internal/provider/data_source_sdwan_",
suffix: "_policy_definition.go",
},
{
path: "./gen/templates/policy_definitions/data_source_test.go",
prefix: "./internal/provider/data_source_sdwan_",
suffix: "_policy_definition_test.go",
},
{
path: "./gen/templates/policy_definitions/resource.go",
prefix: "./internal/provider/resource_sdwan_",
suffix: "_policy_definition.go",
},
{
path: "./gen/templates/policy_definitions/resource_test.go",
prefix: "./internal/provider/resource_sdwan_",
suffix: "_policy_definition_test.go",
},
{
path: "./gen/templates/policy_definitions/data-source.tf",
prefix: "./examples/data-sources/sdwan_",
suffix: "_policy_definition/data-source.tf",
},
{
path: "./gen/templates/policy_definitions/resource.tf",
prefix: "./examples/resources/sdwan_",
suffix: "_policy_definition/resource.tf",
},
{
path: "./gen/templates/policy_definitions/import.sh",
prefix: "./examples/resources/sdwan_",
suffix: "_policy_definition/import.sh",
},
}

var genericTemplates = []t{
{
path: "./gen/templates/generic/model.go",
Expand Down Expand Up @@ -596,7 +552,6 @@ func main() {
providerConfig := make(map[string][]string)
providerConfig["FeatureTemplates"] = make([]string, 0)
providerConfig["PolicyObjects"] = make([]string, 0)
providerConfig["PolicyDefinitions"] = make([]string, 0)
providerConfig["Generic"] = make([]string, 0)

// Load feature template configs
Expand Down Expand Up @@ -654,35 +609,6 @@ func main() {
providerConfig["PolicyObjects"] = append(providerConfig["PolicyObjects"], policyObjectConfigs[i].Name)
}

policyDefinitionFiles, _ := ioutil.ReadDir(policyDefinitionDefinitionsPath)
policyDefinitionConfigs := make([]YamlConfig, len(policyDefinitionFiles))

// Load policy definition configs
for i, filename := range policyDefinitionFiles {
yamlFile, err := os.ReadFile(filepath.Join(policyDefinitionDefinitionsPath, filename.Name()))
if err != nil {
log.Fatalf("Error reading file: %v", err)
}

config := YamlConfig{}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Fatalf("Error parsing yaml: %v", err)
}
policyDefinitionConfigs[i] = config
}

for i := range policyDefinitionConfigs {
// Augment policy definition config
augmentGenericConfig(&policyDefinitionConfigs[i], "policy definition")

// Iterate over templates and render files
for _, t := range policyDefinitionTemplates {
renderTemplate(t.path, t.prefix+SnakeCase(policyDefinitionConfigs[i].Name)+t.suffix, policyDefinitionConfigs[i])
}
providerConfig["PolicyDefinitions"] = append(providerConfig["PolicyDefinitions"], policyDefinitionConfigs[i].Name)
}

genericFiles, _ := ioutil.ReadDir(genericDefinitionsPath)
genericConfigs := make([]YamlConfig, len(genericFiles))

Expand Down
3 changes: 0 additions & 3 deletions gen/templates/policy_definitions/data-source.tf

This file was deleted.

179 changes: 0 additions & 179 deletions gen/templates/policy_definitions/data_source.go

This file was deleted.

Loading

0 comments on commit 666c076

Please sign in to comment.