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

Do not try to update custom fields if no custom field definitions exist #82

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions plugin/providers/phpipam/custom_field_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"reflect"
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pavel-z1/phpipam-sdk-go/controllers/addresses"
Expand Down Expand Up @@ -90,9 +91,10 @@ func trimMap(in map[string]interface{}) {
// what isn't set, and ensure that we clear out the keys that aren't set.
// Since our SDK does not currently support NOT NULL custom fields in
// PHPIPAM, we can safely set these to nil.
// - If we don't have a value for
// custom_fields at all, set all keys to nil and update so that all custom
// fields get blown away.
// - If we don't have a value for custom_fields at all, set all keys to nil
// and update so that all custom fields get blown away. HTTP 404 errors
// indicating that no custom fields are defined will be ignored if no
// custom fields are defined for the resource.
func updateCustomFields(d *schema.ResourceData, client interface{}) error {
log.Printf("Start Update custom fields ...............")
customFields := make(map[string]interface{})
Expand All @@ -114,8 +116,13 @@ func updateCustomFields(d *schema.ResourceData, client interface{}) error {
panic(fmt.Errorf("Invalid client type passed %#v - this is a bug", client))
}
if err != nil {
return fmt.Errorf("Error getting custom fields for updating: %s", err)
if strings.Contains(err.Error(), "404") && len(customFields) == 0 {
Copy link
Collaborator

@pavel-z1 pavel-z1 Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to change this condition to next to handle error from old phpipam versions:

if (strings.Contains(err.Error(), "404") || (strings.Contains(err.Error(), "200") && strings.Contains(err.Error(), "No custom fields defined")) && len(customFields) == 0 {

@tseeker you will have time to extend this condition?
This will allow as to accept your PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me. I won't have the time to test it properly in the foreseeable future though.

return nil
} else {
return fmt.Errorf("Error getting custom fields for updating: %s", err)
}
}

nextKey:
for k := range old {
for l, v := range customFields {
Expand Down