forked from huaweicloud/terraform-provider-huaweicloud
/
data_source_huaweicloud_cce_cluster_v3.go
158 lines (142 loc) · 3.77 KB
/
data_source_huaweicloud_cce_cluster_v3.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
package huaweicloud
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/cce/v3/clusters"
)
func dataSourceCCEClusterV3() *schema.Resource {
return &schema.Resource{
Read: dataSourceCCEClusterV3Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"flavor_id": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"cluster_version": {
Type: schema.TypeString,
Computed: true,
},
"cluster_type": {
Type: schema.TypeString,
Optional: true,
},
"billing_mode": {
Type: schema.TypeInt,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"highway_subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"container_network_type": {
Type: schema.TypeString,
Computed: true,
},
"container_network_cidr": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"endpoints": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
}},
},
},
}
}
func dataSourceCCEClusterV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cceClient, err := config.cceV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Unable to create HuaweiCloud CCE client : %s", err)
}
listOpts := clusters.ListOpts{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Type: d.Get("cluster_type").(string),
Phase: d.Get("status").(string),
VpcID: d.Get("vpc_id").(string),
}
refinedClusters, err := clusters.List(cceClient, listOpts)
log.Printf("[DEBUG] Value of allClusters: %#v", refinedClusters)
if err != nil {
return fmt.Errorf("Unable to retrieve clusters: %s", err)
}
if len(refinedClusters) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(refinedClusters) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
Cluster := refinedClusters[0]
log.Printf("[DEBUG] Retrieved Clusters using given filter %s: %+v", Cluster.Metadata.Id, Cluster)
var v []map[string]interface{}
for _, endpoint := range Cluster.Status.Endpoints {
mapping := map[string]interface{}{
"url": endpoint.Url,
"type": endpoint.Type,
}
v = append(v, mapping)
}
d.SetId(Cluster.Metadata.Id)
d.Set("name", Cluster.Metadata.Name)
d.Set("flavor_id", Cluster.Spec.Flavor)
d.Set("description", Cluster.Spec.Description)
d.Set("cluster_version", Cluster.Spec.Version)
d.Set("cluster_type", Cluster.Spec.Type)
d.Set("billing_mode", Cluster.Spec.BillingMode)
d.Set("vpc_id", Cluster.Spec.HostNetwork.VpcId)
d.Set("subnet_id", Cluster.Spec.HostNetwork.SubnetId)
d.Set("highway_subnet_id", Cluster.Spec.HostNetwork.HighwaySubnet)
d.Set("container_network_cidr", Cluster.Spec.ContainerNetwork.Cidr)
d.Set("container_network_type", Cluster.Spec.ContainerNetwork.Mode)
d.Set("status", Cluster.Status.Phase)
if err := d.Set("endpoints", v); err != nil {
return err
}
d.Set("region", GetRegion(d, config))
return nil
}