Skip to content

Commit

Permalink
Fix error handling in case of state file out of sync (#430)
Browse files Browse the repository at this point in the history
* Add 404 handler on read operations

* Add 404 handler on read operations
  • Loading branch information
wcmjunior authored Aug 3, 2023
1 parent a8b3951 commit 68fb1eb
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 11 deletions.
18 changes: 18 additions & 0 deletions cyral/error_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ func (h *DeleteIgnoreHttpNotFound) HandleError(
log.Printf("[DEBUG] %s not found. Skipping deletion.", h.resName)
return nil
}

type ReadIgnoreHttpNotFound struct {
resName string
}

func (h *ReadIgnoreHttpNotFound) HandleError(
r *schema.ResourceData,
_ *client.Client,
err error,
) error {
httpError, ok := err.(*client.HttpError)
if !ok || httpError.StatusCode != http.StatusNotFound {
return err
}
r.SetId("")
log.Printf("[DEBUG] %s not found. Marking resource for recreation.", h.resName)
return nil
}
3 changes: 2 additions & 1 deletion cyral/resource_cyral_integration_datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var ReadDatadogConfig = ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/datadog/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &DatadogIntegration{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &DatadogIntegration{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Integration datadog"},
}

func resourceIntegrationDatadog() *schema.Resource {
Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_integration_elk.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ var ReadELKConfig = ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/elk/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &ELKIntegration{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &ELKIntegration{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Integration elk"},
}

func resourceIntegrationELK() *schema.Resource {
Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_integration_hcvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var ReadHCVaultIntegrationConfig = ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/secretProviders/hcvault/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &HCVaultIntegration{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &HCVaultIntegration{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Integration hcvault"},
}

func resourceIntegrationHCVault() *schema.Resource {
Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_integration_idp_saml.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func ReadGenericSAMLConfig() ResourceOperationConfig {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/generic-saml/sso/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &ReadGenericSAMLResponse{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &ReadGenericSAMLResponse{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Generic SAML"},
}
}

Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_integration_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ var ReadLoggingIntegration = ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf(loggingApiUrl, c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &LoggingIntegration{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &LoggingIntegration{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Integration logging"},
}

func UpdateLoggingIntegration() ResourceOperationConfig {
Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_integration_slack_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var ReadSlackAlertsConfig = ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/notifications/slack/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &SlackAlertsIntegration{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &SlackAlertsIntegration{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Integration Slack"},
}

func resourceIntegrationSlackAlerts() *schema.Resource {
Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_integration_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var ReadMsTeamsConfig = ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/notifications/teams/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &MsTeamsIntegration{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &MsTeamsIntegration{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Integration Teams"},
}

func resourceIntegrationMsTeams() *schema.Resource {
Expand Down
1 change: 1 addition & 0 deletions cyral/resource_cyral_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ var ReadRepositoryConfig = ResourceOperationConfig{
NewResponseData: func(_ *schema.ResourceData) ResponseData {
return &GetRepoByIDResponse{}
},
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Repository"},
}

func resourceRepository() *schema.Resource {
Expand Down
1 change: 1 addition & 0 deletions cyral/resource_cyral_repository_access_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var ReadRepositoryAccessGatewayConfig = ResourceOperationConfig{
NewResponseData: func(_ *schema.ResourceData) ResponseData {
return &GetOrUpdateAccessGateway{}
},
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Repository access gateway"},
}

func resourceRepositoryAccessGateway() *schema.Resource {
Expand Down
1 change: 1 addition & 0 deletions cyral/resource_cyral_repository_access_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ var ReadRepositoryAccessRulesConfig = ResourceOperationConfig{
NewResponseData: func(_ *schema.ResourceData) ResponseData {
return &AccessRulesResponse{}
},
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Repository access rule"},
}

func resourceRepositoryAccessRules() *schema.Resource {
Expand Down
1 change: 1 addition & 0 deletions cyral/resource_cyral_repository_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ var ReadRepositoryBindingConfig = ResourceOperationConfig{
NewResponseData: func(_ *schema.ResourceData) ResponseData {
return &GetBindingResponse{}
},
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Repository binding"},
}

func resourceRepositoryBinding() *schema.Resource {
Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_repository_conf_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ func ReadConfAuthConfig() ResourceOperationConfig {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf(repositoryConfAuthURLFormat, c.ControlPlane, d.Get("repository_id"))
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &ReadRepositoryConfAuthResponse{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &ReadRepositoryConfAuthResponse{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Repository conf auth"},
}
}

Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_repository_network_access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ func readRepositoryNetworkAccessPolicy() ResourceOperationConfig {
return fmt.Sprintf(repositoryNetworkAccessPolicyURLFormat,
c.ControlPlane, d.Get("repository_id"))
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &NetworkAccessPolicy{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &NetworkAccessPolicy{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Repository network access policy"},
}
}

Expand Down
1 change: 1 addition & 0 deletions cyral/resource_cyral_repository_user_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ var ReadRepositoryUserAccountConfig = ResourceOperationConfig{
NewResponseData: func(_ *schema.ResourceData) ResponseData {
return &UserAccountResource{}
},
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "User account"},
}

func resourceRepositoryUserAccount() *schema.Resource {
Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_role_sso_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ var readRoleSSOGroupsConfig = ResourceOperationConfig{
return fmt.Sprintf("https://%s/v1/users/groups/%s/mappings", c.ControlPlane,
d.Get("role_id").(string))
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &RoleSSOGroupsReadResponse{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &RoleSSOGroupsReadResponse{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Role SSO groups"},
}

var deleteRoleSSOGroupsConfig = ResourceOperationConfig{
Expand Down
3 changes: 2 additions & 1 deletion cyral/resource_cyral_sidecar_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ var ReadSidecarListenersConfig = ResourceOperationConfig{
d.Get(SidecarIDKey).(string),
d.Get(ListenerIDKey).(string))
},
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &ReadSidecarListenerAPIResponse{} },
NewResponseData: func(_ *schema.ResourceData) ResponseData { return &ReadSidecarListenerAPIResponse{} },
RequestErrorHandler: &ReadIgnoreHttpNotFound{resName: "Sidecar listener"},
}

type ReadSidecarListenerAPIResponse struct {
Expand Down

0 comments on commit 68fb1eb

Please sign in to comment.