Skip to content

Commit

Permalink
CRUD for user groups
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimutant committed Mar 21, 2024
1 parent cea90c9 commit 39dc8a8
Show file tree
Hide file tree
Showing 5 changed files with 472 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nutanix/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ func Provider() *schema.Provider {
"nutanix_directory_services_v4": iam.DatasourceNutanixDirectoryServicesV2(),
"nutanix_saml_identity_provider_v4": iam.DatasourceNutanixSamlIDPv4(),
"nutanix_saml_identity_providers_v4": iam.DatasourceNutanixSamlIDPsV4(),
"nutanix_user_group_v4": iam.DatasourceNutanixUserGroupV4(),
"nutanix_user_groups_v4": iam.DatasourceNutanixUserGroupsV4(),
},
ResourcesMap: map[string]*schema.Resource{
"nutanix_virtual_machine": prism.ResourceNutanixVirtualMachine(),
Expand Down Expand Up @@ -289,6 +291,7 @@ func Provider() *schema.Provider {
"nutanix_address_groups_v4": networking.ResourceNutanixAddressGroupsV4(),
"nutanix_network_security_policy_v4": networking.ResourceNutanixNetworkSecurityPolicyV4(),
"nutanix_directory_services_v4": iam.ResourceNutanixDirectoryServicesV4(),
"nutanix_user_groups_v4": iam.ResourceNutanixUserGroupsV4(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
2 changes: 2 additions & 0 deletions nutanix/sdks/v4/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Client struct {
DirectoryServiceAPIInstance *api.DirectoryServicesApi
SamlIdentityAPIInstance *api.SAMLIdentityProvidersApi
UserGroupsAPIInstance *api.UserGroupsApi
}

func NewIamClient(credentials client.Credentials) (*Client, error) {
Expand All @@ -30,6 +31,7 @@ func NewIamClient(credentials client.Credentials) (*Client, error) {
f := &Client{
DirectoryServiceAPIInstance: api.NewDirectoryServicesApi(baseClient),
SamlIdentityAPIInstance: api.NewSAMLIdentityProvidersApi(baseClient),
UserGroupsAPIInstance: api.NewUserGroupsApi(baseClient),
}

return f, nil
Expand Down
116 changes: 116 additions & 0 deletions nutanix/services/v2/iam/data_source_nutanix_user_group_v2.go
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 nutanix/services/v2/iam/data_source_nutanix_user_groups_v2.go
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
}
Loading

0 comments on commit 39dc8a8

Please sign in to comment.