-
Notifications
You must be signed in to change notification settings - Fork 669
/
data_source_ibm_container_addons.go
180 lines (169 loc) · 5.44 KB
/
data_source_ibm_container_addons.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package kubernetes
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
v1 "github.com/IBM-Cloud/bluemix-go/api/container/containerv1"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
)
func DataSourceIBMContainerAddOns() *schema.Resource {
return &schema.Resource{
Read: datasourceIBMContainerAddOnsRead,
Schema: map[string]*schema.Schema{
"cluster": {
Type: schema.TypeString,
Required: true,
Description: "Cluster Name or ID",
ValidateFunc: validate.InvokeDataSourceValidator(
"ibm_container_addons",
"cluster"),
},
"resource_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the resource group.",
},
"addons": {
Type: schema.TypeList,
Computed: true,
Description: "The List of AddOns",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The addon name such as 'istio'.",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "The addon version, omit the version if you wish to use the default version.",
},
"allowed_upgrade_versions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The versions that the addon can be upgraded to",
},
"deprecated": {
Type: schema.TypeBool,
Computed: true,
Description: "Determines if this addon version is deprecated",
},
"health_state": {
Type: schema.TypeString,
Computed: true,
Description: "The health state for this addon, a short indication (e.g. critical, pending)",
},
"health_status": {
Type: schema.TypeString,
Computed: true,
Description: "The health status for this addon, provides a description of the state (e.g. error message)",
},
"min_kube_version": {
Type: schema.TypeString,
Computed: true,
Description: "The minimum kubernetes version for this addon.",
},
"min_ocp_version": {
Type: schema.TypeString,
Computed: true,
Description: "The minimum OpenShift version for this addon.",
},
"supported_kube_range": {
Type: schema.TypeString,
Computed: true,
Description: "The supported kubernetes version range for this addon.",
},
"target_version": {
Type: schema.TypeString,
Computed: true,
Description: "The addon target version.",
},
"vlan_spanning_required": {
Type: schema.TypeBool,
Computed: true,
Description: "VLAN spanning required for multi-zone clusters",
},
},
},
},
},
}
}
func DataSourceIBMContainerAddOnsValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "cluster",
ValidateFunctionIdentifier: validate.ValidateCloudData,
Type: validate.TypeString,
Required: true,
CloudDataType: "cluster",
CloudDataRange: []string{"resolved_to:id"}})
iBMContainerAddOnsValidator := validate.ResourceValidator{ResourceName: "ibm_container_addons", Schema: validateSchema}
return &iBMContainerAddOnsValidator
}
func datasourceIBMContainerAddOnsRead(d *schema.ResourceData, meta interface{}) error {
csClient, err := meta.(conns.ClientSession).ContainerAPI()
if err != nil {
return err
}
addOnAPI := csClient.AddOns()
targetEnv, err := getClusterTargetHeader(d, meta)
if err != nil {
return err
}
cluster := d.Get("cluster").(string)
result, err := addOnAPI.GetAddons(cluster, targetEnv)
if err != nil {
return err
}
d.Set("cluster", cluster)
addOns, err := flattenAddOnsList(result)
if err != nil {
fmt.Printf("Error Flattening Addons list %s", err)
}
d.Set("resource_group_id", targetEnv.ResourceGroup)
d.Set("addons", addOns)
d.SetId(cluster)
return nil
}
func flattenAddOnsList(result []v1.AddOn) (addOns []map[string]interface{}, err error) {
for _, addOn := range result {
record := map[string]interface{}{}
record["name"] = addOn.Name
record["version"] = addOn.Version
if len(addOn.AllowedUpgradeVersion) > 0 {
record["allowed_upgrade_versions"] = addOn.AllowedUpgradeVersion
}
if &addOn.Deprecated != nil {
record["deprecated"] = addOn.Deprecated
}
if &addOn.HealthState != nil {
record["health_state"] = addOn.HealthState
}
if &addOn.HealthStatus != nil {
record["health_status"] = addOn.HealthStatus
}
if addOn.MinKubeVersion != "" {
record["min_kube_version"] = addOn.MinKubeVersion
}
if addOn.MinOCPVersion != "" {
record["min_ocp_version"] = addOn.MinOCPVersion
}
if addOn.SupportedKubeRange != "" {
record["supported_kube_range"] = addOn.SupportedKubeRange
}
if addOn.TargetVersion != "" {
record["target_version"] = addOn.TargetVersion
}
if &addOn.VlanSpanningRequired != nil {
record["vlan_spanning_required"] = addOn.VlanSpanningRequired
}
addOns = append(addOns, record)
}
return addOns, nil
}