-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_snapshot_clone.go
85 lines (72 loc) · 2.32 KB
/
data_source_ibm_is_snapshot_clone.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceSnapshotClone() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISSnapshotCloneRead,
Schema: map[string]*schema.Schema{
isSnapshot: {
Type: schema.TypeString,
Required: true,
},
isSnapshotCloneAvailable: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this snapshot clone is available for use.",
},
isSnapshotCloneCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that this snapshot clone was created.",
},
isSnapshotCloneZone: {
Type: schema.TypeString,
Required: true,
Description: "The zone this snapshot clone resides in.",
},
},
}
}
func dataSourceIBMISSnapshotCloneRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := d.Get(isSnapshot).(string)
zone := d.Get(isSnapshotCloneZone).(string)
err := getSnapshotClone(context, d, meta, id, zone)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func getSnapshotClone(context context.Context, d *schema.ResourceData, meta interface{}, id, zone string) error {
sess, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return err
}
getSnapshotCloneOptions := &vpcv1.GetSnapshotCloneOptions{
ID: &id,
ZoneName: &zone,
}
clone, response, err := sess.GetSnapshotCloneWithContext(context, getSnapshotCloneOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching snapshot(%s) clone(%s) %s\n%s", id, zone, err, response)
}
if clone != nil && clone.Zone != nil {
d.SetId(*clone.Zone.Name)
d.Set(isSnapshotCloneZone, *clone.Zone.Name)
d.Set(isSnapshotCloneAvailable, *clone.Available)
if clone.CreatedAt != nil {
d.Set(isSnapshotCloneCreatedAt, flex.DateTimeToString(clone.CreatedAt))
}
} else {
return fmt.Errorf("[ERROR] No snapshot(%s) clone(%s) found", id, zone)
}
return nil
}