Skip to content

Commit

Permalink
WIP: Remove Tfid from our codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
kklimonda-cl committed Sep 9, 2024
1 parent ef492d8 commit cf9e803
Show file tree
Hide file tree
Showing 12 changed files with 540 additions and 386 deletions.
2 changes: 2 additions & 0 deletions assets/terraform/internal/manager/entry_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manager

import (
"context"
"log/slog"

sdkerrors "github.com/PaloAltoNetworks/pango/errors"
)
Expand Down Expand Up @@ -56,6 +57,7 @@ func (o *ImportableEntryObjectManager[E, L, IL, IS]) Create(ctx context.Context,
}

obj, err := o.service.Create(ctx, location, importLocs, entry)
slog.Warn("KK: HERE5-1", "obj", obj, "err", err)
return obj, err
}

Expand Down
6 changes: 3 additions & 3 deletions assets/terraform/internal/manager/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func MultiConfig[E sdkmanager.UuidObject](updates *xmlapi.MultiConfig, existingP
EntryName: entryName,
}

switch MultiConfigOperType(op) {
case MultiConfigOperSet, MultiConfigOperEdit:
switch op {
case "set", "edit":
entry := oper.Data.(E)

if existing, found := entriesByName[entryName]; found {
Expand All @@ -123,7 +123,7 @@ func MultiConfig[E sdkmanager.UuidObject](updates *xmlapi.MultiConfig, existingP
uuid += 1
idx += 1
}
case MultiConfigOperDelete:
case "delete":
fixIndices(entriesByName[entryName].Idx)
delete(entriesByName, entryName)
idx -= 1
Expand Down
24 changes: 10 additions & 14 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 @@ -493,17 +492,15 @@ func (o *UuidObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L,
}

processedEntry, found := processedStateEntries[existingEntryName]
if !found {
if exhaustive == Exhaustive {
// If existing entry is not found in the processedStateEntries map,
// entry is not managed by terraform. If Exhaustive update has been
// called, delete it from the server.
updates.Add(&xmlapi.Config{
Action: "delete",
Xpath: util.AsXpath(path),
Target: o.client.GetTarget(),
})
}
if !found && exhaustive == Exhaustive {
// If existing entry is not found in the processedStateEntries map,
// entry is not managed by terraform. If Exhaustive update has been
// called, delete it from the server.
updates.Add(&xmlapi.Config{
Action: "delete",
Xpath: util.AsXpath(path),
Target: o.client.GetTarget(),
})
continue
}

Expand Down Expand Up @@ -645,11 +642,10 @@ func (o *UuidObjectManager[E, L, S]) ReadMany(ctx context.Context, location L, s
commonCount := 0
for _, elt := range existing {
if stateEntry, found := stateEntriesByName[elt.EntryName()]; found {
commonCount++
stateEntry.State = entryOk
stateEntry.Entry = elt
stateEntry.StateIdx = commonCount
stateEntriesByName[elt.EntryName()] = stateEntry
commonCount += 1
}
}

Expand Down
32 changes: 1 addition & 31 deletions assets/terraform/internal/manager/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,44 +147,14 @@ var _ = Describe("Server", func() {
})
})

Context("updating existing entries", func() {
Context("when some of the entries are out of order", func() {
BeforeEach(func() {
initial = []*MockUuidObject{{Name: "1", Value: "A"}, {Name: "2", Value: "B"}, {Name: "3", Value: "C"}}
client = NewMockUuidClient(initial)
service = NewMockUuidService[*MockUuidObject, MockLocation](client)
var ok bool
if mockService, ok = service.(*MockUuidService[*MockUuidObject, MockLocation]); !ok {
panic("failed to cast service to mockService")
}
manager = sdkmanager.NewUuidObjectManager(client, service, MockUuidSpecifier, MockUuidMatcher)

})
FIt("should move the entries in order", func() {
entries := []*MockUuidObject{{Name: "1", Value: "A"}, {Name: "3", Value: "C"}, {Name: "2", Value: "B"}}

processed, err := manager.ReadMany(ctx, location, entries, sdkmanager.NonExhaustive)

Expect(err).ToNot(HaveOccurred())
Expect(processed).To(HaveLen(3))
Expect(processed).NotTo(MatchEntries(entries))

processed, err = manager.UpdateMany(ctx, location, entries, entries, sdkmanager.NonExhaustive, rule.Position{First: &trueVal})
Expect(err).ToNot(HaveOccurred())
Expect(processed).To(HaveLen(3))

Expect(processed).NotTo(Equal(entries))
})
})
})

Context("initially has some entries", func() {
Context("when creating new entries with NonExhaustive type", func() {
Context("and position is set to Last", func() {
It("should create new entries on the bottom of the list", func() {
entries := []*MockUuidObject{{Name: "4", Value: "D"}, {Name: "5", Value: "E"}, {Name: "6", Value: "F"}}

processed, err := manager.CreateMany(ctx, location, entries, sdkmanager.NonExhaustive, rule.Position{Last: &trueVal})

Expect(err).ToNot(HaveOccurred())
Expect(processed).To(HaveLen(3))

Expand Down
174 changes: 0 additions & 174 deletions assets/terraform/internal/provider/tfid.go

This file was deleted.

14 changes: 12 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
names := make(map[string]string)

for _, specPath := range c.specs {
log.Printf("Parsing %s...\n", specPath)
Expand Down Expand Up @@ -126,13 +127,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 {
names[k] = v
}
}

if pluralVariant {
Expand All @@ -149,13 +154,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 {
names[k] = v
}
}
} else if c.commandType == properties.CommandTypeSDK && !spec.GoSdkSkip {
generator := generate.NewCreator(config.Output.GoSdk, c.templatePath, spec)
Expand All @@ -173,6 +182,7 @@ func (c *Command) Execute() error {
newProviderObject := properties.NewTerraformProviderFile(providerSpec.Name)
newProviderObject.DataSources = append(newProviderObject.DataSources, dataSourceList...)
newProviderObject.Resources = append(newProviderObject.Resources, resourceList...)
newProviderObject.Names = names

terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, providerSpec)
err = terraformGenerator.RenderTerraformProvider(newProviderObject, providerSpec, config.TerraformProviderConfig)
Expand Down
Loading

0 comments on commit cf9e803

Please sign in to comment.