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-12954, ENG-12955: Add datasource and resource fot token settings #479

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 6 additions & 4 deletions cyral/internal/datalabel/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package datalabel
import (
"fmt"

cs "github.com/cyralinc/terraform-provider-cyral/cyral/internal/datalabel/classificationrule"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

cs "github.com/cyralinc/terraform-provider-cyral/cyral/internal/datalabel/classificationrule"
"github.com/cyralinc/terraform-provider-cyral/cyral/utils"
)

type Tags []string
Expand All @@ -28,15 +30,15 @@ type DataLabel struct {

func (dl *DataLabel) WriteToSchema(d *schema.ResourceData) error {
if err := d.Set("description", dl.Description); err != nil {
return fmt.Errorf("error setting 'description' field: %w", err)
return fmt.Errorf(utils.ErrorSettingFieldFmt, "description", err)
}

if err := d.Set("tags", dl.Tags.AsInterface()); err != nil {
return fmt.Errorf("error setting 'tags' field: %w", err)
return fmt.Errorf(utils.ErrorSettingFieldFmt, "tags", err)
}

if err := d.Set("classification_rule", dl.ClassificationRule.AsInterface()); err != nil {
return fmt.Errorf("error setting 'classification_rule' field: %w", err)
return fmt.Errorf(utils.ErrorSettingFieldFmt, "classification_rule", err)
}

d.SetId(dl.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ func ResourceRegoPolicyInstance() *schema.Resource {
},
},
RegoPolicyInstanceDurationKey: {
Description: "Policy duration. The policy expires after the duration specified. Should follow the protobuf " +
"duration string format, which corresponds to a sequence of decimal numbers suffixed by a 's' at the " +
"end, representing the duration in seconds. For example: `300s`, `60s`, `10.50s` etc",
Description: fmt.Sprintf(
utils.DurationFieldDescriptionFmt,
"Policy duration. The policy expires after the duration specified",
),
Type: schema.TypeString,
Optional: true,
ValidateFunc: utils.ValidationDurationString,
Expand Down
5 changes: 5 additions & 0 deletions cyral/internal/tokensettings/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tokensettings

const (
accessTokenSettingsID = "settings/access_token"
)
16 changes: 16 additions & 0 deletions cyral/internal/tokensettings/datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tokensettings

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

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

func dataSourceSchema() *schema.Resource {
return &schema.Resource{
Description: "Retrieves the access token settings. See also the resource " +
"[`cyral_access_token_settings`](../resources/access_token_settings.md).",
ReadContext: core.ReadResource(readConfig()),
Schema: getAccessTokenSettingsSchema(true),
}
}
46 changes: 46 additions & 0 deletions cyral/internal/tokensettings/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package tokensettings

import (
"fmt"

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

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

type AccessTokenSettings struct {
MaxValidity string `json:"maxValidity"`
DefaultValidity string `json:"defaultValidity"`
MaxNumberOfTokensPerUser uint32 `json:"maxNumberOfTokensPerUser"`
OfflineTokenValidation bool `json:"offlineTokenValidation"`
TokenLength uint32 `json:"tokenLength"`
}

func (settings *AccessTokenSettings) WriteToSchema(d *schema.ResourceData) error {
if err := d.Set(MaxValidityKey, settings.MaxValidity); err != nil {
return fmt.Errorf(utils.ErrorSettingFieldFmt, MaxValidityKey, err)
}
if err := d.Set(DefaultValidityKey, settings.DefaultValidity); err != nil {
return fmt.Errorf(utils.ErrorSettingFieldFmt, DefaultValidityKey, err)
}
if err := d.Set(MaxNumberOfTokensPerUserKey, settings.MaxNumberOfTokensPerUser); err != nil {
return fmt.Errorf(utils.ErrorSettingFieldFmt, MaxNumberOfTokensPerUserKey, err)
}
if err := d.Set(OfflineTokenValidationKey, settings.OfflineTokenValidation); err != nil {
return fmt.Errorf(utils.ErrorSettingFieldFmt, OfflineTokenValidationKey, err)
}
if err := d.Set(TokenLengthKey, settings.TokenLength); err != nil {
return fmt.Errorf(utils.ErrorSettingFieldFmt, TokenLengthKey, err)
}
d.SetId(accessTokenSettingsID)
return nil
}

func (settings *AccessTokenSettings) ReadFromSchema(d *schema.ResourceData) error {
settings.MaxValidity = d.Get(MaxValidityKey).(string)
settings.DefaultValidity = d.Get(DefaultValidityKey).(string)
settings.MaxNumberOfTokensPerUser = uint32(d.Get(MaxNumberOfTokensPerUserKey).(int))
settings.OfflineTokenValidation = d.Get(OfflineTokenValidationKey).(bool)
settings.TokenLength = uint32(d.Get(TokenLengthKey).(int))
return nil
}
67 changes: 67 additions & 0 deletions cyral/internal/tokensettings/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package tokensettings

import (
"fmt"
"net/http"

"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"
)

func resourceSchema() *schema.Resource {
return &schema.Resource{
Description: "Manages the access token settings. See also the data source " +
"[`cyral_access_token_settings`](../data-source/access_token_settings.md)." +
"\n\n-> **Note** The deletion of this terraform resource will reset the access " +
"token settings to their corresponding default values.",
CreateContext: core.CreateResource(updateConfig(), readConfig()),
ReadContext: core.ReadResource(readConfig()),
UpdateContext: core.UpdateResource(updateConfig(), readConfig()),
DeleteContext: core.DeleteResource(deleteConfig()),
Schema: getAccessTokenSettingsSchema(false),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

func readConfig() core.ResourceOperationConfig {
return core.ResourceOperationConfig{
Name: "AccessTokenSettingsRead",
HttpMethod: http.MethodGet,
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/accessTokens/settings", c.ControlPlane)
},
NewResponseData: func(d *schema.ResourceData) core.ResponseData {
return &AccessTokenSettings{}
},
}
}

func updateConfig() core.ResourceOperationConfig {
return core.ResourceOperationConfig{
Name: "AccessTokenSettingsUpdate",
HttpMethod: http.MethodPut,
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/accessTokens/settings", c.ControlPlane)
},
NewResourceData: func() core.ResourceData {
return &AccessTokenSettings{}
},
}
}

// Since the access token settings resource is a global setting that is never deleted,
// the UpdateAccessTokenSettings API will be called here, with an empty body, so that
// the access token settings are reseted to their corresponding default values.
func deleteConfig() core.ResourceOperationConfig {
return core.ResourceOperationConfig{
Name: "AccessTokenSettingsDelete",
HttpMethod: http.MethodPut,
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/accessTokens/settings", c.ControlPlane)
},
}
}
Loading
Loading