Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort resources and set in product in go compiler #10135

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion mmv1/api/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Product struct {

// Display Name: The full name of the GCP product; eg "Cloud Bigtable"

Objects []interface{}
Objects []*Resource

// The list of permission scopes available for the service
// For example: `https://www.googleapis.com/auth/compute`
Expand Down Expand Up @@ -69,6 +69,14 @@ type Product struct {
ClientName string `yaml:"client_name"`
}

func (p *Product) Validate() {
// TODO Q1 Rewrite super
// super
for _, o := range p.Objects {
o.ProductMetadata = p
}
}

// def validate
// super
// set_variables @objects, :__product
Expand Down
23 changes: 21 additions & 2 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,28 @@ type Resource struct {
// Add a deprecation message for a resource that's been deprecated in the API.
DeprecationMessage string `yaml:"deprecation_message"`

Properties []Type
Properties []*Type

Parameters []Type
Parameters []*Type

ProductMetadata *Product
}

// TODO: rewrite functions
func (r *Resource) Validate() {
// TODO Q1 Rewrite super
// super

r.setResourceMetada(r.Parameters)
r.setResourceMetada(r.Properties)
}

func (r *Resource) setResourceMetada(properties []*Type) {
if properties == nil {
return
}

for _, property := range properties {
property.ResourceMetadata = r
}
}
5 changes: 2 additions & 3 deletions mmv1/api/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,9 @@ type Type struct {
// just as they are in the standard flattener template.
CustomFlatten string `yaml:"custom_flatten"`

__resource Resource
ResourceMetadata *Resource

// TODO: set a specific type intead of interface{}
__parent interface{} // is nil for top-level properties
ParentMetadata *Type // is nil for top-level properties
}

const MAX_NAME = 20
Expand Down
25 changes: 16 additions & 9 deletions mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ func main() {
// TODO Q1: remove these lines, which are for debugging
// log.Printf("productYamlPath %#v", productYamlPath)

var resources []api.Resource
var resources []*api.Resource = make([]*api.Resource, 0)

productYaml, err := os.ReadFile(productYamlPath)
if err != nil {
log.Fatalf("Cannot open the file: %v", productYaml)
}
productApi := api.Product{}
yamlValidator.Parse(productYaml, &productApi)
productApi := &api.Product{}
yamlValidator.Parse(productYaml, productApi)

// TODO Q1: remove these lines, which are for debugging
// prod, _ := json.Marshal(&productApi)
// prod, _ := json.Marshal(productApi)
// log.Printf("prod %s", string(prod))

if !productApi.ExistsAtVersionOrLower(version) {
Expand Down Expand Up @@ -118,21 +118,29 @@ func main() {
if err != nil {
log.Fatalf("Cannot open the file: %v", resourceYamlPath)
}
resource := api.Resource{}
yamlValidator.Parse(resourceYaml, &resource)
resource := &api.Resource{}
yamlValidator.Parse(resourceYaml, resource)

// TODO Q1: remove these lines, which are for debugging
// res, _ := json.Marshal(&resource)
// res, _ := json.Marshal(resource)
// log.Printf("resource %s", string(res))

// TODO Q1: add labels related fields

resource.Validate()
resources = append(resources, resource)
}

// TODO Q2: override resources
log.Printf("resources before sorting %#v", resources)

// TODO Q1: sort resources by name and set in product
// Sort resources by name
sort.Slice(resources, func(i, j int) bool {
return resources[i].Name < resources[j].Name
})

productApi.Objects = resources
productApi.Validate()

// TODO Q2: set other providers via flag
providerToGenerate := provider.NewTerraform(productApi)
Expand All @@ -142,7 +150,6 @@ func main() {
continue
}

// TODO Q1: generate templates
log.Printf("%s: Generating files", productName)
providerToGenerate.Generate(outputPath, productName, generateCode, generateDocs)
}
Expand Down
2 changes: 1 addition & 1 deletion mmv1/provider/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Terraform struct {
ResourcesForVersion []api.Resource
}

func NewTerraform(product api.Product) *Terraform {
func NewTerraform(product *api.Product) *Terraform {
t := Terraform{ResourceCount: 0, IAMResourceCount: 0}

// TODO Q1
Expand Down