Skip to content

Commit

Permalink
Merge branch 'fix/registration-fields-enhancement' into 'master'
Browse files Browse the repository at this point in the history
Resource registration field enhancement

See merge request cidaas-management/terraform!40
  • Loading branch information
Tujit Bora committed Jan 15, 2024
2 parents 5a8557b + d70532f commit d74075d
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 127 deletions.
41 changes: 25 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,23 +367,32 @@ An example of Registration Page Field resource configuration. Please add the bel
* cidaas:field_setup_delete

```hcl
resource "cidaas_registration_page_field" "Enter resource name for resource type registration_page_fields" {
parent_group_id = "DEFAULT"
is_group = false
data_type = "TEXT"
field_key = "Enter registration_page_field name"
required = false
enabled = false
read_only = false
internal = false
scopes = ["terraform-test-scope"]
claimable = true
order = 25
field_type = "CUSTOM"
locale_text_locale = "en-GB"
locale_text_name = "terraform-test-field"
locale_text_language = "en"
resource "cidaas_registration_page_field" "sample" {
claimable = true
data_type = "TEXT"
enabled = false
field_key = "sample_field"
field_type = "CUSTOM"
internal = false
is_group = false
locale_text_language = "en"
locale_text_locale = "en-us"
locale_text_name = "Sample Field"
order = 2
parent_group_id = "DEFAULT"
read_only = false
required = true
required_msg = "sample_field is required"
locale_text_min_length = 10
locale_text_max_length = 100
min_length_error_msg = "minimum length should be 10"
max_length_error_msg = "maximum length should be 100"
scopes = [
"profile",
"cidaas:public_profile",
]
}
```

Use the command below to import an existing cidaas_registration_page_field
Expand Down
124 changes: 76 additions & 48 deletions cidaas/resource_registration_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,26 @@ func resourceRegistrationField() *schema.Resource {
Type: schema.TypeString,
Required: true,
},

"locale_text_min_length": {
Type: schema.TypeInt,
Optional: true,
},
"locale_text_max_length": {
Type: schema.TypeInt,
Optional: true,
},
"min_length_error_msg": {
Type: schema.TypeString,
Optional: true,
},
"max_length_error_msg": {
Type: schema.TypeString,
Optional: true,
},
"required_msg": {
Type: schema.TypeString,
Optional: true,
},
"registration_field_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -137,34 +156,18 @@ func resourceRegistrationField() *schema.Resource {
}
}

type registrationFeildCreationResponse struct {
Success bool
Status int
}

func resourceRegistrationFieldCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
var registrationFieldConfig cidaas.RegistrationFieldConfig
cidaas_client := m.(cidaas.CidaasClient)

registrationFieldConfig.ParentGroupId = d.Get("parent_group_id").(string)
registrationFieldConfig.Scopes = util.InterfaceArray2StringArray(d.Get("scopes").([]interface{}))
registrationFieldConfig.DataType = d.Get("data_type").(string)
registrationFieldConfig.FieldKey = d.Get("field_key").(string)
registrationFieldConfig.Required = d.Get("required").(bool)
registrationFieldConfig.IsGroup = d.Get("is_group").(bool)
registrationFieldConfig.Enabled = d.Get("enabled").(bool)
registrationFieldConfig.ReadOnly = d.Get("read_only").(bool)
registrationFieldConfig.Internal = d.Get("internal").(bool)
registrationFieldConfig.Claimable = d.Get("claimable").(bool)
registrationFieldConfig.Order = d.Get("order").(int)
registrationFieldConfig.FieldType = d.Get("field_type").(string)
registrationFieldConfig.LocaleText = make(map[string]interface{})
registrationFieldConfig.BaseDataType = "string"
registrationFieldConfig.LocaleText["locale"] = d.Get("locale_text_locale").(string)
registrationFieldConfig.LocaleText["name"] = d.Get("locale_text_name").(string)
registrationFieldConfig.LocaleText["language"] = d.Get("locale_text_language").(string)

