Skip to content
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-10802: Support regopolicy instance API (OOTB policies) #345

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions cyral/data_source_cyral_regopolicy_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package cyral

import (
"fmt"
"net/http"

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

"github.com/cyralinc/terraform-provider-cyral/client"
)

type GetRegopolicyInstancesResponse struct {
Instances []*PolicyInstance `json:"instances"`
}

func (resp *GetRegopolicyInstancesResponse) WriteToSchema(d *schema.ResourceData) error {
if err := writeRegopolicyInstancesToDataSourceSchema(resp.Instances, d); err != nil {
return err
}
// creating a new id for data source
d.SetId(uuid.New().String())
return nil
}

func writeRegopolicyInstancesToDataSourceSchema(instances []*PolicyInstance, d *schema.ResourceData) error {
var instancesList []interface{}
for _, instance := range instances {
instancesList = append(instancesList, map[string]interface{}{
"name": instance.Name,
"description": instance.Description,
"template_id": instance.TemplateId,
"tags": instance.TagsAsInterface(),
})
}
if err := d.Set("regopolicy_instance_list", instancesList); err != nil {
return err
}
return nil
}

func dataSourceRegopolicyInstanceReadConfig() ResourceOperationConfig {
return ResourceOperationConfig{
Name: "DatalabelDataSourceRead",
HttpMethod: http.MethodGet,
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
nameFilter := d.Get("name").(string)
typeFilter := d.Get("type").(string)
var pathParams string
if nameFilter != "" {
pathParams = fmt.Sprintf("/%s", nameFilter)
}
queryParams := urlQuery(map[string]string{
"type": typeFilter,
})

return fmt.Sprintf("https://%s/v1/datalabels%s%s", c.ControlPlane, pathParams, queryParams)
},
NewResponseData: func(d *schema.ResourceData) ResponseData {
nameFilter := d.Get("name").(string)
if nameFilter == "" {
return &GetDataLabelsResponse{}
} else {
return &GetDataLabelResponse{}
}
},
}
}

func dataSourceRegopolicyInstance() *schema.Resource {
return &schema.Resource{
Description: "Retrieve and filter policy instances. See also resource [`cyral_regopolicy`](../resources/regopolicy_instance.md).",
ReadContext: ReadResource(dataSourceDatalabelReadConfig()),
Schema: map[string]*schema.Schema{
"regopolicy_instance_list": {
Description: "List of existing regopolicy instances satisfying given filter criteria.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

"name": {
Description: "Name of the policy instance.",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "Description for the policy instance.",
Type: schema.TypeString,
Required: true,
},
"template_id": {
Description: "Template Id on which the instance was based",
Type: schema.TypeString,
Required: true,
},
"tags": {
Description: "Tags used to categorize policy instance.",
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
109 changes: 109 additions & 0 deletions cyral/model_regopolicy_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package cyral

import (
"google.golang.org/protobuf/types/known/timestamppb"
)

type Scope struct {
RepoIds []string `json:"repoIds,omitempty"`
}

type ChangeInfo_ActorType int32

const (
ChangeInfo_USER ChangeInfo_ActorType = 0
ChangeInfo_API_CLIENT ChangeInfo_ActorType = 1
)

type Category int32

const (
Category_UNKNOWN Category = 0
Category_SECURITY Category = 1
Category_GRANT Category = 2
Category_USER_DEFINED Category = 3
)
ptcar2009 marked this conversation as resolved.
Show resolved Hide resolved

type ChangeInfo struct {
Actor string `json:"actor,omitempty"`
ActorType ChangeInfo_ActorType `json:"actorType,omitempty"`
Timestamp *timestamppb.Timestamp `json:"timestamp,omitempty"`
}

type Key struct {
Id string `json:"id,omitempty"`
Category Category `json:"category,omitempty"`
}

func (pi *PolicyInstance) TagsAsInterface() []interface{} {
var tagIfaces []interface{}
for _, tag := range pi.Tags {
tagIfaces = append(tagIfaces, tag)
}
return tagIfaces
}

type PolicyInstance struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
TemplateId string `json:"templateId,omitempty"`
Parameters string `json:"parameters,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Scope *Scope `json:"scope,omitempty"`
Tags []string `json:"tags,omitempty"`
LastUpdated *ChangeInfo `json:"lastUpdated,omitempty"`
Created *ChangeInfo `json:"created,omitempty"`
}

// used for 'data' in requests
type PolicyInstanceDataRequest struct {
Instance *PolicyInstance `json:"policyInstance,omitempty"`
Duration string `json:"duration,omitempty"`
}

type ListPolicyInstancePartial struct {
Key Key `json:"key,omitempty"`
Name string `json:"name,omitempty"`
TemplateId string `json:"templateId,omitempty"`
}

type InsertPolicyInstanceRequest struct {
Category Category `json:"category,omitempty"`
Data PolicyInstanceDataRequest `json:"data,omitempty"`
}

type UpdatePolicyInstanceRequest struct {
Key Key `json:"key,omitempty"`
Data PolicyInstanceDataRequest `json:"data,omitempty"`
}

type DeletePolicyInstanceRequest struct {
Key Key `json:"key,omitempty"`
}

type ReadPolicyInstanceRequest struct {
Key Key `json:"key,omitempty"`
}

type ReadPolicyInstancesRequest struct {
Category Category `json:"category,omitempty"`
}

type InsertPolicyInstanceResponse struct {
Key Key `json:"key,omitempty"`
}

type UpdatePolicyInstanceResponse struct {
}

type DeletePolicyInstanceResponse struct {
instance PolicyInstance `json:"instance,omitempty"`
}

type ReadPolicyInstanceResponse struct {
instance PolicyInstance `json:"instance,omitempty"`
}

type ReadPolicyInstancesResponse struct {
instances []ListPolicyInstancePartial `json:"instances,omitempty"`
}
ptcar2009 marked this conversation as resolved.
Show resolved Hide resolved
Loading