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
56d3d0f
commit d6be879
Showing
7 changed files
with
1,313 additions
and
3 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
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
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,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 | ||
} |
137 changes: 137 additions & 0 deletions
137
nutanix/services/v2/clusters/data_source_nutanix_cluster_entities_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,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 | ||
} |
Oops, something went wrong.