Skip to content

Commit

Permalink
Rename ResourceData and ResponseData
Browse files Browse the repository at this point in the history
  • Loading branch information
wcmjunior committed Nov 7, 2023
1 parent e304244 commit f2755a9
Show file tree
Hide file tree
Showing 42 changed files with 145 additions and 147 deletions.
10 changes: 5 additions & 5 deletions cyral/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ for resources and data sources.
## How to use to create new resources and data sources

There are some main types that must be used to create a new resources and data sources:
`SchemaDescriptor`, `PackageSchema`, `ResourceData`, `ResponseData` and
`SchemaDescriptor`, `PackageSchema`, `ResourceData`, `SchemaWriter` and
`ResourceOperationConfig`. In a nutshell, these abstractions provide the means to
teach the provider how to interact with the API, how to describe the feature as a
Terraform resource/data source and finally teach the provider how to perform the
Expand Down Expand Up @@ -60,7 +60,7 @@ func dataSourceSchema() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/NewFeature/%s", c.ControlPlane, d.Get("name").(string))
},
NewResponseData: func(d *schema.ResourceData) core.ResponseData {
NewResponseData: func(d *schema.ResourceData) core.SchemaWriter {
return &NewFeature{}
},
}),
Expand Down Expand Up @@ -96,7 +96,7 @@ func resourceSchema() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/NewFeature", c.ControlPlane)
},
NewResponseData: func(d *schema.ResourceData) core.ResponseData {
NewResponseData: func(d *schema.ResourceData) core.SchemaWriter {
return &NewFeature{}
},
}, ReadNewFeatureConfig,
Expand All @@ -109,7 +109,7 @@ func resourceSchema() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/NewFeature/%s", c.ControlPlane, d.Id())
},
NewResourceData: func() core.ResourceData { return &NewFeature{} },
NewResourceData: func() core.SchemaReader { return &NewFeature{} },
}, ReadNewFeatureConfig,
),
DeleteContext: core.DeleteResource(
Expand Down Expand Up @@ -142,7 +142,7 @@ var ReadNewFeatureConfig = core.ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/NewFeature/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &NewFeature{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &NewFeature{} },
RequestErrorHandler: &core.ReadIgnoreHttpNotFound{ResName: "NewFeature"},
}
```
Expand Down
26 changes: 12 additions & 14 deletions cyral/core/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ type RequestErrorHandler interface {
HandleError(d *schema.ResourceData, c *client.Client, err error) error
}

// TODO Rename as `SchemaReader` and document properly.
// Teaches a resource or data source how to read from the Terraform schema and
// store in the data structure defined for it.
type ResourceData interface {
type SchemaReader interface {
ReadFromSchema(d *schema.ResourceData) error
}

// TODO Rename as `SchemaWriter` and document properly.
// Teaches a resource or data source how to write to the Terraform schema from
// the data stored in the data structure defined for it.
type ResponseData interface {
type SchemaWriter interface {
WriteToSchema(d *schema.ResourceData) error
}

Expand Down Expand Up @@ -65,18 +63,18 @@ type ResourceOperationConfig struct {
HttpMethod string
CreateURL URLCreatorFunc
RequestErrorHandler
NewResourceData func() ResourceData
NewResourceData func() SchemaReader
// TODO provide a default implementation returning the IDBasedResponse:
// func(_ *schema.ResourceData) core.ResponseData { return &core.IDBasedResponse{} }
NewResponseData func(d *schema.ResourceData) ResponseData
// func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} }
NewResponseData func(d *schema.ResourceData) SchemaWriter
}

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

func CreateResource(createConfig, readConfig ResourceOperationConfig) schema.CreateContextFunc {
return HandleRequests(
return handleRequests(
[]ResourceOperation{
{
Type: OperationTypeCreate,
Expand All @@ -91,7 +89,7 @@ func CreateResource(createConfig, readConfig ResourceOperationConfig) schema.Cre
}

func ReadResource(readConfig ResourceOperationConfig) schema.ReadContextFunc {
return HandleRequests(
return handleRequests(
[]ResourceOperation{
{
Type: OperationTypeRead,
Expand All @@ -102,7 +100,7 @@ func ReadResource(readConfig ResourceOperationConfig) schema.ReadContextFunc {
}

func UpdateResource(updateConfig, readConfig ResourceOperationConfig) schema.UpdateContextFunc {
return HandleRequests(
return handleRequests(
[]ResourceOperation{
{
Type: OperationTypeUpdate,
Expand All @@ -117,7 +115,7 @@ func UpdateResource(updateConfig, readConfig ResourceOperationConfig) schema.Upd
}

func DeleteResource(deleteConfig ResourceOperationConfig) schema.DeleteContextFunc {
return HandleRequests(
return handleRequests(
[]ResourceOperation{
{
Type: OperationTypeDelete,
Expand All @@ -127,15 +125,15 @@ func DeleteResource(deleteConfig ResourceOperationConfig) schema.DeleteContextFu
)
}

func HandleRequests(
func handleRequests(
resourceOperations []ResourceOperation,
) func(context.Context, *schema.ResourceData, any) diag.Diagnostics {
return func(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
for _, operation := range resourceOperations {
log.Printf("[DEBUG] Init %s", operation.Config.Name)
c := m.(*client.Client)

var resourceData ResourceData
var resourceData SchemaReader
if operation.Config.NewResourceData != nil {
if resourceData = operation.Config.NewResourceData(); resourceData != nil {
log.Printf("[DEBUG] Calling ReadFromSchema. Schema: %#v", d)
Expand Down
2 changes: 1 addition & 1 deletion cyral/internal/datalabel/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func readConfig() core.ResourceOperationConfig {

return fmt.Sprintf("https://%s/v1/datalabels%s%s", c.ControlPlane, pathParams, queryParams)
},
NewResponseData: func(d *schema.ResourceData) core.ResponseData {
NewResponseData: func(d *schema.ResourceData) core.SchemaWriter {
nameFilter := d.Get("name").(string)
if nameFilter == "" {
return &GetDataLabelsResponse{}
Expand Down
8 changes: 4 additions & 4 deletions cyral/internal/datalabel/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func resourceSchema() *schema.Resource {
c.ControlPlane,
d.Get("name").(string))
},
NewResourceData: func() core.ResourceData { return &DataLabel{} },
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &DataLabel{} },
NewResourceData: func() core.SchemaReader { return &DataLabel{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &DataLabel{} },
}, readDataLabelConfig,
),
ReadContext: core.ReadResource(readDataLabelConfig),
Expand All @@ -39,7 +39,7 @@ func resourceSchema() *schema.Resource {
c.ControlPlane,
d.Get("name").(string))
},
NewResourceData: func() core.ResourceData { return &DataLabel{} },
NewResourceData: func() core.SchemaReader { return &DataLabel{} },
}, readDataLabelConfig,
),
DeleteContext: core.DeleteResource(
Expand Down Expand Up @@ -129,6 +129,6 @@ var readDataLabelConfig = core.ResourceOperationConfig{
c.ControlPlane,
d.Get("name").(string))
},
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &DataLabel{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &DataLabel{} },
RequestErrorHandler: &core.ReadIgnoreHttpNotFound{ResName: "Data Label"},
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var ReadDatadogConfig = core.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) core.ResponseData { return &DatadogIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &DatadogIntegration{} },
RequestErrorHandler: &core.ReadIgnoreHttpNotFound{ResName: "Integration datadog"},
}

Expand All @@ -50,8 +50,8 @@ func ResourceIntegrationDatadog() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/datadog", c.ControlPlane)
},
NewResourceData: func() core.ResourceData { return &DatadogIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &core.IDBasedResponse{} },
NewResourceData: func() core.SchemaReader { return &DatadogIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadDatadogConfig,
),
ReadContext: core.ReadResource(ReadDatadogConfig),
Expand All @@ -62,7 +62,7 @@ func ResourceIntegrationDatadog() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/datadog/%s", c.ControlPlane, d.Id())
},
NewResourceData: func() core.ResourceData { return &DatadogIntegration{} },
NewResourceData: func() core.SchemaReader { return &DatadogIntegration{} },
}, ReadDatadogConfig,
),
DeleteContext: core.DeleteResource(
Expand Down
8 changes: 4 additions & 4 deletions cyral/internal/deprecated/resource_cyral_integration_elk.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var ReadELKConfig = core.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) core.ResponseData { return &ELKIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &ELKIntegration{} },
RequestErrorHandler: &core.ReadIgnoreHttpNotFound{ResName: "Integration elk"},
}

Expand All @@ -52,8 +52,8 @@ func ResourceIntegrationELK() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/elk", c.ControlPlane)
},
NewResourceData: func() core.ResourceData { return &ELKIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &core.IDBasedResponse{} },
NewResourceData: func() core.SchemaReader { return &ELKIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadELKConfig,
),
ReadContext: core.ReadResource(ReadELKConfig),
Expand All @@ -64,7 +64,7 @@ func ResourceIntegrationELK() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/elk/%s", c.ControlPlane, d.Id())
},
NewResourceData: func() core.ResourceData { return &ELKIntegration{} },
NewResourceData: func() core.SchemaReader { return &ELKIntegration{} },
}, ReadELKConfig,
),
DeleteContext: core.DeleteResource(
Expand Down
14 changes: 7 additions & 7 deletions cyral/internal/deprecated/resource_cyral_integration_idp.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,14 @@ func resourceIntegrationIdPCreate(identityProvider string) schema.CreateContextF
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/saml", c.ControlPlane)
},
NewResourceData: func() core.ResourceData {
NewResourceData: func() core.SchemaReader {
return &SAMLIntegrationData{
SAMLSetting: &SAMLSetting{
IdentityProvider: identityProvider,
},
}
},
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &AliasBasedResponse{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &AliasBasedResponse{} },
}, readIntegrationIdPConfig,
)(ctx, d, m)

Expand All @@ -289,8 +289,8 @@ func resourceIntegrationIdPCreate(identityProvider string) schema.CreateContextF
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/conf/identityProviders/%s", c.ControlPlane, d.Id())
},
NewResourceData: func() core.ResourceData { return &idpsaml.IdentityProviderData{} },
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &idpsaml.IdentityProviderData{} },
NewResourceData: func() core.SchemaReader { return &idpsaml.IdentityProviderData{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &idpsaml.IdentityProviderData{} },
}, readIdentityProviderConfig,
)(ctx, d, m)

Expand Down Expand Up @@ -323,7 +323,7 @@ func resourceIntegrationIdPUpdate(identityProvider string) schema.UpdateContextF
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/saml/%s", c.ControlPlane, d.Id())
},
NewResourceData: func() core.ResourceData {
NewResourceData: func() core.SchemaReader {
return &SAMLIntegrationData{
SAMLSetting: &SAMLSetting{
IdentityProvider: identityProvider,
Expand Down Expand Up @@ -361,7 +361,7 @@ var readIntegrationIdPConfig = core.ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/saml/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &SAMLIntegrationData{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &SAMLIntegrationData{} },
}

var readIdentityProviderConfig = core.ResourceOperationConfig{
Expand All @@ -370,7 +370,7 @@ var readIdentityProviderConfig = core.ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/conf/identityProviders/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &idpsaml.IdentityProviderData{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &idpsaml.IdentityProviderData{} },
}

var deleteIntegrationIdPConfig = core.ResourceOperationConfig{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var ReadLogstashConfig = core.ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/logstash/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &LogstashIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &LogstashIntegration{} },
}

func ResourceIntegrationLogstash() *schema.Resource {
Expand All @@ -55,8 +55,8 @@ func ResourceIntegrationLogstash() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/logstash", c.ControlPlane)
},
NewResourceData: func() core.ResourceData { return &LogstashIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &core.IDBasedResponse{} },
NewResourceData: func() core.SchemaReader { return &LogstashIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadLogstashConfig,
),
ReadContext: core.ReadResource(ReadLogstashConfig),
Expand All @@ -67,7 +67,7 @@ func ResourceIntegrationLogstash() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/logstash/%s", c.ControlPlane, d.Id())
},
NewResourceData: func() core.ResourceData { return &LogstashIntegration{} },
NewResourceData: func() core.SchemaReader { return &LogstashIntegration{} },
}, ReadLogstashConfig,
),
DeleteContext: core.DeleteResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var ReadLookerConfig = core.ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/looker/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &LookerIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &LookerIntegration{} },
}

func ResourceIntegrationLooker() *schema.Resource {
Expand All @@ -49,8 +49,8 @@ func ResourceIntegrationLooker() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/looker", c.ControlPlane)
},
NewResourceData: func() core.ResourceData { return &LookerIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &core.IDBasedResponse{} },
NewResourceData: func() core.SchemaReader { return &LookerIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadLookerConfig,
),
ReadContext: core.ReadResource(ReadLookerConfig),
Expand All @@ -61,7 +61,7 @@ func ResourceIntegrationLooker() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/looker/%s", c.ControlPlane, d.Id())
},
NewResourceData: func() core.ResourceData { return &LookerIntegration{} },
NewResourceData: func() core.SchemaReader { return &LookerIntegration{} },
}, ReadLookerConfig,
),
DeleteContext: core.DeleteResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var ReadSplunkConfig = core.ResourceOperationConfig{
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/splunk/%s", c.ControlPlane, d.Id())
},
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &SplunkIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &SplunkIntegration{} },
}

func ResourceIntegrationSplunk() *schema.Resource {
Expand All @@ -59,8 +59,8 @@ func ResourceIntegrationSplunk() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/splunk", c.ControlPlane)
},
NewResourceData: func() core.ResourceData { return &SplunkIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.ResponseData { return &core.IDBasedResponse{} },
NewResourceData: func() core.SchemaReader { return &SplunkIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadSplunkConfig,
),
ReadContext: core.ReadResource(ReadSplunkConfig),
Expand All @@ -71,7 +71,7 @@ func ResourceIntegrationSplunk() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/splunk/%s", c.ControlPlane, d.Id())
},
NewResourceData: func() core.ResourceData { return &SplunkIntegration{} },
NewResourceData: func() core.SchemaReader { return &SplunkIntegration{} },
}, ReadSplunkConfig,
),
DeleteContext: core.DeleteResource(
Expand Down
Loading

0 comments on commit f2755a9

Please sign in to comment.