This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_opentelekomcloud_vbs_backup_v2.go
135 lines (119 loc) · 3.2 KB
/
data_source_opentelekomcloud_vbs_backup_v2.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package opentelekomcloud
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/vbs/v2/backups"
"github.com/huaweicloud/golangsdk/openstack/vbs/v2/shares"
)
func dataSourceVBSBackupV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceVBSBackupV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
},
"volume_id": {
Type: schema.TypeString,
Optional: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
"container": {
Type: schema.TypeString,
Computed: true,
},
"service_metadata": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_id": {
Type: schema.TypeString,
Optional: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"to_project_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"share_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func dataSourceVBSBackupV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vbsClient, err := config.vbsV2Client(GetRegion(d, config))
listBackupOpts := backups.ListOpts{
Id: d.Id(),
Name: d.Get("name").(string),
Status: d.Get("status").(string),
VolumeId: d.Get("volume_id").(string),
SnapshotId: d.Get("snapshot_id").(string),
}
refinedBackups, err := backups.List(vbsClient, listBackupOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve backups: %s", err)
}
if len(refinedBackups) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(refinedBackups) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
Backup := refinedBackups[0]
log.Printf("[INFO] Retrieved Backup using given filter %s: %+v", Backup.Id, Backup)
d.SetId(Backup.Id)
d.Set("name", Backup.Name)
d.Set("description", Backup.Description)
d.Set("status", Backup.Status)
d.Set("availability_zone", Backup.AvailabilityZone)
d.Set("snapshot_id", Backup.SnapshotId)
d.Set("service_metadata", Backup.ServiceMetadata)
d.Set("size", Backup.Size)
d.Set("container", Backup.Container)
d.Set("volume_id", Backup.VolumeId)
d.Set("region", GetRegion(d, config))
listShareOpts := shares.ListOpts{
BackupID: d.Id(),
}
shares, err := shares.List(vbsClient, listShareOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve shares: %s", err)
}
d.Set("to_project_ids", resourceToProjectIdsV2(shares))
d.Set("share_ids", resourceShareIDsV2(shares))
return nil
}