-
Notifications
You must be signed in to change notification settings - Fork 649
/
resource_ibm_is_share_replica_operations.go
234 lines (201 loc) · 7.93 KB
/
resource_ibm_is_share_replica_operations.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func ResourceIbmIsShareReplicaOperations() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIbmIsShareReplicaOperationsCreate,
ReadContext: resourceIbmIsShareReplicaOperationsRead,
UpdateContext: resourceIbmIsShareReplicaOperationsUpdate,
DeleteContext: resourceIbmIsShareReplicaOperationsDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"share_replica": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The file share identifier.",
},
"fallback_policy": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "fail",
ExactlyOneOf: []string{"split_share", "fallback_policy"},
ValidateFunc: validate.InvokeValidator("ibm_is_share_replica_operations", "fallback_policy"),
Description: "The action to take if the failover request is accepted but cannot be performed or times out",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
RequiredWith: []string{"fallback_policy"},
//ForceNew: true,
ValidateFunc: validate.InvokeValidator("ibm_is_share_replica_operations", "timeout"),
Description: "The failover timeout in seconds",
},
"split_share": {
Type: schema.TypeBool,
Default: false,
ForceNew: true,
Optional: true,
ExactlyOneOf: []string{"split_share", "fallback_policy"},
Description: "If set to true the replication relationship between source share and replica will be removed.",
},
},
}
}
func ResourceIbmIsShareReplicaOperationsValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 1)
fbPolicyAllowedValues := "fail, split"
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "timeout",
ValidateFunctionIdentifier: validate.IntBetween,
Type: validate.TypeInt,
Optional: true,
MinValue: "300",
MaxValue: "3600",
},
validate.ValidateSchema{
Identifier: "fallback_policy",
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Optional: true,
AllowedValues: fbPolicyAllowedValues,
},
)
resourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_share_replica_operations", Schema: validateSchema}
return &resourceValidator
}
func resourceIbmIsShareReplicaOperationsCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
share_id := d.Get("share_replica").(string)
splitShare := d.Get("split_share").(bool)
getShareSourceOptions := &vpcv1.GetShareSourceOptions{
ShareID: &share_id,
}
sourceShare, response, err := vpcClient.GetShareSourceWithContext(context, getShareSourceOptions)
if err != nil || sourceShare == nil {
if response != nil {
if response.StatusCode == 404 {
d.SetId("")
}
log.Printf("[DEBUG] GetShareWithContext failed %s\n%s", err, response)
return nil
}
log.Printf("[DEBUG] GetShareWithContext failed %s\n", err)
return diag.FromErr(fmt.Errorf("[DEBUG] GetShareWithContext failed %s\n", err))
}
if !splitShare {
fallback_policy := d.Get("fallback_policy").(string)
timeout := d.Get("timeout").(int)
failOverShareOptions := &vpcv1.FailoverShareOptions{
ShareID: &share_id,
}
failOverShareOptions.FallbackPolicy = &fallback_policy
if timeout != 0 {
failOverShareOptions.Timeout = core.Int64Ptr(int64(timeout))
}
response, err := vpcClient.FailoverShareWithContext(context, failOverShareOptions)
if err != nil {
log.Printf("[DEBUG] FailoverShareWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] FailoverShareWithContext failed %s\n%s", err, response))
}
} else {
deleteShareSourceOptions := &vpcv1.DeleteShareSourceOptions{
ShareID: &share_id,
}
response, err := vpcClient.DeleteShareSourceWithContext(context, deleteShareSourceOptions)
if err != nil {
log.Printf("[DEBUG] DeleteShareSourceWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] DeleteShareSourceWithContext failed %s\n%s", err, response))
}
}
_, err = isWaitForShareReplicationJobDone(context, vpcClient, share_id, d, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}
_, err = isWaitForShareReplicationJobDone(context, vpcClient, *sourceShare.ID, d, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}
d.SetId(share_id)
return nil
}
func isWaitForShareReplicationJobDone(context context.Context, vpcClient *vpcv1.VpcV1, shareid string, d *schema.ResourceData, timeout time.Duration) (interface{}, error) {
log.Printf("Waiting for share (%s) to be available.", shareid)
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"active", "none"},
Refresh: isShareReplicationJobRefreshFunc(context, vpcClient, shareid, d),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}
return stateConf.WaitForState()
}
func isShareReplicationJobRefreshFunc(context context.Context, vpcClient *vpcv1.VpcV1, shareid string, d *schema.ResourceData) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
shareOptions := &vpcv1.GetShareOptions{}
shareOptions.SetID(shareid)
share, response, err := vpcClient.GetShareWithContext(context, shareOptions)
if err != nil {
return nil, "", fmt.Errorf("Error Getting share: %s\n%s", err, response)
}
if *share.ReplicationStatus == "active" || *share.ReplicationStatus == "none" {
return share, *share.ReplicationStatus, nil
}
return share, "pending", nil
}
}
func resourceIbmIsShareReplicaOperationsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}
func resourceIbmIsShareReplicaOperationsUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}
func resourceIbmIsShareReplicaOperationsDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}
func isWaitForShareSplit(context context.Context, vpcClient *vpcv1.VpcV1, shareid string, d *schema.ResourceData, timeout time.Duration) (interface{}, error) {
log.Printf("Waiting for share (%s) to be available.", shareid)
stateConf := &resource.StateChangeConf{
Pending: []string{"split_pending"},
Target: []string{"none"},
Refresh: isShareSplitRefreshFunc(context, vpcClient, shareid, d),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}
return stateConf.WaitForState()
}
func isShareSplitRefreshFunc(context context.Context, vpcClient *vpcv1.VpcV1, shareid string, d *schema.ResourceData) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
shareOptions := &vpcv1.GetShareOptions{}
shareOptions.SetID(shareid)
share, response, err := vpcClient.GetShareWithContext(context, shareOptions)
if err != nil {
return nil, "", fmt.Errorf("Error Getting share: %s\n%s", err, response)
}
d.Set("replication_status", *share.LifecycleState)
if *share.LifecycleState == "none" {
return share, *share.LifecycleState, nil
}
return share, "split_pending", nil
}
}