-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_cloud_project_containerregistry_oidc.go
203 lines (172 loc) · 5.75 KB
/
resource_cloud_project_containerregistry_oidc.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 ovh
import (
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCloudProjectContainerRegistryOIDC() *schema.Resource {
return &schema.Resource{
Create: resourceCloudProjectContainerRegistryOIDCCreate,
Read: resourceCloudProjectContainerRegistryOIDCRead,
Delete: resourceCloudProjectContainerRegistryOIDCDelete,
Update: resourceCloudProjectContainerRegistryOIDCUpdate,
Importer: &schema.ResourceImporter{
State: resourceCloudProjectContainerRegistryOIDCImportState,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Default: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"registry_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"delete_users": {
Type: schema.TypeBool,
Required: false,
Optional: true,
ForceNew: true,
},
"oidc_name": {
Type: schema.TypeString,
Required: true,
},
"oidc_endpoint": {
Type: schema.TypeString,
Required: true,
},
"oidc_client_id": {
Type: schema.TypeString,
Required: true,
},
"oidc_client_secret": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"oidc_scope": {
Type: schema.TypeString,
Required: true,
},
"oidc_groups_claim": {
Type: schema.TypeString,
Required: false,
Optional: true,
},
"oidc_admin_group": {
Type: schema.TypeString,
Required: false,
Optional: true,
},
"oidc_verify_cert": {
Type: schema.TypeBool,
Required: false,
Optional: true,
},
"oidc_auto_onboard": {
Type: schema.TypeBool,
Required: false,
Optional: true,
},
"oidc_user_claim": {
Type: schema.TypeString,
Required: false,
Optional: true,
},
},
}
}
func resourceCloudProjectContainerRegistryOIDCImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
log.Printf("[DEBUG] Importing cloud project registry OIDC %s", givenId)
splitId := strings.SplitN(givenId, "/", 3)
if len(splitId) != 2 {
return nil, fmt.Errorf("import Id is not service_name/registryid formatted")
}
serviceName := splitId[0]
registryID := splitId[1]
d.SetId(serviceName + "/" + registryID)
d.Set("registry_id", registryID)
d.Set("service_name", serviceName)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceCloudProjectContainerRegistryOIDCCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
registryID := d.Get("registry_id").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/containerRegistry/%s/openIdConnect", serviceName, registryID)
params := (&CloudProjectContainerRegistryOIDCCreateOpts{}).FromResource(d)
res := &CloudProjectKubeOIDCResponse{}
log.Printf("[DEBUG] Will create registry %s OIDC: %+v", registryID, params)
err := config.OVHClient.Post(endpoint, params, res)
if err != nil {
return fmt.Errorf("calling Post %s with params %v:\n\t %w", endpoint, params, err)
}
d.SetId(serviceName + "/" + registryID)
log.Printf("[DEBUG] Registry %s OIDC created", registryID)
return resourceCloudProjectContainerRegistryOIDCRead(d, meta)
}
func resourceCloudProjectContainerRegistryOIDCRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
registryID := d.Get("registry_id").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/containerRegistry/%s/openIdConnect", serviceName, registryID)
res := &CloudProjectContainerRegistryOIDCResponse{}
log.Printf("[DEBUG] Will read oidc from registry %s and project: %s", registryID, serviceName)
err := config.OVHClient.Get(endpoint, res)
if err != nil {
return fmt.Errorf("calling get %s %w", endpoint, err)
}
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(serviceName + "/" + registryID)
}
}
log.Printf("[DEBUG] Read registry %+v", res)
return nil
}
func resourceCloudProjectContainerRegistryOIDCUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
registryID := d.Get("registry_id").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/containerRegistry/%s/openIdConnect", serviceName, registryID)
params := (&CloudProjectContainerRegistryOIDCUpdateOpts{}).FromResource(d)
res := &CloudProjectContainerRegistryOIDCResponse{}
log.Printf("[DEBUG] Will update registry %s OIDC: %+v", registryID, params)
err := config.OVHClient.Post(endpoint, params, res)
if err != nil {
return fmt.Errorf("calling Post %s with params %v:\n\t %w", endpoint, params, err)
}
log.Printf("[DEBUG] Registry %s OIDC updated", registryID)
return nil
}
func resourceCloudProjectContainerRegistryOIDCDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
registryID := d.Get("registry_id").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/containerRegistry/%s/openIdConnect", serviceName, registryID)
log.Printf("[DEBUG] Will delete registry %s OIDC", registryID)
err := config.OVHClient.Delete(endpoint, nil)
if err != nil {
return fmt.Errorf("calling delete %s %w", endpoint, err)
}
log.Printf("[DEBUG] Registry %s OIDC deleted", registryID)
return nil
}