Skip to content

Commit

Permalink
fix: add annotations and labels to resourceTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
artaasadi committed Nov 6, 2024
1 parent 052250a commit 51fb020
Show file tree
Hide file tree
Showing 3 changed files with 2,525 additions and 1,587 deletions.
9 changes: 5 additions & 4 deletions pkg/sdk/models/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ type SingleResourceDescriber func(context.Context, configs.AccountCredentials, e

type ResourceType struct {
IntegrationType integration.Type

ResourceName string

Tags map[string][]string
ResourceName string

ListDescriber ResourceDescriber
GetDescriber SingleResourceDescriber

Annotations map[string]string
Labels map[string]string
Tags map[string][]string
}

func (r ResourceType) GetIntegrationType() integration.Type {
Expand Down
88 changes: 77 additions & 11 deletions pkg/sdk/runable/resource_type/resource_types_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,49 @@ import (
"text/template"
)

// Embed the JSON file containing resource types
//
//go:embed resource-types.json
var ResourceTypes string

// Define the ResourceType struct with Labels and Annotations
type ResourceType struct {
ResourceName string
Tags map[string][]string
TagsString string `json:"-"`
ListDescriber string
GetDescriber string
SteampipeTable string
Model string
ResourceName string
Tags map[string][]string
TagsString string `json:"-"`
ListDescriber string
GetDescriber string
SteampipeTable string
Model string
Annotations map[string]string
Labels map[string]string
AnnotationsString string `json:"-"`
LabelsString string `json:"-"`
}

var (
output = flag.String("output", "", "")
indexMap = flag.String("index-map", "", "")
output = flag.String("output", "", "Path to the output file for resource types")
indexMap = flag.String("index-map", "", "Path to the output file for index map")
)

func main() {
flag.Parse()

var resourceTypes []ResourceType

// Parse the embedded JSON into resourceTypes slice
if err := json.Unmarshal([]byte(ResourceTypes), &resourceTypes); err != nil {
panic(err)
}

// Define the template with Labels and Annotations included
tmpl, err := template.New("").Parse(fmt.Sprintf(`
"{{ .ResourceName }}": {
IntegrationType: configs.IntegrationName,
ResourceName: "{{ .ResourceName }}",
Tags: {{ .TagsString }},
Labels: {{ .LabelsString }},
Annotations: {{ .AnnotationsString }},
ListDescriber: {{ .ListDescriber }},
GetDescriber: {{ if .GetDescriber }}{{ .GetDescriber }}{{ else }}nil{{ end }},
},
Expand All @@ -52,6 +63,7 @@ func main() {
panic(err)
}

// Set default output paths if not provided
if output == nil || len(*output) == 0 {
v := "resource_types.go"
output = &v
Expand All @@ -62,6 +74,7 @@ func main() {
indexMap = &v
}

// Initialize a strings.Builder to construct the output file content
b := &strings.Builder{}
b.WriteString(fmt.Sprintf(`package provider
import (
Expand All @@ -71,9 +84,12 @@ import (
)
var ResourceTypes = map[string]model.ResourceType{
`, configs.OGPluginRepoURL))

// Iterate over each resource type to build its string representations
for _, resourceType := range resourceTypes {
var arr []string

// Build TagsString
tagsStringBuilder := strings.Builder{}
tagsStringBuilder.WriteString("map[string][]string{\n")

Expand All @@ -86,25 +102,69 @@ var ResourceTypes = map[string]model.ResourceType{
tagsLines = append(tagsLines, fmt.Sprintf(" \"%s\": {%s},\n", k, strings.Join(arr, ",")))
}

sort.Strings(tagsLines)
sort.Strings(tagsLines) // Sort for consistency
for _, l := range tagsLines {
tagsStringBuilder.WriteString(l)
}

tagsStringBuilder.WriteString(" }")
resourceType.TagsString = tagsStringBuilder.String()

// Build LabelsString
labelsStringBuilder := strings.Builder{}
labelsStringBuilder.WriteString("map[string]string{\n")

var labelsLines []string
for k, v := range resourceType.Labels {
// Escape quotes in keys and values
escapedKey := escapeString(k)
escapedValue := escapeString(v)
labelsLines = append(labelsLines, fmt.Sprintf(" \"%s\": \"%s\",\n", escapedKey, escapedValue))
}

sort.Strings(labelsLines) // Sort for consistency
for _, l := range labelsLines {
labelsStringBuilder.WriteString(l)
}

labelsStringBuilder.WriteString(" }")
resourceType.LabelsString = labelsStringBuilder.String()

// Build AnnotationsString
annotationsStringBuilder := strings.Builder{}
annotationsStringBuilder.WriteString("map[string]string{\n")

var annotationsLines []string
for k, v := range resourceType.Annotations {
// Escape quotes in keys and values
escapedKey := escapeString(k)
escapedValue := escapeString(v)
annotationsLines = append(annotationsLines, fmt.Sprintf(" \"%s\": \"%s\",\n", escapedKey, escapedValue))
}

sort.Strings(annotationsLines) // Sort for consistency
for _, l := range annotationsLines {
annotationsStringBuilder.WriteString(l)
}

annotationsStringBuilder.WriteString(" }")
resourceType.AnnotationsString = annotationsStringBuilder.String()

// Execute the template with the current resourceType
err = tmpl.Execute(b, resourceType)
if err != nil {
panic(err)
}
}
b.WriteString("}\n")

// Write the generated content to the output file
err = os.WriteFile(*output, []byte(b.String()), os.ModePerm)
if err != nil {
panic(err)
}

// Generate the index map file as before
b = &strings.Builder{}
b.WriteString(fmt.Sprintf(`package steampipe
Expand All @@ -129,14 +189,20 @@ var DescriptionMap = map[string]interface{}{
var ReverseMap = map[string]string{
`))

// reverse map
// Build the reverse map
for _, resourceType := range resourceTypes {
b.WriteString(fmt.Sprintf(" \"%s\": \"%s\",\n", resourceType.SteampipeTable, resourceType.ResourceName))
}
b.WriteString("}\n")

// Write the index map to the specified file
err = os.WriteFile(*indexMap, []byte(b.String()), os.ModePerm)
if err != nil {
panic(err)
}
}

// escapeString ensures that any quotes in the strings are properly escaped
func escapeString(s string) string {
return strings.ReplaceAll(s, `"`, `\"`)
}
Loading

0 comments on commit 51fb020

Please sign in to comment.