generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
apiKey.go
205 lines (169 loc) · 5.65 KB
/
apiKey.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
204
205
package apikey
import (
"context"
"net/http"
"github.com/antihax/optional"
"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 ResourceApiKey() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating ApiKeys.",
ReadContext: resourceApiKeyRead,
CreateContext: resourceApiKeyCreateOrUpdate,
UpdateContext: resourceApiKeyCreateOrUpdate,
DeleteContext: resourceApiKeyDelete,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Identifier of the API Key",
Type: schema.TypeString,
Required: true,
},
"name": {
Description: "Name of the API Key",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "Description of the API Key",
Type: schema.TypeString,
Optional: true,
},
"tags": {
Description: "Tags for the API Key",
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"apikey_type": {
Description: "Type of the API Key",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"USER", "SERVICE_ACCOUNT"}, false),
},
"parent_id": {
Description: "Parent Entity Identifier of the API Key",
Type: schema.TypeString,
Required: true,
},
"default_time_to_expire_token": {
Description: "Default expiration time of the Token within API Key",
Type: schema.TypeInt,
Optional: true,
},
"account_id": {
Description: "Account Identifier for the Entity",
Type: schema.TypeString,
Required: true,
},
"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,
},
},
}
return resource
}
func resourceApiKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Id()
type_ := d.Get("apikey_type").(string)
parentId := d.Get("parent_id").(string)
resp, httpResp, err := c.ApiKeyApi.GetAggregatedApiKey(ctx, c.AccountId, type_, parentId, id, &nextgen.ApiKeyApiGetAggregatedApiKeyOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readApiKey(d, resp.Data.ApiKey)
return nil
}
func resourceApiKeyCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var resp nextgen.ResponseDtoApiKey
var httpResp *http.Response
id := d.Id()
apiKey := buildApiKey(d)
if id == "" {
resp, httpResp, err = c.ApiKeyApi.CreateApiKey(ctx, c.AccountId, &nextgen.ApiKeyApiCreateApiKeyOpts{Body: optional.NewInterface(apiKey)})
} else {
resp, httpResp, err = c.ApiKeyApi.UpdateApiKey(ctx, c.AccountId, &nextgen.ApiKeyApiUpdateApiKeyOpts{Body: optional.NewInterface(apiKey)})
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readApiKey(d, resp.Data)
return nil
}
func resourceApiKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
type_ := d.Get("apikey_type").(string)
parentId := d.Get("parent_id").(string)
_, httpResp, err := c.ApiKeyApi.DeleteApiKey(ctx, c.AccountId, type_, parentId, d.Id(), &nextgen.ApiKeyApiDeleteApiKeyOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func buildApiKey(d *schema.ResourceData) *nextgen.ApiKey {
apiKey := &nextgen.ApiKey{}
if attr, ok := d.GetOk("identifier"); ok {
apiKey.Identifier = attr.(string)
}
if attr, ok := d.GetOk("name"); ok {
apiKey.Name = attr.(string)
}
if attr, ok := d.GetOk("description"); ok {
apiKey.Description = attr.(string)
}
if attr, ok := d.GetOk("tags"); ok {
apiKey.Tags = attr.(map[string]string)
}
if attr, ok := d.GetOk("apikey_type"); ok {
apiKey.ApiKeyType = attr.(string)
}
if attr, ok := d.GetOk("parent_id"); ok {
apiKey.ParentIdentifier = attr.(string)
}
if attr, ok := d.GetOk("default_time_to_expire_token"); ok {
apiKey.DefaultTimeToExpireToken = int64(attr.(int))
}
if attr, ok := d.GetOk("account_id"); ok {
apiKey.AccountIdentifier = attr.(string)
}
if attr, ok := d.GetOk("org_id"); ok {
apiKey.OrgIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
apiKey.ProjectIdentifier = attr.(string)
}
return apiKey
}
func readApiKey(d *schema.ResourceData, apiKey *nextgen.ApiKey) {
d.SetId(apiKey.Identifier)
d.Set("name", apiKey.Name)
d.Set("description", apiKey.Description)
d.Set("tags", apiKey.Tags)
d.Set("apikey_type", apiKey.ApiKeyType)
d.Set("parent_id", apiKey.ParentIdentifier)
d.Set("default_time_to_expire_token", apiKey.DefaultTimeToExpireToken)
d.Set("account_id", apiKey.AccountIdentifier)
d.Set("project_id", apiKey.ProjectIdentifier)
d.Set("org_id", apiKey.OrgIdentifier)
}