|
| 1 | +package spectrocloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 8 | + "github.com/spectrocloud/palette-sdk-go/api/models" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func resourcePasswordPolicy() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + CreateContext: resourcePasswordPolicyCreate, |
| 15 | + ReadContext: resourcePasswordPolicyRead, |
| 16 | + UpdateContext: resourcePasswordPolicyUpdate, |
| 17 | + DeleteContext: resourcePasswordPolicyDelete, |
| 18 | + |
| 19 | + Timeouts: &schema.ResourceTimeout{ |
| 20 | + Create: schema.DefaultTimeout(10 * time.Minute), |
| 21 | + Update: schema.DefaultTimeout(10 * time.Minute), |
| 22 | + Delete: schema.DefaultTimeout(10 * time.Minute), |
| 23 | + }, |
| 24 | + SchemaVersion: 2, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "password_regex": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Optional: true, |
| 29 | + Default: "", |
| 30 | + ConflictsWith: []string{"min_password_length", "min_uppercase_letters", |
| 31 | + "min_digits", "min_lowercase_letters", "min_special_characters"}, |
| 32 | + RequiredWith: []string{"password_expiry_days", "first_reminder_days"}, |
| 33 | + Description: "A regular expression (regex) to define custom password patterns, such as enforcing specific characters or sequences in the password.", |
| 34 | + }, |
| 35 | + "password_expiry_days": { |
| 36 | + Type: schema.TypeInt, |
| 37 | + Optional: true, |
| 38 | + Default: 999, |
| 39 | + ValidateFunc: validation.IntBetween(1, 1000), |
| 40 | + Description: "The number of days before the password expires. Must be between 1 and 1000 days. Defines how often passwords must be changed.", |
| 41 | + }, |
| 42 | + "first_reminder_days": { |
| 43 | + Type: schema.TypeInt, |
| 44 | + Optional: true, |
| 45 | + Default: 5, |
| 46 | + Description: "The number of days before the password expiry to send the first reminder to the user. Default is 5 days before expiry.", |
| 47 | + }, |
| 48 | + "min_password_length": { |
| 49 | + Type: schema.TypeInt, |
| 50 | + Optional: true, |
| 51 | + Default: 12, |
| 52 | + Description: "The minimum length required for the password. Enforces a stronger password policy by ensuring a minimum number of characters.", |
| 53 | + }, |
| 54 | + "min_uppercase_letters": { |
| 55 | + Type: schema.TypeInt, |
| 56 | + Optional: true, |
| 57 | + Default: 1, |
| 58 | + Description: "The minimum number of uppercase letters (A-Z) required in the password. Helps ensure password complexity with a mix of case-sensitive characters.", |
| 59 | + }, |
| 60 | + "min_digits": { |
| 61 | + Type: schema.TypeInt, |
| 62 | + Optional: true, |
| 63 | + Default: 1, |
| 64 | + Description: "The minimum number of numeric digits (0-9) required in the password. Ensures that passwords contain numerical characters.", |
| 65 | + }, |
| 66 | + "min_lowercase_letters": { |
| 67 | + Type: schema.TypeInt, |
| 68 | + Optional: true, |
| 69 | + Default: 1, |
| 70 | + Description: "The minimum number of lowercase letters (a-z) required in the password. Ensures that lowercase characters are included for password complexity.", |
| 71 | + }, |
| 72 | + "min_special_characters": { |
| 73 | + Type: schema.TypeInt, |
| 74 | + Optional: true, |
| 75 | + Default: 1, |
| 76 | + Description: "The minimum number of special characters (e.g., !, @, #, $, %) required in the password. This increases the password's security level by including symbols.", |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func toPasswordPolicy(d *schema.ResourceData) (*models.V1TenantPasswordPolicyEntity, error) { |
| 83 | + if d.Get("password_regex").(string) != "" { |
| 84 | + return &models.V1TenantPasswordPolicyEntity{ |
| 85 | + IsRegex: true, |
| 86 | + Regex: d.Get("password_regex").(string), |
| 87 | + ExpiryDurationInDays: int64(d.Get("password_expiry_days").(int)), |
| 88 | + FirstReminderInDays: int64(d.Get("first_reminder_days").(int)), |
| 89 | + }, nil |
| 90 | + } |
| 91 | + return &models.V1TenantPasswordPolicyEntity{ |
| 92 | + ExpiryDurationInDays: int64(d.Get("password_expiry_days").(int)), |
| 93 | + FirstReminderInDays: int64(d.Get("first_reminder_days").(int)), |
| 94 | + IsRegex: false, |
| 95 | + MinLength: int64(d.Get("min_password_length").(int)), |
| 96 | + MinNumOfBlockLetters: int64(d.Get("min_uppercase_letters").(int)), |
| 97 | + MinNumOfDigits: int64(d.Get("min_digits").(int)), |
| 98 | + MinNumOfSmallLetters: int64(d.Get("min_lowercase_letters").(int)), |
| 99 | + MinNumOfSpecialCharacters: int64(d.Get("min_special_characters").(int)), |
| 100 | + Regex: "", |
| 101 | + }, nil |
| 102 | +} |
| 103 | + |
| 104 | +func toPasswordPolicyDefault(d *schema.ResourceData) (*models.V1TenantPasswordPolicyEntity, error) { |
| 105 | + return &models.V1TenantPasswordPolicyEntity{ |
| 106 | + ExpiryDurationInDays: 999, |
| 107 | + FirstReminderInDays: 5, |
| 108 | + IsRegex: false, |
| 109 | + MinLength: 6, |
| 110 | + MinNumOfBlockLetters: 1, |
| 111 | + MinNumOfDigits: 1, |
| 112 | + MinNumOfSmallLetters: 1, |
| 113 | + MinNumOfSpecialCharacters: 1, |
| 114 | + Regex: "", |
| 115 | + }, nil |
| 116 | +} |
| 117 | + |
| 118 | +func flattenPasswordPolicy(passwordPolicy *models.V1TenantPasswordPolicyEntity, d *schema.ResourceData) error { |
| 119 | + var err error |
| 120 | + err = d.Set("password_regex", passwordPolicy.Regex) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + err = d.Set("password_expiry_days", passwordPolicy.ExpiryDurationInDays) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + err = d.Set("first_reminder_days", passwordPolicy.FirstReminderInDays) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + err = d.Set("min_password_length", passwordPolicy.MinLength) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + err = d.Set("min_uppercase_letters", passwordPolicy.MinNumOfBlockLetters) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + err = d.Set("min_digits", passwordPolicy.MinNumOfDigits) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + err = d.Set("min_lowercase_letters", passwordPolicy.MinNumOfSmallLetters) |
| 145 | + if err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + err = d.Set("min_special_characters", passwordPolicy.MinNumOfSpecialCharacters) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func resourcePasswordPolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 156 | + c := getV1ClientWithResourceContext(m, tenantString) |
| 157 | + var diags diag.Diagnostics |
| 158 | + passwordPolicy, err := toPasswordPolicy(d) |
| 159 | + if err != nil { |
| 160 | + return diag.FromErr(err) |
| 161 | + } |
| 162 | + tenantUID, err := c.GetTenantUID() |
| 163 | + if err != nil { |
| 164 | + return diag.FromErr(err) |
| 165 | + } |
| 166 | + // For Password Policy we don't have support for creation it's always an update |
| 167 | + err = c.UpdatePasswordPolicy(tenantUID, passwordPolicy) |
| 168 | + if err != nil { |
| 169 | + return diag.FromErr(err) |
| 170 | + } |
| 171 | + d.SetId("default-password-policy-id") |
| 172 | + return diags |
| 173 | +} |
| 174 | + |
| 175 | +func resourcePasswordPolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 176 | + //c := getV1ClientWithResourceContext(m, tenantString) |
| 177 | + var diags diag.Diagnostics |
| 178 | + return diags |
| 179 | +} |
| 180 | + |
| 181 | +func resourcePasswordPolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 182 | + c := getV1ClientWithResourceContext(m, tenantString) |
| 183 | + var diags diag.Diagnostics |
| 184 | + passwordPolicy, err := toPasswordPolicy(d) |
| 185 | + if err != nil { |
| 186 | + return diag.FromErr(err) |
| 187 | + } |
| 188 | + tenantUID, err := c.GetTenantUID() |
| 189 | + if err != nil { |
| 190 | + return diag.FromErr(err) |
| 191 | + } |
| 192 | + // For Password Policy we don't have support for creation it's always an update |
| 193 | + err = c.UpdatePasswordPolicy(tenantUID, passwordPolicy) |
| 194 | + if err != nil { |
| 195 | + return diag.FromErr(err) |
| 196 | + } |
| 197 | + |
| 198 | + return diags |
| 199 | +} |
| 200 | + |
| 201 | +func resourcePasswordPolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 202 | + c := getV1ClientWithResourceContext(m, tenantString) |
| 203 | + var diags diag.Diagnostics |
| 204 | + // We can't delete the base password policy, instead |
| 205 | + passwordPolicy, err := toPasswordPolicyDefault(d) |
| 206 | + if err != nil { |
| 207 | + return diag.FromErr(err) |
| 208 | + } |
| 209 | + tenantUID, err := c.GetTenantUID() |
| 210 | + if err != nil { |
| 211 | + return diag.FromErr(err) |
| 212 | + } |
| 213 | + // For Password Policy we don't have support for creation it's always an update |
| 214 | + err = c.UpdatePasswordPolicy(tenantUID, passwordPolicy) |
| 215 | + if err != nil { |
| 216 | + return diag.FromErr(err) |
| 217 | + } |
| 218 | + d.SetId("") |
| 219 | + return diags |
| 220 | +} |
0 commit comments