diff --git a/cyral/core/context_handler.go b/cyral/core/context_handler.go index 547ac130..dcde2c9a 100644 --- a/cyral/core/context_handler.go +++ b/cyral/core/context_handler.go @@ -23,7 +23,7 @@ type ResourceMethod func(context.Context, *client.Client, *schema.ResourceData) // 404 errors, following a create/update with a get etc). The resource implementation // needs to supply functions that implement the basic CRUD operations on the resource // using gRPC or whatever else. Note that if REST APIs are used, it is recommended -// to use the DefaultContextHandler instead. +// to use the HTTPContextHandler instead. type ContextHandler struct { ResourceName string ResourceType resourcetype.ResourceType diff --git a/cyral/core/default_context_handler.go b/cyral/core/http_context_handler.go similarity index 84% rename from cyral/core/default_context_handler.go rename to cyral/core/http_context_handler.go index 616dbc23..f1ae9463 100644 --- a/cyral/core/default_context_handler.go +++ b/cyral/core/http_context_handler.go @@ -12,7 +12,8 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -// Implementation of a default context handler that can be used by all resources. +// HTTPContextHandler facilitates easy resource and data source implementation +// for any resource that is accessed and modified using an HTTP/REST API. // // 1. `SchemaWriterFactoryGetMethod“ must be provided. // 2. In case `SchemaWriterFactoryPostMethod“ is not provided, @@ -31,7 +32,7 @@ import ( // - PUT: https:///// // - PATCH: https:///// // - DELETE: https:///// -type DefaultContextHandler struct { +type HTTPContextHandler struct { ResourceName string ResourceType rt.ResourceType SchemaReaderFactory SchemaReaderFactoryFunc @@ -55,7 +56,7 @@ func DefaultSchemaWriterFactory(d *schema.ResourceData) SchemaWriter { return &IDBasedResponse{} } -func (dch DefaultContextHandler) defaultOperationHandler( +func (dch HTTPContextHandler) defaultOperationHandler( operationType ot.OperationType, httpMethod string, schemaReaderFactory SchemaReaderFactoryFunc, @@ -92,11 +93,11 @@ func (dch DefaultContextHandler) defaultOperationHandler( return result } -func (dch DefaultContextHandler) CreateContext() schema.CreateContextFunc { +func (dch HTTPContextHandler) CreateContext() schema.CreateContextFunc { return dch.CreateContextCustomErrorHandling(&IgnoreHttpNotFound{ResName: dch.ResourceName}, nil) } -func (dch DefaultContextHandler) CreateContextCustomErrorHandling(getErrorHandler RequestErrorHandler, +func (dch HTTPContextHandler) CreateContextCustomErrorHandling(getErrorHandler RequestErrorHandler, postErrorHandler RequestErrorHandler) schema.CreateContextFunc { // By default, assumes that if no SchemaWriterFactoryPostMethod is provided, // the POST api will return an ID @@ -110,21 +111,21 @@ func (dch DefaultContextHandler) CreateContextCustomErrorHandling(getErrorHandle ) } -func (dch DefaultContextHandler) ReadContext() schema.ReadContextFunc { +func (dch HTTPContextHandler) ReadContext() schema.ReadContextFunc { return dch.ReadContextCustomErrorHandling(&IgnoreHttpNotFound{ResName: dch.ResourceName}) } -func (dch DefaultContextHandler) ReadContextCustomErrorHandling(getErrorHandler RequestErrorHandler) schema.ReadContextFunc { +func (dch HTTPContextHandler) ReadContextCustomErrorHandling(getErrorHandler RequestErrorHandler) schema.ReadContextFunc { return ReadResource( dch.defaultOperationHandler(ot.Read, http.MethodGet, nil, dch.SchemaWriterFactoryGetMethod, getErrorHandler), ) } -func (dch DefaultContextHandler) UpdateContext() schema.UpdateContextFunc { +func (dch HTTPContextHandler) UpdateContext() schema.UpdateContextFunc { return dch.UpdateContextCustomErrorHandling(&IgnoreHttpNotFound{ResName: dch.ResourceName}, nil) } -func (dch DefaultContextHandler) UpdateContextCustomErrorHandling(getErrorHandler RequestErrorHandler, +func (dch HTTPContextHandler) UpdateContextCustomErrorHandling(getErrorHandler RequestErrorHandler, putErrorHandler RequestErrorHandler) schema.UpdateContextFunc { updateMethod := http.MethodPut if dch.UpdateMethod != "" { @@ -136,10 +137,10 @@ func (dch DefaultContextHandler) UpdateContextCustomErrorHandling(getErrorHandle ) } -func (dch DefaultContextHandler) DeleteContext() schema.DeleteContextFunc { +func (dch HTTPContextHandler) DeleteContext() schema.DeleteContextFunc { return dch.DeleteContextCustomErrorHandling(&IgnoreHttpNotFound{ResName: dch.ResourceName}) } -func (dch DefaultContextHandler) DeleteContextCustomErrorHandling(deleteErrorHandler RequestErrorHandler) schema.DeleteContextFunc { +func (dch HTTPContextHandler) DeleteContextCustomErrorHandling(deleteErrorHandler RequestErrorHandler) schema.DeleteContextFunc { return DeleteResource(dch.defaultOperationHandler(ot.Delete, http.MethodDelete, nil, nil, deleteErrorHandler)) } diff --git a/cyral/internal/datalabel/resource.go b/cyral/internal/datalabel/resource.go index 13f0f539..c89aa385 100644 --- a/cyral/internal/datalabel/resource.go +++ b/cyral/internal/datalabel/resource.go @@ -22,7 +22,7 @@ var readUpdateDeleteURLFactory = func(d *schema.ResourceData, c *client.Client) } func resourceSchema() *schema.Resource { - contextHandler := core.DefaultContextHandler{ + contextHandler := core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &DataLabel{} }, diff --git a/cyral/internal/deprecated/policy/resource.go b/cyral/internal/deprecated/policy/resource.go index 69f66cd2..f52f2cbf 100644 --- a/cyral/internal/deprecated/policy/resource.go +++ b/cyral/internal/deprecated/policy/resource.go @@ -15,7 +15,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &Policy{} }, diff --git a/cyral/internal/deprecated/policy/rule/resource.go b/cyral/internal/deprecated/policy/rule/resource.go index 85f7b099..cf4ca67d 100644 --- a/cyral/internal/deprecated/policy/rule/resource.go +++ b/cyral/internal/deprecated/policy/rule/resource.go @@ -13,7 +13,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &PolicyRule{} }, diff --git a/cyral/internal/deprecated/resource_cyral_integration_datadog.go b/cyral/internal/deprecated/resource_cyral_integration_datadog.go index 4ce80f41..6d33d288 100644 --- a/cyral/internal/deprecated/resource_cyral_integration_datadog.go +++ b/cyral/internal/deprecated/resource_cyral_integration_datadog.go @@ -29,7 +29,7 @@ func (data *DatadogIntegration) ReadFromSchema(d *schema.ResourceData) error { } func ResourceIntegrationDatadog() *schema.Resource { - contextHandler := core.DefaultContextHandler{ + contextHandler := core.HTTPContextHandler{ ResourceName: "Datadog Integration", ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &DatadogIntegration{} }, diff --git a/cyral/internal/deprecated/resource_cyral_integration_elk.go b/cyral/internal/deprecated/resource_cyral_integration_elk.go index 1768e357..70703b75 100644 --- a/cyral/internal/deprecated/resource_cyral_integration_elk.go +++ b/cyral/internal/deprecated/resource_cyral_integration_elk.go @@ -32,7 +32,7 @@ func (data *ELKIntegration) ReadFromSchema(d *schema.ResourceData) error { } func ResourceIntegrationELK() *schema.Resource { - contextHandler := core.DefaultContextHandler{ + contextHandler := core.HTTPContextHandler{ ResourceName: "ELK Integration", ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &ELKIntegration{} }, diff --git a/cyral/internal/deprecated/resource_cyral_integration_logstash.go b/cyral/internal/deprecated/resource_cyral_integration_logstash.go index 88ba38a0..8b2e3047 100644 --- a/cyral/internal/deprecated/resource_cyral_integration_logstash.go +++ b/cyral/internal/deprecated/resource_cyral_integration_logstash.go @@ -36,7 +36,7 @@ func (data *LogstashIntegration) ReadFromSchema(d *schema.ResourceData) error { } func ResourceIntegrationLogstash() *schema.Resource { - contextHandler := core.DefaultContextHandler{ + contextHandler := core.HTTPContextHandler{ ResourceName: "Logstash Integration", ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &LogstashIntegration{} }, diff --git a/cyral/internal/deprecated/resource_cyral_integration_looker.go b/cyral/internal/deprecated/resource_cyral_integration_looker.go index f67f6cf8..28509a08 100644 --- a/cyral/internal/deprecated/resource_cyral_integration_looker.go +++ b/cyral/internal/deprecated/resource_cyral_integration_looker.go @@ -30,7 +30,7 @@ func (data *LookerIntegration) ReadFromSchema(d *schema.ResourceData) error { } func ResourceIntegrationLooker() *schema.Resource { - contextHandler := core.DefaultContextHandler{ + contextHandler := core.HTTPContextHandler{ ResourceName: "Looker Integration", ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &LookerIntegration{} }, diff --git a/cyral/internal/deprecated/resource_cyral_integration_splunk.go b/cyral/internal/deprecated/resource_cyral_integration_splunk.go index 50c41d96..ebe1dedf 100644 --- a/cyral/internal/deprecated/resource_cyral_integration_splunk.go +++ b/cyral/internal/deprecated/resource_cyral_integration_splunk.go @@ -40,7 +40,7 @@ func (data *SplunkIntegration) ReadFromSchema(d *schema.ResourceData) error { } func ResourceIntegrationSplunk() *schema.Resource { - contextHandler := core.DefaultContextHandler{ + contextHandler := core.HTTPContextHandler{ ResourceName: "Splunk Integration", ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &SplunkIntegration{} }, diff --git a/cyral/internal/deprecated/resource_cyral_integration_sumo_logic.go b/cyral/internal/deprecated/resource_cyral_integration_sumo_logic.go index 9459bef1..0198e5c7 100644 --- a/cyral/internal/deprecated/resource_cyral_integration_sumo_logic.go +++ b/cyral/internal/deprecated/resource_cyral_integration_sumo_logic.go @@ -27,7 +27,7 @@ func (data *SumoLogicIntegration) ReadFromSchema(d *schema.ResourceData) error { } func ResourceIntegrationSumoLogic() *schema.Resource { - contextHandler := core.DefaultContextHandler{ + contextHandler := core.HTTPContextHandler{ ResourceName: "SumoLogic Integration", ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &SumoLogicIntegration{} }, diff --git a/cyral/internal/integration/awsiam/resource.go b/cyral/internal/integration/awsiam/resource.go index 25a34e82..066ee003 100644 --- a/cyral/internal/integration/awsiam/resource.go +++ b/cyral/internal/integration/awsiam/resource.go @@ -11,7 +11,7 @@ import ( ) func resourceSchema() *schema.Resource { - contextHandler := core.DefaultContextHandler{ + contextHandler := core.HTTPContextHandler{ ResourceName: "AWS IAM Integration", ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &AWSIAMIntegrationWrapper{} }, diff --git a/cyral/internal/integration/hcvault/resource.go b/cyral/internal/integration/hcvault/resource.go index f0fd8b77..6ec574c9 100644 --- a/cyral/internal/integration/hcvault/resource.go +++ b/cyral/internal/integration/hcvault/resource.go @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &HCVaultIntegration{} }, diff --git a/cyral/internal/integration/idpsaml/draft/resource.go b/cyral/internal/integration/idpsaml/draft/resource.go index de0754ec..6fca7409 100644 --- a/cyral/internal/integration/idpsaml/draft/resource.go +++ b/cyral/internal/integration/idpsaml/draft/resource.go @@ -33,7 +33,7 @@ import ( // 2. Provide the IdP metadata to the `cyral_integration_idp_saml` resource. // -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &CreateGenericSAMLDraftRequest{} }, diff --git a/cyral/internal/integration/logging/resource.go b/cyral/internal/integration/logging/resource.go index a3c8570a..b4f51d2b 100644 --- a/cyral/internal/integration/logging/resource.go +++ b/cyral/internal/integration/logging/resource.go @@ -10,7 +10,7 @@ import ( "github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &LoggingIntegration{} }, diff --git a/cyral/internal/integration/slack/resource.go b/cyral/internal/integration/slack/resource.go index 3d1e9dda..48a2c8a2 100644 --- a/cyral/internal/integration/slack/resource.go +++ b/cyral/internal/integration/slack/resource.go @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &SlackAlertsIntegration{} }, diff --git a/cyral/internal/integration/teams/resource.go b/cyral/internal/integration/teams/resource.go index 3799d049..91656669 100644 --- a/cyral/internal/integration/teams/resource.go +++ b/cyral/internal/integration/teams/resource.go @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &MsTeamsIntegration{} }, diff --git a/cyral/internal/regopolicy/resource.go b/cyral/internal/regopolicy/resource.go index d57df80e..d26c3113 100644 --- a/cyral/internal/regopolicy/resource.go +++ b/cyral/internal/regopolicy/resource.go @@ -14,7 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &RegoPolicyInstancePayload{} }, diff --git a/cyral/internal/repository/binding/resource.go b/cyral/internal/repository/binding/resource.go index 6b934656..10f1845e 100644 --- a/cyral/internal/repository/binding/resource.go +++ b/cyral/internal/repository/binding/resource.go @@ -11,7 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &CreateBindingRequest{} }, diff --git a/cyral/internal/repository/confanalysis/resource.go b/cyral/internal/repository/confanalysis/resource.go index 044497a3..d3884d2d 100644 --- a/cyral/internal/repository/confanalysis/resource.go +++ b/cyral/internal/repository/confanalysis/resource.go @@ -25,7 +25,7 @@ var urlFactory = func(d *schema.ResourceData, c *client.Client) string { ) } -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &UserConfig{} }, diff --git a/cyral/internal/repository/confauth/resource.go b/cyral/internal/repository/confauth/resource.go index d583165e..fb181728 100644 --- a/cyral/internal/repository/confauth/resource.go +++ b/cyral/internal/repository/confauth/resource.go @@ -28,7 +28,7 @@ var urlFactory = func(d *schema.ResourceData, c *client.Client) string { ) } -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &RepositoryConfAuthData{} }, diff --git a/cyral/internal/repository/datasource.go b/cyral/internal/repository/datasource.go index cf02d63d..258e42b5 100644 --- a/cyral/internal/repository/datasource.go +++ b/cyral/internal/repository/datasource.go @@ -57,7 +57,7 @@ func (resp *GetReposResponse) WriteToSchema(d *schema.ResourceData) error { return nil } -var dsContextHandler = core.DefaultContextHandler{ +var dsContextHandler = core.HTTPContextHandler{ ResourceName: dataSourceName, ResourceType: resourcetype.DataSource, SchemaWriterFactoryGetMethod: func(_ *schema.ResourceData) core.SchemaWriter { return &GetReposResponse{} }, diff --git a/cyral/internal/repository/network/resource.go b/cyral/internal/repository/network/resource.go index 172782d0..b08a3030 100644 --- a/cyral/internal/repository/network/resource.go +++ b/cyral/internal/repository/network/resource.go @@ -18,7 +18,7 @@ var urlFactory = func(d *schema.ResourceData, c *client.Client) string { ) } -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &NetworkAccessPolicy{} }, diff --git a/cyral/internal/repository/resource.go b/cyral/internal/repository/resource.go index a7a7442c..6ee8b8bd 100644 --- a/cyral/internal/repository/resource.go +++ b/cyral/internal/repository/resource.go @@ -11,7 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &RepoInfo{} }, diff --git a/cyral/internal/serviceaccount/resource.go b/cyral/internal/serviceaccount/resource.go index 7966dd5d..86296db2 100644 --- a/cyral/internal/serviceaccount/resource.go +++ b/cyral/internal/serviceaccount/resource.go @@ -11,7 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &ServiceAccount{} }, diff --git a/cyral/internal/sidecar/credentials/resource.go b/cyral/internal/sidecar/credentials/resource.go index 51a2434a..3c65c54e 100644 --- a/cyral/internal/sidecar/credentials/resource.go +++ b/cyral/internal/sidecar/credentials/resource.go @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &CreateSidecarCredentialsRequest{} }, diff --git a/cyral/internal/sidecar/listener/datasource.go b/cyral/internal/sidecar/listener/datasource.go index 0e40a7ee..5a9726c0 100644 --- a/cyral/internal/sidecar/listener/datasource.go +++ b/cyral/internal/sidecar/listener/datasource.go @@ -12,7 +12,7 @@ import ( "github.com/cyralinc/terraform-provider-cyral/cyral/utils" ) -var dsContextHandler = core.DefaultContextHandler{ +var dsContextHandler = core.HTTPContextHandler{ ResourceName: dataSourceName, ResourceType: resourcetype.DataSource, SchemaWriterFactoryGetMethod: func(_ *schema.ResourceData) core.SchemaWriter { return &ReadDataSourceSidecarListenerAPIResponse{} }, diff --git a/cyral/internal/sidecar/listener/resource.go b/cyral/internal/sidecar/listener/resource.go index be540fba..75486d8a 100644 --- a/cyral/internal/sidecar/listener/resource.go +++ b/cyral/internal/sidecar/listener/resource.go @@ -13,7 +13,7 @@ import ( "github.com/cyralinc/terraform-provider-cyral/cyral/utils" ) -var resourceContextHandler = core.DefaultContextHandler{ +var resourceContextHandler = core.HTTPContextHandler{ ResourceName: resourceName, ResourceType: resourcetype.Resource, SchemaReaderFactory: func() core.SchemaReader { return &SidecarListenerResource{} },