Skip to content

Commit

Permalink
Implement resource importing without Tfid
Browse files Browse the repository at this point in the history
move schemaType type to properties package

Support for importing list resources
  • Loading branch information
kklimonda-cl committed Sep 23, 2024
1 parent 73cfe3f commit 22e3bd8
Show file tree
Hide file tree
Showing 11 changed files with 675 additions and 439 deletions.
3 changes: 1 addition & 2 deletions assets/terraform/internal/manager/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@ func (o *UuidObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L,
} else {
// If entry from the plan was found in the state check if they match and set the
// processedEntry State accordingly.
elt.SetEntryUuid(stateEntry.Entry.EntryUuid())
processedEntry = uuidObjectWithState[E]{
Entry: elt,
StateIdx: idx,
Expand Down Expand Up @@ -503,8 +502,8 @@ func (o *UuidObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L,
Xpath: util.AsXpath(path),
Target: o.client.GetTarget(),
})
continue
}
continue
}

existingEntryUuid := existingEntry.EntryUuid()
Expand Down
174 changes: 0 additions & 174 deletions assets/terraform/internal/provider/tfid.go

This file was deleted.

15 changes: 13 additions & 2 deletions pkg/commands/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (c *Command) Execute() error {
}
var resourceList []string
var dataSourceList []string
specMetadata := make(map[string]properties.TerraformProviderSpecMetadata)

for _, specPath := range c.specs {
log.Printf("Parsing %s...\n", specPath)
Expand Down Expand Up @@ -126,13 +127,18 @@ func (c *Command) Execute() error {
}

terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, resourceTyp)
dataSources, resources, partialNames, err := terraformGenerator.RenderTerraformProviderFile(spec, resourceTyp)
if err != nil {
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
}

resourceList = append(resourceList, resources...)
dataSourceList = append(dataSourceList, dataSources...)

for k, v := range partialNames {
specMetadata[k] = v
}

}

if pluralVariant {
Expand All @@ -149,13 +155,17 @@ func (c *Command) Execute() error {
}

terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, resourceTyp)
dataSources, resources, partialNames, err := terraformGenerator.RenderTerraformProviderFile(spec, resourceTyp)
if err != nil {
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
}

resourceList = append(resourceList, resources...)
dataSourceList = append(dataSourceList, dataSources...)

for k, v := range partialNames {
specMetadata[k] = v
}
}
} else if c.commandType == properties.CommandTypeSDK && !spec.GoSdkSkip {
generator := generate.NewCreator(config.Output.GoSdk, c.templatePath, spec)
Expand All @@ -173,6 +183,7 @@ func (c *Command) Execute() error {
newProviderObject := properties.NewTerraformProviderFile(providerSpec.Name)
newProviderObject.DataSources = append(newProviderObject.DataSources, dataSourceList...)
newProviderObject.Resources = append(newProviderObject.Resources, resourceList...)
newProviderObject.SpecMetadata = specMetadata

terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, providerSpec)
err = terraformGenerator.RenderTerraformProvider(newProviderObject, providerSpec, config.TerraformProviderConfig)
Expand Down
14 changes: 7 additions & 7 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *Creator) RenderTemplate() error {
}

// RenderTerraformProviderFile generates a Go file for a Terraform provider based on the provided TerraformProviderFile and Normalization arguments.
func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, typ properties.ResourceType) ([]string, []string, error) {
func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, typ properties.ResourceType) ([]string, []string, map[string]properties.TerraformProviderSpecMetadata, error) {
var name string
switch typ {
case properties.ResourceUuidPlural:
Expand All @@ -72,19 +72,19 @@ func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, ty
tfp := terraform_provider.GenerateTerraformProvider{}

if err := tfp.GenerateTerraformDataSource(typ, spec, terraformProvider); err != nil {
return nil, nil, err
return nil, nil, nil, err
}

if err := tfp.GenerateTerraformResource(typ, spec, terraformProvider); err != nil {
return nil, nil, err
return nil, nil, nil, err
}

if err := tfp.GenerateCommonCode(typ, spec, terraformProvider); err != nil {
return nil, nil, err
return nil, nil, nil, err
}

if err := tfp.GenerateTerraformProviderFile(spec, terraformProvider); err != nil {
return nil, nil, err
return nil, nil, nil, err
}

var filePath string
Expand All @@ -100,10 +100,10 @@ func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, ty
}

if err := c.writeFormattedContentToFile(filePath, terraformProvider.Code.String()); err != nil {
return nil, nil, err
return nil, nil, nil, err
}

return terraformProvider.DataSources, terraformProvider.Resources, nil
return terraformProvider.DataSources, terraformProvider.Resources, terraformProvider.SpecMetadata, nil
}

