-
Notifications
You must be signed in to change notification settings - Fork 221
/
data_source_rancher2_cluster_template.go
93 lines (82 loc) · 2.1 KB
/
data_source_rancher2_cluster_template.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
package rancher2
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceRancher2ClusterTemplate() *schema.Resource {
return &schema.Resource{
Read: dataSourceRancher2ClusterTemplateRead,
Schema: map[string]*schema.Schema{
"default_revision_id": {
Type: schema.TypeString,
Computed: true,
Description: "Default cluster template revision ID",
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Cluster template description",
},
"members": {
Type: schema.TypeList,
Computed: true,
Description: "Cluster template members",
Elem: &schema.Resource{
Schema: memberFields(),
},
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Cluster template name",
},
"template_revisions": {
Type: schema.TypeList,
Computed: true,
Description: "Cluster template revisions",
Elem: &schema.Resource{
Schema: clusterTemplateRevisionFieldsData(),
},
},
"annotations": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceRancher2ClusterTemplateRead(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}
name := d.Get("name").(string)
description := d.Get("description").(string)
filters := map[string]interface{}{
"name": name,
}
if len(description) > 0 {
filters["description"] = description
}
listOpts := NewListOpts(filters)
clusterTemplates, err := client.ClusterTemplate.List(listOpts)
if err != nil {
return err
}
count := len(clusterTemplates.Data)
if count <= 0 {
return fmt.Errorf("[ERROR] cluster template with name \"%s\" not found", name)
}
if count > 1 {
return fmt.Errorf("[ERROR] found %d cluster template with name \"%s\"", count, name)
}
d.SetId(clusterTemplates.Data[0].ID)
return resourceRancher2ClusterTemplateRead(d, meta)
}