-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathresource_rancher2_oidc_client.go
More file actions
175 lines (146 loc) · 4.86 KB
/
resource_rancher2_oidc_client.go
File metadata and controls
175 lines (146 loc) · 4.86 KB
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
package rancher2
import (
"fmt"
"log"
"maps"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
)
func resourceRancher2OIDCClient() *schema.Resource {
return &schema.Resource{
Create: resourceRancher2OIDCClientCreate,
Read: resourceRancher2OIDCClientRead,
Update: resourceRancher2OIDCClientUpdate,
Delete: resourceRancher2OIDCClientDelete,
Importer: &schema.ResourceImporter{
State: resourceRancher2OIDCClientImport,
},
Schema: oidcClientFields(),
}
}
func oidcClientFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"token_expiration_seconds": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The duration (in seconds) before an access token and ID token expire.",
},
"refresh_token_expiration_seconds": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The duration (in seconds) a refresh token remains valid before it expires.",
},
"redirect_uris": {
Description: "List of allowed redirect_uris for this client.",
Required: true,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "OIDCClient description",
},
"client_id": {
Type: schema.TypeString,
Computed: true,
Description: "The Client ID for OIDC Access.",
},
}
maps.Copy(s, commonAnnotationLabelFields())
return s
}
func resourceRancher2OIDCClientCreate(d *schema.ResourceData, meta any) error {
log.Printf("[INFO] Creating OIDCClient")
oidcClient, err := expandOIDCClient(d)
if err != nil {
return err
}
client, err := meta.(managementClientGetter).ManagementClient()
if err != nil {
return fmt.Errorf("getting a client when creating OIDC Client: %w", err)
}
createdClient, err := client.OIDCClient.Create(oidcClient)
if err != nil {
return fmt.Errorf("creating OIDCClient: %w", err)
}
d.SetId(createdClient.ID)
return resourceRancher2OIDCClientRead(d, meta)
}
func resourceRancher2OIDCClientRead(d *schema.ResourceData, meta any) error {
log.Printf("[INFO] Refreshing OIDCClient ID %s", d.Id())
client, err := meta.(managementClientGetter).ManagementClient()
if err != nil {
return fmt.Errorf("getting a client when reading OIDC Client: %w", err)
}
oidcClient, err := client.OIDCClient.ByID(d.Id())
if err != nil {
if IsNotFound(err) || IsForbidden(err) || IsNotAccessibleByID(err) {
d.SetId("")
return nil
}
return fmt.Errorf("reading OIDC Client %s: %w", d.Id(), err)
}
return flattenOIDCClient(d, oidcClient)
}
func resourceRancher2OIDCClientUpdate(d *schema.ResourceData, meta any) error {
log.Printf("[INFO] Updating OIDCClient ID %s", d.Id())
client, err := meta.(managementClientGetter).ManagementClient()
if err != nil {
return fmt.Errorf("getting a client when updating OIDC Client: %w", err)
}
oidcClient, err := client.OIDCClient.ByID(d.Id())
if err != nil {
return fmt.Errorf("getting OIDC Client %s for update: %w", d.Id(), err)
}
update := map[string]any{
"labels": toMapString(d.Get("labels").(map[string]any)),
"annotations": toMapString(d.Get("annotations").(map[string]any)),
"description": d.Get("description"),
"redirectURIs": toArrayString(d.Get("redirect_uris").([]any)),
"tokenExpirationSeconds": d.Get("token_expiration_seconds"),
"refreshTokenExpirationSeconds": d.Get("refresh_token_expiration_seconds"),
}
_, err = client.OIDCClient.Update(oidcClient, update)
if err != nil {
return fmt.Errorf("updating OIDC Client %s: %w", d.Id(), err)
}
return resourceRancher2OIDCClientRead(d, meta)
}
func resourceRancher2OIDCClientDelete(d *schema.ResourceData, meta any) error {
log.Printf("[INFO] Deleting OIDCClient ID %s", d.Id())
client, err := meta.(managementClientGetter).ManagementClient()
if err != nil {
return fmt.Errorf("getting a client when deleting OIDC Client: %w", err)
}
oidcClient, err := client.OIDCClient.ByID(d.Id())
if err != nil {
if IsNotFound(err) || IsForbidden(err) || IsNotAccessibleByID(err) {
d.SetId("")
return nil
}
return fmt.Errorf("getting OIDC Client %s for deletion: %w", d.Id(), err)
}
err = client.OIDCClient.Delete(oidcClient)
if err != nil {
if !IsNotFound(err) {
return fmt.Errorf("deleting OIDC Client %s: %w", d.Id(), err)
}
}
d.SetId("")
return nil
}
func resourceRancher2OIDCClientImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
err := resourceRancher2OIDCClientRead(d, meta)
if err != nil || d.Id() == "" {
return []*schema.ResourceData{}, err
}
return []*schema.ResourceData{d}, nil
}
type managementClientGetter interface {
ManagementClient() (*managementClient.Client, error)
}