From ee7737dce97891dabb1839676644751581c48304 Mon Sep 17 00:00:00 2001 From: Haroon-Dweikat-Ntx Date: Thu, 19 Dec 2024 14:44:31 +0200 Subject: [PATCH] restore point info dev --- .../prismv2/data_source_restore_point_v2.go | 126 +++++++++++++++++ .../prismv2/data_source_restore_points_v2.go | 127 ++++++++++++++++++ .../prismv2/resource_nutanix_restore_pc_v2.go | 81 +++++------ 3 files changed, 294 insertions(+), 40 deletions(-) create mode 100644 nutanix/services/prismv2/data_source_restore_point_v2.go create mode 100644 nutanix/services/prismv2/data_source_restore_points_v2.go diff --git a/nutanix/services/prismv2/data_source_restore_point_v2.go b/nutanix/services/prismv2/data_source_restore_point_v2.go new file mode 100644 index 00000000..1824ad79 --- /dev/null +++ b/nutanix/services/prismv2/data_source_restore_point_v2.go @@ -0,0 +1,126 @@ +package prismv2 + +import ( + "context" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/nutanix/ntnx-api-golang-clients/prism-go-client/v4/models/prism/v4/config" + "github.com/nutanix/ntnx-api-golang-clients/prism-go-client/v4/models/prism/v4/management" + conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix" + "github.com/terraform-providers/terraform-provider-nutanix/utils" +) + +func DatasourceNutanixFetchRestorePointV2() *schema.Resource { + return &schema.Resource{ + ReadContext: DatasourceNutanixRestorePointV2Read, + Schema: map[string]*schema.Schema{ + "restore_source_ext_id": { + Type: schema.TypeString, + Required: true, + }, + "restorable_domain_manager_ext_id": { + Type: schema.TypeString, + Required: true, + }, + "ext_id": { + Type: schema.TypeString, + Required: true, + }, + "tenant_id": { + Type: schema.TypeString, + Computed: true, + }, + "links": schemaForLinks(), + "creation_time": { + Type: schema.TypeString, + Computed: true, + }, + "domain_manager": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "tenant_id": { + Type: schema.TypeString, + Computed: true, + }, + "ext_id": { + Type: schema.TypeString, + Computed: true, + }, + "config": schemaForPcConfig(), + "is_registered_with_hosting_cluster": { + Type: schema.TypeBool, + Computed: true, + }, + "network": schemaForPcNetwork(), + "hosting_cluster_ext_id": { + Type: schema.TypeString, + Computed: true, + }, + "should_enable_high_availability": { + Type: schema.TypeBool, + Computed: true, + }, + "node_ext_ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + }, + } +} + +func DatasourceNutanixRestorePointV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.Client).PrismAPI + + restoreSourceExtID := utils.StringPtr(d.Get("restore_source_ext_id").(string)) + restorableDomainManagerExtID := utils.StringPtr(d.Get("restorable_domain_manager_ext_id").(string)) + extID := utils.StringPtr(d.Get("ext_id").(string)) + + resp, err := conn.DomainManagerBackupsAPIInstance.GetRestorePointById(restoreSourceExtID, restorableDomainManagerExtID, extID) + + if err != nil { + return diag.Errorf("error while fetching Domain Manager Restore Point Detail: %s", err) + } + + restorePoint := resp.Data.GetValue().(management.RestorePoint) + + if err := d.Set("tenant_id", utils.StringValue(restorePoint.TenantId)); err != nil { + return diag.Errorf("error setting tenant_id: %s", err) + } + if err := d.Set("links", flattenLinks(restorePoint.Links)); err != nil { + return diag.Errorf("error setting links: %s", err) + } + if err := d.Set("creation_time", flattenTime(restorePoint.CreationTime)); err != nil { + return diag.Errorf("error setting creation_time: %s", err) + } + if err := d.Set("domain_manager", flattenDomainManager(restorePoint.DomainManager)); err != nil { + return diag.Errorf("error setting domain_manager: %s", err) + } + return nil +} + +func flattenDomainManager(domainManager *config.DomainManager) []map[string]interface{} { + if domainManager == nil { + return nil + } + + return []map[string]interface{}{ + { + "tenant_id": utils.StringValue(domainManager.TenantId), + "ext_id": utils.StringValue(domainManager.ExtId), + "config": flattenPCConfig(domainManager.Config), + "is_registered_with_hosting_cluster": utils.BoolValue(domainManager.IsRegisteredWithHostingCluster), + "network": flattenPCNetwork(domainManager.Network), + "hosting_cluster_ext_id": utils.StringValue(domainManager.HostingClusterExtId), + "should_enable_high_availability": utils.BoolValue(domainManager.ShouldEnableHighAvailability), + "node_ext_ids": domainManager.NodeExtIds, + }, + } +} diff --git a/nutanix/services/prismv2/data_source_restore_points_v2.go b/nutanix/services/prismv2/data_source_restore_points_v2.go new file mode 100644 index 00000000..b40da82c --- /dev/null +++ b/nutanix/services/prismv2/data_source_restore_points_v2.go @@ -0,0 +1,127 @@ +package prismv2 + +import ( + "context" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "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/prism-go-client/v4/models/prism/v4/management" + conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix" + "github.com/terraform-providers/terraform-provider-nutanix/utils" +) + +func DatasourceNutanixFetchRestorePointsV2() *schema.Resource { + return &schema.Resource{ + ReadContext: DatasourceNutanixRestorePointsV2Read, + Schema: map[string]*schema.Schema{ + "restore_source_ext_id": { + Type: schema.TypeString, + Required: true, + }, + "restorable_domain_manager_ext_id": { + Type: schema.TypeString, + Required: true, + }, + "page": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntAtLeast(0), + }, + "limit": { + Type: schema.TypeInt, + Optional: true, + Default: 50, + ValidateFunc: validation.IntBetween(1, 100), + }, + "filter": { + Type: schema.TypeString, + Optional: true, + }, + "order_by": { + Type: schema.TypeString, + Optional: true, + }, + "select": { + Type: schema.TypeString, + Optional: true, + }, + "restore_points": { + Type: schema.TypeList, + Computed: true, + Elem: DatasourceNutanixFetchRestorePointV2(), + }, + }, + } +} + +func DatasourceNutanixRestorePointsV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.Client).PrismAPI + + restoreSourceExtID := utils.StringPtr(d.Get("restore_source_ext_id").(string)) + restorableDomainManagerExtID := utils.StringPtr(d.Get("restorable_domain_manager_ext_id").(string)) + + var selects, orderBy, filter *string + var page, limit *int + + if selectf, ok := d.GetOk("select"); ok { + selects = utils.StringPtr(selectf.(string)) + } else { + selects = nil + } + if orderByf, ok := d.GetOk("order_by"); ok { + orderBy = utils.StringPtr(orderByf.(string)) + } else { + orderBy = nil + } + if filterf, ok := d.GetOk("filter"); ok { + filter = utils.StringPtr(filterf.(string)) + } else { + filter = nil + } + 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 + } + + resp, err := conn.DomainManagerBackupsAPIInstance.ListRestorePoints(restoreSourceExtID, restorableDomainManagerExtID, page, limit, filter, orderBy, selects) + + if err != nil { + return diag.Errorf("error while fetching Domain Manager Restore Point Detail: %s", err) + } + + if resp.Data == nil { + if err := d.Set("restore_points", []map[string]interface{}{}); err != nil { + return diag.Errorf("Error setting restore_points: %v", err) + } + return nil + } + + restorePoints := resp.Data.GetValue().([]management.RestorePoint) + + if err := d.Set("restore_points", flattenRestorePoints(restorePoints)); err != nil { + return diag.Errorf("Error setting restore_points: %v", err) + } + return nil +} + +func flattenRestorePoints(restorePoints []management.RestorePoint) []map[string]interface{} { + restorePointsList := make([]map[string]interface{}, 0) + for _, restorePoint := range restorePoints { + restorePointMap := map[string]interface{}{ + "tenant_id": utils.StringValue(restorePoint.TenantId), + "ext_id": utils.StringValue(restorePoint.ExtId), + "links": flattenLinks(restorePoint.Links), + "creation_time": flattenTime(restorePoint.CreationTime), + "domain_manager": flattenDomainManager(restorePoint.DomainManager), + } + restorePointsList = append(restorePointsList, restorePointMap) + } + return restorePointsList +} diff --git a/nutanix/services/prismv2/resource_nutanix_restore_pc_v2.go b/nutanix/services/prismv2/resource_nutanix_restore_pc_v2.go index e38f1576..31d67cb6 100644 --- a/nutanix/services/prismv2/resource_nutanix_restore_pc_v2.go +++ b/nutanix/services/prismv2/resource_nutanix_restore_pc_v2.go @@ -39,38 +39,48 @@ func ResourceNutanixRestorePcV2() *schema.Resource { MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "config": schemaForPcConfig(), + "tenant_id": { + Type: schema.TypeString, + Computed: true, + }, + "ext_id": { + Type: schema.TypeString, + Computed: true, + }, + "config": schemaForPcConfig(), + "is_registered_with_hosting_cluster": { + Type: schema.TypeBool, + Computed: true, + }, "network": schemaForPcNetwork(), + "hosting_cluster_ext_id": { + Type: schema.TypeString, + Computed: true, + }, "should_enable_high_availability": { Type: schema.TypeBool, - Optional: true, - Default: false, + Computed: true, + }, + "node_ext_ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, }, }, }, }, - // read schema "tenant_id": { Type: schema.TypeString, Computed: true, }, "links": schemaForLinks(), - "is_registered_with_hosting_cluster": { - Type: schema.TypeBool, - Computed: true, - }, - "hosting_cluster_ext_id": { + "creation_time": { Type: schema.TypeString, Computed: true, }, - "node_ext_ids": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - }, - }, }, } } @@ -79,7 +89,7 @@ func ResourceNutanixRestorePcCreate(ctx context.Context, d *schema.ResourceData, conn := meta.(*conns.Client).PrismAPI restoreSourceExtID := d.Get("restore_source_ext_id").(string) - restoreableDomainManagerExtID := d.Get("restorable_domain_manager_ext_id").(string) + restorableDomainManagerExtID := d.Get("restorable_domain_manager_ext_id").(string) restorePointExtID := d.Get("ext_id").(string) restorePcBody := config.NewDomainManager() @@ -98,7 +108,7 @@ func ResourceNutanixRestorePcCreate(ctx context.Context, d *schema.ResourceData, aJSON, _ := json.MarshalIndent(restoreSpec, "", " ") log.Printf("[DEBUG] Restore PC Body: %s", string(aJSON)) - resp, err := conn.DomainManagerBackupsAPIInstance.Restore(utils.StringPtr(restoreSourceExtID), utils.StringPtr(restoreableDomainManagerExtID), utils.StringPtr(restorePointExtID), restoreSpec) + resp, err := conn.DomainManagerBackupsAPIInstance.Restore(utils.StringPtr(restoreSourceExtID), utils.StringPtr(restorableDomainManagerExtID), utils.StringPtr(restorePointExtID), restoreSpec) if err != nil { return diag.Errorf("error while restoring Domain Manager: %s", err) } @@ -137,40 +147,31 @@ func ResourceNutanixRestorePcCreate(ctx context.Context, d *schema.ResourceData, func ResourceNutanixRestorePcRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.Client).PrismAPI - resp, err := conn.DomainManagerAPIInstance.GetDomainManagerById(utils.StringPtr(d.Id())) + restoreSourceExtID := utils.StringPtr(d.Get("restore_source_ext_id").(string)) + restorableDomainManagerExtID := utils.StringPtr(d.Get("restorable_domain_manager_ext_id").(string)) + + resp, err := conn.DomainManagerBackupsAPIInstance.GetRestorePointById(restoreSourceExtID, restorableDomainManagerExtID, utils.StringPtr(d.Id())) if err != nil { - return diag.Errorf("error while fetching Domain Manager: %s", err) + return diag.Errorf("error while fetching Domain Manager Restore Point Detail: %s", err) } - deployPcBody := resp.Data.GetValue().(config.DomainManager) + restorePoint := resp.Data.GetValue().(management.RestorePoint) - if err := d.Set("tenant_id", utils.StringValue(deployPcBody.TenantId)); err != nil { + if err := d.Set("tenant_id", utils.StringValue(restorePoint.TenantId)); err != nil { return diag.Errorf("error setting tenant_id: %s", err) } - if err := d.Set("ext_id", utils.StringValue(deployPcBody.ExtId)); err != nil { + if err := d.Set("ext_id", utils.StringValue(restorePoint.ExtId)); err != nil { return diag.Errorf("error setting ext_id: %s", err) } - if err := d.Set("links", flattenLinks(deployPcBody.Links)); err != nil { + if err := d.Set("links", flattenLinks(restorePoint.Links)); err != nil { return diag.Errorf("error setting links: %s", err) } - if err := d.Set("config", flattenPCConfig(deployPcBody.Config)); err != nil { - return diag.Errorf("error setting config: %s", err) - } - if err := d.Set("is_registered_with_hosting_cluster", utils.BoolValue(deployPcBody.IsRegisteredWithHostingCluster)); err != nil { - return diag.Errorf("error setting is_registered_with_hosting_cluster: %s", err) - } - if err := d.Set("network", flattenPCNetwork(deployPcBody.Network)); err != nil { - return diag.Errorf("error setting network: %s", err) - } - if err := d.Set("hosting_cluster_ext_id", utils.StringValue(deployPcBody.HostingClusterExtId)); err != nil { - return diag.Errorf("error setting hosting_cluster_ext_id: %s", err) - } - if err := d.Set("should_enable_high_availability", utils.BoolValue(deployPcBody.ShouldEnableHighAvailability)); err != nil { - return diag.Errorf("error setting should_enable_high_availability: %s", err) + if err := d.Set("creation_time", flattenTime(restorePoint.CreationTime)); err != nil { + return diag.Errorf("error setting creation_time: %s", err) } - if err := d.Set("node_ext_ids", deployPcBody.NodeExtIds); err != nil { - return diag.Errorf("error setting node_ext_ids: %s", err) + if err := d.Set("domain_manager", flattenDomainManager(restorePoint.DomainManager)); err != nil { + return diag.Errorf("error setting domain_manager: %s", err) } return nil }