diff --git a/nutanix/services/prismv2/data_source_backup_target_v2.go b/nutanix/services/prismv2/data_source_backup_target_v2.go new file mode 100644 index 00000000..5e5563b3 --- /dev/null +++ b/nutanix/services/prismv2/data_source_backup_target_v2.go @@ -0,0 +1,292 @@ +package prismv2 + +import ( + "context" + "time" + + "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/management" + conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix" + "github.com/terraform-providers/terraform-provider-nutanix/utils" +) + +func DatasourceNutanixBackupTargetV2() *schema.Resource { + return &schema.Resource{ + ReadContext: DatasourceNutanixBackupTargetV2Read, + Schema: map[string]*schema.Schema{ + "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(), + "location": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "cluster_location": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "config": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "ext_id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + }, + }, + "object_store_location": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "provider_config": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "bucket_name": { + Type: schema.TypeString, + Computed: true, + }, + "region": { + Type: schema.TypeString, + Computed: true, + }, + "credentials": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "access_key_id": { + Type: schema.TypeString, + Computed: true, + }, + "secret_access_key": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + }, + }, + "backup_policy": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "rpo_in_minutes": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + "last_sync_time": { + Type: schema.TypeString, + Computed: true, + }, + "is_backup_paused": { + Type: schema.TypeBool, + Computed: true, + }, + "backup_pause_reason": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func DatasourceNutanixBackupTargetV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.Client).PrismAPI + + domainManagerExtID := d.Get("domain_manager_ext_id").(string) + backupTargetExtID := d.Get("ext_id").(string) + + resp, err := conn.DomainManagerBackupsAPIInstance.GetBackupTargetById(utils.StringPtr(domainManagerExtID), utils.StringPtr(backupTargetExtID), nil) + + if err != nil { + return diag.Errorf("error while fetching Backup Target: %s", err) + } + + backupTarget := resp.Data.GetValue().(management.BackupTarget) + + if err := d.Set("tenant_id", backupTarget.TenantId); err != nil { + return diag.Errorf("error setting tenant_id: %s", err) + } + if err := d.Set("ext_id", backupTarget.ExtId); err != nil { + return diag.Errorf("error setting ext_id: %s", err) + } + if err := d.Set("links", flattenLinks(backupTarget.Links)); err != nil { + return diag.Errorf("error setting links: %s", err) + } + if err := d.Set("last_sync_time", flattenTime(backupTarget.LastSyncTime)); err != nil { + return diag.Errorf("error setting last_sync_time: %s", err) + } + if err := d.Set("is_backup_paused", backupTarget.IsBackupPaused); err != nil { + return diag.Errorf("error setting is_backup_paused: %s", err) + } + if err := d.Set("backup_pause_reason", backupTarget.BackupPauseReason); err != nil { + return diag.Errorf("error setting backup_pause_reason: %s", err) + } + if err := d.Set("location", flattenBackupTargetLocation(backupTarget.Location)); err != nil { + return diag.Errorf("error setting location: %s", err) + } + + return nil +} + +func flattenTime(time *time.Time) *string { + if time == nil { + return nil + } + return utils.StringPtr(time.String()) +} + +func flattenClusterLocation(location management.ClusterLocation) []map[string]interface{} { + if &location == nil { + return nil + } + + clusterLocation := make([]map[string]interface{}, 0) + clusterLocationMap := make(map[string]interface{}) + clusterLocationMap["config"] = flattenClusterReference(location.Config) + + clusterLocation = append(clusterLocation, clusterLocationMap) + + return clusterLocation +} + +func flattenClusterReference(clusterReference *management.ClusterReference) []map[string]interface{} { + if clusterReference == nil { + return nil + } + + clusterRef := make([]map[string]interface{}, 0) + clusterRefMap := make(map[string]interface{}) + clusterRefMap["ext_id"] = clusterReference.ExtId + clusterRefMap["name"] = clusterReference.Name + + clusterRef = append(clusterRef, clusterRefMap) + + return clusterRef +} + +func flattenBackupTargetLocation(location *management.OneOfBackupTargetLocation) []map[string]interface{} { + if location == nil { + return nil + } + + backupTargetLocation := make([]map[string]interface{}, 0) + + if utils.StringValue(location.ObjectType_) == clustersLocationObjectType { + clusterLocation := location.GetValue().(management.ClusterLocation) + + clusterLocationMap := make(map[string]interface{}) + clusterLocationMap["cluster_location"] = flattenClusterLocation(clusterLocation) + backupTargetLocation = append(backupTargetLocation, clusterLocationMap) + return backupTargetLocation + } + + if utils.StringValue(location.ObjectType_) == objectStoreLocationObjectType { + objectStoreLocation := location.GetValue().(management.ObjectStoreLocation) + + objectStoreLocationMap := make(map[string]interface{}) + objectStoreLocationMap["object_store_location"] = flattenObjectStoreLocation(objectStoreLocation) + backupTargetLocation = append(backupTargetLocation, objectStoreLocationMap) + return backupTargetLocation + } + + return backupTargetLocation +} + +func flattenObjectStoreLocation(objectStoreLocation management.ObjectStoreLocation) []map[string]interface{} { + if &objectStoreLocation == nil { + return nil + } + + objectStoreLocationMap := make(map[string]interface{}) + objectStoreLocationMap["provider_config"] = flattenProviderConfig(objectStoreLocation.ProviderConfig) + objectStoreLocationMap["backup_policy"] = flattenBackupPolicy(objectStoreLocation.BackupPolicy) + + objectStoreLocationList := make([]map[string]interface{}, 0) + objectStoreLocationList = append(objectStoreLocationList, objectStoreLocationMap) + + return objectStoreLocationList +} + +func flattenProviderConfig(providerConfig *management.AWSS3Config) []map[string]interface{} { + if providerConfig == nil { + return nil + } + + providerConfigMap := make(map[string]interface{}) + providerConfigMap["bucket_name"] = providerConfig.BucketName + providerConfigMap["region"] = providerConfig.Region + providerConfigMap["credentials"] = flattenAccessKeyCredentials(providerConfig.Credentials) + + providerConfigList := make([]map[string]interface{}, 0) + providerConfigList = append(providerConfigList, providerConfigMap) + + return providerConfigList +} + +func flattenAccessKeyCredentials(credentials *management.AccessKeyCredentials) []map[string]interface{} { + if credentials == nil { + return nil + } + + credentialsMap := make(map[string]interface{}) + credentialsMap["access_key_id"] = credentials.AccessKeyId + credentialsMap["secret_access_key"] = credentials.SecretAccessKey + + credentialsList := make([]map[string]interface{}, 0) + credentialsList = append(credentialsList, credentialsMap) + + return credentialsList +} + +func flattenBackupPolicy(policy *management.BackupPolicy) []map[string]interface{} { + if policy == nil { + return nil + } + + backupPolicyMap := make(map[string]interface{}) + backupPolicyMap["rpo_in_minutes"] = policy.RpoInMinutes + + backupPolicyList := make([]map[string]interface{}, 0) + backupPolicyList = append(backupPolicyList, backupPolicyMap) + + return backupPolicyList +} diff --git a/nutanix/services/prismv2/data_source_backup_targets_v2.go b/nutanix/services/prismv2/data_source_backup_targets_v2.go new file mode 100644 index 00000000..802153ef --- /dev/null +++ b/nutanix/services/prismv2/data_source_backup_targets_v2.go @@ -0,0 +1,71 @@ +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/management" + conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix" + "github.com/terraform-providers/terraform-provider-nutanix/utils" +) + +func DatasourceNutanixBackupTargetsV2() *schema.Resource { + return &schema.Resource{ + ReadContext: DatasourceNutanixBackupTargetsV2Read, + Schema: map[string]*schema.Schema{ + "domain_manager_ext_id": { + Type: schema.TypeString, + Required: true, + }, + "backup_targets": { + Type: schema.TypeList, + Computed: true, + Elem: DatasourceNutanixBackupTargetV2(), + }, + }, + } +} + +func DatasourceNutanixBackupTargetsV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.Client).PrismAPI + + domainManagerExtID := d.Get("domain_manager_ext_id").(string) + + resp, err := conn.DomainManagerBackupsAPIInstance.ListBackupTargets(utils.StringPtr(domainManagerExtID)) + + if err != nil { + return diag.Errorf("error while Listing Backup Targets for : %s err: %s", domainManagerExtID, err) + } + + if resp.Data == nil { + if err := d.Set("backup_targets", []interface{}{}); err != nil { + return diag.Errorf("error setting backup_targets: %s", err) + } + } + + if err := d.Set("backup_targets", flattenBackupTargets(resp.Data.GetValue().([]management.BackupTarget))); err != nil { + return diag.Errorf("error setting backup_targets: %s", err) + } + return nil +} + +func flattenBackupTargets(backupTargets []management.BackupTarget) []map[string]interface{} { + if backupTargets == nil || len(backupTargets) == 0 { + return []map[string]interface{}{} + } + + result := make([]map[string]interface{}, 0) + for _, backupTarget := range backupTargets { + backupTargetMap := map[string]interface{}{ + "ext_id": backupTarget.ExtId, + "tenant_id": backupTarget.TenantId, + "links": flattenLinks(backupTarget.Links), + "location": flattenBackupTargetLocation(backupTarget.Location), + "last_sync_time": flattenTime(backupTarget.LastSyncTime), + "is_backup_paused": backupTarget.IsBackupPaused, + "backup_pause_reason": backupTarget.BackupPauseReason, + } + result = append(result, backupTargetMap) + } + return result +} diff --git a/nutanix/services/prismv2/resource_nutanix_backup_target_v2.go b/nutanix/services/prismv2/resource_nutanix_backup_target_v2.go index 3d69d850..36cdc883 100644 --- a/nutanix/services/prismv2/resource_nutanix_backup_target_v2.go +++ b/nutanix/services/prismv2/resource_nutanix_backup_target_v2.go @@ -3,15 +3,13 @@ package prismv2 import ( "context" "encoding/json" - "log" - "time" - "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" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "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" + "log" conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix" "github.com/terraform-providers/terraform-provider-nutanix/utils" @@ -59,6 +57,10 @@ func ResourceNutanixBackupTargetV2() *schema.Resource { Type: schema.TypeString, Required: true, }, + "name": { + Type: schema.TypeString, + Computed: true, + }, }, }, }, @@ -208,7 +210,7 @@ func ResourceNutanixBackupTargetV2Create(ctx context.Context, d *schema.Resource taskconn := meta.(*conns.Client).PrismAPI // Wait for the cluster to be available stateConf := &resource.StateChangeConf{ - Pending: []string{"RUNNING", "QUEUED"}, + Pending: []string{"PENDING", "RUNNING", "QUEUED"}, Target: []string{"SUCCEEDED"}, Refresh: taskStateRefreshPrismTaskGroupFunc(ctx, taskconn, utils.StringValue(taskUUID)), Timeout: d.Timeout(schema.TimeoutCreate), @@ -234,7 +236,6 @@ func ResourceNutanixBackupTargetV2Create(ctx context.Context, d *schema.Resource } func ResourceNutanixBackupTargetV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*conns.Client).PrismAPI domainManagerExtID := d.Get("domain_manager_ext_id").(string) @@ -273,7 +274,6 @@ func ResourceNutanixBackupTargetV2Read(ctx context.Context, d *schema.ResourceDa } func ResourceNutanixBackupTargetV2Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - conn := meta.(*conns.Client).PrismAPI domainManagerExtID := d.Get("domain_manager_ext_id").(string) @@ -346,7 +346,7 @@ func ResourceNutanixBackupTargetV2Update(ctx context.Context, d *schema.Resource taskconn := meta.(*conns.Client).PrismAPI // Wait for the backup target to be updated stateConf := &resource.StateChangeConf{ - Pending: []string{"RUNNING", "QUEUED"}, + Pending: []string{"PENDING", "RUNNING", "QUEUED"}, Target: []string{"SUCCEEDED"}, Refresh: taskStateRefreshPrismTaskGroupFunc(ctx, taskconn, utils.StringValue(taskUUID)), } @@ -394,7 +394,7 @@ func ResourceNutanixBackupTargetV2Delete(ctx context.Context, d *schema.Resource taskconn := meta.(*conns.Client).PrismAPI // Wait for the backup target to be deleted stateConf := &resource.StateChangeConf{ - Pending: []string{"RUNNING", "QUEUED"}, + Pending: []string{"PENDING", "RUNNING", "QUEUED"}, Target: []string{"SUCCEEDED"}, Refresh: taskStateRefreshPrismTaskGroupFunc(ctx, taskconn, utils.StringValue(taskUUID)), } @@ -463,129 +463,3 @@ func expandBackupPolicy(policy interface{}) *management.BackupPolicy { return &backupPolicy } - -// f - -func flattenTime(time *time.Time) *string { - if time == nil { - return nil - } - return utils.StringPtr(time.String()) -} - -func flattenClusterLocation(location management.ClusterLocation) []map[string]interface{} { - if &location == nil { - return nil - } - - clusterLocation := make([]map[string]interface{}, 0) - clusterLocationMap := make(map[string]interface{}) - clusterLocationMap["config"] = flattenClusterReference(location.Config) - - clusterLocation = append(clusterLocation, clusterLocationMap) - - return clusterLocation -} - -func flattenClusterReference(clusterReference *management.ClusterReference) []map[string]interface{} { - if clusterReference == nil { - return nil - } - - clusterRef := make([]map[string]interface{}, 0) - clusterRefMap := make(map[string]interface{}) - clusterRefMap["ext_id"] = clusterReference.ExtId - clusterRefMap["name"] = clusterReference.Name - - clusterRef = append(clusterRef, clusterRefMap) - - return clusterRef -} - -func flattenBackupTargetLocation(location *management.OneOfBackupTargetLocation) []map[string]interface{} { - if location == nil { - return nil - } - - backupTargetLocation := make([]map[string]interface{}, 0) - - if utils.StringValue(location.ObjectType_) == clustersLocationObjectType { - clusterLocation := location.GetValue().(management.ClusterLocation) - - clusterLocationMap := make(map[string]interface{}) - clusterLocationMap["cluster_location"] = flattenClusterLocation(clusterLocation) - backupTargetLocation = append(backupTargetLocation, clusterLocationMap) - return backupTargetLocation - } - - if utils.StringValue(location.ObjectType_) == objectStoreLocationObjectType { - objectStoreLocation := location.GetValue().(management.ObjectStoreLocation) - - objectStoreLocationMap := make(map[string]interface{}) - objectStoreLocationMap["object_store_location"] = flattenObjectStoreLocation(objectStoreLocation) - backupTargetLocation = append(backupTargetLocation, objectStoreLocationMap) - return backupTargetLocation - } - - return backupTargetLocation -} - -func flattenObjectStoreLocation(objectStoreLocation management.ObjectStoreLocation) []map[string]interface{} { - if &objectStoreLocation == nil { - return nil - } - - objectStoreLocationMap := make(map[string]interface{}) - objectStoreLocationMap["provider_config"] = flattenProviderConfig(objectStoreLocation.ProviderConfig) - objectStoreLocationMap["backup_policy"] = flattenBackupPolicy(objectStoreLocation.BackupPolicy) - - objectStoreLocationList := make([]map[string]interface{}, 0) - objectStoreLocationList = append(objectStoreLocationList, objectStoreLocationMap) - - return objectStoreLocationList -} - -func flattenProviderConfig(providerConfig *management.AWSS3Config) []map[string]interface{} { - if providerConfig == nil { - return nil - } - - providerConfigMap := make(map[string]interface{}) - providerConfigMap["bucket_name"] = providerConfig.BucketName - providerConfigMap["region"] = providerConfig.Region - providerConfigMap["credentials"] = flattenAccessKeyCredentials(providerConfig.Credentials) - - providerConfigList := make([]map[string]interface{}, 0) - providerConfigList = append(providerConfigList, providerConfigMap) - - return providerConfigList -} - -func flattenAccessKeyCredentials(credentials *management.AccessKeyCredentials) []map[string]interface{} { - if credentials == nil { - return nil - } - - credentialsMap := make(map[string]interface{}) - credentialsMap["access_key_id"] = credentials.AccessKeyId - credentialsMap["secret_access_key"] = credentials.SecretAccessKey - - credentialsList := make([]map[string]interface{}, 0) - credentialsList = append(credentialsList, credentialsMap) - - return credentialsList -} - -func flattenBackupPolicy(policy *management.BackupPolicy) []map[string]interface{} { - if policy == nil { - return nil - } - - backupPolicyMap := make(map[string]interface{}) - backupPolicyMap["rpo_in_minutes"] = policy.RpoInMinutes - - backupPolicyList := make([]map[string]interface{}, 0) - backupPolicyList = append(backupPolicyList, backupPolicyMap) - - return backupPolicyList -} diff --git a/nutanix/services/prismv2/resource_nutanix_restore_source_v2.go b/nutanix/services/prismv2/resource_nutanix_restore_source_v2.go new file mode 100644 index 00000000..d02702d0 --- /dev/null +++ b/nutanix/services/prismv2/resource_nutanix_restore_source_v2.go @@ -0,0 +1,327 @@ +package prismv2 + +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" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "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" + "log" + + conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix" + "github.com/terraform-providers/terraform-provider-nutanix/utils" +) + +func ResourceNutanixRestoreSourceV2() *schema.Resource { + return &schema.Resource{ + CreateContext: ResourceNutanixRestoreSourceV2Create, + ReadContext: ResourceNutanixRestoreSourceV2Read, + UpdateContext: ResourceNutanixRestoreSourceV2Update, + DeleteContext: ResourceNutanixRestoreSourceV2Delete, + Schema: map[string]*schema.Schema{ + "location": { + Type: schema.TypeList, + MaxItems: 1, + Required: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "cluster_location": { + Type: schema.TypeList, + Optional: true, + ExactlyOneOf: exactlyOneOfLocation, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "config": { + Type: schema.TypeList, + Required: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "ext_id": { + Type: schema.TypeString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + }, + }, + "object_store_location": { + Type: schema.TypeList, + Optional: true, + ExactlyOneOf: exactlyOneOfLocation, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "provider_config": { + Type: schema.TypeList, + Required: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "bucket_name": { + Type: schema.TypeString, + Required: true, + }, + "region": { + Type: schema.TypeString, + Optional: true, + Default: "us-east-1", + }, + "credentials": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "access_key_id": { + Type: schema.TypeString, + Required: true, + }, + "secret_access_key": { + Type: schema.TypeString, + Required: true, + }, + }, + }, + }, + }, + }, + }, + "backup_policy": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "rpo_in_minutes": { + Type: schema.TypeInt, + Required: true, + ValidateFunc: validation.IntBetween(60, 1440), + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + // computed attributes for read + "tenant_id": { + Type: schema.TypeString, + Computed: true, + }, + "ext_id": { + Type: schema.TypeString, + Computed: true, + }, + "links": schemaForLinks(), + "last_sync_time": { + Type: schema.TypeString, + Computed: true, + }, + "is_backup_paused": { + Type: schema.TypeBool, + Computed: true, + }, + "backup_pause_reason": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func ResourceNutanixRestoreSourceV2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.Client).PrismAPI + + body := management.RestoreSource{} + + oneOfRestoreSourceLocation := management.NewOneOfRestoreSourceLocation() + locationI := d.Get("location").([]interface{}) + location := locationI[0].(map[string]interface{}) + + if location["cluster_location"] != nil && len(location["cluster_location"].([]interface{})) > 0 { + clusterLocation := location["cluster_location"].([]interface{})[0].(map[string]interface{}) + clusterConfig := clusterLocation["config"].([]interface{})[0].(map[string]interface{}) + + clusterConfigBody := management.NewClusterLocation() + clusterRef := management.NewClusterReference() + + clusterRef.ExtId = utils.StringPtr(clusterConfig["ext_id"].(string)) + + clusterConfigBody.Config = clusterRef + + err := oneOfRestoreSourceLocation.SetValue(*clusterConfigBody) + if err != nil { + return diag.Errorf("error while setting cluster location : %v", err) + } + } else if location["object_store_location"] != nil && len(location["object_store_location"].([]interface{})) > 0 { + objectStoreLocation := location["object_store_location"].([]interface{})[0].(map[string]interface{}) + providerConfig := objectStoreLocation["provider_config"] + backupPolicy := objectStoreLocation["backup_policy"] + + objectStoreLocationBody := management.NewObjectStoreLocation() + + objectStoreLocationBody.ProviderConfig = expandProviderConfig(providerConfig) + objectStoreLocationBody.BackupPolicy = expandBackupPolicy(backupPolicy) + + err := oneOfRestoreSourceLocation.SetValue(*objectStoreLocationBody) + if err != nil { + return diag.Errorf("error while setting object store location : %v", err) + } + } + + body.Location = oneOfRestoreSourceLocation + + aJSON, _ := json.MarshalIndent(body, "", " ") + log.Printf("[DEBUG] Restore Source Create Body: %s", string(aJSON)) + + resp, err := conn.DomainManagerBackupsAPIInstance.CreateRestoreSource(&body) + + if err != nil { + return diag.Errorf("error while Creating Restore Source: %s", err) + } + + TaskRef := resp.Data.GetValue().(config.TaskReference) + taskUUID := TaskRef.ExtId + + taskconn := meta.(*conns.Client).PrismAPI + // Wait for the cluster to be available + stateConf := &resource.StateChangeConf{ + Pending: []string{"PENDING", "RUNNING", "QUEUED"}, + Target: []string{"SUCCEEDED"}, + Refresh: taskStateRefreshPrismTaskGroupFunc(ctx, taskconn, utils.StringValue(taskUUID)), + Timeout: d.Timeout(schema.TimeoutCreate), + } + + if _, err = stateConf.WaitForStateContext(ctx); err != nil { + return diag.Errorf("error waiting for Restore Source to be created: %s", err) + } + + resourceUUID, err := taskconn.TaskRefAPI.GetTaskById(taskUUID, nil) + if err != nil { + return diag.Errorf("error while fetching Restore Source Task Details: %s", err) + } + + rUUID := resourceUUID.Data.GetValue().(config.Task) + aJSON, _ = json.MarshalIndent(rUUID, "", " ") + log.Printf("[DEBUG] Create Restore Source Task Details: %s", string(aJSON)) + + uuid := rUUID.EntitiesAffected[0].ExtId + d.SetId(*uuid) + + return ResourceNutanixRestoreSourceV2Read(ctx, d, meta) +} + +func ResourceNutanixRestoreSourceV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.Client).PrismAPI + + resp, err := conn.DomainManagerBackupsAPIInstance.GetRestoreSourceById(utils.StringPtr(d.Id())) + + if err != nil { + return diag.Errorf("error while fetching Restore Source: %s", err) + } + + restoreSource := resp.Data.GetValue().(management.RestoreSource) + + if err := d.Set("tenant_id", restoreSource.TenantId); err != nil { + return diag.Errorf("error setting tenant_id: %s", err) + } + if err := d.Set("ext_id", restoreSource.ExtId); err != nil { + return diag.Errorf("error setting ext_id: %s", err) + } + if err := d.Set("links", flattenLinks(restoreSource.Links)); err != nil { + return diag.Errorf("error setting links: %s", err) + } + if err := d.Set("location", flattenRestoreSourceLocation(restoreSource.Location)); err != nil { + return diag.Errorf("error setting location: %s", err) + } + + return nil +} + +func ResourceNutanixRestoreSourceV2Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + return ResourceNutanixRestoreSourceV2Read(ctx, d, meta) +} + +func ResourceNutanixRestoreSourceV2Delete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.Client).PrismAPI + + readResp, err := conn.DomainManagerBackupsAPIInstance.GetRestoreSourceById(utils.StringPtr(d.Id())) + if err != nil { + return diag.Errorf("error while fetching Restore Source: %s", err) + } + + // extract the etag from the read response + args := make(map[string]interface{}) + eTag := conn.DomainManagerBackupsAPIInstance.ApiClient.GetEtag(readResp) + args["If-Match"] = utils.StringPtr(eTag) + + resp, err := conn.DomainManagerBackupsAPIInstance.DeleteRestoreSourceById(utils.StringPtr(d.Id()), args) + + if err != nil { + return diag.Errorf("error while deleting Restore Source: %s", err) + } + + TaskRef := resp.Data.GetValue().(config.TaskReference) + taskUUID := TaskRef.ExtId + + taskconn := meta.(*conns.Client).PrismAPI + // Wait for the backup target to be deleted + stateConf := &resource.StateChangeConf{ + Pending: []string{"PENDING", "RUNNING", "QUEUED"}, + Target: []string{"SUCCEEDED"}, + Refresh: taskStateRefreshPrismTaskGroupFunc(ctx, taskconn, utils.StringValue(taskUUID)), + } + + if _, err = stateConf.WaitForStateContext(ctx); err != nil { + return diag.Errorf("error waiting for Backup Target to be deleted: %s", err) + } + + resourceUUID, err := taskconn.TaskRefAPI.GetTaskById(taskUUID, nil) + if err != nil { + return diag.Errorf("error while fetching Backup Target Task Details: %s", err) + } + + rUUID := resourceUUID.Data.GetValue().(config.Task) + + aJSON, _ := json.MarshalIndent(rUUID, "", " ") + log.Printf("[DEBUG] Delete Backup Target Task Details: %s", string(aJSON)) + + return nil +} + +func flattenRestoreSourceLocation(location *management.OneOfRestoreSourceLocation) []map[string]interface{} { + if location == nil { + return nil + } + + restoreSourceLocation := make([]map[string]interface{}, 0) + + if utils.StringValue(location.ObjectType_) == clustersLocationObjectType { + clusterLocation := location.GetValue().(management.ClusterLocation) + + clusterLocationMap := make(map[string]interface{}) + clusterLocationMap["cluster_location"] = flattenClusterLocation(clusterLocation) + restoreSourceLocation = append(restoreSourceLocation, clusterLocationMap) + return restoreSourceLocation + } + + if utils.StringValue(location.ObjectType_) == objectStoreLocationObjectType { + objectStoreLocation := location.GetValue().(management.ObjectStoreLocation) + + objectStoreLocationMap := make(map[string]interface{}) + objectStoreLocationMap["object_store_location"] = flattenObjectStoreLocation(objectStoreLocation) + restoreSourceLocation = append(restoreSourceLocation, objectStoreLocationMap) + return restoreSourceLocation + } + + return restoreSourceLocation +}