forked from nutanix/terraform-provider-nutanix
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cea90c9
commit 39dc8a8
Showing
5 changed files
with
472 additions
and
0 deletions.
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
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
116 changes: 116 additions & 0 deletions
116
nutanix/services/v2/iam/data_source_nutanix_user_group_v2.go
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,116 @@ | ||
package iam | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
import1 "github.com/nutanix-core/ntnx-api-golang-sdk-internal/iam-go-client/v16/models/iam/v4/authn" | ||
conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix" | ||
|
||
"github.com/terraform-providers/terraform-provider-nutanix/utils" | ||
) | ||
|
||
func DatasourceNutanixUserGroupV4() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: DatasourceNutanixUserGroupV4Read, | ||
Schema: map[string]*schema.Schema{ | ||
"ext_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"group_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"idp_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"distinguished_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"last_updated_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func DatasourceNutanixUserGroupV4Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.Client).IamAPI | ||
|
||
extID := d.Get("ext_id") | ||
resp, err := conn.UserGroupsAPIInstance.GetUserGroupById(utils.StringPtr(extID.(string))) | ||
if err != nil { | ||
var errordata map[string]interface{} | ||
e := json.Unmarshal([]byte(err.Error()), &errordata) | ||
if e != nil { | ||
return diag.FromErr(e) | ||
} | ||
data := errordata["data"].(map[string]interface{}) | ||
errorList := data["error"].([]interface{}) | ||
errorMessage := errorList[0].(map[string]interface{}) | ||
return diag.Errorf("error while fetching user groups: %v", errorMessage["message"]) | ||
} | ||
|
||
getResp := resp.Data.GetValue().(import1.UserGroup) | ||
|
||
if err := d.Set("group_type", flattenGroupType(getResp.GroupType)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("idp_id", getResp.IdpId); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("name", getResp.Name); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("distinguished_name", getResp.DistinguishedName); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if getResp.CreatedTime != nil { | ||
t := getResp.CreatedTime | ||
if err := d.Set("created_time", t.String()); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
if getResp.LastUpdatedTime != nil { | ||
t := getResp.LastUpdatedTime | ||
if err := d.Set("last_updated_time", t.String()); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
if err := d.Set("created_by", getResp.CreatedBy); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(*getResp.ExtId) | ||
return nil | ||
} | ||
|
||
func flattenGroupType(pr *import1.GroupType) string { | ||
if pr != nil { | ||
if *pr == import1.GroupType(2) { | ||
return "SAML" | ||
} | ||
if *pr == import1.GroupType(3) { | ||
return "LDAP" | ||
} | ||
} | ||
return "UNKNOWN" | ||
} |
173 changes: 173 additions & 0 deletions
173
nutanix/services/v2/iam/data_source_nutanix_user_groups_v2.go
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,173 @@ | ||
package iam | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
import1 "github.com/nutanix-core/ntnx-api-golang-sdk-internal/iam-go-client/v16/models/iam/v4/authn" | ||
conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix" | ||
"github.com/terraform-providers/terraform-provider-nutanix/utils" | ||
) | ||
|
||
func DatasourceNutanixUserGroupsV4() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: DatasourceNutanixUserGroupsV4Read, | ||
Schema: map[string]*schema.Schema{ | ||
"page": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"limit": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"filter": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"order_by": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"select": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"user_groups": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"ext_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"group_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"idp_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"distinguished_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"last_updated_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func DatasourceNutanixUserGroupsV4Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.Client).IamAPI | ||
|
||
// initialize query params | ||
var filter, orderBy, selects *string | ||
var page, limit *int | ||
|
||
if pagef, ok := d.GetOk("page"); ok { | ||
page = utils.IntPtr(pagef.(int)) | ||
} else { | ||
page = nil | ||
} | ||
if limitf, ok := d.GetOk("limit"); ok { | ||
limit = utils.IntPtr(limitf.(int)) | ||
} else { | ||
limit = nil | ||
} | ||
if filterf, ok := d.GetOk("filter"); ok { | ||
filter = utils.StringPtr(filterf.(string)) | ||
} else { | ||
filter = nil | ||
} | ||
if order, ok := d.GetOk("order_by"); ok { | ||
orderBy = utils.StringPtr(order.(string)) | ||
} else { | ||
orderBy = nil | ||
} | ||
if selectf, ok := d.GetOk("select"); ok { | ||
selects = utils.StringPtr(selectf.(string)) | ||
} else { | ||
selects = nil | ||
} | ||
|
||
resp, err := conn.UserGroupsAPIInstance.ListUserGroups(page, limit, filter, orderBy, selects) | ||
if err != nil { | ||
var errordata map[string]interface{} | ||
e := json.Unmarshal([]byte(err.Error()), &errordata) | ||
if e != nil { | ||
return diag.FromErr(e) | ||
} | ||
data := errordata["data"].(map[string]interface{}) | ||
errorList := data["error"].([]interface{}) | ||
errorMessage := errorList[0].(map[string]interface{}) | ||
return diag.Errorf("error while fetching user groups: %v", errorMessage["message"]) | ||
} | ||
getResp := resp.Data.GetValue().([]import1.UserGroup) | ||
if err := d.Set("user_groups", flattenUserGroupEntities(getResp)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(resource.UniqueId()) | ||
return nil | ||
} | ||
|
||
func flattenUserGroupEntities(pr []import1.UserGroup) []interface{} { | ||
if len(pr) > 0 { | ||
ugs := make([]interface{}, len(pr)) | ||
|
||
for k, v := range pr { | ||
ug := make(map[string]interface{}) | ||
|
||
if v.Name != nil { | ||
ug["name"] = v.Name | ||
} | ||
if v.DistinguishedName != nil { | ||
ug["distinguished_name"] = v.DistinguishedName | ||
} | ||
if v.IdpId != nil { | ||
ug["idp_id"] = v.IdpId | ||
} | ||
if v.GroupType != nil { | ||
ug["group_type"] = flattenGroupType(v.GroupType) | ||
} | ||
|
||
ug["created_by"] = v.CreatedBy | ||
|
||
if v.CreatedTime != nil { | ||
t := v.CreatedTime | ||
ug["created_time"] = t.String() | ||
} | ||
if v.LastUpdatedTime != nil { | ||
t := v.LastUpdatedTime | ||
ug["last_updated_time"] = t.String() | ||
} | ||
|
||
ugs[k] = ug | ||
} | ||
return ugs | ||
} | ||
return nil | ||
} |
Oops, something went wrong.