registrationFieldConfig := prepareRegistrationFieldConfig(d)
isValid, msg := cidaas.ValidateRequest(registrationFieldConfig)
if !isValid {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: msg,
})
return diags
}
response, err := cidaas_client.CreateRegistrationField(registrationFieldConfig)
if err != nil {
diags = append(diags, diag.Diagnostic{
Expand Down Expand Up @@ -218,35 +221,31 @@ func resourceRegistrationFieldRead(ctx context.Context, d *schema.ResourceData,
d.Set("locale_text_locale", response.Data.LocaleText[0]["locale"])
d.Set("locale_text_name", response.Data.LocaleText[0]["name"])
d.Set("locale_text_language", response.Data.LocaleText[0]["language"])
d.Set("min_length_error_msg", response.Data.LocaleText[0]["minLength"])
d.Set("max_length_error_msg", response.Data.LocaleText[0]["maxLength"])
d.Set("required_msg", response.Data.LocaleText[0]["required"])
}
d.Set("locale_text_min_length", response.Data.FieldDefinition.MinLength)
d.Set("locale_text_max_length", response.Data.FieldDefinition.MaxLength)

return diags
}

func resourceRegistrationFieldUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
cidaas_client := m.(cidaas.CidaasClient)
var rfg cidaas.RegistrationFieldConfig

rfg.ParentGroupId = d.Get("parent_group_id").(string)
rfg.Scopes = util.InterfaceArray2StringArray(d.Get("scopes").([]interface{}))
rfg.DataType = d.Get("data_type").(string)
rfg.FieldKey = d.Get("field_key").(string)
rfg.Required = d.Get("required").(bool)
rfg.IsGroup = d.Get("is_group").(bool)
rfg.Enabled = d.Get("enabled").(bool)
rfg.ReadOnly = d.Get("read_only").(bool)
rfg.Internal = d.Get("internal").(bool)
rfg.Claimable = d.Get("claimable").(bool)
rfg.Order = d.Get("order").(int)
rfg.FieldType = d.Get("field_type").(string)
rfg.LocaleText = make(map[string]interface{})
rfg.BaseDataType = d.Get("base_data_type").(string)
rfg.Id = d.Get("registration_field_id").(string)
rfg.LocaleText["locale"] = d.Get("locale_text_locale").(string)
rfg.LocaleText["name"] = d.Get("locale_text_name").(string)
rfg.LocaleText["language"] = d.Get("locale_text_language").(string)

_, err := cidaas_client.UpdateRegistrationField(rfg)
registrationFieldConfig := prepareRegistrationFieldConfig(d)
registrationFieldConfig.Id = d.Get("registration_field_id").(string)
registrationFieldConfig.BaseDataType = d.Get("base_data_type").(string)
isValid, msg := cidaas.ValidateRequest(registrationFieldConfig)
if !isValid {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: msg,
})
return diags
}
_, err := cidaas_client.UpdateRegistrationField(registrationFieldConfig)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Expand All @@ -272,3 +271,32 @@ func resourceRegistrationFieldDelete(ctx context.Context, d *schema.ResourceData
}
return diags
}

