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(),