-
Notifications
You must be signed in to change notification settings - Fork 31
/
helpers.go
111 lines (84 loc) · 3.19 KB
/
helpers.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
/*
Copyright 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0
*/
package akscluster
import (
"context"
"encoding/json"
"sort"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
models "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/akscluster"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common"
)
type retryInterval string
func setResourceState(data *schema.ResourceData, cluster *models.VmwareTanzuManageV1alpha1AksCluster, nodepools []*models.VmwareTanzuManageV1alpha1AksclusterNodepoolNodepool) error {
data.SetId(cluster.Meta.UID)
if err := data.Set(common.MetaKey, common.FlattenMeta(cluster.Meta)); err != nil {
return err
}
sort.Slice(nodepools, func(i, j int) bool { return nodepools[i].FullName.Name < nodepools[j].FullName.Name })
specMap := toClusterSpecMap(cluster.Spec, nodepools)
if err := data.Set(clusterSpecKey, specMap); err != nil {
return err
}
return nil
}
func clusterIsReady(resp *models.VmwareTanzuManageV1alpha1AksclusterGetAksClusterResponse) bool {
if resp == nil || resp.AksCluster == nil || resp.AksCluster.Status == nil || *resp.AksCluster.Status.Phase != models.VmwareTanzuManageV1alpha1AksclusterPhaseREADY {
return false
}
return true
}
func clusterHasFatalError(resp *models.VmwareTanzuManageV1alpha1AksclusterGetAksClusterResponse) bool {
if resp == nil || resp.AksCluster == nil || resp.AksCluster.Status == nil || *resp.AksCluster.Status.Phase != models.VmwareTanzuManageV1alpha1AksclusterPhaseERROR {
return false
}
return true
}
func nodepoolIsReady(resp *models.VmwareTanzuManageV1alpha1AksclusterNodepoolGetNodepoolResponse) bool {
if resp == nil || resp.Nodepool == nil || resp.Nodepool.Status == nil || *resp.Nodepool.Status.Phase != models.VmwareTanzuManageV1alpha1AksclusterNodepoolPhaseREADY {
return false
}
return true
}
func nodepoolHasFatalError(resp *models.VmwareTanzuManageV1alpha1AksclusterNodepoolGetNodepoolResponse) bool {
if resp == nil || resp.Nodepool == nil || resp.Nodepool.Status == nil || *resp.Nodepool.Status.Phase != models.VmwareTanzuManageV1alpha1AksclusterNodepoolPhaseERROR {
return false
}
return true
}
func getErrorReason(conditions map[string]models.VmwareTanzuCoreV1alpha1StatusCondition) string {
msg, err := json.Marshal(conditions)
if err != nil {
return "unknown error"
}
return string(msg)
}
func getTimeOut(data *schema.ResourceData) time.Duration {
timeout, ok := data.GetOk(waitKey)
if !ok {
return defaultTimeout
}
duration, err := time.ParseDuration(timeout.(string))
if err != nil {
return defaultTimeout
}
return duration
}
func getPollInterval(ctx context.Context) time.Duration {
value := ctx.Value(RetryInterval)
if value != nil {
return value.(time.Duration)
}
return defaultInterval
}
func extractClusterFullName(d *schema.ResourceData) *models.VmwareTanzuManageV1alpha1AksclusterFullName {
fn := &models.VmwareTanzuManageV1alpha1AksclusterFullName{}
fn.CredentialName, _ = d.Get(CredentialNameKey).(string)
fn.SubscriptionID, _ = d.Get(SubscriptionIDKey).(string)
fn.ResourceGroupName, _ = d.Get(ResourceGroupNameKey).(string)
fn.Name, _ = d.Get(NameKey).(string)
return fn
}