-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_fgs_application_templates.go
133 lines (119 loc) · 3.21 KB
/
data_source_huaweicloud_fgs_application_templates.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
package fgs
import (
"context"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/openstack/fgs/v2/application"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
// @API FunctionGraph GET /v2/{project_id}/fgs/application/templates
func DataSourceFunctionGraphApplicationTemplates() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceFunctionGraphApplicationTemplatesRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"runtime": {
Type: schema.TypeString,
Optional: true,
},
"category": {
Type: schema.TypeString,
Optional: true,
},
"templates": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"runtime": {
Type: schema.TypeString,
Computed: true,
},
"category": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceFunctionGraphApplicationTemplatesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.FgsV2Client(region)
if err != nil {
return diag.Errorf("error creating FunctionGraph v2 client: %s", err)
}
opts := application.ListOpts{
Runtime: d.Get("runtime").(string),
}
templateList, err := application.ListTemplates(client, opts)
if err != nil {
return diag.Errorf("error retrieving application templates: %s", err)
}
randUUID, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(randUUID)
mErr := multierror.Append(
nil,
d.Set("region", region),
d.Set("templates", flattenListTemplates(filterListTemplateByCategory(templateList, d))),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenListTemplates(templates []application.Template) []map[string]interface{} {
if len(templates) < 1 {
return nil
}
result := make([]map[string]interface{}, len(templates))
for i, template := range templates {
result[i] = map[string]interface{}{
"id": template.ID,
"name": template.Name,
"runtime": template.Runtime,
"category": template.Category,
"type": template.Type,
"description": template.Description,
}
}
return result
}
func filterListTemplateByCategory(all []application.Template, d *schema.ResourceData) []application.Template {
if len(all) == 0 {
return nil
}
rst := make([]application.Template, 0, len(all))
for _, v := range all {
if param, ok := d.GetOk("category"); ok && v.Category != param {
continue
}
rst = append(rst, v)
}
return rst
}