From 87fb481e4b6be80610b1d66fbe368773cffc063f Mon Sep 17 00:00:00 2001 From: James Toyer Date: Wed, 1 Dec 2021 14:30:00 +0000 Subject: [PATCH 1/3] Stop 404s for permission exists requests erroring --- pkg/artifactory/resource_artifactory_permission_target.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/artifactory/resource_artifactory_permission_target.go b/pkg/artifactory/resource_artifactory_permission_target.go index 67b3ff43b..a1162fa72 100644 --- a/pkg/artifactory/resource_artifactory_permission_target.go +++ b/pkg/artifactory/resource_artifactory_permission_target.go @@ -338,7 +338,11 @@ func resourcePermissionTargetExists(d *schema.ResourceData, m interface{}) (bool } func permTargetExists(id string, m interface{}) (bool, error) { - _, err := m.(*resty.Client).R().Head(permissionsEndPoint + id) + resp, err := m.(*resty.Client).R().Head(permissionsEndPoint + id) + if err != nil && resp != nil && resp.StatusCode() == http.StatusNotFound { + // Do not error on 404s as this causes errors when the upstream permission has been manually removed + return false, nil + } return err == nil, err } From 289d35a75ef616fcfe06e68485fb3e5d54b09971 Mon Sep 17 00:00:00 2001 From: James Toyer Date: Wed, 1 Dec 2021 14:39:18 +0000 Subject: [PATCH 2/3] Stop 404s for user exists requests erroring --- pkg/artifactory/resource_artifactory_user.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/artifactory/resource_artifactory_user.go b/pkg/artifactory/resource_artifactory_user.go index 56cd303bc..afa4e737d 100644 --- a/pkg/artifactory/resource_artifactory_user.go +++ b/pkg/artifactory/resource_artifactory_user.go @@ -102,7 +102,12 @@ func resourceUserExists(data *schema.ResourceData, m interface{}) (bool, error) } func userExists(client *resty.Client, userName string) (bool, error) { - _, err := client.R().Head("artifactory/api/security/users/" + userName) + resp, err := client.R().Head("artifactory/api/security/users/" + userName) + if err != nil && resp != nil && resp.StatusCode() == http.StatusNotFound { + // Do not error on 404s as this causes errors when the upstream user has been manually removed + return false, nil + } + return err == nil, err } From b2e18a636dd5e2a573b13845fabf4a736cd44477 Mon Sep 17 00:00:00 2001 From: James Toyer Date: Wed, 1 Dec 2021 15:54:29 +0000 Subject: [PATCH 3/3] Add 404 checking to group exists --- pkg/artifactory/resource_artifactory_group.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/artifactory/resource_artifactory_group.go b/pkg/artifactory/resource_artifactory_group.go index fdf993f59..17b3471f5 100644 --- a/pkg/artifactory/resource_artifactory_group.go +++ b/pkg/artifactory/resource_artifactory_group.go @@ -2,6 +2,7 @@ package artifactory import ( "fmt" + "net/http" "github.com/go-resty/resty/v2" @@ -197,7 +198,12 @@ func resourceGroupExists(d *schema.ResourceData, m interface{}) (bool, error) { } func groupExists(client *resty.Client, groupName string) (bool, error) { - _, err := client.R().Head(groupsEndpoint + groupName) + resp, err := client.R().Head(groupsEndpoint + groupName) + if err != nil && resp != nil && resp.StatusCode() == http.StatusNotFound { + // Do not error on 404s as this causes errors when the upstream user has been manually removed + return false, nil + } + return err == nil, err }