Skip to content

Commit

Permalink
Add datasource and resource fot token settings
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorGFM committed Nov 17, 2023
1 parent e304244 commit 314f5c2
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 1 deletion.
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"
"github.com/cyralinc/terraform-provider-cyral/cyral/utils"
)

func dataSourceSchema() *schema.Resource {
return &schema.Resource{
Description: "",
ReadContext: core.ReadResource(readConfig()),
Schema: utils.ConvertSchemaFieldsToComputed(getAccessTokenSettingsSchema()),
}
}
41 changes: 41 additions & 0 deletions cyral/internal/tokensettings/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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"`
}

func (settings *AccessTokenSettings) WriteToSchema(d *schema.ResourceData) error {
if err := d.Set(MaxValidityKey, settings.MaxValidity); err != nil {
return fmt.Errorf(utils.ErrSettingFieldFmt, MaxValidityKey, err)
}
if err := d.Set(DefaultValidityKey, settings.DefaultValidity); err != nil {
return fmt.Errorf(utils.ErrSettingFieldFmt, DefaultValidityKey, err)
}
if err := d.Set(MaxNumberOfTokensPerUserKey, settings.MaxNumberOfTokensPerUser); err != nil {
return fmt.Errorf(utils.ErrSettingFieldFmt, MaxNumberOfTokensPerUserKey, err)
}
if err := d.Set(OfflineTokenValidationKey, settings.OfflineTokenValidation); err != nil {
return fmt.Errorf(utils.ErrSettingFieldFmt, OfflineTokenValidationKey, err)
}
d.SetId("settings/access_token")
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)
return nil
}
64 changes: 64 additions & 0 deletions cyral/internal/tokensettings/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package tokensettings

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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: "",
CreateContext: core.CreateResource(updateConfig(), readConfig()),
ReadContext: core.ReadResource(readConfig()),
UpdateContext: core.UpdateResource(updateConfig(), readConfig()),
DeleteContext: resourceAccessTokenSettingsDelete,
Schema: getAccessTokenSettingsSchema(),
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{}
},
}
}

func resourceAccessTokenSettingsDelete(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) diag.Diagnostics {
// Since access token settings cannot be deleted, we just set the ID to
// empty so that the resource can be removed from the terraform state.
d.SetId("")
return diag.Diagnostics{}
}
67 changes: 67 additions & 0 deletions cyral/internal/tokensettings/schema_loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package tokensettings

import (
"github.com/cyralinc/terraform-provider-cyral/cyral/core"
"github.com/cyralinc/terraform-provider-cyral/cyral/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const (
MaxValidityKey = "max_validity"
DefaultValidityKey = "default_validity"
MaxNumberOfTokensPerUserKey = "max_number_of_tokens_per_user"
OfflineTokenValidationKey = "offline_token_validation"
)

func getAccessTokenSettingsSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
MaxValidityKey: {
Description: "",
Type: schema.TypeString,
Required: true,
ValidateFunc: utils.ValidationDurationString,
},
DefaultValidityKey: {
Description: "",
Type: schema.TypeString,
Required: true,
ValidateFunc: utils.ValidationDurationString,
},
MaxNumberOfTokensPerUserKey: {
Description: "",
Type: schema.TypeInt,
Required: true,
},
OfflineTokenValidationKey: {
Description: "",
Type: schema.TypeBool,
Required: true,
},
}
}

type packageSchema struct {
}

func (p *packageSchema) Name() string {
return "tokensettings"
}

func (p *packageSchema) Schemas() []*core.SchemaDescriptor {
return []*core.SchemaDescriptor{
{
Name: "cyral_access_token_settings",
Type: core.DataSourceSchemaType,
Schema: dataSourceSchema,
},
{
Name: "cyral_access_token_settings",
Type: core.ResourceSchemaType,
Schema: resourceSchema,
},
}
}

func PackageSchema() core.PackageSchema {
return &packageSchema{}
}
2 changes: 2 additions & 0 deletions cyral/provider/schema_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"github.com/cyralinc/terraform-provider-cyral/cyral/core"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/datalabel"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/repository/datamap"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/tokensettings"
)

func packagesSchemas() []core.PackageSchema {
v := []core.PackageSchema{
datalabel.PackageSchema(),
datamap.PackageSchema(),
tokensettings.PackageSchema(),
}
return v
}
4 changes: 3 additions & 1 deletion cyral/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// Common keys.
const (
// Common keys.
IDKey = "id"
NameKey = "name"
DescriptionKey = "description"
Expand All @@ -25,6 +25,8 @@ const (
SidecarIDKey = "sidecar_id"
ListenerIDKey = "listener_id"
StatusKey = "status"
// Common string formatters
ErrSettingFieldFmt = "error setting '%s' field: %w"
)

func ConvertToInterfaceList[T any](list []T) []any {
Expand Down

0 comments on commit 314f5c2

Please sign in to comment.