Skip to content
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
17 changes: 16 additions & 1 deletion mmv1/api/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,22 @@ func (p *Product) SetDisplayName() {
}

func (p *Product) SetCompiler(t string) {
p.Compiler = fmt.Sprintf("%s-codegen", strings.ToLower(t))
switch t {
case "tgc":
p.Compiler = "terraformgoogleconversion-codegen"
case "tgc_next":
p.Compiler = "terraformgoogleconversionnext-codegen"
case "tgc_cai2hcl":
p.Compiler = "caitoterraformconversion-codegen"
case "terraform", "oics", "bics":
p.Compiler = "terraform-codegen"
default:
p.Compiler = fmt.Sprintf("%s-codegen", strings.ToLower(t))
}
}

func (p Product) IsTgcCompiler() bool {
return p.Compiler == "terraformgoogleconversionnext-codegen"
}

// ====================
Expand Down
23 changes: 10 additions & 13 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,6 @@ type Resource struct {
// The version name provided by the user through CI
TargetVersionName string `yaml:"-"`

// The compiler to generate the downstream files, for example "terraformgoogleconversion-codegen".
Compiler string `yaml:"-"`

// The API "resource type kind" used for this resource e.g., "Function".
// If this is not set, then :name is used instead, which is strongly
// preferred wherever possible. Its main purpose is for supporting
Expand Down Expand Up @@ -896,11 +893,15 @@ func (r *Resource) addWriteOnlyFields(props []*Type, propWithWoConfigured *Type)
if len(propWithWoConfigured.RequiredWith) > 0 {
log.Fatalf("WriteOnly property '%s' in resource '%s' cannot have RequiredWith set. This combination is not supported.", propWithWoConfigured.Name, r.Name)
}
woFieldName := fmt.Sprintf("%sWo", propWithWoConfigured.Name)
woVersionFieldName := fmt.Sprintf("%sVersion", woFieldName)
writeOnlyField := buildWriteOnlyField(woFieldName, woVersionFieldName, propWithWoConfigured)
writeOnlyVersionField := buildWriteOnlyVersionField(woVersionFieldName, propWithWoConfigured, writeOnlyField)
props = append(props, writeOnlyField, writeOnlyVersionField)
// Don't add write only fields to tgc, as write only fields don't exist in tfplan json,
// the input of tfplan2cai.
if !strings.Contains(r.ProductMetadata.Compiler, "terraformgoogleconversion") {
woFieldName := fmt.Sprintf("%sWo", propWithWoConfigured.Name)
woVersionFieldName := fmt.Sprintf("%sVersion", woFieldName)
writeOnlyField := buildWriteOnlyField(woFieldName, woVersionFieldName, propWithWoConfigured)
writeOnlyVersionField := buildWriteOnlyVersionField(woVersionFieldName, propWithWoConfigured, writeOnlyField)
props = append(props, writeOnlyField, writeOnlyVersionField)
}
return props
}

Expand Down Expand Up @@ -1444,10 +1445,6 @@ func ignoreReadFields(props []*Type) []string {
return fields
}

func (r *Resource) SetCompiler(t string) {
r.Compiler = fmt.Sprintf("%s-codegen", strings.ToLower(t))
}

// Returns the id format of an object, or self_link_uri if none is explicitly defined
// We prefer the long name of a resource as the id so that users can reference
// resources in a standard way, and most APIs accept short name, long name or self_link
Expand Down Expand Up @@ -2592,5 +2589,5 @@ func (r Resource) CaiResourceName() string {
}

