|
| 1 | +// Copyright IBM Corp. 2017, 2021 All Rights Reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package power |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "log" |
| 9 | + |
| 10 | + "github.com/IBM-Cloud/power-go-client/clients/instance" |
| 11 | + "github.com/IBM-Cloud/power-go-client/power/models" |
| 12 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" |
| 13 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" |
| 14 | + "github.com/hashicorp/go-uuid" |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 18 | +) |
| 19 | + |
| 20 | +func DataSourceIBMPIVolumes() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + ReadContext: dataSourceIBMPIVolumesRead, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + // Arguments |
| 25 | + Arg_CloudInstanceID: { |
| 26 | + Description: "The GUID of the service instance associated with an account.", |
| 27 | + Required: true, |
| 28 | + Type: schema.TypeString, |
| 29 | + ValidateFunc: validation.NoZeroValues, |
| 30 | + }, |
| 31 | + |
| 32 | + // Attributes |
| 33 | + Attr_Volumes: { |
| 34 | + Computed: true, |
| 35 | + Description: "List of all volumes.", |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + Attr_Auxiliary: { |
| 39 | + Computed: true, |
| 40 | + Description: "Indicates if the volume is auxiliary or not.", |
| 41 | + Type: schema.TypeBool, |
| 42 | + }, |
| 43 | + Attr_AuxiliaryVolumeName: { |
| 44 | + Computed: true, |
| 45 | + Description: "The auxiliary volume name.", |
| 46 | + Type: schema.TypeString, |
| 47 | + }, |
| 48 | + Attr_Bootable: { |
| 49 | + Computed: true, |
| 50 | + Description: "Indicates if the volume is boot capable.", |
| 51 | + Type: schema.TypeBool, |
| 52 | + }, |
| 53 | + Attr_CRN: { |
| 54 | + Computed: true, |
| 55 | + Description: "The CRN of this resource.", |
| 56 | + Type: schema.TypeString, |
| 57 | + }, |
| 58 | + Attr_ConsistencyGroupName: { |
| 59 | + Computed: true, |
| 60 | + Description: "Consistency group name if volume is a part of volume group.", |
| 61 | + Type: schema.TypeString, |
| 62 | + }, |
| 63 | + Attr_CreationDate: { |
| 64 | + Computed: true, |
| 65 | + Description: "Date volume was created.", |
| 66 | + Type: schema.TypeString, |
| 67 | + }, |
| 68 | + Attr_DiskType: { |
| 69 | + Computed: true, |
| 70 | + Description: "The disk type that is used for the volume.", |
| 71 | + Type: schema.TypeString, |
| 72 | + }, |
| 73 | + Attr_FreezeTime: { |
| 74 | + Computed: true, |
| 75 | + Description: "The freeze time of remote copy.", |
| 76 | + Type: schema.TypeString, |
| 77 | + }, |
| 78 | + Attr_GroupID: { |
| 79 | + Computed: true, |
| 80 | + Description: "The volume group id in which the volume belongs.", |
| 81 | + Type: schema.TypeString, |
| 82 | + }, |
| 83 | + Attr_ID: { |
| 84 | + Computed: true, |
| 85 | + Description: "The unique identifier of the volume.", |
| 86 | + Type: schema.TypeString, |
| 87 | + }, |
| 88 | + Attr_IOThrottleRate: { |
| 89 | + Computed: true, |
| 90 | + Description: "Amount of iops assigned to the volume", |
| 91 | + Type: schema.TypeString, |
| 92 | + }, |
| 93 | + Attr_LastUpdateDate: { |
| 94 | + Computed: true, |
| 95 | + Description: "The last updated date of the volume.", |
| 96 | + Type: schema.TypeString, |
| 97 | + }, |
| 98 | + Attr_MasterVolumeName: { |
| 99 | + Computed: true, |
| 100 | + Description: "The master volume name.", |
| 101 | + Type: schema.TypeString, |
| 102 | + }, |
| 103 | + Attr_MirroringState: { |
| 104 | + Computed: true, |
| 105 | + Description: "Mirroring state for replication enabled volume.", |
| 106 | + Type: schema.TypeString, |
| 107 | + }, |
| 108 | + Attr_OutOfBandDeleted: { |
| 109 | + Computed: true, |
| 110 | + Description: "Indicates if the volume does not exist on storage controller.", |
| 111 | + Type: schema.TypeBool, |
| 112 | + }, |
| 113 | + Attr_PrimaryRole: { |
| 114 | + Computed: true, |
| 115 | + Description: "Indicates whether master/auxiliary volume is playing the primary role.", |
| 116 | + Type: schema.TypeString, |
| 117 | + }, |
| 118 | + Attr_ReplicationEnabled: { |
| 119 | + Computed: true, |
| 120 | + Description: "Indicates if the volume should be replication enabled or not.", |
| 121 | + Type: schema.TypeBool, |
| 122 | + }, |
| 123 | + Attr_ReplicationSites: { |
| 124 | + Computed: true, |
| 125 | + Description: "List of replication sites for volume replication.", |
| 126 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 127 | + Type: schema.TypeList, |
| 128 | + }, |
| 129 | + Attr_ReplicationStatus: { |
| 130 | + Computed: true, |
| 131 | + Description: "The replication status of the volume.", |
| 132 | + Type: schema.TypeString, |
| 133 | + }, |
| 134 | + Attr_ReplicationType: { |
| 135 | + Computed: true, |
| 136 | + Description: "The replication type of the volume, metro or global.", |
| 137 | + Type: schema.TypeString, |
| 138 | + }, |
| 139 | + Attr_Shareable: { |
| 140 | + Computed: true, |
| 141 | + Description: "Indicates if the volume is shareable between VMs.", |
| 142 | + Type: schema.TypeBool, |
| 143 | + }, |
| 144 | + Attr_Size: { |
| 145 | + Computed: true, |
| 146 | + Description: "The size of the volume in GB.", |
| 147 | + Type: schema.TypeInt, |
| 148 | + }, |
| 149 | + Attr_State: { |
| 150 | + Computed: true, |
| 151 | + Description: "The state of the volume.", |
| 152 | + Type: schema.TypeString, |
| 153 | + }, |
| 154 | + Attr_UserTags: { |
| 155 | + Computed: true, |
| 156 | + Description: "List of user tags attached to the resource.", |
| 157 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 158 | + Set: schema.HashString, |
| 159 | + Type: schema.TypeSet, |
| 160 | + }, |
| 161 | + Attr_VolumeName: { |
| 162 | + Computed: true, |
| 163 | + Description: "The name of the volume.", |
| 164 | + Type: schema.TypeString, |
| 165 | + }, |
| 166 | + Attr_VolumePool: { |
| 167 | + Computed: true, |
| 168 | + Description: "Volume pool, name of storage pool where the volume is located.", |
| 169 | + Type: schema.TypeString, |
| 170 | + }, |
| 171 | + Attr_WWN: { |
| 172 | + Computed: true, |
| 173 | + Description: "The world wide name of the volume.", |
| 174 | + Type: schema.TypeString, |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + Type: schema.TypeList, |
| 179 | + }, |
| 180 | + }, |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func dataSourceIBMPIVolumesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 185 | + sess, err := meta.(conns.ClientSession).IBMPISession() |
| 186 | + if err != nil { |
| 187 | + return diag.FromErr(err) |
| 188 | + } |
| 189 | + |
| 190 | + cloudInstanceID := d.Get(Arg_CloudInstanceID).(string) |
| 191 | + volumeC := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID) |
| 192 | + volumeData, err := volumeC.GetAll() |
| 193 | + if err != nil { |
| 194 | + return diag.FromErr(err) |
| 195 | + } |
| 196 | + |
| 197 | + var clientgenU, _ = uuid.GenerateUUID() |
| 198 | + d.SetId(clientgenU) |
| 199 | + d.Set(Attr_Volumes, flattenVolumes(volumeData.Volumes, meta)) |
| 200 | + return nil |
| 201 | +} |
| 202 | + |
| 203 | +func flattenVolumes(list []*models.VolumeReference, meta any) []map[string]any { |
| 204 | + log.Printf("Calling the flattenVolumeGroups call with list %d", len(list)) |
| 205 | + result := make([]map[string]any, 0, len(list)) |
| 206 | + for _, i := range list { |
| 207 | + volume := map[string]any{ |
| 208 | + Attr_Auxiliary: *i.Auxiliary, |
| 209 | + Attr_AuxiliaryVolumeName: i.AuxVolumeName, |
| 210 | + Attr_Bootable: *i.Bootable, |
| 211 | + Attr_ConsistencyGroupName: i.ConsistencyGroupName, |
| 212 | + Attr_CreationDate: i.CreationDate.String(), |
| 213 | + Attr_DiskType: *i.DiskType, |
| 214 | + Attr_GroupID: i.GroupID, |
| 215 | + Attr_ID: *i.VolumeID, |
| 216 | + Attr_IOThrottleRate: i.IoThrottleRate, |
| 217 | + Attr_LastUpdateDate: i.LastUpdateDate.String(), |
| 218 | + Attr_MasterVolumeName: i.MasterVolumeName, |
| 219 | + Attr_MirroringState: i.MirroringState, |
| 220 | + Attr_OutOfBandDeleted: i.OutOfBandDeleted, |
| 221 | + Attr_PrimaryRole: i.PrimaryRole, |
| 222 | + Attr_ReplicationEnabled: *i.ReplicationEnabled, |
| 223 | + Attr_ReplicationStatus: i.ReplicationStatus, |
| 224 | + Attr_ReplicationType: i.ReplicationType, |
| 225 | + Attr_Shareable: *i.Shareable, |
| 226 | + Attr_Size: *i.Size, |
| 227 | + Attr_State: *i.State, |
| 228 | + Attr_VolumeName: *i.Name, |
| 229 | + Attr_VolumePool: i.VolumePool, |
| 230 | + Attr_WWN: *i.Wwn, |
| 231 | + } |
| 232 | + if i.FreezeTime != nil { |
| 233 | + volume[Attr_FreezeTime] = i.FreezeTime.String() |
| 234 | + } |
| 235 | + if len(i.ReplicationSites) > 0 { |
| 236 | + volume[Attr_ReplicationSites] = i.ReplicationSites |
| 237 | + } |
| 238 | + volumeCRN := string(i.Crn) |
| 239 | + if volumeCRN != "" { |
| 240 | + volume[Attr_CRN] = i.Crn |
| 241 | + tags, err := flex.GetGlobalTagsUsingCRN(meta, volumeCRN, "", UserTagType) |
| 242 | + if err != nil { |
| 243 | + log.Printf("Error on get of pi volume (%s) user_tags: %s", *i.VolumeID, err) |
| 244 | + } |
| 245 | + volume[Attr_UserTags] = tags |
| 246 | + } |
| 247 | + result = append(result, volume) |
| 248 | + } |
| 249 | + return result |
| 250 | +} |
0 commit comments