Skip to content

Commit

Permalink
Fix Exists methods of resources to return false on 404 on v11 #1162
Browse files Browse the repository at this point in the history
  • Loading branch information
hkantare committed Mar 9, 2020
1 parent 6374de7 commit 5abbeb3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
8 changes: 7 additions & 1 deletion ibm/resource_ibm_certificate_manager_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"

"github.com/IBM-Cloud/bluemix-go/bmxerror"
"github.com/IBM-Cloud/bluemix-go/models"
)

Expand Down Expand Up @@ -221,7 +222,12 @@ func resourceIBMCertificateManagerExists(d *schema.ResourceData, meta interface{

_, err = client.GetCertData(certID)
if err != nil {
return false, err
if apiErr, ok := err.(bmxerror.RequestFailure); ok {
if apiErr.StatusCode() == 404 {
return false, nil
}
}
return false, fmt.Errorf("Error communicating with the API: %s", err)
}
return true, nil
}
3 changes: 0 additions & 3 deletions ibm/resource_ibm_iam_access_group_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,6 @@ func resourceIBMIAMAccessGroupPolicyExists(d *schema.ResourceData, meta interfac
accgrpPolicyID := parts[1]

accgrpPolicy, err := iampapClient.V1Policy().Get(accgrpPolicyID)
if err != nil {
return false, fmt.Errorf("Error retrieving access group policy: %s", err)
}
if err != nil {
if apiErr, ok := err.(bmxerror.RequestFailure); ok {
if apiErr.StatusCode() == 404 {
Expand Down
3 changes: 0 additions & 3 deletions ibm/resource_ibm_iam_authorization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ func resourceIBMIAMAuthorizationPolicyExists(d *schema.ResourceData, meta interf
}

authorizationPolicy, err := iampapClient.V1Policy().Get(d.Id())
if err != nil {
return false, fmt.Errorf("Error retrieving authorizationPolicy: %s", err)
}
if err != nil {
if apiErr, ok := err.(bmxerror.RequestFailure); ok {
if apiErr.StatusCode() == 404 {
Expand Down
3 changes: 0 additions & 3 deletions ibm/resource_ibm_iam_service_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,6 @@ func resourceIBMIAMServicePolicyExists(d *schema.ResourceData, meta interface{})
servicePolicyID := parts[1]

servicePolicy, err := iampapClient.V1Policy().Get(servicePolicyID)
if err != nil {
return false, fmt.Errorf("Error retrieving servicePolicy: %s", err)
}
if err != nil {
if apiErr, ok := err.(bmxerror.RequestFailure); ok {
if apiErr.StatusCode() == 404 {
Expand Down
2 changes: 1 addition & 1 deletion ibm/resource_ibm_iam_user_invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ func resourceIBMIAMGetUserProfileExists(d *schema.ResourceData, meta interface{}
}
}
if !isFound {
return false, fmt.Errorf("Didn't find the user : %s", user)
return false, nil
}
}
return true, nil
Expand Down
25 changes: 25 additions & 0 deletions ibm/resource_ibm_kp_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func resourceIBMkey() *schema.Resource {
Read: resourceIBMKeyRead,
// Update: resourceIBMContainerVpcALBUpdate,
Delete: resourceIBMKeyDelete,
Exists: resourceIBMKeyExists,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Expand Down Expand Up @@ -219,3 +220,27 @@ func resourceIBMKeyDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

func resourceIBMKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
api, err := meta.(ClientSession).keyProtectAPI()
if err != nil {
return false, err
}
crn := d.Id()
crnData := strings.Split(crn, ":")

instanceID := crnData[len(crnData)-3]
keyid := crnData[len(crnData)-1]
api.Config.InstanceID = instanceID
// keyid := d.Id()
_, err = api.GetKey(context.Background(), keyid)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 {
return false, nil
}
return false, err
}
return true, nil

}

0 comments on commit 5abbeb3

Please sign in to comment.