Skip to content

Commit

Permalink
Added scope and various test updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
gengdahlCyral committed Jun 11, 2024
1 parent 3d86dd9 commit c8f08ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
26 changes: 15 additions & 11 deletions cyral/internal/policyv2/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func dataSourceSchema() *schema.Resource {
Required: true,
Description: "The unique ID for the policy. This field is automatically set.",
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "The type of the policy.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -51,12 +56,16 @@ func dataSourceSchema() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},
"scope": {
Type: schema.TypeMap,
Computed: true,
Description: "Defines the applicability of the policy (e.g., which repositories).",
Elem: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"repo_ids": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
},
},
},
"valid_from": {
Expand Down Expand Up @@ -95,11 +104,6 @@ func dataSourceSchema() *schema.Resource {
Computed: true,
Description: "Indicates if the policy is enforced.",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the policy.",
},
},
}
}
16 changes: 16 additions & 0 deletions cyral/internal/policyv2/resource.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package policyv2

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/cyralinc/terraform-provider-cyral/cyral/client"
"github.com/cyralinc/terraform-provider-cyral/cyral/core"
"github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype"
"github.com/cyralinc/terraform-provider-cyral/cyral/utils"
)

var resourceContextHandler = core.DefaultContextHandler{
Expand All @@ -34,6 +36,9 @@ func resourceSchema() *schema.Resource {
ReadContext: resourceContextHandler.ReadContext(),
UpdateContext: resourceContextHandler.UpdateContext(),
DeleteContext: resourceContextHandler.DeleteContext(),
Importer: &schema.ResourceImporter{
StateContext: importPolicyV2StateContext,
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -128,3 +133,14 @@ func resourceSchema() *schema.Resource {
},
}
}
func importPolicyV2StateContext(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
ids, err := utils.UnMarshalComposedID(d.Id(), "/", 2)
if err != nil {
return nil, err
}
policyType := ids[0]
policyID := ids[1]
d.Set("type", policyType)
d.SetId(policyID)
return []*schema.ResourceData{d}, nil
}

0 comments on commit c8f08ec

Please sign in to comment.