func (r Resource) IsTgcCompiler() bool {
return r.Compiler == "terraformgoogleconversionnext-codegen"
return r.ProductMetadata.Compiler == "terraformgoogleconversionnext-codegen"
}
48 changes: 36 additions & 12 deletions mmv1/api/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,15 @@ func TestHasPostCreateComputedFields(t *testing.T) {
func TestResourceAddExtraFields(t *testing.T) {
t.Parallel()

createTestResource := func(name string) *Resource {
return &Resource{
createTestResource := func(name, pn string) *Resource {
r := &Resource{
Name: name,
ProductMetadata: &Product{
Name: "testproduct",
},
}
r.ProductMetadata.SetCompiler(pn)
return r
}

createTestType := func(name, typeStr string, options ...func(*Type)) *Type {
Expand Down Expand Up @@ -439,7 +441,7 @@ func TestResourceAddExtraFields(t *testing.T) {
t.Run("WriteOnly property adds companion fields", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")
writeOnlyProp := createTestType("password", "String",
withWriteOnly(true),
withRequired(true),
Expand Down Expand Up @@ -484,10 +486,32 @@ func TestResourceAddExtraFields(t *testing.T) {
}
})

t.Run("WriteOnly property doesn't add companion fields for tgc", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource", "terraformgoogleconversionnext")
writeOnlyProp := createTestType("password", "String",
withWriteOnly(true),
withRequired(true),
withDescription("A password field"),
)

props := []*Type{writeOnlyProp}
result := resource.AddExtraFields(props, nil)

if len(result) != 1 {
t.Errorf("Expected 1 property as WriteOnly fields should not be added, got %d", len(result))
}

if writeOnlyProp.WriteOnly {
t.Error("Original WriteOnly property should have WriteOnly set to false after processing")
}
})

t.Run("KeyValueLabels property adds terraform and effective labels", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")
labelsType := &Type{
Name: "labels",
Type: "KeyValueLabels",
Expand Down Expand Up @@ -540,7 +564,7 @@ func TestResourceAddExtraFields(t *testing.T) {
t.Run("KeyValueLabels with ExcludeAttributionLabel adds different CustomDiff", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")
resource.ExcludeAttributionLabel = true

labelsType := &Type{
Expand All @@ -560,7 +584,7 @@ func TestResourceAddExtraFields(t *testing.T) {
t.Run("KeyValueLabels with metadata parent adds metadata CustomDiff", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")
parent := &Type{Name: "metadata"}

labelsType := &Type{
Expand All @@ -580,7 +604,7 @@ func TestResourceAddExtraFields(t *testing.T) {
t.Run("KeyValueAnnotations property adds effective annotations", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")
annotationsType := &Type{
Name: "annotations",
Type: "KeyValueAnnotations",
Expand Down Expand Up @@ -621,7 +645,7 @@ func TestResourceAddExtraFields(t *testing.T) {
t.Run("NestedObject with properties processes recursively", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")

nestedWriteOnly := createTestType("nestedPassword", "String", withWriteOnly(true))
nestedObject := createTestType("config", "NestedObject", withProperties([]*Type{nestedWriteOnly}))
Expand All @@ -645,7 +669,7 @@ func TestResourceAddExtraFields(t *testing.T) {
t.Run("Empty NestedObject properties are not processed", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")
emptyNestedObject := createTestType("config", "NestedObject", withProperties([]*Type{}))

props := []*Type{emptyNestedObject}
Expand All @@ -662,7 +686,7 @@ func TestResourceAddExtraFields(t *testing.T) {
t.Run("WriteOnly property already ending with Wo is skipped", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")
woProperty := createTestType("passwordWo", "String", withWriteOnly(true))

props := []*Type{woProperty}
Expand All @@ -680,7 +704,7 @@ func TestResourceAddExtraFields(t *testing.T) {
t.Run("Regular properties are passed through unchanged", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")
regularProp := createTestType("name", "String", withRequired(true))

props := []*Type{regularProp}
Expand All @@ -701,7 +725,7 @@ func TestResourceAddExtraFields(t *testing.T) {
t.Run("Multiple property types processed correctly", func(t *testing.T) {
t.Parallel()

resource := createTestResource("testresource")
resource := createTestResource("testresource", "terraform")

regularProp := createTestType("name", "String")
writeOnlyProp := createTestType("password", "String", withWriteOnly(true))
Expand Down
2 changes: 1 addition & 1 deletion mmv1/api/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func (t *Type) GetPrefix() string {
if t.ParentMetadata == nil {
nestedPrefix := ""
// TODO: Use the nestedPrefix for tgc provider to be consistent with terraform provider
if t.ResourceMetadata.NestedQuery != nil && !strings.Contains(t.ResourceMetadata.Compiler, "terraformgoogleconversion") {
if t.ResourceMetadata.NestedQuery != nil && !strings.Contains(t.ResourceMetadata.ProductMetadata.Compiler, "terraformgoogleconversion") {
nestedPrefix = "Nested"
}

Expand Down
11 changes: 6 additions & 5 deletions mmv1/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewLoader(config Config) *Loader {
return l
}

func (l *Loader) LoadProducts() {
func (l *Loader) LoadProducts(providerName string) {
if l.version == "" {
log.Printf("No version specified, assuming ga")
l.version = "ga"
Expand Down Expand Up @@ -91,10 +91,10 @@ func (l *Loader) LoadProducts() {
}
}

l.Products = l.batchLoadProducts(allProductFiles)
l.Products = l.batchLoadProducts(allProductFiles, providerName)
}

func (l *Loader) batchLoadProducts(productNames []string) map[string]*api.Product {
func (l *Loader) batchLoadProducts(productNames []string, providerName string) map[string]*api.Product {
products := make(map[string]*api.Product)

// Create result type for clarity
Expand All @@ -116,7 +116,7 @@ func (l *Loader) batchLoadProducts(productNames []string) map[string]*api.Produc
go func(name string) {
defer wg.Done()

product, err := l.LoadProduct(name)
product, err := l.LoadProduct(name, providerName)
productChan <- loadResult{
name: name,
product: product,
Expand Down Expand Up @@ -153,7 +153,7 @@ func (l *Loader) batchLoadProducts(productNames []string) map[string]*api.Produc

// Load compiles a product with all its resources from the given path and optional overrides
// This loads the product configuration and all its resources into memory without generating any files
func (l *Loader) LoadProduct(productName string) (*api.Product, error) {
func (l *Loader) LoadProduct(productName, providerName string) (*api.Product, error) {
p := &api.Product{}
productYamlPath := filepath.Join(productName, "product.yaml")

Expand Down Expand Up @@ -200,6 +200,7 @@ func (l *Loader) LoadProduct(productName string) (*api.Product, error) {
p.Version = p.VersionObjOrClosest(l.version)

p.Objects = resources
p.SetCompiler(providerName)
p.Validate()

return p, nil
Expand Down
2 changes: 1 addition & 1 deletion mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func GenerateProducts(product, resource, providerName, version, outputPath, base
}

loader := loader.NewLoader(loader.Config{Version: version, BaseDirectory: baseDirectory, OverrideDirectory: overrideDirectory, Sysfs: ofs})
loader.LoadProducts()
loader.LoadProducts(providerName)
loader.AddExtraFields()
loader.Validate()
loadedProducts := loader.Products
Expand Down
2 changes: 0 additions & 2 deletions mmv1/provider/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ func NewTerraform(product *api.Product, versionName string, startTime time.Time,
templateFS: templateFS,
}

t.Product.SetCompiler(ProviderName(t))
t.Product.ImportPath = ImportPathFromVersion(versionName)
for _, r := range t.Product.Objects {
r.SetCompiler(ProviderName(t))
r.ImportPath = t.Product.ImportPath
}

Expand Down
2 changes: 0 additions & 2 deletions mmv1/provider/terraform_tgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ func NewTerraformGoogleConversion(product *api.Product, versionName string, star
templateFS: templateFS,
}

t.Product.SetCompiler(ProviderName(t))
for _, r := range t.Product.Objects {
r.SetCompiler(ProviderName(t))
r.ImportPath = ImportPathFromVersion(versionName)
}

Expand Down
2 changes: 0 additions & 2 deletions mmv1/provider/terraform_tgc_cai2hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ func NewCaiToTerraformConversion(product *api.Product, versionName string, start
templateFS: templateFS,
}

t.Product.SetCompiler(ProviderName(t))
for _, r := range t.Product.Objects {
r.SetCompiler(ProviderName(t))
r.ImportPath = ImportPathFromVersion(versionName)
}

Expand Down
2 changes: 0 additions & 2 deletions mmv1/provider/terraform_tgc_next.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ func NewTerraformGoogleConversionNext(product *api.Product, versionName string,
templateFS: templateFS,
}

t.Product.SetCompiler(ProviderName(t))
for _, r := range t.Product.Objects {
r.SetCompiler(ProviderName(t))
r.ImportPath = ImportPathFromVersion(versionName)
}

Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/agent_pool.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if not (contains $.Compiler "terraformgoogleconversion") }}
{{- if not (contains $.ProductMetadata.Compiler "terraformgoogleconversion") }}
// waitForAgentPoolReady waits for an agent pool to leave the
// "CREATING" state and become "CREATED", to indicate that it's ready.
func waitForAgentPoolReady(d *schema.ResourceData, config *transport_tpg.Config, timeout time.Duration) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if not (contains $.Compiler "terraformgoogleconversion") }}
{{- if not (contains $.ProductMetadata.Compiler "terraformgoogleconversion") }}
// waitForNatAddressReady waits for an NatAddress to leave the
// "CREATING" state and become "RESERVED", to indicate that it's ready.
func waitForNatAddressReserved(d *schema.ResourceData, config *transport_tpg.Config, timeout time.Duration) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func sensitiveParamCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v
}
}

{{- if not (contains $.Compiler "terraformgoogleconversion") }}
{{- if not (contains $.ProductMetadata.Compiler "terraformgoogleconversion") }}
for _, sp := range sensitiveWoParams {
mapLabel := diff.Get("params." + sp[:len(sp)-3]).(string)
authLabel, _ := diff.GetRawConfigAt(cty.GetAttrPath("sensitive_params").IndexInt(0).GetAttr(sp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func validateDefaultTableExpirationMs(v interface{}, k string) (ws []string, err
return
}

{{- if ne $.Compiler "terraformgoogleconversion-codegen" }}
{{- if ne $.ProductMetadata.Compiler "terraformgoogleconversion-codegen" }}
// bigqueryDatasetAccessHash is a custom hash function for the access block.
// It normalizes
// 1) the 'role' field before hashing, treating legacy roles
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if not (contains $.Compiler "terraformgoogleconversion") }}
{{- if not (contains $.ProductMetadata.Compiler "terraformgoogleconversion") }}
// waitForRegistrationActive waits for a registration to leave the
// "REGISTRATION_PENDING" state and become "ACTIVE" or any other state.
func waitForRegistrationActive(d *schema.ResourceData, config *transport_tpg.Config, timeout time.Duration) error {
Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/colab_runtime.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ModifyColabRuntimeOperation(config *transport_tpg.Config, d *schema.Resourc
return res, nil
}

{{- if not (contains $.Compiler "terraformgoogleconversion") }}
{{- if not (contains $.ProductMetadata.Compiler "terraformgoogleconversion") }}
func waitForColabOperation(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string, response map[string]interface{}) error {
var opRes map[string]interface{}
err := ColabOperationWaitTimeWithResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func resourceDatastreamStreamCustomDiff(_ context.Context, diff *schema.Resource
return resourceDatastreamStreamCustomDiffFunc(diff)
}

{{- if not (contains $.Compiler "terraformgoogleconversion") }}
{{- if not (contains $.ProductMetadata.Compiler "terraformgoogleconversion") }}
// waitForDatastreamStreamReady waits for an agent pool to reach a stable state to indicate that it's ready.
func waitForDatastreamStreamReady(d *schema.ResourceData, config *transport_tpg.Config, timeout time.Duration) error {
return retry.Retry(timeout, func() *retry.RetryError {
Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/disk.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "errors"

var _ = errors.New

{{- if ne $.Compiler "terraformgoogleconversion-codegen" }}
{{- if ne $.ProductMetadata.Compiler "terraformgoogleconversion-codegen" }}
// diffsuppress for hyperdisk provisioned_iops
func hyperDiskIopsUpdateDiffSuppress(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
if !strings.Contains(d.Get("type").(string), "hyperdisk") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if not (contains $.Compiler "terraformgoogleconversion") }}
{{- if not (contains $.ProductMetadata.Compiler "terraformgoogleconversion") }}
// waitforConnectionReady waits for an connecion to leave the
// "CREATING" state, to indicate that it's ready.
func waitforConnectionReady(d *schema.ResourceData, config *transport_tpg.Config, timeout time.Duration) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if not (contains $.Compiler "terraformgoogleconversion") }}
{{- if not (contains $.ProductMetadata.Compiler "terraformgoogleconversion") }}
// waitForAttachmentToBeProvisioned waits for an attachment to leave the
// "UNPROVISIONED" state, to indicate that it's either ready or awaiting partner
// activity.
Expand Down
Loading
Loading