generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
resource_template_filters.go
203 lines (169 loc) · 5.99 KB
/
resource_template_filters.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package template_filters
import (
"context"
"net/http"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceTemplateFilters() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness Template Filters.",
ReadContext: resourceTemplateFiltersRead,
UpdateContext: resourceTemplateFiltersCreateOrUpdate,
DeleteContext: resourceTemplateFiltersDelete,
CreateContext: resourceTemplateFiltersCreateOrUpdate,
Importer: helpers.MultiLevelFilterImporter,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Unique identifier of the resource.",
Type: schema.TypeString,
Required: true,
},
"name": {
Description: "Name of the template filters.",
Type: schema.TypeString,
Required: true,
},
"type": {
Description: "Type of template filters. Currently supported types are { Template}",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"Template"}, false),
},
"org_id": {
Description: "Organization Identifier for the Entity.",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "Project Identifier for the Entity.",
Type: schema.TypeString,
Optional: true,
},
"filter_properties": {
Description: "Properties of the filters entity defined in Harness.",
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"filter_type": {
Description: "Corresponding Entity of the filters. Currently supported types are {TemplateSetup, TemplateExecution, Template}.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"TemplateSetup", "TemplateExecution", "Template"}, false),
},
"tags": {
Description: "Tags to associate with the resource. Tags should be in the form `name:value`.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"filter_visibility": {
Description: "This indicates visibility of filters. By default, everyone can view this filter.",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"EveryOne", "OnlyCreator"}, false),
},
},
}
return resource
}
func resourceTemplateFiltersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Id()
type_ := d.Get("type").(string)
resp, httpResp, err := c.FilterApi.TemplategetFilter(ctx, c.AccountId, id, type_, &nextgen.FilterApiTemplategetFilterOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
if err != nil {
return helpers.HandleReadApiError(err, d, httpResp)
}
readTemplateFilter(d, resp.Data)
return nil
}
func resourceTemplateFiltersCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var resp nextgen.ResponseDtoFilter
var httpResp *http.Response
id := d.Id()
filter := buildTemplateFilter(d)
if id == "" {
resp, httpResp, err = c.FilterApi.TemplatepostFilter(ctx, *filter, c.AccountId)
} else {
resp, httpResp, err = c.FilterApi.TemplateupdateFilter(ctx, *filter, c.AccountId)
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readTemplateFilter(d, resp.Data)
return nil
}
func resourceTemplateFiltersDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
type_ := d.Get("type").(string)
_, httpResp, err := c.FilterApi.TemplatedeleteFilter(ctx, c.AccountId, d.Id(), type_, &nextgen.FilterApiTemplatedeleteFilterOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func buildTemplateFilter(d *schema.ResourceData) *nextgen.Filter {
filter := &nextgen.Filter{
FilterProperties: &nextgen.FilterProperties{},
}
if attr, ok := d.GetOk("org_id"); ok {
filter.OrgIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
filter.ProjectIdentifier = attr.(string)
}
if attr, ok := d.GetOk("filter_visibility"); ok {
filter.FilterVisibility = attr.(string)
}
if attr, ok := d.GetOk("name"); ok {
filter.Name = attr.(string)
}
if attr, ok := d.GetOk("identifier"); ok {
filter.Identifier = attr.(string)
}
if attr, ok := d.GetOk("filter_properties"); ok {
config := attr.([]interface{})[0].(map[string]interface{})
if attr, ok := config["filter_type"]; ok {
filter.FilterProperties.FilterType = attr.(string)
}
if attr := config["tags"].(*schema.Set).List(); len(attr) > 0 {
filter.FilterProperties.Tags = helpers.ExpandTags(attr)
}
}
return filter
}
func readTemplateFilter(d *schema.ResourceData, filter *nextgen.Filter) {
d.SetId(filter.Identifier)
d.Set("identifier", filter.Identifier)
d.Set("org_id", filter.OrgIdentifier)
d.Set("project_id", filter.ProjectIdentifier)
d.Set("name", filter.Name)
d.Set("type", filter.FilterProperties.FilterType)
d.Set("filter_visibility", filter.FilterVisibility)
d.Set("filter_properties", []interface{}{
map[string]interface{}{
"filter_type": filter.FilterProperties.FilterType,
"tags": helpers.FlattenTags(filter.FilterProperties.Tags),
},
})
}