From 21bc0451c8e6abe8c439e49778ea93074f8d3770 Mon Sep 17 00:00:00 2001 From: Deepak Gupta Date: Fri, 13 Dec 2024 16:11:16 -0500 Subject: [PATCH 1/8] ENG-14612: Implement cyral_policy_wizards resource --- cyral/internal/policyset/constants.go | 5 +- cyral/internal/policyset/datasource.go | 64 +++++++++++++++++++++-- cyral/internal/policyset/model.go | 59 +++++++++++++++++++-- cyral/internal/policyset/resource.go | 14 ++--- cyral/internal/policyset/schema_loader.go | 14 +++-- 5 files changed, 134 insertions(+), 22 deletions(-) diff --git a/cyral/internal/policyset/constants.go b/cyral/internal/policyset/constants.go index cceefe56..3603714a 100644 --- a/cyral/internal/policyset/constants.go +++ b/cyral/internal/policyset/constants.go @@ -1,6 +1,7 @@ package policyset const ( - resourceName = "cyral_policy_set" - dataSourceName = resourceName + policySetResourceName = "cyral_policy_set" + policySetDataSourceName = policySetResourceName + policyWizardsDataSourceName = "cyral_policy_wizards" ) diff --git a/cyral/internal/policyset/datasource.go b/cyral/internal/policyset/datasource.go index 844c5077..e1b24b81 100644 --- a/cyral/internal/policyset/datasource.go +++ b/cyral/internal/policyset/datasource.go @@ -7,16 +7,16 @@ import ( "github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype" ) -var dsContextHandler = core.ContextHandler{ - ResourceName: dataSourceName, +var policySetDataSourceContextHandler = core.ContextHandler{ + ResourceName: policySetDataSourceName, ResourceType: resourcetype.DataSource, Read: readPolicySet, } -func dataSourceSchema() *schema.Resource { +func policySetDataSourceSchema() *schema.Resource { return &schema.Resource{ Description: "This data source provides information about a policy set.", - ReadContext: dsContextHandler.ReadContext, + ReadContext: policySetDataSourceContextHandler.ReadContext, Schema: map[string]*schema.Schema{ "id": { Description: "Identifier for the policy set.", @@ -103,3 +103,59 @@ func dataSourceSchema() *schema.Resource { }, } } + +var policyWizardsDataSourceContextHandler = core.ContextHandler{ + ResourceName: policySetDataSourceName, + ResourceType: resourcetype.DataSource, + Read: readPolicyWizards, +} + +func policyWizardsDataSourceSchema() *schema.Resource { + return &schema.Resource{ + Description: "This data source provides information policy wizards", + ReadContext: policyWizardsDataSourceContextHandler.ReadContext, + Schema: map[string]*schema.Schema{ + "wizard_id": { + Description: "id of the policy wizard of interest.", + Type: schema.TypeString, + Optional: true, + }, + "wizards": { + Description: "Set of supported policy wizards.", + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Description: "Information about a policy wizard.", + Schema: map[string]*schema.Schema{ + "id": { + Description: "Identifier for the policy wizard, use as the value of wizard_id parameter in the policy set resource.", + Type: schema.TypeString, + Required: true, + }, + "name": { + Description: "Name of the policy wizard.", + Type: schema.TypeString, + Computed: true, + }, + "description": { + Description: "Description of the policy wizard.", + Type: schema.TypeString, + Computed: true, + }, + "tags": { + Description: "Tags associated with the policy wizard.", + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "parameter_schema": { + Description: "JSON schema for the policy wizard parameters.", + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} diff --git a/cyral/internal/policyset/model.go b/cyral/internal/policyset/model.go index fc1941f8..3e4d2bac 100644 --- a/cyral/internal/policyset/model.go +++ b/cyral/internal/policyset/model.go @@ -47,8 +47,8 @@ func scopeToMap(s *msg.Scope) []map[string]interface{} { } } -// updateSchema writes the policy set data to the schema -func updateSchema(ps *msg.PolicySet, d *schema.ResourceData) error { +// updatePolicySetSchema writes the policy set data to the schema +func updatePolicySetSchema(ps *msg.PolicySet, d *schema.ResourceData) error { if err := d.Set("id", ps.GetId()); err != nil { return fmt.Errorf("error setting 'id' field: %w", err) } @@ -141,7 +141,7 @@ func readPolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceDa if err != nil { return err } - return updateSchema(resp.GetPolicySet(), rd) + return updatePolicySetSchema(resp.GetPolicySet(), rd) } func updatePolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { @@ -155,7 +155,7 @@ func updatePolicySet(ctx context.Context, cl *client.Client, rd *schema.Resource if err != nil { return err } - return updateSchema(resp.GetPolicySet(), rd) + return updatePolicySetSchema(resp.GetPolicySet(), rd) } func deletePolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { @@ -166,3 +166,54 @@ func deletePolicySet(ctx context.Context, cl *client.Client, rd *schema.Resource _, err := grpcClient.DeletePolicySet(ctx, req) return err } + +func readPolicyWizards(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { + var wizardList []*msg.PolicyWizard + + wizId := rd.Get("wizard_id").(string) + grpcClient := methods.NewPolicyWizardServiceClient(cl.GRPCClient()) + if wizId != "" { + req := &msg.ReadPolicyWizardRequest{ + Id: wizId, + } + resp, err := grpcClient.ReadPolicyWizard(ctx, req) + if err != nil { + return err + } + wizardList = []*msg.PolicyWizard{resp.GetPolicyWizard()} + } else { + req := &msg.ListPolicyWizardsRequest{} + resp, err := grpcClient.ListPolicyWizards(ctx, req) + if err != nil { + return err + } + wizardList = resp.GetPolicyWizards() + } + updatePolicyWizardsSchema(wizardList, rd) + return nil +} + +func wizardToMap(wiz *msg.PolicyWizard) map[string]any { + return map[string]any{ + "id": wiz.GetId(), + "name": wiz.GetName(), + "description": wiz.GetDescription(), + "parameter_schema": wiz.GetParameterSchema(), + "tags": func() []any { + tags := make([]any, 0, len(wiz.GetTags())) + for _, t := range wiz.GetTags() { + tags = append(tags, t) + } + return tags + }(), + } +} + +func updatePolicyWizardsSchema(wizards []*msg.PolicyWizard, rd *schema.ResourceData) { + wizardList := make([]any, 0, len(wizards)) + for _, wiz := range wizards { + wizardList = append(wizardList, wizardToMap(wiz)) + } + rd.Set("wizards", wizardList) + rd.SetId("cyral-wizard-list") +} diff --git a/cyral/internal/policyset/resource.go b/cyral/internal/policyset/resource.go index 459d0a1c..b6afa0b1 100644 --- a/cyral/internal/policyset/resource.go +++ b/cyral/internal/policyset/resource.go @@ -9,8 +9,8 @@ import ( "github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype" ) -var resourceContextHandler = core.ContextHandler{ - ResourceName: resourceName, +var policySetResourceContextHandler = core.ContextHandler{ + ResourceName: policySetResourceName, ResourceType: resourcetype.Resource, Create: createPolicySet, Read: readPolicySet, @@ -18,13 +18,13 @@ var resourceContextHandler = core.ContextHandler{ Delete: deletePolicySet, } -func resourceSchema() *schema.Resource { +func policySetResourceSchema() *schema.Resource { return &schema.Resource{ Description: "This resource allows management of policy sets in the Cyral platform.", - CreateContext: resourceContextHandler.CreateContext, - ReadContext: resourceContextHandler.ReadContext, - UpdateContext: resourceContextHandler.UpdateContext, - DeleteContext: resourceContextHandler.DeleteContext, + CreateContext: policySetResourceContextHandler.CreateContext, + ReadContext: policySetResourceContextHandler.ReadContext, + UpdateContext: policySetResourceContextHandler.UpdateContext, + DeleteContext: policySetResourceContextHandler.DeleteContext, Importer: &schema.ResourceImporter{ StateContext: importPolicySetStateContext, }, diff --git a/cyral/internal/policyset/schema_loader.go b/cyral/internal/policyset/schema_loader.go index 229e9642..91264448 100644 --- a/cyral/internal/policyset/schema_loader.go +++ b/cyral/internal/policyset/schema_loader.go @@ -13,15 +13,19 @@ func (p *packageSchema) Schemas() []*core.SchemaDescriptor { return []*core.SchemaDescriptor{ { - Name: dataSourceName, + Name: policySetDataSourceName, Type: core.DataSourceSchemaType, - Schema: dataSourceSchema, + Schema: policySetDataSourceSchema, + }, + { + Name: policyWizardsDataSourceName, + Type: core.DataSourceSchemaType, + Schema: policyWizardsDataSourceSchema, }, - { - Name: resourceName, + Name: policySetResourceName, Type: core.ResourceSchemaType, - Schema: resourceSchema, + Schema: policySetResourceSchema, }, } } From 8884d65dfa846e264267d84961daa9de6acfd939 Mon Sep 17 00:00:00 2001 From: Deepak Gupta Date: Fri, 13 Dec 2024 17:43:46 -0500 Subject: [PATCH 2/8] add acceptance test --- cyral/internal/policy/policywizards_test.go | 72 +++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 cyral/internal/policy/policywizards_test.go diff --git a/cyral/internal/policy/policywizards_test.go b/cyral/internal/policy/policywizards_test.go new file mode 100644 index 00000000..b71f382f --- /dev/null +++ b/cyral/internal/policy/policywizards_test.go @@ -0,0 +1,72 @@ +package policyv2 + +import ( + "testing" + + "github.com/cyralinc/terraform-provider-cyral/cyral/provider" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccPolicyWizardsDataSource(t *testing.T) { + dsName := "data.cyral_policy_wizards.wizard_list" + resource.ParallelTest(t, resource.TestCase{ + ProviderFactories: provider.ProviderFactories, + Steps: []resource.TestStep{ + { + Config: ` +data "cyral_policy_wizards" "wizard_list" { +} +`, + Check: checkAllWizards(dsName), + }, + { + Config: ` +data "cyral_policy_wizards" "wizard_list" { + wizard_id = "data-firewall" +} +`, + Check: checkOneWizard(dsName, "data-firewall"), + }, + }, + }) +} + +// checkAllWizards ensures that a few well known wizard ids are present in the +// datasource state. It does not attempt to make very exhaustive checks because +// wizard names, descriptions (and even the wizard list) is subject to change. +func checkAllWizards(dsName string) resource.TestCheckFunc { + return resource.ComposeTestCheckFunc( + resource.TestCheckTypeSetElemNestedAttrs( + dsName, "wizards.*", + map[string]string{ + "id": "data-firewall", + }, + ), + resource.TestCheckTypeSetElemNestedAttrs( + dsName, "wizards.*", + map[string]string{ + "id": "data-masking", + }, + ), + resource.TestCheckTypeSetElemNestedAttrs( + dsName, "wizards.*", + map[string]string{ + "id": "user-segmentation", + }, + ), + ) +} + +// checkOneWizard ensures that the data source state contains only one wizard +// with the given id. +func checkOneWizard(dsName, id string) resource.TestCheckFunc { + return resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dsName, "wizards.#", "1"), + resource.TestCheckTypeSetElemNestedAttrs( + dsName, "wizards.*", + map[string]string{ + "id": id, + }, + ), + ) +} From 8b7940f9a169bd181f914baa3d9f64693261f802 Mon Sep 17 00:00:00 2001 From: Deepak Gupta Date: Fri, 13 Dec 2024 17:47:17 -0500 Subject: [PATCH 3/8] generate documentation --- docs/data-sources/policy_wizards.md | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 docs/data-sources/policy_wizards.md diff --git a/docs/data-sources/policy_wizards.md b/docs/data-sources/policy_wizards.md new file mode 100644 index 00000000..98fcbf32 --- /dev/null +++ b/docs/data-sources/policy_wizards.md @@ -0,0 +1,36 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "cyral_policy_wizards Data Source - terraform-provider-cyral" +subcategory: "" +description: |- + This data source provides information policy wizards +--- + +# cyral_policy_wizards (Data Source) + +This data source provides information policy wizards + + + +## Schema + +### Optional + +- `wizard_id` (String) id of the policy wizard of interest. + +### Read-Only + +- `id` (String) The ID of this resource. +- `wizards` (Set of Object) Set of supported policy wizards. (see [below for nested schema](#nestedatt--wizards)) + + + +### Nested Schema for `wizards` + +Read-Only: + +- `description` (String) +- `id` (String) +- `name` (String) +- `parameter_schema` (String) +- `tags` (List of String) From 533c32457451cf69fd5a11d2f8ba2b146cd2e2bb Mon Sep 17 00:00:00 2001 From: Deepak Gupta Date: Fri, 13 Dec 2024 18:02:10 -0500 Subject: [PATCH 4/8] fix not-found case --- cyral/internal/policy/policywizards_test.go | 8 ++++++++ cyral/internal/policyset/model.go | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cyral/internal/policy/policywizards_test.go b/cyral/internal/policy/policywizards_test.go index b71f382f..d84bed04 100644 --- a/cyral/internal/policy/policywizards_test.go +++ b/cyral/internal/policy/policywizards_test.go @@ -27,6 +27,14 @@ data "cyral_policy_wizards" "wizard_list" { `, Check: checkOneWizard(dsName, "data-firewall"), }, + { + Config: ` +data "cyral_policy_wizards" "wizard_list" { + wizard_id = "XXX" +} +`, + Check: resource.TestCheckResourceAttr(dsName, "wizards.#", "0"), + }, }, }) } diff --git a/cyral/internal/policyset/model.go b/cyral/internal/policyset/model.go index 3e4d2bac..ae0b7ff8 100644 --- a/cyral/internal/policyset/model.go +++ b/cyral/internal/policyset/model.go @@ -8,6 +8,8 @@ import ( methods "buf.build/gen/go/cyral/policy/grpc/go/policy/v1/policyv1grpc" msg "buf.build/gen/go/cyral/policy/protocolbuffers/go/policy/v1" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" "github.com/cyralinc/terraform-provider-cyral/cyral/client" "github.com/cyralinc/terraform-provider-cyral/cyral/utils" @@ -177,10 +179,12 @@ func readPolicyWizards(ctx context.Context, cl *client.Client, rd *schema.Resour Id: wizId, } resp, err := grpcClient.ReadPolicyWizard(ctx, req) - if err != nil { + if err != nil && status.Code(err) != codes.NotFound { return err } - wizardList = []*msg.PolicyWizard{resp.GetPolicyWizard()} + if status.Code(err) != codes.NotFound { + wizardList = []*msg.PolicyWizard{resp.GetPolicyWizard()} + } } else { req := &msg.ListPolicyWizardsRequest{} resp, err := grpcClient.ListPolicyWizards(ctx, req) From 2e8c25abac4b261c4575ef29711aa6a4196da970 Mon Sep 17 00:00:00 2001 From: Deepak Gupta Date: Fri, 13 Dec 2024 18:04:15 -0500 Subject: [PATCH 5/8] move test file --- cyral/internal/policy/{ => v2}/policywizards_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename cyral/internal/policy/{ => v2}/policywizards_test.go (98%) diff --git a/cyral/internal/policy/policywizards_test.go b/cyral/internal/policy/v2/policywizards_test.go similarity index 98% rename from cyral/internal/policy/policywizards_test.go rename to cyral/internal/policy/v2/policywizards_test.go index d84bed04..19e9d802 100644 --- a/cyral/internal/policy/policywizards_test.go +++ b/cyral/internal/policy/v2/policywizards_test.go @@ -1,4 +1,4 @@ -package policyv2 +package policyv2_test import ( "testing" From ca539a64c120019314df0b2f28b7a0ec7645a0fa Mon Sep 17 00:00:00 2001 From: Deepak Gupta Date: Sun, 15 Dec 2024 07:41:55 -0500 Subject: [PATCH 6/8] restructure packages --- cyral/internal/policy/set/constants.go | 6 ++ .../{policyset => policy/set}/datasource.go | 56 ---------------- .../{policyset => policy/set}/model.go | 55 ---------------- .../{policyset => policy/set}/resource.go | 0 .../set}/resource_test.go | 0 .../set}/schema_loader.go | 5 -- cyral/internal/policy/wizard/constants.go | 5 ++ cyral/internal/policy/wizard/datasource.go | 64 ++++++++++++++++++ .../datasource_test.go} | 2 +- cyral/internal/policy/wizard/model.go | 66 +++++++++++++++++++ cyral/internal/policy/wizard/schema_loader.go | 24 +++++++ cyral/internal/policyset/constants.go | 7 -- cyral/provider/provider.go | 2 - cyral/provider/schema_loader.go | 4 +- 14 files changed, 169 insertions(+), 127 deletions(-) create mode 100644 cyral/internal/policy/set/constants.go rename cyral/internal/{policyset => policy/set}/datasource.go (64%) rename cyral/internal/{policyset => policy/set}/model.go (77%) rename cyral/internal/{policyset => policy/set}/resource.go (100%) rename cyral/internal/{policyset => policy/set}/resource_test.go (100%) rename cyral/internal/{policyset => policy/set}/schema_loader.go (81%) create mode 100644 cyral/internal/policy/wizard/constants.go create mode 100644 cyral/internal/policy/wizard/datasource.go rename cyral/internal/policy/{v2/policywizards_test.go => wizard/datasource_test.go} (98%) create mode 100644 cyral/internal/policy/wizard/model.go create mode 100644 cyral/internal/policy/wizard/schema_loader.go delete mode 100644 cyral/internal/policyset/constants.go diff --git a/cyral/internal/policy/set/constants.go b/cyral/internal/policy/set/constants.go new file mode 100644 index 00000000..0b588850 --- /dev/null +++ b/cyral/internal/policy/set/constants.go @@ -0,0 +1,6 @@ +package policyset + +const ( + policySetResourceName = "cyral_policy_set" + policySetDataSourceName = policySetResourceName +) diff --git a/cyral/internal/policyset/datasource.go b/cyral/internal/policy/set/datasource.go similarity index 64% rename from cyral/internal/policyset/datasource.go rename to cyral/internal/policy/set/datasource.go index e1b24b81..c2a3575f 100644 --- a/cyral/internal/policyset/datasource.go +++ b/cyral/internal/policy/set/datasource.go @@ -103,59 +103,3 @@ func policySetDataSourceSchema() *schema.Resource { }, } } - -var policyWizardsDataSourceContextHandler = core.ContextHandler{ - ResourceName: policySetDataSourceName, - ResourceType: resourcetype.DataSource, - Read: readPolicyWizards, -} - -func policyWizardsDataSourceSchema() *schema.Resource { - return &schema.Resource{ - Description: "This data source provides information policy wizards", - ReadContext: policyWizardsDataSourceContextHandler.ReadContext, - Schema: map[string]*schema.Schema{ - "wizard_id": { - Description: "id of the policy wizard of interest.", - Type: schema.TypeString, - Optional: true, - }, - "wizards": { - Description: "Set of supported policy wizards.", - Type: schema.TypeSet, - Computed: true, - Elem: &schema.Resource{ - Description: "Information about a policy wizard.", - Schema: map[string]*schema.Schema{ - "id": { - Description: "Identifier for the policy wizard, use as the value of wizard_id parameter in the policy set resource.", - Type: schema.TypeString, - Required: true, - }, - "name": { - Description: "Name of the policy wizard.", - Type: schema.TypeString, - Computed: true, - }, - "description": { - Description: "Description of the policy wizard.", - Type: schema.TypeString, - Computed: true, - }, - "tags": { - Description: "Tags associated with the policy wizard.", - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "parameter_schema": { - Description: "JSON schema for the policy wizard parameters.", - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, - }, - } -} diff --git a/cyral/internal/policyset/model.go b/cyral/internal/policy/set/model.go similarity index 77% rename from cyral/internal/policyset/model.go rename to cyral/internal/policy/set/model.go index ae0b7ff8..37c3d251 100644 --- a/cyral/internal/policyset/model.go +++ b/cyral/internal/policy/set/model.go @@ -8,8 +8,6 @@ import ( methods "buf.build/gen/go/cyral/policy/grpc/go/policy/v1/policyv1grpc" msg "buf.build/gen/go/cyral/policy/protocolbuffers/go/policy/v1" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "github.com/cyralinc/terraform-provider-cyral/cyral/client" "github.com/cyralinc/terraform-provider-cyral/cyral/utils" @@ -168,56 +166,3 @@ func deletePolicySet(ctx context.Context, cl *client.Client, rd *schema.Resource _, err := grpcClient.DeletePolicySet(ctx, req) return err } - -func readPolicyWizards(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { - var wizardList []*msg.PolicyWizard - - wizId := rd.Get("wizard_id").(string) - grpcClient := methods.NewPolicyWizardServiceClient(cl.GRPCClient()) - if wizId != "" { - req := &msg.ReadPolicyWizardRequest{ - Id: wizId, - } - resp, err := grpcClient.ReadPolicyWizard(ctx, req) - if err != nil && status.Code(err) != codes.NotFound { - return err - } - if status.Code(err) != codes.NotFound { - wizardList = []*msg.PolicyWizard{resp.GetPolicyWizard()} - } - } else { - req := &msg.ListPolicyWizardsRequest{} - resp, err := grpcClient.ListPolicyWizards(ctx, req) - if err != nil { - return err - } - wizardList = resp.GetPolicyWizards() - } - updatePolicyWizardsSchema(wizardList, rd) - return nil -} - -func wizardToMap(wiz *msg.PolicyWizard) map[string]any { - return map[string]any{ - "id": wiz.GetId(), - "name": wiz.GetName(), - "description": wiz.GetDescription(), - "parameter_schema": wiz.GetParameterSchema(), - "tags": func() []any { - tags := make([]any, 0, len(wiz.GetTags())) - for _, t := range wiz.GetTags() { - tags = append(tags, t) - } - return tags - }(), - } -} - -func updatePolicyWizardsSchema(wizards []*msg.PolicyWizard, rd *schema.ResourceData) { - wizardList := make([]any, 0, len(wizards)) - for _, wiz := range wizards { - wizardList = append(wizardList, wizardToMap(wiz)) - } - rd.Set("wizards", wizardList) - rd.SetId("cyral-wizard-list") -} diff --git a/cyral/internal/policyset/resource.go b/cyral/internal/policy/set/resource.go similarity index 100% rename from cyral/internal/policyset/resource.go rename to cyral/internal/policy/set/resource.go diff --git a/cyral/internal/policyset/resource_test.go b/cyral/internal/policy/set/resource_test.go similarity index 100% rename from cyral/internal/policyset/resource_test.go rename to cyral/internal/policy/set/resource_test.go diff --git a/cyral/internal/policyset/schema_loader.go b/cyral/internal/policy/set/schema_loader.go similarity index 81% rename from cyral/internal/policyset/schema_loader.go rename to cyral/internal/policy/set/schema_loader.go index 91264448..b39644d3 100644 --- a/cyral/internal/policyset/schema_loader.go +++ b/cyral/internal/policy/set/schema_loader.go @@ -17,11 +17,6 @@ func (p *packageSchema) Schemas() []*core.SchemaDescriptor { Type: core.DataSourceSchemaType, Schema: policySetDataSourceSchema, }, - { - Name: policyWizardsDataSourceName, - Type: core.DataSourceSchemaType, - Schema: policyWizardsDataSourceSchema, - }, { Name: policySetResourceName, Type: core.ResourceSchemaType, diff --git a/cyral/internal/policy/wizard/constants.go b/cyral/internal/policy/wizard/constants.go new file mode 100644 index 00000000..ed6d062a --- /dev/null +++ b/cyral/internal/policy/wizard/constants.go @@ -0,0 +1,5 @@ +package wizard + +const ( + policyWizardsDataSourceName = "cyral_policy_wizards" +) diff --git a/cyral/internal/policy/wizard/datasource.go b/cyral/internal/policy/wizard/datasource.go new file mode 100644 index 00000000..631f8560 --- /dev/null +++ b/cyral/internal/policy/wizard/datasource.go @@ -0,0 +1,64 @@ +package wizard + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/cyralinc/terraform-provider-cyral/cyral/core" + "github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype" +) + +var policyWizardsDataSourceContextHandler = core.ContextHandler{ + ResourceName: policyWizardsDataSourceName, + ResourceType: resourcetype.DataSource, + Read: readPolicyWizards, +} + +func policyWizardsDataSourceSchema() *schema.Resource { + return &schema.Resource{ + Description: "This data source provides information policy wizards", + ReadContext: policyWizardsDataSourceContextHandler.ReadContext, + Schema: map[string]*schema.Schema{ + "wizard_id": { + Description: "id of the policy wizard of interest.", + Type: schema.TypeString, + Optional: true, + }, + "wizards": { + Description: "Set of supported policy wizards.", + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Description: "Information about a policy wizard.", + Schema: map[string]*schema.Schema{ + "id": { + Description: "Identifier for the policy wizard, use as the value of wizard_id parameter in the policy set resource.", + Type: schema.TypeString, + Required: true, + }, + "name": { + Description: "Name of the policy wizard.", + Type: schema.TypeString, + Computed: true, + }, + "description": { + Description: "Description of the policy wizard.", + Type: schema.TypeString, + Computed: true, + }, + "tags": { + Description: "Tags associated with the policy wizard.", + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "parameter_schema": { + Description: "JSON schema for the policy wizard parameters.", + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} diff --git a/cyral/internal/policy/v2/policywizards_test.go b/cyral/internal/policy/wizard/datasource_test.go similarity index 98% rename from cyral/internal/policy/v2/policywizards_test.go rename to cyral/internal/policy/wizard/datasource_test.go index 19e9d802..89c1cdba 100644 --- a/cyral/internal/policy/v2/policywizards_test.go +++ b/cyral/internal/policy/wizard/datasource_test.go @@ -1,4 +1,4 @@ -package policyv2_test +package wizard_test import ( "testing" diff --git a/cyral/internal/policy/wizard/model.go b/cyral/internal/policy/wizard/model.go new file mode 100644 index 00000000..8eaf3b96 --- /dev/null +++ b/cyral/internal/policy/wizard/model.go @@ -0,0 +1,66 @@ +package wizard + +import ( + "context" + + methods "buf.build/gen/go/cyral/policy/grpc/go/policy/v1/policyv1grpc" + msg "buf.build/gen/go/cyral/policy/protocolbuffers/go/policy/v1" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + "github.com/cyralinc/terraform-provider-cyral/cyral/client" +) + +func readPolicyWizards(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { + var wizardList []*msg.PolicyWizard + + wizId := rd.Get("wizard_id").(string) + grpcClient := methods.NewPolicyWizardServiceClient(cl.GRPCClient()) + if wizId != "" { + req := &msg.ReadPolicyWizardRequest{ + Id: wizId, + } + resp, err := grpcClient.ReadPolicyWizard(ctx, req) + if err != nil && status.Code(err) != codes.NotFound { + return err + } + if status.Code(err) != codes.NotFound { + wizardList = []*msg.PolicyWizard{resp.GetPolicyWizard()} + } + } else { + req := &msg.ListPolicyWizardsRequest{} + resp, err := grpcClient.ListPolicyWizards(ctx, req) + if err != nil { + return err + } + wizardList = resp.GetPolicyWizards() + } + updatePolicyWizardsSchema(wizardList, rd) + return nil +} + +func wizardToMap(wiz *msg.PolicyWizard) map[string]any { + return map[string]any{ + "id": wiz.GetId(), + "name": wiz.GetName(), + "description": wiz.GetDescription(), + "parameter_schema": wiz.GetParameterSchema(), + "tags": func() []any { + tags := make([]any, 0, len(wiz.GetTags())) + for _, t := range wiz.GetTags() { + tags = append(tags, t) + } + return tags + }(), + } +} + +func updatePolicyWizardsSchema(wizards []*msg.PolicyWizard, rd *schema.ResourceData) { + wizardList := make([]any, 0, len(wizards)) + for _, wiz := range wizards { + wizardList = append(wizardList, wizardToMap(wiz)) + } + rd.Set("wizards", wizardList) + rd.SetId("cyral-wizard-list") +} diff --git a/cyral/internal/policy/wizard/schema_loader.go b/cyral/internal/policy/wizard/schema_loader.go new file mode 100644 index 00000000..558a9e64 --- /dev/null +++ b/cyral/internal/policy/wizard/schema_loader.go @@ -0,0 +1,24 @@ +package wizard + +import "github.com/cyralinc/terraform-provider-cyral/cyral/core" + +type packageSchema struct { +} + +func (p *packageSchema) Name() string { + return "policyset" +} + +func (p *packageSchema) Schemas() []*core.SchemaDescriptor { + return []*core.SchemaDescriptor{ + { + Name: policyWizardsDataSourceName, + Type: core.DataSourceSchemaType, + Schema: policyWizardsDataSourceSchema, + }, + } +} + +func PackageSchema() core.PackageSchema { + return &packageSchema{} +} diff --git a/cyral/internal/policyset/constants.go b/cyral/internal/policyset/constants.go deleted file mode 100644 index 3603714a..00000000 --- a/cyral/internal/policyset/constants.go +++ /dev/null @@ -1,7 +0,0 @@ -package policyset - -const ( - policySetResourceName = "cyral_policy_set" - policySetDataSourceName = policySetResourceName - policyWizardsDataSourceName = "cyral_policy_wizards" -) diff --git a/cyral/provider/provider.go b/cyral/provider/provider.go index 7d174974..41708dfe 100644 --- a/cyral/provider/provider.go +++ b/cyral/provider/provider.go @@ -180,8 +180,6 @@ func getCredentials(d *schema.ResourceData) (string, string, diag.Diagnostics) { return clientID, clientSecret, diags } -var provider = Provider() - var ProviderFactories = map[string]func() (*schema.Provider, error){ "cyral": func() (*schema.Provider, error) { return Provider(), nil diff --git a/cyral/provider/schema_loader.go b/cyral/provider/schema_loader.go index 9c01d263..49444ed2 100644 --- a/cyral/provider/schema_loader.go +++ b/cyral/provider/schema_loader.go @@ -15,8 +15,9 @@ import ( integration_slack "github.com/cyralinc/terraform-provider-cyral/cyral/internal/integration/slack" integration_teams "github.com/cyralinc/terraform-provider-cyral/cyral/internal/integration/teams" "github.com/cyralinc/terraform-provider-cyral/cyral/internal/permission" + policyset "github.com/cyralinc/terraform-provider-cyral/cyral/internal/policy/set" policyv2 "github.com/cyralinc/terraform-provider-cyral/cyral/internal/policy/v2" - "github.com/cyralinc/terraform-provider-cyral/cyral/internal/policyset" + policywizard "github.com/cyralinc/terraform-provider-cyral/cyral/internal/policy/wizard" "github.com/cyralinc/terraform-provider-cyral/cyral/internal/regopolicy" "github.com/cyralinc/terraform-provider-cyral/cyral/internal/repository" repository_accessgateway "github.com/cyralinc/terraform-provider-cyral/cyral/internal/repository/accessgateway" @@ -58,6 +59,7 @@ func packagesSchemas() []core.PackageSchema { permission.PackageSchema(), policyv2.PackageSchema(), policyset.PackageSchema(), + policywizard.PackageSchema(), regopolicy.PackageSchema(), repository.PackageSchema(), repository_accessgateway.PackageSchema(), From 791d7d8ac22ddf53f05a492c3d9bca04b0be07f7 Mon Sep 17 00:00:00 2001 From: Deepak Gupta Date: Tue, 17 Dec 2024 09:48:56 -0500 Subject: [PATCH 7/8] use standard variable naming convention --- cyral/internal/policy/set/constants.go | 4 ++-- cyral/internal/policy/set/datasource.go | 8 ++++---- cyral/internal/policy/set/model.go | 8 ++++---- cyral/internal/policy/set/resource.go | 14 +++++++------- cyral/internal/policy/set/schema_loader.go | 8 ++++---- cyral/internal/policy/wizard/constants.go | 2 +- cyral/internal/policy/wizard/datasource.go | 8 ++++---- cyral/internal/policy/wizard/model.go | 4 ++-- cyral/internal/policy/wizard/schema_loader.go | 4 ++-- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/cyral/internal/policy/set/constants.go b/cyral/internal/policy/set/constants.go index 0b588850..cceefe56 100644 --- a/cyral/internal/policy/set/constants.go +++ b/cyral/internal/policy/set/constants.go @@ -1,6 +1,6 @@ package policyset const ( - policySetResourceName = "cyral_policy_set" - policySetDataSourceName = policySetResourceName + resourceName = "cyral_policy_set" + dataSourceName = resourceName ) diff --git a/cyral/internal/policy/set/datasource.go b/cyral/internal/policy/set/datasource.go index c2a3575f..844c5077 100644 --- a/cyral/internal/policy/set/datasource.go +++ b/cyral/internal/policy/set/datasource.go @@ -7,16 +7,16 @@ import ( "github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype" ) -var policySetDataSourceContextHandler = core.ContextHandler{ - ResourceName: policySetDataSourceName, +var dsContextHandler = core.ContextHandler{ + ResourceName: dataSourceName, ResourceType: resourcetype.DataSource, Read: readPolicySet, } -func policySetDataSourceSchema() *schema.Resource { +func dataSourceSchema() *schema.Resource { return &schema.Resource{ Description: "This data source provides information about a policy set.", - ReadContext: policySetDataSourceContextHandler.ReadContext, + ReadContext: dsContextHandler.ReadContext, Schema: map[string]*schema.Schema{ "id": { Description: "Identifier for the policy set.", diff --git a/cyral/internal/policy/set/model.go b/cyral/internal/policy/set/model.go index 37c3d251..fc1941f8 100644 --- a/cyral/internal/policy/set/model.go +++ b/cyral/internal/policy/set/model.go @@ -47,8 +47,8 @@ func scopeToMap(s *msg.Scope) []map[string]interface{} { } } -// updatePolicySetSchema writes the policy set data to the schema -func updatePolicySetSchema(ps *msg.PolicySet, d *schema.ResourceData) error { +// updateSchema writes the policy set data to the schema +func updateSchema(ps *msg.PolicySet, d *schema.ResourceData) error { if err := d.Set("id", ps.GetId()); err != nil { return fmt.Errorf("error setting 'id' field: %w", err) } @@ -141,7 +141,7 @@ func readPolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceDa if err != nil { return err } - return updatePolicySetSchema(resp.GetPolicySet(), rd) + return updateSchema(resp.GetPolicySet(), rd) } func updatePolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { @@ -155,7 +155,7 @@ func updatePolicySet(ctx context.Context, cl *client.Client, rd *schema.Resource if err != nil { return err } - return updatePolicySetSchema(resp.GetPolicySet(), rd) + return updateSchema(resp.GetPolicySet(), rd) } func deletePolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { diff --git a/cyral/internal/policy/set/resource.go b/cyral/internal/policy/set/resource.go index b6afa0b1..459d0a1c 100644 --- a/cyral/internal/policy/set/resource.go +++ b/cyral/internal/policy/set/resource.go @@ -9,8 +9,8 @@ import ( "github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype" ) -var policySetResourceContextHandler = core.ContextHandler{ - ResourceName: policySetResourceName, +var resourceContextHandler = core.ContextHandler{ + ResourceName: resourceName, ResourceType: resourcetype.Resource, Create: createPolicySet, Read: readPolicySet, @@ -18,13 +18,13 @@ var policySetResourceContextHandler = core.ContextHandler{ Delete: deletePolicySet, } -func policySetResourceSchema() *schema.Resource { +func resourceSchema() *schema.Resource { return &schema.Resource{ Description: "This resource allows management of policy sets in the Cyral platform.", - CreateContext: policySetResourceContextHandler.CreateContext, - ReadContext: policySetResourceContextHandler.ReadContext, - UpdateContext: policySetResourceContextHandler.UpdateContext, - DeleteContext: policySetResourceContextHandler.DeleteContext, + CreateContext: resourceContextHandler.CreateContext, + ReadContext: resourceContextHandler.ReadContext, + UpdateContext: resourceContextHandler.UpdateContext, + DeleteContext: resourceContextHandler.DeleteContext, Importer: &schema.ResourceImporter{ StateContext: importPolicySetStateContext, }, diff --git a/cyral/internal/policy/set/schema_loader.go b/cyral/internal/policy/set/schema_loader.go index b39644d3..37d4a9f3 100644 --- a/cyral/internal/policy/set/schema_loader.go +++ b/cyral/internal/policy/set/schema_loader.go @@ -13,14 +13,14 @@ func (p *packageSchema) Schemas() []*core.SchemaDescriptor { return []*core.SchemaDescriptor{ { - Name: policySetDataSourceName, + Name: dataSourceName, Type: core.DataSourceSchemaType, - Schema: policySetDataSourceSchema, + Schema: dataSourceSchema, }, { - Name: policySetResourceName, + Name: resourceName, Type: core.ResourceSchemaType, - Schema: policySetResourceSchema, + Schema: resourceSchema, }, } } diff --git a/cyral/internal/policy/wizard/constants.go b/cyral/internal/policy/wizard/constants.go index ed6d062a..b8f73ed6 100644 --- a/cyral/internal/policy/wizard/constants.go +++ b/cyral/internal/policy/wizard/constants.go @@ -1,5 +1,5 @@ package wizard const ( - policyWizardsDataSourceName = "cyral_policy_wizards" + dataSourceName = "cyral_policy_wizards" ) diff --git a/cyral/internal/policy/wizard/datasource.go b/cyral/internal/policy/wizard/datasource.go index 631f8560..993c7f07 100644 --- a/cyral/internal/policy/wizard/datasource.go +++ b/cyral/internal/policy/wizard/datasource.go @@ -7,16 +7,16 @@ import ( "github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype" ) -var policyWizardsDataSourceContextHandler = core.ContextHandler{ - ResourceName: policyWizardsDataSourceName, +var dsContextHandler = core.ContextHandler{ + ResourceName: dataSourceName, ResourceType: resourcetype.DataSource, Read: readPolicyWizards, } -func policyWizardsDataSourceSchema() *schema.Resource { +func dataSourceSchema() *schema.Resource { return &schema.Resource{ Description: "This data source provides information policy wizards", - ReadContext: policyWizardsDataSourceContextHandler.ReadContext, + ReadContext: dsContextHandler.ReadContext, Schema: map[string]*schema.Schema{ "wizard_id": { Description: "id of the policy wizard of interest.", diff --git a/cyral/internal/policy/wizard/model.go b/cyral/internal/policy/wizard/model.go index 8eaf3b96..d7eebc0d 100644 --- a/cyral/internal/policy/wizard/model.go +++ b/cyral/internal/policy/wizard/model.go @@ -36,7 +36,7 @@ func readPolicyWizards(ctx context.Context, cl *client.Client, rd *schema.Resour } wizardList = resp.GetPolicyWizards() } - updatePolicyWizardsSchema(wizardList, rd) + updateSchema(wizardList, rd) return nil } @@ -56,7 +56,7 @@ func wizardToMap(wiz *msg.PolicyWizard) map[string]any { } } -func updatePolicyWizardsSchema(wizards []*msg.PolicyWizard, rd *schema.ResourceData) { +func updateSchema(wizards []*msg.PolicyWizard, rd *schema.ResourceData) { wizardList := make([]any, 0, len(wizards)) for _, wiz := range wizards { wizardList = append(wizardList, wizardToMap(wiz)) diff --git a/cyral/internal/policy/wizard/schema_loader.go b/cyral/internal/policy/wizard/schema_loader.go index 558a9e64..25c77562 100644 --- a/cyral/internal/policy/wizard/schema_loader.go +++ b/cyral/internal/policy/wizard/schema_loader.go @@ -12,9 +12,9 @@ func (p *packageSchema) Name() string { func (p *packageSchema) Schemas() []*core.SchemaDescriptor { return []*core.SchemaDescriptor{ { - Name: policyWizardsDataSourceName, + Name: dataSourceName, Type: core.DataSourceSchemaType, - Schema: policyWizardsDataSourceSchema, + Schema: dataSourceSchema, }, } } From 42f95ef0b1b8a90d27d92bf0ed9f4d71919e7d43 Mon Sep 17 00:00:00 2001 From: Wilson de Carvalho <796900+wcmjunior@users.noreply.github.com> Date: Tue, 17 Dec 2024 22:29:09 -0800 Subject: [PATCH 8/8] Rename package policyv2 to policy --- cyral/internal/policy/{v2 => }/constants.go | 2 +- cyral/internal/policy/{v2 => }/datasource.go | 2 +- cyral/internal/policy/{v2 => }/model.go | 2 +- cyral/internal/policy/{v2 => }/resource.go | 2 +- cyral/internal/policy/{v2 => }/resource_test.go | 2 +- cyral/internal/policy/{v2 => }/schema_loader.go | 2 +- cyral/provider/schema_loader.go | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) rename cyral/internal/policy/{v2 => }/constants.go (82%) rename cyral/internal/policy/{v2 => }/datasource.go (99%) rename cyral/internal/policy/{v2 => }/model.go (99%) rename cyral/internal/policy/{v2 => }/resource.go (99%) rename cyral/internal/policy/{v2 => }/resource_test.go (99%) rename cyral/internal/policy/{v2 => }/schema_loader.go (96%) diff --git a/cyral/internal/policy/v2/constants.go b/cyral/internal/policy/constants.go similarity index 82% rename from cyral/internal/policy/v2/constants.go rename to cyral/internal/policy/constants.go index 761313fe..3368a409 100644 --- a/cyral/internal/policy/v2/constants.go +++ b/cyral/internal/policy/constants.go @@ -1,4 +1,4 @@ -package policyv2 +package policy const ( resourceName = "cyral_policy_v2" diff --git a/cyral/internal/policy/v2/datasource.go b/cyral/internal/policy/datasource.go similarity index 99% rename from cyral/internal/policy/v2/datasource.go rename to cyral/internal/policy/datasource.go index 604bc870..f4e186d3 100644 --- a/cyral/internal/policy/v2/datasource.go +++ b/cyral/internal/policy/datasource.go @@ -1,4 +1,4 @@ -package policyv2 +package policy import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" diff --git a/cyral/internal/policy/v2/model.go b/cyral/internal/policy/model.go similarity index 99% rename from cyral/internal/policy/v2/model.go rename to cyral/internal/policy/model.go index 3ad70f19..4e563d3c 100644 --- a/cyral/internal/policy/v2/model.go +++ b/cyral/internal/policy/model.go @@ -1,4 +1,4 @@ -package policyv2 +package policy import ( "context" diff --git a/cyral/internal/policy/v2/resource.go b/cyral/internal/policy/resource.go similarity index 99% rename from cyral/internal/policy/v2/resource.go rename to cyral/internal/policy/resource.go index 58f9893b..9e9ba135 100644 --- a/cyral/internal/policy/v2/resource.go +++ b/cyral/internal/policy/resource.go @@ -1,4 +1,4 @@ -package policyv2 +package policy import ( "context" diff --git a/cyral/internal/policy/v2/resource_test.go b/cyral/internal/policy/resource_test.go similarity index 99% rename from cyral/internal/policy/v2/resource_test.go rename to cyral/internal/policy/resource_test.go index 9c002f0b..c80f30a6 100644 --- a/cyral/internal/policy/v2/resource_test.go +++ b/cyral/internal/policy/resource_test.go @@ -1,4 +1,4 @@ -package policyv2_test +package policy_test import ( "fmt" diff --git a/cyral/internal/policy/v2/schema_loader.go b/cyral/internal/policy/schema_loader.go similarity index 96% rename from cyral/internal/policy/v2/schema_loader.go rename to cyral/internal/policy/schema_loader.go index e4aecccd..08797044 100644 --- a/cyral/internal/policy/v2/schema_loader.go +++ b/cyral/internal/policy/schema_loader.go @@ -1,4 +1,4 @@ -package policyv2 +package policy import "github.com/cyralinc/terraform-provider-cyral/cyral/core" diff --git a/cyral/provider/schema_loader.go b/cyral/provider/schema_loader.go index 49444ed2..3e86fa42 100644 --- a/cyral/provider/schema_loader.go +++ b/cyral/provider/schema_loader.go @@ -15,8 +15,8 @@ import ( integration_slack "github.com/cyralinc/terraform-provider-cyral/cyral/internal/integration/slack" integration_teams "github.com/cyralinc/terraform-provider-cyral/cyral/internal/integration/teams" "github.com/cyralinc/terraform-provider-cyral/cyral/internal/permission" + "github.com/cyralinc/terraform-provider-cyral/cyral/internal/policy" policyset "github.com/cyralinc/terraform-provider-cyral/cyral/internal/policy/set" - policyv2 "github.com/cyralinc/terraform-provider-cyral/cyral/internal/policy/v2" policywizard "github.com/cyralinc/terraform-provider-cyral/cyral/internal/policy/wizard" "github.com/cyralinc/terraform-provider-cyral/cyral/internal/regopolicy" "github.com/cyralinc/terraform-provider-cyral/cyral/internal/repository" @@ -57,7 +57,7 @@ func packagesSchemas() []core.PackageSchema { integration_slack.PackageSchema(), integration_teams.PackageSchema(), permission.PackageSchema(), - policyv2.PackageSchema(), + policy.PackageSchema(), policyset.PackageSchema(), policywizard.PackageSchema(), regopolicy.PackageSchema(),