/
storage_endpoint_data_source.go
90 lines (72 loc) · 2.66 KB
/
storage_endpoint_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright 2023 Pure Storage Inc
SPDX-License-Identifier: Apache-2.0
*/
package fusion
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/PureStorage-OpenConnect/terraform-provider-fusion/internal/utilities"
hmrest "github.com/PureStorage-OpenConnect/terraform-provider-fusion/internal/hmrest"
)
// Implements DataSource
type storageEndpointDataSource struct{}
// This is our entry point for the Storage Endpoint data source
func dataSourceStorageEndpoint() *schema.Resource {
ds := &storageEndpointDataSource{}
seSchema := schemaStorageEndpoint()
seSchema[optionIscsi].ExactlyOneOf = nil
seSchema[optionCbsAzureIscsi].ExactlyOneOf = nil
dsSchema := map[string]*schema.Schema{
optionItems: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: seSchema,
},
Description: "List of matching Storage Endpoints.",
},
optionRegion: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The name of the Region in which this Storage Endpoint is located.",
},
optionAvailabilityZone: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The name of the Availability Zone in which this Storage Endpoint is located.",
},
}
storageEndpointDataSourceFunctions := NewBaseDataSourceFunctions(resourceKindStorageEndpoint, ds, dsSchema)
return storageEndpointDataSourceFunctions.Resource
}
func (ds *storageEndpointDataSource) ReadDataSource(ctx context.Context, client *hmrest.APIClient, d *schema.ResourceData) error {
resp, _, err := client.StorageEndpointsApi.ListStorageEndpoints(ctx, rdString(ctx, d, optionRegion), rdString(ctx, d, optionAvailabilityZone), nil)
if err != nil {
return err
}
storageEndpointsList := make([]map[string]interface{}, 0, resp.Count)
for _, se := range resp.Items {
storageEndpointOut := map[string]interface{}{
optionName: se.Name,
optionDisplayName: se.DisplayName,
optionRegion: se.Region.Name,
optionAvailabilityZone: se.AvailabilityZone.Name,
}
switch se.EndpointType {
case endpointTypeIscsi:
storageEndpointOut[optionIscsi] = parseStorageEndpointIscsi(se.Iscsi)
case endpointTypeCbsAzureIscsi:
storageEndpointOut[optionCbsAzureIscsi] = parseStorageEndpointCbsAzureIscsi(se.CbsAzureIscsi)
}
storageEndpointsList = append(storageEndpointsList, storageEndpointOut)
}
if err := d.Set(optionItems, storageEndpointsList); err != nil {
return err
}
d.SetId(utilities.GetIdForDataSource())
return nil
}