/
data_source_backup_storage_location.go
75 lines (61 loc) · 1.66 KB
/
data_source_backup_storage_location.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
package spectrocloud
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spectrocloud/hapi/models"
"github.com/spectrocloud/palette-sdk-go/client"
)
func dataSourceBackupStorageLocation() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBackupStorageLocationRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"id", "name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"id", "name"},
},
},
}
}
func dataSourceBackupStorageLocationRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.V1Client)
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
projectScope := true
bsls, err := c.ListBackupStorageLocation(projectScope)
if err != nil {
return diag.FromErr(err)
}
var bsl *models.V1UserAssetsLocation
for _, a := range bsls {
if v, ok := d.GetOk("id"); ok && v.(string) == a.Metadata.UID {
bsl = a
break
} else if v, ok := d.GetOk("name"); ok && v.(string) == a.Metadata.Name {
bsl = a
break
}
}
if bsl == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to find backup storage location",
Detail: "Unable to find the specified backup storage location",
})
return diags
}
d.SetId(bsl.Metadata.UID)
err = d.Set("name", bsl.Metadata.Name)
if err != nil {
return diag.FromErr(err)
}
return diags
}