Skip to content

Commit

Permalink
convert terraform resources to use sdk manager
Browse files Browse the repository at this point in the history
  • Loading branch information
kklimonda-cl committed Sep 3, 2024
1 parent c3ab00a commit 5d871cf
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 987 deletions.
9 changes: 9 additions & 0 deletions assets/pango/errors/panos.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func (e Panos) ObjectNotFound() bool {
return e.Code == 7
}

func IsObjectNotFound(e error) bool {
e2, ok := e.(Panos)
if ok && e2.ObjectNotFound() {
return true
}

return false
}

// ObjectNotFound returns an object not found error.
func ObjectNotFound() Panos {
return Panos{
Expand Down
13 changes: 0 additions & 13 deletions assets/terraform/internal/provider/errors.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
package provider

import (
"github.com/PaloAltoNetworks/pango/errors"
)

var InspectionModeError = "Resources are unavailable when the provider is in inspection mode. Resources are only available in API mode."

func IsObjectNotFound(e error) bool {
e2, ok := e.(errors.Panos)
if ok && e2.ObjectNotFound() {
return true
}

return false
}
17 changes: 13 additions & 4 deletions pkg/translate/terraform_provider/device_group_parent_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const deviceGroupParentImports = `
import (
"encoding/xml"
"github.com/PaloAltoNetworks/pango/xmlapi"
sdkerrors "github.com/PaloAltoNetworks/pango/errors"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/xmlapi"
)
`

Expand Down Expand Up @@ -106,7 +107,11 @@ if resp.Diagnostics.HasError() {
name := state.DeviceGroup.ValueString()
hierarchy, err := getParents(ctx, o.client, name)
if err != nil {
resp.Diagnostics.AddError("Failed to query for the device group parent", err.Error())
if sdkerrors.IsObjectNotFound(err) {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("Failed to query for the device group parent", err.Error())
}
return
}
Expand All @@ -130,13 +135,17 @@ if resp.Diagnostics.HasError() {
name := state.DeviceGroup.ValueString()
hierarchy, err := getParents(ctx, o.client, name)
if err != nil {
resp.Diagnostics.AddError("Failed to query for the device group parent", err.Error())
if sdkerrors.IsObjectNotFound(err) {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("Failed to query for the device group parent", err.Error())
}
return
}
parent, ok := hierarchy[name]
if !ok {
resp.Diagnostics.AddError("Failed to query for the device group parent", fmt.Sprintf("Device Group '%s' doesn't exist", name))
resp.State.RemoveResource(ctx)
return
}
state.Parent = types.StringValue(parent)
Expand Down
32 changes: 10 additions & 22 deletions pkg/translate/terraform_provider/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,29 +971,17 @@ func createSchemaAttributeForParameter(schemaTyp schemaType, packageName string,
}
}

// TODO(kklimonda): This is pretty one-off implementation to
// support uuid-style resources, but could be expanded to be more
// generic if needed.
var modifiers *modifierCtx
if schemaTyp == schemaResource && computed && param.Default == "" {
modifiers = &modifierCtx{
SchemaType: fmt.Sprintf("planmodifier.%s", pascalCase(param.Type)),
Modifiers: []string{fmt.Sprintf("%splanmodifier.UseStateForUnknown()", param.Type)},
}
}

return attributeCtx{
Package: packageName,
Name: param.Name,
SchemaType: schemaType,
ElementType: elementType,
Description: param.Description,
Required: param.Required,
Optional: !param.Required,
Sensitive: param.Sensitive,
Default: defaultValue,
Computed: computed,
PlanModifiers: modifiers,
Package: packageName,
Name: param.Name,
SchemaType: schemaType,
ElementType: elementType,
Description: param.Description,
Required: param.Required,
Optional: !param.Required,
Sensitive: param.Sensitive,
Default: defaultValue,
Computed: computed,
}
}

Expand Down
Loading

0 comments on commit 5d871cf

Please sign in to comment.