// RenderTerraformProvider generates and writes a Terraform provider file.
Expand Down
51 changes: 51 additions & 0 deletions pkg/properties/provider_file.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package properties

import (
"fmt"
"strings"

"github.com/paloaltonetworks/pan-os-codegen/pkg/imports"
"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
)

// NewTerraformProviderFile returns a new handler for a file that the
Expand All @@ -19,16 +21,65 @@ func NewTerraformProviderFile(filename string) *TerraformProviderFile {
ImportManager: imports.NewManager(),
DataSources: make([]string, 0, 10),
Resources: make([]string, 0, 10),
SpecMetadata: make(map[string]TerraformProviderSpecMetadata),
Code: &code,
}
}

type TerraformNameProvider struct {
TfName string
MetaName string
StructName string
DataSourceStructName string
ResourceStructName string
PackageName string
}

func NewTerraformNameProvider(spec *Normalization, resourceTyp ResourceType) *TerraformNameProvider {
var tfName string
switch resourceTyp {
case ResourceEntry, ResourceCustom:
tfName = spec.TerraformProviderConfig.Suffix
case ResourceEntryPlural:
tfName = spec.TerraformProviderConfig.PluralSuffix
case ResourceUuid:
tfName = spec.TerraformProviderConfig.Suffix
case ResourceUuidPlural:
suffix := spec.TerraformProviderConfig.Suffix
pluralName := spec.TerraformProviderConfig.PluralName
tfName = fmt.Sprintf("%s_%s", suffix, pluralName)
}
objectName := tfName

metaName := fmt.Sprintf("_%s", naming.Underscore("", strings.ToLower(objectName), ""))
structName := naming.CamelCase("", tfName, "", true)
dataSourceStructName := naming.CamelCase("", tfName, "DataSource", true)
resourceStructName := naming.CamelCase("", tfName, "Resource", true)
packageName := spec.GoSdkPath[len(spec.GoSdkPath)-1]
return &TerraformNameProvider{tfName, metaName, structName, dataSourceStructName, resourceStructName, packageName}
}

type TerraformSpecFlags uint

const (
TerraformSpecDatasource = 0x01
TerraformSpecResource = 0x02
TerraformSpecImportable = 0x04
)

type TerraformProviderSpecMetadata struct {
ResourceSuffix string
StructName string
Flags TerraformSpecFlags
}

// TerraformProviderFile is a Terraform provider file handler.
type TerraformProviderFile struct {
Filename string
Directory []string
ImportManager *imports.Manager
DataSources []string
Resources []string
SpecMetadata map[string]TerraformProviderSpecMetadata
Code *strings.Builder
}
9 changes: 9 additions & 0 deletions pkg/properties/resourcetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ const (
ResourceUuid ResourceType = iota
ResourceUuidPlural ResourceType = iota
)

type SchemaType int

const (
SchemaResource SchemaType = iota
SchemaDataSource SchemaType = iota
SchemaCommon SchemaType = iota
SchemaProvider SchemaType = iota
)
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ if err := assignParent(ctx, r.client, deviceGroup, parent); err != nil {
return
}
state.Tfid = types.StringValue("")
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
`
const deviceGroupParentResourceUpdate = deviceGroupParentResourceCreate
Expand Down
Loading

0 comments on commit 22e3bd8

Please sign in to comment.