-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENG-14612: Implement cyral_policy_wizards resource #593
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21bc045
ENG-14612: Implement cyral_policy_wizards resource
yoursnerdly 8884d65
add acceptance test
yoursnerdly 8b7940f
generate documentation
yoursnerdly 533c324
fix not-found case
yoursnerdly 2e8c25a
move test file
yoursnerdly ca539a6
restructure packages
yoursnerdly 791d7d8
use standard variable naming convention
yoursnerdly 42f95ef
Rename package policyv2 to policy
wcmjunior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
cyral/internal/policy/v2/constants.go → cyral/internal/policy/constants.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package policyv2 | ||
package policy | ||
|
||
const ( | ||
resourceName = "cyral_policy_v2" | ||
|
2 changes: 1 addition & 1 deletion
2
cyral/internal/policy/v2/datasource.go → cyral/internal/policy/datasource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package policyv2 | ||
package policy | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
2 changes: 1 addition & 1 deletion
2
cyral/internal/policy/v2/model.go → cyral/internal/policy/model.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package policyv2 | ||
package policy | ||
|
||
import ( | ||
"context" | ||
|
2 changes: 1 addition & 1 deletion
2
cyral/internal/policy/v2/resource.go → cyral/internal/policy/resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package policyv2 | ||
package policy | ||
|
||
import ( | ||
"context" | ||
|
2 changes: 1 addition & 1 deletion
2
cyral/internal/policy/v2/resource_test.go → cyral/internal/policy/resource_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package policyv2_test | ||
package policy_test | ||
|
||
import ( | ||
"fmt" | ||
|
2 changes: 1 addition & 1 deletion
2
cyral/internal/policy/v2/schema_loader.go → cyral/internal/policy/schema_loader.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package policyv2 | ||
package policy | ||
|
||
import "github.com/cyralinc/terraform-provider-cyral/cyral/core" | ||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package wizard | ||
|
||
const ( | ||
dataSourceName = "cyral_policy_wizards" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 dsContextHandler = core.ContextHandler{ | ||
ResourceName: dataSourceName, | ||
ResourceType: resourcetype.DataSource, | ||
Read: readPolicyWizards, | ||
} | ||
|
||
func dataSourceSchema() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "This data source provides information policy wizards", | ||
ReadContext: dsContextHandler.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, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package wizard_test | ||
|
||
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"), | ||
}, | ||
{ | ||
Config: ` | ||
data "cyral_policy_wizards" "wizard_list" { | ||
wizard_id = "XXX" | ||
} | ||
`, | ||
Check: resource.TestCheckResourceAttr(dsName, "wizards.#", "0"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// 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, | ||
}, | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
updateSchema(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 updateSchema(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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: dataSourceName, | ||
Type: core.DataSourceSchemaType, | ||
Schema: dataSourceSchema, | ||
}, | ||
} | ||
} | ||
|
||
func PackageSchema() core.PackageSchema { | ||
return &packageSchema{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 generated by tfplugindocs --> | ||
|
||
## 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)) | ||
|
||
<a id="nestedatt--wizards"></a> | ||
|
||
### Nested Schema for `wizards` | ||
|
||
Read-Only: | ||
|
||
- `description` (String) | ||
- `id` (String) | ||
- `name` (String) | ||
- `parameter_schema` (String) | ||
- `tags` (List of String) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unused private variable that causes a linter warning.