generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
input_set_resource.go
167 lines (127 loc) · 4.63 KB
/
input_set_resource.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
package input_set
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"
)
func ResourceInputSet() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness Resource Group",
ReadContext: resourceInputSetRead,
UpdateContext: resourceInputSetCreateOrUpdate,
CreateContext: resourceInputSetCreateOrUpdate,
DeleteContext: resourceInputSetDelete,
Importer: helpers.PipelineResourceImporter,
Schema: map[string]*schema.Schema{
"pipeline_id": {
Description: "Identifier of the pipeline",
Type: schema.TypeString,
Required: true,
},
"yaml": {
Description: "Input Set YAML",
Type: schema.TypeString,
Required: true,
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourceInputSetRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Get("identifier").(string)
orgId := d.Get("org_id").(string)
projectId := d.Get("project_id").(string)
pipelineId := d.Get("pipeline_id").(string)
resp, httpResp, err := c.InputSetsApi.GetInputSet(ctx, id, c.AccountId, orgId, projectId, pipelineId, &nextgen.InputSetsApiGetInputSetOpts{})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
if resp.Data == nil {
return nil
}
readInputSet(d, resp.Data)
return nil
}
func resourceInputSetCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var resp nextgen.ResponseDtoInputSetResponse
var httpResp *http.Response
id := d.Id()
inputSet := buildInputSet(d)
orgIdentifier := d.Get("org_id").(string)
projectIdentifier := d.Get("project_id").(string)
pipelineIdentifier := d.Get("pipeline_id").(string)
if id == "" {
resp, httpResp, err = c.InputSetsApi.PostInputSet(ctx, inputSet.InputSetYaml, c.AccountId, orgIdentifier, projectIdentifier, pipelineIdentifier,
&nextgen.InputSetsApiPostInputSetOpts{})
} else {
resp, httpResp, err = c.InputSetsApi.PutInputSet(ctx, inputSet.InputSetYaml, c.AccountId, orgIdentifier, projectIdentifier, pipelineIdentifier, d.Id(),
&nextgen.InputSetsApiPutInputSetOpts{})
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readInputSet(d, resp.Data)
return nil
}
func resourceInputSetDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
orgIdentifier := helpers.BuildField(d, "org_id").Value()
projectIdentifier := helpers.BuildField(d, "project_id").Value()
pipelineIdentifier := helpers.BuildField(d, "pipeline_id").Value()
_, httpResp, err := c.InputSetsApi.DeleteInputSet(ctx, d.Id(), c.AccountId, orgIdentifier, projectIdentifier, pipelineIdentifier,
&nextgen.InputSetsApiDeleteInputSetOpts{})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func buildInputSet(d *schema.ResourceData) *nextgen.InputSetResponse {
inputSet := &nextgen.InputSetResponse{}
if attr, ok := d.GetOk("account_id"); ok {
inputSet.AccountId = attr.(string)
}
if attr, ok := d.GetOk("org_id"); ok {
inputSet.OrgIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
inputSet.ProjectIdentifier = attr.(string)
}
if attr, ok := d.GetOk("pipeline_id"); ok {
inputSet.PipelineIdentifier = attr.(string)
}
if attr, ok := d.GetOk("identifier"); ok {
inputSet.Identifier = attr.(string)
}
if attr, ok := d.GetOk("yaml"); ok {
inputSet.InputSetYaml = attr.(string)
}
if attr, ok := d.GetOk("name"); ok {
inputSet.Name = attr.(string)
}
if attr, ok := d.GetOk("description"); ok {
inputSet.Description = attr.(string)
}
if attr := d.Get("tags").(*schema.Set).List(); len(attr) > 0 {
inputSet.Tags = helpers.ExpandTags(attr)
}
return inputSet
}
func readInputSet(d *schema.ResourceData, inputSet *nextgen.InputSetResponse) {
d.SetId(inputSet.Identifier)
d.Set("identifier", inputSet.Identifier)
d.Set("name", inputSet.Name)
d.Set("description", inputSet.Description)
d.Set("tags", helpers.FlattenTags(inputSet.Tags))
d.Set("org_id", inputSet.OrgIdentifier)
d.Set("project_id", inputSet.ProjectIdentifier)
d.Set("pipeline_id", inputSet.PipelineIdentifier)
d.Set("yaml", inputSet.InputSetYaml)
}