-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
data_source_kubernetes_service_version.go
101 lines (81 loc) · 2.7 KB
/
data_source_kubernetes_service_version.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
package azurerm
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmKubernetesServiceVersions() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmKubernetesServiceVersionsRead,
Schema: map[string]*schema.Schema{
"location": azure.SchemaLocation(),
"version_prefix": {
Type: schema.TypeString,
Optional: true,
},
"versions": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"latest_version": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceArmKubernetesServiceVersionsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).containers.ServicesClient
ctx := meta.(*ArmClient).StopContext
location := azure.NormalizeLocation(d.Get("location").(string))
listResp, err := client.ListOrchestrators(ctx, location, "managedClusters")
if err != nil {
if utils.ResponseWasNotFound(listResp.Response) {
return fmt.Errorf("Error: No Kubernetes Service versions found for location %q", location)
}
return fmt.Errorf("Error retrieving Kubernetes Versions in %q: %+v", location, err)
}
lv, err := version.NewVersion("0.0.0")
if err != nil {
return fmt.Errorf("Cannot set version baseline (likely an issue in go-version): %+v", err)
}
var versions []string
versionPrefix := d.Get("version_prefix").(string)
if props := listResp.OrchestratorVersionProfileProperties; props != nil {
if orchestrators := props.Orchestrators; orchestrators != nil {
for _, rawV := range *orchestrators {
if rawV.OrchestratorType == nil || rawV.OrchestratorVersion == nil {
continue
}
orchestratorType := *rawV.OrchestratorType
kubeVersion := *rawV.OrchestratorVersion
if !strings.EqualFold(orchestratorType, "Kubernetes") {
log.Printf("[DEBUG] Orchestrator %q was not Kubernetes", orchestratorType)
continue
}
if versionPrefix != "" && !strings.HasPrefix(kubeVersion, versionPrefix) {
log.Printf("[DEBUG] Version %q doesn't match the prefix %q", kubeVersion, versionPrefix)
continue
}
versions = append(versions, kubeVersion)
v, err := version.NewVersion(kubeVersion)
if err != nil {
log.Printf("[WARN] Cannot parse orchestrator version %q - skipping: %s", kubeVersion, err)
continue
}
if v.GreaterThan(lv) {
lv = v
}
}
}
}
d.SetId(*listResp.ID)
d.Set("versions", versions)
d.Set("latest_version", lv.Original())
return nil
}