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

fix: Properly handle empty plural resources #291

Merged
merged 2 commits into from
Jan 22, 2025
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
2 changes: 1 addition & 1 deletion assets/terraform/internal/manager/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (o *EntryObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L
}

existing, err = o.service.List(ctx, location, "get", "", "")
if err != nil {
if err != nil && !sdkerrors.IsObjectNotFound(err) {
return nil, fmt.Errorf("Failed to list remote entries: %w", err)
}

Expand Down
21 changes: 18 additions & 3 deletions assets/terraform/internal/manager/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (o *UuidObjectManager[E, L, S]) CreateMany(ctx context.Context, location L,
}

existing, err = o.service.List(ctx, location, "get", "", "")
if err != nil {
if err != nil && !sdkerrors.IsObjectNotFound(err) {
return nil, fmt.Errorf("Failed to list remote entries: %w", err)
}

Expand Down Expand Up @@ -535,6 +535,8 @@ func (o *UuidObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L,
}
}

createOps := make([]*xmlapi.Config, len(planEntries))

for _, elt := range processedStateEntries {
path, err := location.XpathWithEntryName(o.client.Versioning(), elt.Entry.EntryName())
if err != nil {
Expand All @@ -547,7 +549,14 @@ func (o *UuidObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L,
}

switch elt.State {
case entryMissing, entryOutdated:
case entryMissing:
createOps[elt.StateIdx] = &xmlapi.Config{
Action: "edit",
Xpath: util.AsXpath(path),
Element: xmlEntry,
Target: o.client.GetTarget(),
}
case entryOutdated:
updates.Add(&xmlapi.Config{
Action: "edit",
Xpath: util.AsXpath(path),
Expand Down Expand Up @@ -582,6 +591,12 @@ func (o *UuidObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L,
}
}

for _, elt := range createOps {
if elt != nil {
updates.Add(elt)
}
}

if len(updates.Operations) > 0 {
if _, _, _, err := o.client.MultiConfig(ctx, updates, false, nil); err != nil {
return nil, &Error{err: err, message: "failed to execute MultiConfig command"}
Expand All @@ -602,7 +617,7 @@ func (o *UuidObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L,
}

existing, err = o.service.List(ctx, location, "get", "", "")
if err != nil {
if err != nil && !sdkerrors.IsObjectNotFound(err) {
return nil, fmt.Errorf("Failed to list remote entries: %w", err)
}

Expand Down
28 changes: 28 additions & 0 deletions assets/terraform/test/resource_addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ func TestAccAddresses(t *testing.T) {
ProtoV6ProviderFactories: testAccProviders,
CheckDestroy: testAccAddressesDestroy(prefix),
Steps: []resource.TestStep{
{
Config: testAccAddressesResourceTmpl,
ConfigVariables: map[string]config.Variable{
"prefix": config.StringVariable(prefix),
"addresses": config.MapVariable(map[string]config.Variable{})},
},
{
Config: testAccAddressesResourceTmpl,
ConfigVariables: map[string]config.Variable{
"prefix": config.StringVariable(prefix),
"addresses": config.MapVariable(map[string]config.Variable{})},
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
{
Config: testAccAddressesResourceTmpl,
ConfigVariables: map[string]config.Variable{
Expand Down Expand Up @@ -141,6 +155,20 @@ func TestAccAddresses(t *testing.T) {
),
},
},
{
Config: testAccAddressesResourceTmpl,
ConfigVariables: map[string]config.Variable{
"prefix": config.StringVariable(prefix),
"addresses": config.MapVariable(map[string]config.Variable{})},
},
{
Config: testAccAddressesResourceTmpl,
ConfigVariables: map[string]config.Variable{
"prefix": config.StringVariable(prefix),
"addresses": config.MapVariable(map[string]config.Variable{})},
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}
Expand Down
32 changes: 32 additions & 0 deletions assets/terraform/test/resource_security_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,22 @@ func TestAccSecurityPolicyOrdering(t *testing.T) {
ProtoV6ProviderFactories: testAccProviders,
CheckDestroy: securityPolicyCheckDestroy(prefix, sdkLocation),
Steps: []resource.TestStep{
{
Config: securityPolicyOrderingTmpl,
ConfigVariables: map[string]config.Variable{
"rule_names": config.ListVariable([]config.Variable{}...),
"location": cfgLocation,
},
},
{
Config: securityPolicyOrderingTmpl,
ConfigVariables: map[string]config.Variable{
"rule_names": config.ListVariable([]config.Variable{}...),
"location": cfgLocation,
},
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
{
Config: securityPolicyOrderingTmpl,
ConfigVariables: map[string]config.Variable{
Expand Down Expand Up @@ -523,6 +539,22 @@ func TestAccSecurityPolicyOrdering(t *testing.T) {
ExpectServerSecurityRulesOrder(prefix, sdkLocation, rulesReordered),
},
},
{
Config: securityPolicyOrderingTmpl,
ConfigVariables: map[string]config.Variable{
"rule_names": config.ListVariable([]config.Variable{}...),
"location": cfgLocation,
},
},
{
Config: securityPolicyOrderingTmpl,
ConfigVariables: map[string]config.Variable{
"rule_names": config.ListVariable([]config.Variable{}...),
"location": cfgLocation,
},
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/translate/terraform_provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ if resp.Diagnostics.HasError() {

elements := make(map[string]{{ $resourceTFStructName }})
resp.Diagnostics.Append(state.{{ .ListAttribute.CamelCase }}.ElementsAs(ctx, &elements, false)...)
if resp.Diagnostics.HasError() {
if len(elements) == 0 || resp.Diagnostics.HasError() {
return
}

Expand Down Expand Up @@ -786,6 +786,10 @@ if resp.Diagnostics.HasError() {

var elements []{{ $resourceTFStructName }}
state.{{ .ListAttribute.CamelCase }}.ElementsAs(ctx, &elements, false)
if len(elements) == 0 {
return
}

entries := make([]*{{ $resourceSDKStructName }}, 0, len(elements))
for _, elt := range elements {
var entry *{{ $resourceSDKStructName }}
Expand Down Expand Up @@ -949,7 +953,7 @@ for name, elt := range elements {
}

existing, err := r.manager.ReadMany(ctx, location, stateEntries)
if err != nil && !sdkerrors.IsObjectNotFound(err) {
if err != nil && !errors.Is(err, sdkmanager.ErrObjectNotFound) {
resp.Diagnostics.AddError("Error while reading entries from the server", err.Error())
return
}
Expand Down Expand Up @@ -1044,7 +1048,7 @@ position := state.Position.CopyToPango()
{{- end }}

existing, err := r.manager.ReadMany(ctx, location, stateEntries, {{ $exhaustive }})
if err != nil && !sdkerrors.IsObjectNotFound(err) {
if err != nil && !errors.Is(err, sdkmanager.ErrObjectNotFound) {
resp.Diagnostics.AddError("Error while reading entries from the server", err.Error())
return
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/translate/terraform_provider/terraform_provider_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,9 @@ func (g *GenerateTerraformProvider) GenerateTerraformResource(resourceTyp proper
switch resourceTyp {
case properties.ResourceUuid:
terraformProvider.ImportManager.AddSdkImport("github.com/PaloAltoNetworks/pango/rule", "")
terraformProvider.ImportManager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "sdkerrors")
case properties.ResourceEntry:
case properties.ResourceUuidPlural:
terraformProvider.ImportManager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "sdkerrors")
case properties.ResourceEntryPlural:
terraformProvider.ImportManager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "sdkerrors")
case properties.ResourceCustom, properties.ResourceConfig:
}

Expand Down
Loading