Skip to content

Commit

Permalink
datadource for clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimutant committed Jan 4, 2024
1 parent 56d3d0f commit d6be879
Show file tree
Hide file tree
Showing 7 changed files with 1,313 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/mitchellh/gox v1.0.1
github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4 v4.0.1-beta.1 // indirect
github.com/nutanix/ntnx-api-golang-clients/networking-go-client/v4 v4.0.1-beta.1
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96d
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=
github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4 v4.0.1-beta.1 h1:JfxB9XZK/dUFWHNANH6GPpWNoXQ9LAH6Y318CR6fxuo=
github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4 v4.0.1-beta.1/go.mod h1:sd4Fnk6MVfEDVY+8WyRoQTmLhi2SgZ3riySWErVHf8E=
github.com/nutanix/ntnx-api-golang-clients/networking-go-client/v4 v4.0.1-beta.1 h1:EBTbuV4zFgv+MY7a5MYIELStXPMMds1aULHllA5xdFM=
github.com/nutanix/ntnx-api-golang-clients/networking-go-client/v4 v4.0.1-beta.1/go.mod h1:+eZgV1+xL/r84qmuFSVt5R8OFRO70rEz92jOnVgJNco=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
Expand Down
12 changes: 9 additions & 3 deletions nutanix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package nutanix
import (
"fmt"

"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v3/karbon"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v4/networking"

"github.com/terraform-providers/terraform-provider-nutanix/nutanix/client"
era "github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v3/era"
foundation_central "github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v3/fc"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v3/foundation"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v3/karbon"
v3 "github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v3/prism"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v4/clusters"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v4/networking"
)

// Version represents api version
Expand Down Expand Up @@ -77,6 +77,10 @@ func (c *Config) Client() (*Client, error) {
if err != nil {
return nil, err
}
clustersClient, err := clusters.NewClustersClient(configCreds)
if err != nil {
return nil, err
}
return &Client{
WaitTimeout: c.WaitTimeout,
API: v3Client,
Expand All @@ -85,6 +89,7 @@ func (c *Config) Client() (*Client, error) {
FoundationCentral: fcClient,
Era: eraClient,
NetworkingAPI: networkingClient,
ClusterAPI: clustersClient,
}, nil
}

Expand All @@ -97,4 +102,5 @@ type Client struct {
FoundationCentral *foundation_central.Client
Era *era.Client
NetworkingAPI *networking.Client
ClusterAPI *clusters.Client
}
3 changes: 3 additions & 0 deletions nutanix/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/services/v1/ndb"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/services/v1/nke"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/services/v1/prism"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/services/v2/clusters"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/services/v2/networking"
)

Expand Down Expand Up @@ -227,6 +228,8 @@ func Provider() *schema.Provider {
"nutanix_floating_ips_v4": networking.DatasourceNutanixFloatingIPsV4(),
"nutanix_pbr_v4": networking.DatasourceNutanixPbrV4(),
"nutanix_pbrs_v4": networking.DatasourceNutanixPbrsV4(),
"nutanix_cluster_v4": clusters.DatasourceNutanixClusterEntityV2(),
"nutanix_clusters_v4": clusters.DatasourceNutanixClusterEntitiesV2(),
},
ResourcesMap: map[string]*schema.Resource{
"nutanix_virtual_machine": prism.ResourceNutanixVirtualMachine(),
Expand Down
34 changes: 34 additions & 0 deletions nutanix/sdks/v4/clusters/clusters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package clusters

import (
"github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4/api"
network "github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4/client"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/client"
)

type Client struct {
ClusterEntityAPI *api.ClusterApi
}

func NewClustersClient(credentials client.Credentials) (*Client, error) {
var baseClient *network.ApiClient

// check if all required fields are present. Else create an empty client
if credentials.Username != "" && credentials.Password != "" && credentials.Endpoint != "" {
pcClient := network.NewApiClient()

pcClient.Host = credentials.Endpoint
pcClient.Password = credentials.Password
pcClient.Username = credentials.Username
pcClient.Port = 9440
pcClient.VerifySSL = false

baseClient = pcClient
}

f := &Client{
ClusterEntityAPI: api.NewClusterApi(baseClient),
}

return f, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package clusters

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/ntnx-api-golang-clients/clustermgmt-go-client/v4/models/clustermgmt/v4/config"
conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

func DatasourceNutanixClusterEntitiesV2() *schema.Resource {
return &schema.Resource{
ReadContext: DatasourceNutanixClusterEntitiesV2Read,
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,
},
"apply": {
Type: schema.TypeString,
Optional: true,
},
"select": {
Type: schema.TypeString,
Optional: true,
},
"cluster_entities": {
Type: schema.TypeList,
Computed: true,
Elem: DatasourceNutanixClusterEntityV2(),
},
},
}
}

func DatasourceNutanixClusterEntitiesV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.Client).ClusterAPI

// initialize query params
var filter, orderBy, apply, selectQ *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 applyf, ok := d.GetOk("apply"); ok {
apply = utils.StringPtr(applyf.(string))
} else {
apply = nil
}
if selectQy, ok := d.GetOk("apply"); ok {
selectQ = utils.StringPtr(selectQy.(string))
} else {
selectQ = nil
}

resp, err := conn.ClusterEntityAPI.GetClusters(page, limit, filter, orderBy, apply, selectQ)
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 cluster entities : %v", errorMessage["message"])
}

getResp := resp.Data.GetValue().([]import1.Cluster)

if err := d.Set("cluster_entities", flattenClusterEntities(getResp)); err != nil {
return diag.FromErr(err)
}

d.SetId(resource.UniqueId())
return nil
}

func flattenClusterEntities(pr []import1.Cluster) []interface{} {
if len(pr) > 0 {
clsList := make([]interface{}, len(pr))

for k, v := range pr {
cls := make(map[string]interface{})

cls["ext_id"] = v.ExtId
cls["tenant_id"] = v.TenantId
cls["links"] = flattenLinks(v.Links)
cls["name"] = v.Name
cls["nodes"] = flattenNodeReference(v.Nodes)
cls["network"] = flattenClusterNetworkReference(v.Network)
cls["config"] = flattenClusterConfigReference(v.Config)
cls["upgrade_status"] = flattenUpgradeStatus(v.UpgradeStatus)
cls["vm_count"] = v.VmCount
cls["inefficient_vm_count"] = v.InefficientVmCount
cls["container_name"] = v.ContainerName

clsList[k] = cls
}
return clsList
}
return nil
}
Loading

0 comments on commit d6be879

Please sign in to comment.