Skip to content

Commit

Permalink
Simplify way further :)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcmjunior committed Nov 21, 2023
1 parent a73d003 commit bc17c34
Show file tree
Hide file tree
Showing 44 changed files with 833 additions and 726 deletions.
40 changes: 25 additions & 15 deletions cyral/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ func (r *NewFeature) ReadFromSchema(d *schema.ResourceData) error {
r.Description = d.Get("description").(string)
return nil
}

var contextHandler = core.DefaultContextHandler{
ResourceName: "New Feature",
ResourceType: resourcetype.DataSource,
SchemaReaderFactory: func() core.SchemaReader { return &NewFeature{} },
SchemaWriterFactory: func(_ *schema.ResourceData) core.SchemaWriter { return &NewFeature{} },
BaseURLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/NewFeature", c.ControlPlane)
},
}
```

### datasource.go
Expand All @@ -66,10 +56,20 @@ added by the default read handler returned in `contextHandler.ReadContext()`.
// datasource.go
package newfeature

var dsContextHandler = core.DefaultContextHandler{
ResourceName: "New Feature",
ResourceType: resourcetype.DataSource,
SchemaReaderFactory: func() core.SchemaReader { return &NewFeature{} },
SchemaWriterFactory: func(_ *schema.ResourceData) core.SchemaWriter { return &NewFeature{} },
BaseURLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/NewFeature", c.ControlPlane)
},
}

func dataSourceSchema() *schema.Resource {
return &schema.Resource{
Description: "Some description.",
ReadContext: contextHandler.ReadContext(),
ReadContext: dsContextHandler.ReadContext(),
Schema: map[string]*schema.Schema{
"name": {
Description: "Retrieve the unique label with this name, if it exists.",
Expand All @@ -92,13 +92,23 @@ func dataSourceSchema() *schema.Resource {
// resource.go
package newfeature

var resourceContextHandler = core.DefaultContextHandler{
ResourceName: "New Feature",
ResourceType: resourcetype.Resource,
SchemaReaderFactory: func() core.SchemaReader { return &NewFeature{} },
SchemaWriterFactory: func(_ *schema.ResourceData) core.SchemaWriter { return &NewFeature{} },
BaseURLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/NewFeature", c.ControlPlane)
},
}

func resourceSchema() *schema.Resource {
return &schema.Resource{
Description: "Some description.",
CreateContext: contextHandler.CreateContext(),
ReadContext: contextHandler.ReadContext(),
UpdateContext: contextHandler.UpdateContext(),
DeleteContext: contextHandler.DeleteContext(),
CreateContext: resourceContextHandler.CreateContext(),
ReadContext: resourceContextHandler.ReadContext(),
UpdateContext: resourceContextHandler.UpdateContext(),
DeleteContext: resourceContextHandler.DeleteContext(),
Schema: map[string]*schema.Schema{
"name": {
Description: "...",
Expand Down
5 changes: 3 additions & 2 deletions cyral/core/default_context_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func defaultSchemaWriterFactory(d *schema.ResourceData) SchemaWriter {
func defaultOperationHandler(
resourceName string,
resourceType rt.ResourceType,
operationtype ot.OperationType,
operationType ot.OperationType,
baseURLFactory URLFactoryFunc,
httpMethod string,
schemaReaderFactory SchemaReaderFactoryFunc,
Expand All @@ -53,7 +53,7 @@ func defaultOperationHandler(
url = fmt.Sprintf("%s/%s", baseURLFactory(d, c), d.Id())
}
tflog.Debug(context.Background(), fmt.Sprintf("Returning base URL for %s '%s' operation '%s' and httpMethod %s: %s",
resourceType, resourceName, operationtype, httpMethod, url))
resourceType, resourceName, operationType, httpMethod, url))
return url
}

Expand All @@ -65,6 +65,7 @@ func defaultOperationHandler(
}
result := ResourceOperationConfig{
ResourceName: resourceName,
Type: operationType,
ResourceType: resourceType,
HttpMethod: httpMethod,
URLFactory: endpoint,
Expand Down
76 changes: 26 additions & 50 deletions cyral/core/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type ResourceOperation struct {
Type operationtype.OperationType
Config ResourceOperationConfig
}

type URLFactoryFunc = func(d *schema.ResourceData, c *client.Client) string
type SchemaReaderFactoryFunc = func() SchemaReader
type SchemaWriterFactoryFunc = func(d *schema.ResourceData) SchemaWriter
Expand Down Expand Up @@ -65,6 +60,7 @@ type PackageSchema interface {
type ResourceOperationConfig struct {
// Human-readable resource name that will be used in log messages
ResourceName string
Type operationtype.OperationType
// Resource type
ResourceType resourcetype.ResourceType
HttpMethod string
Expand All @@ -74,91 +70,71 @@ type ResourceOperationConfig struct {
SchemaWriterFactory SchemaWriterFactoryFunc
}

func CRUDResources(resourceOperations []ResourceOperation) func(context.Context, *schema.ResourceData, any) diag.Diagnostics {
return handleRequests(resourceOperations)
func CRUDResources(operations []ResourceOperationConfig) func(context.Context, *schema.ResourceData, any) diag.Diagnostics {
return handleRequests(operations)
}

func CreateResource(createConfig, readConfig ResourceOperationConfig) schema.CreateContextFunc {
return handleRequests(
[]ResourceOperation{
{
Type: operationtype.Create,
Config: createConfig,
},
{
Type: operationtype.Read,
Config: readConfig,
},
[]ResourceOperationConfig{
createConfig, readConfig,
},
)
}

func ReadResource(readConfig ResourceOperationConfig) schema.ReadContextFunc {
return handleRequests(
[]ResourceOperation{
{
Type: operationtype.Read,
Config: readConfig,
},
[]ResourceOperationConfig{
readConfig,
},
)
}

func UpdateResource(updateConfig, readConfig ResourceOperationConfig) schema.UpdateContextFunc {
return handleRequests(
[]ResourceOperation{
{
Type: operationtype.Update,
Config: updateConfig,
},
{
Type: operationtype.Read,
Config: readConfig,
},
[]ResourceOperationConfig{
updateConfig, readConfig,
},
)
}

func DeleteResource(deleteConfig ResourceOperationConfig) schema.DeleteContextFunc {
return handleRequests(
[]ResourceOperation{
{
Type: operationtype.Delete,
Config: deleteConfig,
},
[]ResourceOperationConfig{
deleteConfig,
},
)
}

func handleRequests(resourceOperations []ResourceOperation) func(context.Context, *schema.ResourceData, any) diag.Diagnostics {
func handleRequests(operations []ResourceOperationConfig) func(context.Context, *schema.ResourceData, any) diag.Diagnostics {
return func(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
for _, operation := range resourceOperations {
tflog.Debug(ctx, fmt.Sprintf("Init %s %s", operation.Config.ResourceName, operation.Type))
for _, operation := range operations {
tflog.Debug(ctx, fmt.Sprintf("Init %s - %s", operation.ResourceName, operation.Type))
c := m.(*client.Client)

var resourceData SchemaReader
if operation.Config.SchemaReaderFactory != nil {
if resourceData = operation.Config.SchemaReaderFactory(); resourceData != nil {
if operation.SchemaReaderFactory != nil {
if resourceData = operation.SchemaReaderFactory(); resourceData != nil {
tflog.Debug(ctx, fmt.Sprintf("Calling ReadFromSchema. Schema: %#v", d))
if err := resourceData.ReadFromSchema(d); err != nil {
return utils.CreateError(
fmt.Sprintf("Unable to %s resource %s", operation.Type, operation.Config.ResourceName),
fmt.Sprintf("Unable to %s resource %s", operation.Type, operation.ResourceName),
err.Error(),
)
}
tflog.Debug(ctx, fmt.Sprintf("Succesful call to ReadFromSchema. resourceData: %#v", resourceData))
}
}

url := operation.Config.URLFactory(d, c)
url := operation.URLFactory(d, c)

body, err := c.DoRequest(url, operation.Config.HttpMethod, resourceData)
if operation.Config.RequestErrorHandler != nil {
err = operation.Config.RequestErrorHandler.HandleError(d, c, err)
body, err := c.DoRequest(url, operation.HttpMethod, resourceData)
if operation.RequestErrorHandler != nil {
err = operation.RequestErrorHandler.HandleError(d, c, err)
}
if err != nil {
return utils.CreateError(
fmt.Sprintf("Unable to %s resource %s", operation.Type, operation.Config.ResourceName),
fmt.Sprintf("Unable to %s resource %s", operation.Type, operation.ResourceName),
err.Error(),
)
}
Expand All @@ -168,11 +144,11 @@ func handleRequests(resourceOperations []ResourceOperation) func(context.Context
/// TODO: Remove this feature after refactoring all resources to use the `DefaultContext`.
var responseDataFunc SchemaWriterFactoryFunc
if body != nil {
if operation.Config.SchemaWriterFactory == nil && operation.Type == operationtype.Create {
if operation.SchemaWriterFactory == nil && operation.Type == operationtype.Create {
responseDataFunc = defaultSchemaWriterFactory
tflog.Debug(ctx, "NewResponseData function set to defaultSchemaWriterFactory.")
} else {
responseDataFunc = operation.Config.SchemaWriterFactory
responseDataFunc = operation.SchemaWriterFactory
}
}
if responseDataFunc != nil {
Expand All @@ -185,15 +161,15 @@ func handleRequests(resourceOperations []ResourceOperation) func(context.Context
tflog.Debug(ctx, fmt.Sprintf("Calling WriteToSchema: responseData: %#v", responseData))
if err := responseData.WriteToSchema(d); err != nil {
return utils.CreateError(
fmt.Sprintf("Unable to %s resource %s", operation.Type, operation.Config.ResourceName),
fmt.Sprintf("Unable to %s resource %s", operation.Type, operation.ResourceName),
err.Error(),
)
}
tflog.Debug(ctx, fmt.Sprintf("Succesful call to WriteToSchema. d: %#v", d))
}
}

tflog.Debug(ctx, fmt.Sprintf("End %s", operation.Config.ResourceName))
tflog.Debug(ctx, fmt.Sprintf("End %s - %s", operation.ResourceName, operation.Type))
}
return diag.Diagnostics{}
}
Expand Down
2 changes: 2 additions & 0 deletions cyral/internal/datalabel/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/cyralinc/terraform-provider-cyral/cyral/client"
"github.com/cyralinc/terraform-provider-cyral/cyral/core"
"github.com/cyralinc/terraform-provider-cyral/cyral/core/types/operationtype"
"github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype"
"github.com/cyralinc/terraform-provider-cyral/cyral/utils"
)
Expand Down Expand Up @@ -99,6 +100,7 @@ func dataSourceSchema() *schema.Resource {
func readConfig() core.ResourceOperationConfig {
return core.ResourceOperationConfig{
ResourceName: "Data Label",
Type: operationtype.Read,
ResourceType: resourcetype.DataSource,
HttpMethod: http.MethodGet,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
Expand Down
2 changes: 2 additions & 0 deletions cyral/internal/datalabel/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/cyralinc/terraform-provider-cyral/cyral/client"
"github.com/cyralinc/terraform-provider-cyral/cyral/core"
"github.com/cyralinc/terraform-provider-cyral/cyral/core/types/operationtype"
"github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/datalabel/classificationrule"
"github.com/cyralinc/terraform-provider-cyral/cyral/utils"
Expand All @@ -30,6 +31,7 @@ func resourceSchema() *schema.Resource {
CreateContext: core.CreateResource(
core.ResourceOperationConfig{
ResourceName: "DataLabelResourceCreate",
Type: operationtype.Create,
HttpMethod: http.MethodPut,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/datalabels/%s",
Expand Down
8 changes: 8 additions & 0 deletions cyral/internal/deprecated/resource_cyral_integration_idp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/cyralinc/terraform-provider-cyral/cyral/client"
"github.com/cyralinc/terraform-provider-cyral/cyral/core"
"github.com/cyralinc/terraform-provider-cyral/cyral/core/types/operationtype"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/integration/idpsaml"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -266,6 +267,7 @@ func resourceIntegrationIdPCreate(identityProvider string) schema.CreateContextF
diag := core.CreateResource(
core.ResourceOperationConfig{
ResourceName: "resourceIntegrationIdPCreate - Integration",
Type: operationtype.Create,
HttpMethod: http.MethodPost,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/saml", c.ControlPlane)
Expand All @@ -285,6 +287,7 @@ func resourceIntegrationIdPCreate(identityProvider string) schema.CreateContextF
diag = core.CreateResource(
core.ResourceOperationConfig{
ResourceName: "resourceIntegrationIdPCreate - IdentityProvider",
Type: operationtype.Create,
HttpMethod: http.MethodPost,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/conf/identityProviders/%s", c.ControlPlane, d.Id())
Expand Down Expand Up @@ -319,6 +322,7 @@ func resourceIntegrationIdPUpdate(identityProvider string) schema.UpdateContextF
diag := core.UpdateResource(
core.ResourceOperationConfig{
ResourceName: "resourceIntegrationIdPUpdate - Integration",
Type: operationtype.Update,
HttpMethod: http.MethodPut,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/saml/%s", c.ControlPlane, d.Id())
Expand All @@ -344,6 +348,7 @@ func resourceIntegrationIdPDelete(ctx context.Context, d *schema.ResourceData, m
diag = core.DeleteResource(
core.ResourceOperationConfig{
ResourceName: "resourceIntegrationIdPDelete - IdentityProvider",
Type: operationtype.Delete,
HttpMethod: http.MethodDelete,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/conf/identityProviders/%s", c.ControlPlane, d.Id())
Expand All @@ -357,6 +362,7 @@ func resourceIntegrationIdPDelete(ctx context.Context, d *schema.ResourceData, m

var readIntegrationIdPConfig = core.ResourceOperationConfig{
ResourceName: "resourceIntegrationIdPRead - Integration",
Type: operationtype.Read,
HttpMethod: http.MethodGet,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/saml/%s", c.ControlPlane, d.Id())
Expand All @@ -366,6 +372,7 @@ var readIntegrationIdPConfig = core.ResourceOperationConfig{

var readIdentityProviderConfig = core.ResourceOperationConfig{
ResourceName: "resourceIntegrationIdPRead - IdentityProvider",
Type: operationtype.Read,
HttpMethod: http.MethodGet,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/conf/identityProviders/%s", c.ControlPlane, d.Id())
Expand All @@ -375,6 +382,7 @@ var readIdentityProviderConfig = core.ResourceOperationConfig{

var deleteIntegrationIdPConfig = core.ResourceOperationConfig{
ResourceName: "resourceIntegrationIdPDelete - Integration",
Type: operationtype.Delete,
HttpMethod: http.MethodDelete,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/saml/%s", c.ControlPlane, d.Id())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/cyralinc/terraform-provider-cyral/cyral/client"
"github.com/cyralinc/terraform-provider-cyral/cyral/core"
"github.com/cyralinc/terraform-provider-cyral/cyral/core/types/operationtype"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -91,6 +92,7 @@ func (data *IntegrationConfExtension) ReadFromSchema(d *schema.ResourceData) err
func ConfExtensionIntegrationCreate(templateType string) core.ResourceOperationConfig {
return core.ResourceOperationConfig{
ResourceName: fmt.Sprintf("%s_IntegrationResourceCreate", templateType),
Type: operationtype.Create,
HttpMethod: http.MethodPost,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf(
Expand All @@ -106,6 +108,7 @@ func ConfExtensionIntegrationCreate(templateType string) core.ResourceOperationC
func ConfExtensionIntegrationRead(templateType string) core.ResourceOperationConfig {
return core.ResourceOperationConfig{
ResourceName: fmt.Sprintf("%s_IntegrationResourceRead", templateType),
Type: operationtype.Read,
HttpMethod: http.MethodGet,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf(
Expand All @@ -122,6 +125,7 @@ func ConfExtensionIntegrationRead(templateType string) core.ResourceOperationCon
func ConfExtensionIntegrationUpdate(templateType string) core.ResourceOperationConfig {
return core.ResourceOperationConfig{
ResourceName: fmt.Sprintf("%s_IntegrationResourceUpdate", templateType),
Type: operationtype.Update,
HttpMethod: http.MethodPut,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf(
Expand All @@ -137,6 +141,7 @@ func ConfExtensionIntegrationUpdate(templateType string) core.ResourceOperationC
func ConfExtensionIntegrationDelete(templateType string) core.ResourceOperationConfig {
return core.ResourceOperationConfig{
ResourceName: fmt.Sprintf("%s_IntegrationResourceDelete", templateType),
Type: operationtype.Delete,
HttpMethod: http.MethodDelete,
URLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf(
Expand Down
Loading

0 comments on commit bc17c34

Please sign in to comment.