/
polling.go
130 lines (120 loc) · 5.19 KB
/
polling.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
// Copyright 2023 Cloudera. All Rights Reserved.
//
// This file is licensed under the Apache License Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
//
// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. Refer to the License for the specific
// permissions and limitations governing your use of the file.
package environments
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client"
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client/operations"
environmentsmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/models"
"github.com/cloudera/terraform-provider-cdp/utils"
)
func waitForEnvironmentToBeDeleted(environmentName string, fallbackTimeout time.Duration, client *client.Environments, ctx context.Context, options *utils.PollingOptions) error {
timeout, err := utils.CalculateTimeoutOrDefault(ctx, options, fallbackTimeout)
if err != nil {
return err
}
stateConf := &retry.StateChangeConf{
Pending: []string{"STORAGE_CONSUMPTION_COLLECTION_UNSCHEDULING_IN_PROGRESS",
"NETWORK_DELETE_IN_PROGRESS",
"FREEIPA_DELETE_IN_PROGRESS",
"RDBMS_DELETE_IN_PROGRESS",
"IDBROKER_MAPPINGS_DELETE_IN_PROGRESS",
"S3GUARD_TABLE_DELETE_IN_PROGRESS",
"CLUSTER_DEFINITION_DELETE_PROGRESS",
"CLUSTER_DEFINITION_CLEANUP_PROGRESS",
"UMS_RESOURCE_DELETE_IN_PROGRESS",
"DELETE_INITIATED",
"DATAHUB_CLUSTERS_DELETE_IN_PROGRESS",
"DATALAKE_CLUSTERS_DELETE_IN_PROGRESS",
"PUBLICKEY_DELETE_IN_PROGRESS",
"EVENT_CLEANUP_IN_PROGRESS",
"EXPERIENCE_DELETE_IN_PROGRESS",
"ENVIRONMENT_RESOURCE_ENCRYPTION_DELETE_IN_PROGRESS",
"ENVIRONMENT_ENCRYPTION_RESOURCES_DELETED"},
Target: []string{},
Delay: 5 * time.Second,
Timeout: *timeout,
PollInterval: 10 * time.Second,
Refresh: func() (interface{}, string, error) {
log.Printf("About to describe environment")
params := operations.NewDescribeEnvironmentParamsWithContext(ctx)
params.WithInput(&environmentsmodels.DescribeEnvironmentRequest{EnvironmentName: &environmentName})
resp, err := client.Operations.DescribeEnvironment(params)
if err != nil {
log.Printf("Error describing environment: %s", err)
if envErr, ok := err.(*operations.DescribeEnvironmentDefault); ok {
if isEnvNotFoundError(envErr) {
return nil, "", nil
}
}
return nil, "", err
}
if resp.GetPayload().Environment == nil {
log.Printf("Described environment. No environment.")
return nil, "", nil
}
log.Printf("Described environment's status: %s", *resp.GetPayload().Environment.Status)
return checkResponseStatusForError(resp)
},
}
_, err = stateConf.WaitForStateContext(ctx)
return err
}
func waitForEnvironmentToBeAvailable(environmentName string, fallbackTimeout time.Duration, client *client.Environments, ctx context.Context, pollingOptions *utils.PollingOptions,
stateSaverCb func(*environmentsmodels.Environment)) error {
timeout, err := utils.CalculateTimeoutOrDefault(ctx, pollingOptions, fallbackTimeout)
if err != nil {
return err
}
stateConf := &retry.StateChangeConf{
Pending: []string{"CREATION_INITIATED",
"NETWORK_CREATION_IN_PROGRESS",
"PUBLICKEY_CREATE_IN_PROGRESS",
"ENVIRONMENT_RESOURCE_ENCRYPTION_INITIALIZATION_IN_PROGRESS",
"ENVIRONMENT_VALIDATION_IN_PROGRESS",
"ENVIRONMENT_INITIALIZATION_IN_PROGRESS",
"FREEIPA_CREATION_IN_PROGRESS"},
Target: []string{"AVAILABLE"},
Delay: 5 * time.Second,
Timeout: *timeout,
PollInterval: 10 * time.Second,
Refresh: func() (interface{}, string, error) {
log.Printf("[DEBUG] About to describe environment %s", environmentName)
params := operations.NewDescribeEnvironmentParamsWithContext(ctx)
params.WithInput(&environmentsmodels.DescribeEnvironmentRequest{EnvironmentName: &environmentName})
resp, err := client.Operations.DescribeEnvironment(params)
if err != nil {
// Envs that have just been created may not be returned from Describe Environment request because of eventual
// consistency. We return an empty state to retry.
if isEnvNotFoundError(err) {
log.Printf("[DEBUG] Recoverable error describing environment: %s", err)
return nil, "", nil
}
log.Printf("Error describing environment: %s", err)
return nil, "", err
}
stateSaverCb(resp.Payload.Environment)
log.Printf("Described environment's status: %s", *resp.GetPayload().Environment.Status)
return checkResponseStatusForError(resp)
},
}
_, err = stateConf.WaitForStateContext(ctx)
return err
}
func checkResponseStatusForError(resp *operations.DescribeEnvironmentOK) (interface{}, string, error) {
if utils.ContainsAsSubstring([]string{"FAILED", "ERROR"}, *resp.GetPayload().Environment.Status) {
return nil, "", fmt.Errorf("unexpected Enviornment status: %s. Reason: %s", *resp.GetPayload().Environment.Status, resp.GetPayload().Environment.StatusReason)
}
return resp, *resp.GetPayload().Environment.Status, nil
}