/
utils.go
137 lines (120 loc) · 4.12 KB
/
utils.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
// 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 utils
import (
"context"
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp"
)
func GetCdpClientForResource(req resource.ConfigureRequest, resp *resource.ConfigureResponse) *cdp.Client {
if req.ProviderData == nil {
return nil
}
client, ok := req.ProviderData.(*cdp.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *cdp.Client, got: %T. Please report this issue to Cloudera.", req.ProviderData),
)
return nil
}
return client
}
func GetCdpClientForDataSource(req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) *cdp.Client {
if req.ProviderData == nil {
return nil
}
client, ok := req.ProviderData.(*cdp.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *cdp.Client, got: %T. Please report this issue to Cloudera.", req.ProviderData),
)
return nil
}
return client
}
func GetMapFromSingleItemList(d *schema.ResourceData, name string) (map[string]interface{}, error) {
if l := d.Get(name).([]interface{}); len(l) == 1 && l[0] != nil {
return l[0].(map[string]interface{}), nil
}
return nil, fmt.Errorf("error getting %s", name)
}
func GetMapFromSingleItemListInMap(d map[string]interface{}, name string) (map[string]interface{}, error) {
if l := d[name].([]interface{}); len(l) == 1 && l[0] != nil {
return l[0].(map[string]interface{}), nil
}
return nil, fmt.Errorf("error getting %s", name)
}
func ToStringList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
for _, v := range configured {
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, v.(string))
}
}
return vs
}
func CalculateTimeoutOrDefault(ctx context.Context, options *PollingOptions, fallback time.Duration) (*time.Duration, error) {
tflog.Debug(ctx, fmt.Sprintf("About to calculate polling timeout using the desired timeout (%+v) and the given fallback timeout (%+v)", options, fallback))
var timeout time.Duration
if options != nil && !options.PollingTimeout.IsNull() {
timeout = time.Duration(options.PollingTimeout.ValueInt64()) * time.Minute
} else {
tflog.Info(ctx, "No desired polling timeout is given, the fallback value will be used.")
timeout = fallback
}
if timeout.Minutes() <= 0 {
msg := "no meaningful timeout value can be calculated based on the given parameters, thus operation shall fail immediately"
tflog.Warn(ctx, msg)
return nil, fmt.Errorf(msg)
}
tflog.Info(ctx, fmt.Sprintf("The following polling timeout calculated: %+v", timeout))
return &timeout, nil
}
func ToBaseTypesStringMap(in map[string]string) map[string]types.String {
res := map[string]types.String{}
for k, v := range in {
res[k] = types.StringValue(v)
}
return res
}
func FromListValueToStringList(tl types.List) []string {
if tl.IsNull() {
return []string{}
}
res := make([]string, len(tl.Elements()))
for i, elem := range tl.Elements() {
res[i] = elem.(types.String).ValueString()
}
return res
}
func FromSetValueToStringList(tl types.Set) []string {
if tl.IsNull() {
return []string{}
}
res := make([]string, len(tl.Elements()))
for i, elem := range tl.Elements() {
res[i] = elem.(types.String).ValueString()
}
return res
}
func Int64To32Pointer(in types.Int64) *int32 {
n64 := in.ValueInt64()
var n2 = int32(n64)
return &n2
}