Skip to content

Commit

Permalink
adding flatten funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimutant committed Jan 12, 2024
1 parent 77d4d7c commit 341728c
Showing 1 changed file with 95 additions and 17 deletions.
112 changes: 95 additions & 17 deletions nutanix/services/v2/iam/resource_nutanix_users_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/nutanix/ntnx-api-golang-clients/iam-go-client/v4/models/common/v1/config"
import1 "github.com/nutanix/ntnx-api-golang-clients/iam-go-client/v4/models/iam/v4/authn"
conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
Expand Down Expand Up @@ -207,21 +210,24 @@ func resourceNutanixUserV2Create(ctx context.Context, d *schema.ResourceData, me
p := import1.UserStatusType(pInt.(int))
spec.Status = &p
}
// if un, ok := d.GetOk("last_login_time"); ok {
// spec.LastLoginTime = utils.StringPtr(un.(string))
// }
// if un, ok := d.GetOk("created_time"); ok {
// spec.CreatedTime = utils.StringPtr(un.(string))
// }
// if un, ok := d.GetOk("last_updated_time"); ok {
// spec.LastUpdatedTime = utils.StringPtr(un.(string))
// }
if un, ok := d.GetOk("created_by"); ok {
spec.CreatedBy = utils.StringPtr(un.(string))
}
// if un, ok := d.GetOk("additional_attributes"); ok {
// spec.AdditionalAttributes = utils.StringPtr(un.(string))
// }
if lastLogin, ok := d.GetOk("last_login_time"); ok {
t := time.Time(lastLogin.(time.Time))
spec.LastLoginTime = &t
}
if cTime, ok := d.GetOk("created_time"); ok {
t := time.Time(cTime.(time.Time))
spec.CreatedTime = &t
}
if lastUpdate, ok := d.GetOk("last_updated_time"); ok {
t := time.Time(lastUpdate.(time.Time))
spec.LastUpdatedTime = &t
}
if cBy, ok := d.GetOk("created_by"); ok {
spec.CreatedBy = utils.StringPtr(cBy.(string))
}
if addAttr, ok := d.GetOk("additional_attributes"); ok {
spec.AdditionalAttributes = expandKVPair(addAttr.([]interface{}))
}

resp, err := conn.UsersAPI.CreateUser(spec)
if err != nil {
Expand Down Expand Up @@ -263,7 +269,7 @@ func resourceNutanixUserV2Read(ctx context.Context, d *schema.ResourceData, meta
if err = d.Set("username", getResp.Username); err != nil {
return diag.Errorf("error setting username for users %s: %s", d.Id(), err)
}
if err = d.Set("user_type", getResp.UserType); err != nil {
if err = d.Set("user_type", flattenUserType(getResp.UserType)); err != nil {
return diag.Errorf("error setting user_type for users %s: %s", d.Id(), err)
}
if err = d.Set("idp_id", getResp.IdpId); err != nil {
Expand Down Expand Up @@ -296,9 +302,18 @@ func resourceNutanixUserV2Read(ctx context.Context, d *schema.ResourceData, meta
if err = d.Set("force_reset_password", getResp.IsForceResetPasswordEnabled); err != nil {
return diag.Errorf("error setting force_reset_password for users %s: %s", d.Id(), err)
}
if err = d.Set("status", getResp.Status); err != nil {
if err = d.Set("status", flattenUserStatusType(getResp.Status)); err != nil {
return diag.Errorf("error setting status for users %s: %s", d.Id(), err)
}
if err = d.Set("last_login_time", getResp.LastLoginTime); err != nil {
return diag.Errorf("error setting last_login_time for users %s: %s", d.Id(), err)
}
if err = d.Set("created_time", getResp.CreatedTime); err != nil {
return diag.Errorf("error setting created_time for users %s: %s", d.Id(), err)
}
if err = d.Set("last_updated_time", getResp.LastUpdatedTime); err != nil {
return diag.Errorf("error setting last_updated_time for users %s: %s", d.Id(), err)
}
if err = d.Set("created_by", getResp.CreatedBy); err != nil {
return diag.Errorf("error setting created_by for users %s: %s", d.Id(), err)
}
Expand Down Expand Up @@ -375,6 +390,15 @@ func resourceNutanixUserV2Update(ctx context.Context, d *schema.ResourceData, me
p := import1.UserStatusType(pInt.(int))
updateSpec.Status = &p
}
if d.HasChange("last_login_time") {
updateSpec.LastLoginTime = d.Get("last_login_time").(*time.Time)
}
if d.HasChange("created_time") {
updateSpec.CreatedTime = d.Get("created_time").(*time.Time)
}
if d.HasChange("last_updated_time") {
updateSpec.LastUpdatedTime = d.Get("last_updated_time").(*time.Time)
}
if d.HasChange("created_by") {
updateSpec.CreatedBy = utils.StringPtr(d.Get("created_by").(string))
}
Expand All @@ -400,3 +424,57 @@ func resourceNutanixUserV2Update(ctx context.Context, d *schema.ResourceData, me
func resourceNutanixUserV2Delete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}

func expandKVPair(pr []interface{}) []config.KVPair {
if len(pr) > 0 {
kvPairs := make([]config.KVPair, len(pr))

for k, v := range pr {
val := v.(map[string]interface{})
pair := &config.KVPair{}

if name, ok := val["name"]; ok {
pair.Name = utils.StringPtr(name.(string))
}
if value, ok := val["value"]; ok {
pair.Value = value.(*config.OneOfKVPairValue)
}
kvPairs[k] = *pair
}
return kvPairs
}
return nil
}

func flattenUserType(pr *import1.UserType) string {
if pr != nil {
const two, three, four, five = 2, 3, 4, 5
if *pr == import1.UserType(two) {
return "LOCAL"
}
if *pr == import1.UserType(three) {
return "SAML"
}
if *pr == import1.UserType(four) {
return "LDAP"
}
if *pr == import1.UserType(five) {
return "EXTERNAL"
}
}
return "UNKNOWN"
}

func flattenUserStatusType(pr *import1.UserStatusType) string {
if pr != nil {
const two, three = 2, 3

if *pr == import1.UserStatusType(two) {
return "ACTIVE"
}
if *pr == import1.UserStatusType(three) {
return "INACTIVE"
}
}
return "UNKNOWN"
}

0 comments on commit 341728c

Please sign in to comment.