func prepareRegistrationFieldConfig(d *schema.ResourceData) cidaas.RegistrationFieldConfig {
var registrationFieldConfig cidaas.RegistrationFieldConfig
registrationFieldConfig.ParentGroupId = d.Get("parent_group_id").(string)
registrationFieldConfig.Scopes = util.InterfaceArray2StringArray(d.Get("scopes").([]interface{}))
registrationFieldConfig.DataType = d.Get("data_type").(string)
registrationFieldConfig.FieldKey = d.Get("field_key").(string)
registrationFieldConfig.Required = d.Get("required").(bool)
registrationFieldConfig.IsGroup = d.Get("is_group").(bool)
registrationFieldConfig.Enabled = d.Get("enabled").(bool)
registrationFieldConfig.ReadOnly = d.Get("read_only").(bool)
registrationFieldConfig.Internal = d.Get("internal").(bool)
registrationFieldConfig.Claimable = d.Get("claimable").(bool)
registrationFieldConfig.Order = d.Get("order").(int)
registrationFieldConfig.FieldType = d.Get("field_type").(string)
registrationFieldConfig.BaseDataType = "string"
registrationFieldConfig.LocaleText.Locale = d.Get("locale_text_locale").(string)
registrationFieldConfig.LocaleText.Name = d.Get("locale_text_name").(string)
registrationFieldConfig.LocaleText.Language = d.Get("locale_text_language").(string)
registrationFieldConfig.LocaleText.MinLengthErrorMsg = d.Get("min_length_error_msg").(string)
registrationFieldConfig.LocaleText.MaxLengthErrorMsg = d.Get("max_length_error_msg").(string)
registrationFieldConfig.LocaleText.RequiredMsg = d.Get("required_msg").(string)
registrationFieldConfig.FieldDefinition.Locale = d.Get("locale_text_locale").(string)
registrationFieldConfig.FieldDefinition.Name = d.Get("locale_text_name").(string)
registrationFieldConfig.FieldDefinition.Language = d.Get("locale_text_language").(string)
registrationFieldConfig.FieldDefinition.MinLength = d.Get("locale_text_min_length").(int)
registrationFieldConfig.FieldDefinition.MaxLength = d.Get("locale_text_max_length").(int)
return registrationFieldConfig
}
45 changes: 28 additions & 17 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ terraform import cidaas_custom_provider.<resource name> provider_name

##### Cidaas App Resource

An example of App resource configuration. Please add the below scopes to the client with client_id set in the env in order to perform CRUD on cidaas_app
An example of App resource configuration.

Please add the below scopes to the client with client_id set in the env in order to perform CRUD on cidaas_app

* cidaas:apps_read
* cidaas:apps_write
Expand Down Expand Up @@ -365,23 +367,32 @@ An example of Registration Page Field resource configuration. Please add the bel
* cidaas:field_setup_delete

```hcl
resource "cidaas_registration_page_field" "Enter resource name for resource type registration_page_fields" {
parent_group_id = "DEFAULT"
is_group = false
data_type = "TEXT"
field_key = "Enter registration_page_field name"
required = false
enabled = false
read_only = false
internal = false
scopes = ["terraform-test-scope"]
claimable = true
order = 25
field_type = "CUSTOM"
locale_text_locale = "en-GB"
locale_text_name = "terraform-test-field"
locale_text_language = "en"
resource "cidaas_registration_page_field" "sample" {
claimable = true
data_type = "TEXT"
enabled = false
field_key = "sample_field"
field_type = "CUSTOM"
internal = false
is_group = false
locale_text_language = "en"
locale_text_locale = "en-us"
locale_text_name = "Sample Field"
order = 2
parent_group_id = "DEFAULT"
read_only = false
required = true
required_msg = "sample_field is required"
locale_text_min_length = 10
locale_text_max_length = 100
min_length_error_msg = "minimum length should be 10"
max_length_error_msg = "maximum length should be 100"
scopes = [
"profile",
"cidaas:public_profile",
]
}
```

Use the command below to import an existing cidaas_registration_page_field
Expand Down
33 changes: 19 additions & 14 deletions example/registrarion_field.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
resource "cidaas_registration_page_field" "sample" {
claimable = true
data_type = "TEXT"
enabled = false
field_key = "given_name_test_v21"
field_type = "CUSTOM"
internal = false
is_group = false
locale_text_language = "en"
locale_text_locale = "en-us"
locale_text_name = "Given Name TEST v21"
order = 2
parent_group_id = "DEFAULT"
read_only = false
required = true
claimable = true
data_type = "TEXT"
enabled = false
field_key = "sample_field"
field_type = "CUSTOM"
internal = false
is_group = false
locale_text_language = "en"
locale_text_locale = "en-us"
locale_text_name = "Sample Field"
order = 2
parent_group_id = "DEFAULT"
read_only = false
required = true
required_msg = "sample_field is required"
locale_text_min_length = 10
locale_text_max_length = 100
min_length_error_msg = "minimum length should be 10"
max_length_error_msg = "maximum length should be 100"
scopes = [
"profile",
"cidaas:public_profile",
Expand Down
Loading

0 comments on commit d74075d

Please sign in to comment.