|
| 1 | +// Copyright IBM Corp. 2024 All Rights Reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package power |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/IBM-Cloud/power-go-client/clients/instance" |
| 10 | + "github.com/IBM-Cloud/power-go-client/power/models" |
| 11 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" |
| 12 | + "github.com/hashicorp/go-uuid" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 16 | +) |
| 17 | + |
| 18 | +func DataSourceIBMPIVolumeSnapshots() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + ReadContext: dataSourceIBMPIVolumeSnapshotsRead, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + // Arguments |
| 24 | + Arg_CloudInstanceID: { |
| 25 | + Description: "The GUID of the service instance associated with an account.", |
| 26 | + Required: true, |
| 27 | + Type: schema.TypeString, |
| 28 | + ValidateFunc: validation.NoZeroValues, |
| 29 | + }, |
| 30 | + |
| 31 | + // Attributes |
| 32 | + Attr_VolumesSnapshots: { |
| 33 | + Computed: true, |
| 34 | + Description: "The list of volume snapshots.", |
| 35 | + Elem: &schema.Resource{ |
| 36 | + Schema: map[string]*schema.Schema{ |
| 37 | + Attr_CreationDate: { |
| 38 | + Computed: true, |
| 39 | + Description: "The date and time when the volume snapshot was created.", |
| 40 | + Type: schema.TypeString, |
| 41 | + }, |
| 42 | + Attr_CRN: { |
| 43 | + Computed: true, |
| 44 | + Description: "The CRN of the volume snapshot.", |
| 45 | + Type: schema.TypeString, |
| 46 | + }, |
| 47 | + Attr_ID: { |
| 48 | + Computed: true, |
| 49 | + Description: "The snapshot UUID.", |
| 50 | + Type: schema.TypeString, |
| 51 | + }, |
| 52 | + Attr_Name: { |
| 53 | + Computed: true, |
| 54 | + Description: "The volume snapshot name.", |
| 55 | + Type: schema.TypeString, |
| 56 | + }, |
| 57 | + Attr_Size: { |
| 58 | + Computed: true, |
| 59 | + Description: "The size of the volume snapshot, in gibibytes (GiB).", |
| 60 | + Type: schema.TypeFloat, |
| 61 | + }, |
| 62 | + Attr_Status: { |
| 63 | + Computed: true, |
| 64 | + Description: "The status for the volume snapshot.", |
| 65 | + Type: schema.TypeString, |
| 66 | + }, |
| 67 | + Attr_UpdatedDate: { |
| 68 | + Computed: true, |
| 69 | + Description: "The date and time when the volume snapshot was last updated.", |
| 70 | + Type: schema.TypeString, |
| 71 | + }, |
| 72 | + Attr_VolumeID: { |
| 73 | + Computed: true, |
| 74 | + Description: "The volume UUID associated with the snapshot.", |
| 75 | + Type: schema.TypeString, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + Type: schema.TypeSet, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func dataSourceIBMPIVolumeSnapshotsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 86 | + sess, err := meta.(conns.ClientSession).IBMPISession() |
| 87 | + if err != nil { |
| 88 | + return diag.FromErr(err) |
| 89 | + } |
| 90 | + |
| 91 | + cloudInstanceID := d.Get(Arg_CloudInstanceID).(string) |
| 92 | + client := instance.NewIBMPISnapshotClient(ctx, sess, cloudInstanceID) |
| 93 | + snapshots, err := client.V1VolumeSnapshotsGetall() |
| 94 | + if err != nil { |
| 95 | + return diag.FromErr(err) |
| 96 | + } |
| 97 | + d.Set(Attr_VolumesSnapshots, flattenSnapshotsV1(snapshots.VolumeSnapshots)) |
| 98 | + var clientgenU, _ = uuid.GenerateUUID() |
| 99 | + d.SetId(clientgenU) |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func flattenSnapshotsV1(snapshotList []*models.SnapshotV1) []map[string]interface{} { |
| 105 | + snapshots := make([]map[string]interface{}, 0, len(snapshotList)) |
| 106 | + for _, snap := range snapshotList { |
| 107 | + snapshot := map[string]interface{}{ |
| 108 | + Attr_CreationDate: snap.CreationDate.String(), |
| 109 | + Attr_CRN: snap.Crn, |
| 110 | + Attr_ID: *snap.ID, |
| 111 | + Attr_Name: *snap.Name, |
| 112 | + Attr_Size: *snap.Size, |
| 113 | + Attr_Status: *snap.Status, |
| 114 | + Attr_UpdatedDate: snap.UpdatedDate.String(), |
| 115 | + Attr_VolumeID: *snap.VolumeID, |
| 116 | + } |
| 117 | + snapshots = append(snapshots, snapshot) |
| 118 | + } |
| 119 | + return snapshots |
| 120 | +} |
0 commit comments