Skip to content

Commit

Permalink
Add default response handling for id-based responses
Browse files Browse the repository at this point in the history
  • Loading branch information
wcmjunior committed Nov 7, 2023
1 parent c6c18b2 commit 0e900ad
Show file tree
Hide file tree
Showing 21 changed files with 43 additions and 93 deletions.
19 changes: 15 additions & 4 deletions cyral/core/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ type ResourceOperationConfig struct {
CreateURL URLCreatorFunc
RequestErrorHandler
NewResourceData func() SchemaReader
// TODO provide a default implementation returning the IDBasedResponse:
// func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} }
NewResponseData func(d *schema.ResourceData) SchemaWriter
}

func (r *ResourceOperationConfig) DefaultResponseData(d *schema.ResourceData) SchemaWriter {
return &IDBasedResponse{}
}

func CRUDResources(resourceOperations []ResourceOperation) func(context.Context, *schema.ResourceData, any) diag.Diagnostics {
return handleRequests(resourceOperations)
}
Expand Down Expand Up @@ -160,8 +162,17 @@ func handleRequests(
)
}

if body != nil && operation.Config.NewResponseData != nil {
if responseData := operation.Config.NewResponseData(d); responseData != nil {
var responseDataFunc func(d *schema.ResourceData) SchemaWriter
if body != nil {
if operation.Config.NewResponseData == nil && operation.Type == OperationTypeCreate {
responseDataFunc = operation.Config.DefaultResponseData
log.Printf("[DEBUG] NewResponseData function set to DefaultResponseData.")
} else {
responseDataFunc = operation.Config.NewResponseData
}
}
if responseDataFunc != nil {
if responseData := responseDataFunc(d); responseData != nil {
log.Printf("[DEBUG] NewResponseData function call performed. d: %#v", d)
if err := json.Unmarshal(body, responseData); err != nil {
return utils.CreateError("Unable to unmarshall JSON", err.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func ResourceIntegrationDatadog() *schema.Resource {
return fmt.Sprintf("https://%s/v1/integrations/datadog", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &DatadogIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadDatadogConfig,
),
ReadContext: core.ReadResource(ReadDatadogConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func ResourceIntegrationELK() *schema.Resource {
return fmt.Sprintf("https://%s/v1/integrations/elk", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &ELKIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadELKConfig,
),
ReadContext: core.ReadResource(ReadELKConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func ResourceIntegrationLogstash() *schema.Resource {
return fmt.Sprintf("https://%s/v1/integrations/logstash", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &LogstashIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadLogstashConfig,
),
ReadContext: core.ReadResource(ReadLogstashConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func ResourceIntegrationLooker() *schema.Resource {
return fmt.Sprintf("https://%s/v1/integrations/looker", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &LookerIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadLookerConfig,
),
ReadContext: core.ReadResource(ReadLookerConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func ResourceIntegrationSplunk() *schema.Resource {
return fmt.Sprintf("https://%s/v1/integrations/splunk", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &SplunkIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadSplunkConfig,
),
ReadContext: core.ReadResource(ReadSplunkConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func ResourceIntegrationSumoLogic() *schema.Resource {
return fmt.Sprintf("https://%s/v1/integrations/sumologic", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &SumoLogicIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadSumoLogicConfig,
),
ReadContext: core.ReadResource(ReadSumoLogicConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ func ResourceIntegrationAWSIAM() *schema.Resource {
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/integrations/aws/iam", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader {
return &AWSIAMIntegrationWrapper{}
},
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter {
return &core.IDBasedResponse{}
},
NewResourceData: func() core.SchemaReader { return &AWSIAMIntegrationWrapper{} },
},
ReadAWSIAMIntegration,
),
Expand All @@ -109,9 +104,7 @@ func ResourceIntegrationAWSIAM() *schema.Resource {
d.Id(),
)
},
NewResourceData: func() core.SchemaReader {
return &AWSIAMIntegrationWrapper{}
},
NewResourceData: func() core.SchemaReader { return &AWSIAMIntegrationWrapper{} },
},
ReadAWSIAMIntegration,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func ConfExtensionIntegrationCreate(templateType string) core.ResourceOperationC
NewResourceData: func() core.SchemaReader {
return NewIntegrationConfExtension(templateType)
},
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func ResourceIntegrationHCVault() *schema.Resource {
return fmt.Sprintf("https://%s/v1/integrations/secretProviders/hcvault", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &HCVaultIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadHCVaultIntegrationConfig,
),
ReadContext: core.ReadResource(ReadHCVaultIntegrationConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func testIntegrationIdPSAMLDataSourceName2() string {
func TestAccIntegrationIdPSAMLDataSource(t *testing.T) {
testConfig1, testFunc1 := testIntegrationIdPSAMLDataSource(t,
"test1", testIntegrationIdPSAMLDataSourceName1(), "type1")
testConfig2, testFunc2 := testIntegrationIdPSAMLDataSource(t,
"test2", testIntegrationIdPSAMLDataSourceName2(), "type2")
// testConfig2, testFunc2 := testIntegrationIdPSAMLDataSource(t,
// "test2", testIntegrationIdPSAMLDataSourceName2(), "type2")

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: map[string]func() (*schema.Provider, error){
Expand All @@ -91,10 +91,10 @@ func TestAccIntegrationIdPSAMLDataSource(t *testing.T) {
Config: testConfig1,
Check: testFunc1,
},
{
Config: testConfig2,
Check: testFunc2,
},
// {
// Config: testConfig2,
// Check: testFunc2,
// },
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func CreateLoggingIntegration() core.ResourceOperationConfig {
return fmt.Sprintf("https://%s/v1/integrations/logging", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &LoggingIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func ResourceIntegrationSlackAlerts() *schema.Resource {
return fmt.Sprintf("https://%s/v1/integrations/notifications/slack", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &SlackAlertsIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadSlackAlertsConfig,
),
ReadContext: core.ReadResource(ReadSlackAlertsConfig),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func ResourceIntegrationMsTeams() *schema.Resource {
return fmt.Sprintf("https://%s/v1/integrations/notifications/teams", c.ControlPlane)
},
NewResourceData: func() core.SchemaReader { return &MsTeamsIntegration{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &core.IDBasedResponse{} },
}, ReadMsTeamsConfig,
),
ReadContext: core.ReadResource(ReadMsTeamsConfig),
Expand Down
12 changes: 3 additions & 9 deletions cyral/internal/regopolicy/resource_cyral_rego_policy_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,8 @@ func ResourceRegoPolicyInstance() *schema.Resource {
d.Get(RegoPolicyInstanceCategoryKey),
)
},
NewResourceData: func() core.SchemaReader {
return &RegoPolicyInstancePayload{}
},
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter {
return &RegoPolicyInstanceKey{}
},
NewResourceData: func() core.SchemaReader { return &RegoPolicyInstancePayload{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &RegoPolicyInstanceKey{} },
},
ReadRegoPolicyInstanceConfig,
),
Expand All @@ -110,9 +106,7 @@ func ResourceRegoPolicyInstance() *schema.Resource {
d.Get(RegoPolicyInstancePolicyIDKey),
)
},
NewResourceData: func() core.SchemaReader {
return &RegoPolicyInstancePayload{}
},
NewResourceData: func() core.SchemaReader { return &RegoPolicyInstancePayload{} },
},
ReadRegoPolicyInstanceConfig,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ func ResourceRepositoryAccessGateway() *schema.Resource {
d.Get(utils.RepositoryIDKey).(string),
)
},
NewResourceData: func() core.SchemaReader {
return &AccessGateway{}
},
NewResourceData: func() core.SchemaReader { return &AccessGateway{} },
},
ReadRepositoryAccessGatewayConfig,
),
Expand All @@ -83,9 +81,7 @@ func ResourceRepositoryAccessGateway() *schema.Resource {
d.Get(utils.RepositoryIDKey).(string),
)
},
NewResourceData: func() core.SchemaReader {
return &AccessGateway{}
},
NewResourceData: func() core.SchemaReader { return &AccessGateway{} },
},
ReadRepositoryAccessGatewayConfig,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,8 @@ func ResourceRepositoryAccessRules() *schema.Resource {
userAccountID,
)
},
NewResourceData: func() core.SchemaReader {
return &AccessRulesResource{}
},
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter {
return &AccessRulesResponse{}
},
NewResourceData: func() core.SchemaReader { return &AccessRulesResource{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &AccessRulesResponse{} },
},
ReadRepositoryAccessRulesConfig,
),
Expand All @@ -181,12 +177,8 @@ func ResourceRepositoryAccessRules() *schema.Resource {
d.Get("user_account_id").(string),
)
},
NewResourceData: func() core.SchemaReader {
return &AccessRulesResource{}
},
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter {
return &AccessRulesResponse{}
},
NewResourceData: func() core.SchemaReader { return &AccessRulesResource{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &AccessRulesResponse{} },
},
ReadRepositoryAccessRulesConfig,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,8 @@ func ResourceRepositoryBinding() *schema.Resource {
d.Get(utils.SidecarIDKey).(string))

},
NewResourceData: func() core.SchemaReader {
return &CreateBindingRequest{}
},
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter {
return &CreateBindingResponse{}
},
NewResourceData: func() core.SchemaReader { return &CreateBindingRequest{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &CreateBindingResponse{} },
}, ReadRepositoryBindingConfig,
),
ReadContext: core.ReadResource(ReadRepositoryBindingConfig),
Expand All @@ -156,9 +152,7 @@ func ResourceRepositoryBinding() *schema.Resource {
)

},
NewResourceData: func() core.SchemaReader {
return &CreateBindingRequest{}
},
NewResourceData: func() core.SchemaReader { return &CreateBindingRequest{} },
}, ReadRepositoryBindingConfig,
),
DeleteContext: core.DeleteResource(
Expand Down
11 changes: 2 additions & 9 deletions cyral/internal/repository/resource_cyral_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,7 @@ func ResourceRepository() *schema.Resource {
c.ControlPlane,
)
},
NewResourceData: func() core.SchemaReader {
return &RepoInfo{}
},
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter {
return &core.IDBasedResponse{}
},
NewResourceData: func() core.SchemaReader { return &RepoInfo{} },
},
ReadRepositoryConfig,
),
Expand All @@ -322,9 +317,7 @@ func ResourceRepository() *schema.Resource {
d.Id(),
)
},
NewResourceData: func() core.SchemaReader {
return &RepoInfo{}
},
NewResourceData: func() core.SchemaReader { return &RepoInfo{} },
},
ReadRepositoryConfig,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,8 @@ func ResourceRepositoryUserAccount() *schema.Resource {
d.Get("repository_id").(string),
)
},
NewResourceData: func() core.SchemaReader {
return &UserAccountResource{}
},
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter {
return &CreateUserAccountResponse{}
},
NewResourceData: func() core.SchemaReader { return &UserAccountResource{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &CreateUserAccountResponse{} },
},
ReadRepositoryUserAccountConfig,
),
Expand All @@ -369,9 +365,7 @@ func ResourceRepositoryUserAccount() *schema.Resource {
userAccountID,
)
},
NewResourceData: func() core.SchemaReader {
return &UserAccountResource{}
},
NewResourceData: func() core.SchemaReader { return &UserAccountResource{} },
},
ReadRepositoryUserAccountConfig,
),
Expand Down
12 changes: 3 additions & 9 deletions cyral/internal/serviceaccount/resource_cyral_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,8 @@ func ResourceServiceAccount() *schema.Resource {
c.ControlPlane,
)
},
NewResourceData: func() core.SchemaReader {
return &ServiceAccount{}
},
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter {
return &ServiceAccount{}
},
NewResourceData: func() core.SchemaReader { return &ServiceAccount{} },
NewResponseData: func(_ *schema.ResourceData) core.SchemaWriter { return &ServiceAccount{} },
},
ReadServiceAccountConfig,
),
Expand All @@ -74,9 +70,7 @@ func ResourceServiceAccount() *schema.Resource {
d.Id(),
)
},
NewResourceData: func() core.SchemaReader {
return &ServiceAccount{}
},
NewResourceData: func() core.SchemaReader { return &ServiceAccount{} },
},
ReadServiceAccountConfig,
),
Expand Down

0 comments on commit 0e900ad

Please sign in to comment.