-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add datasource and resource fot token settings
- Loading branch information
Showing
6 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
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,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()), | ||
} | ||
} |
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,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 | ||
} |
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 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{} | ||
} |
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,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{} | ||
} |
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