Skip to content

Commit 08f7cb9

Browse files
authored
feat: improve OAuth secret handling and support k8s agent environments
1 parent f3cd0ec commit 08f7cb9

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

docs/resources/environment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ resource "meu_portainer_environment" "docker_tls" {
9797
| `name` | string | ✅ yes | Display name of the environment in Portainer. |
9898
| `environment_address` | string | ✅ yes | Target environment address (e.g. `tcp://host:9001`). |
9999
| `public_ip` | string | 🚫 optional | Public URL/IP shown in Portainer UI (maps to `PublicURL` field). Useful for correct Published Ports rendering. |
100-
| `type` | int | ✅ yes | Environment type: `1` = Docker, `2` = Agent, `3` = Azure, `4` = Edge Agent, `5` = Kubernetes. |
100+
| `type` | int | ✅ yes | Environment type: `1` = Docker, `2` = Agent, `3` = Azure, `4` = Edge Agent, `5` = Kubernetes, `6` = Kubernetes via Agent |
101101
| `group_id` | int | 🚫 optional (default `1`) | ID of the Portainer endpoint group. Default is `1` (Unassigned). |
102102
| `tag_ids` | list(int) | 🚫 optional | List of Portainer tag IDs to assign to the environment. Only used during creation. |
103103
| `tls_enabled` | bool | 🚫 optional (default `true`) | Enable TLS for connection to the agent. Must be `true` for agent-based environments. |

internal/resource_environment.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func resourceEnvironment() *schema.Resource {
3838
"type": {
3939
Type: schema.TypeInt,
4040
Required: true,
41-
Description: "Environment type: 1 = Docker, 2 = Agent, 3 = Azure, 4 = Edge Agent, 5 = Kubernetes",
41+
Description: "Environment type: 1 = Docker, 2 = Agent, 3 = Azure, 4 = Edge Agent, 5 = Kubernetes, 6 = Kubernetes via agent",
4242
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
4343
t := val.(int)
44-
if t < 1 || t > 5 {
45-
errs = append(errs, fmt.Errorf("%q must be between 1 and 5", key))
44+
if t < 1 || t > 6 {
45+
errs = append(errs, fmt.Errorf("%q must be between 1 and 6", key))
4646
}
4747
return
4848
},
@@ -123,9 +123,15 @@ func resourceEnvironmentCreate(d *schema.ResourceData, meta interface{}) error {
123123
var requestBody bytes.Buffer
124124
writer := multipart.NewWriter(&requestBody)
125125

126+
envType := d.Get("type").(int)
127+
endpointCreationType := envType
128+
if envType == 6 {
129+
endpointCreationType = 2
130+
}
131+
132+
_ = writer.WriteField("EndpointCreationType", strconv.Itoa(endpointCreationType))
126133
_ = writer.WriteField("Name", d.Get("name").(string))
127134
_ = writer.WriteField("URL", d.Get("environment_address").(string))
128-
_ = writer.WriteField("EndpointCreationType", strconv.Itoa(d.Get("type").(int)))
129135
_ = writer.WriteField("GroupID", strconv.Itoa(d.Get("group_id").(int)))
130136
_ = writer.WriteField("TLS", strconv.FormatBool(d.Get("tls_enabled").(bool)))
131137
_ = writer.WriteField("TLSSkipVerify", strconv.FormatBool(d.Get("tls_skip_verify").(bool)))

internal/resource_settings.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ func resourceSettings() *schema.Resource {
203203
Optional: true,
204204
Computed: true,
205205
Sensitive: true,
206-
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
207-
return old == "" || new == ""
208-
},
206+
// DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
207+
// return old == "" || new == ""
208+
// },
209209
},
210210
"default_team_id": {Type: schema.TypeInt, Optional: true, Computed: true},
211211
"logout_uri": {Type: schema.TypeString, Optional: true, Computed: true},
@@ -238,9 +238,9 @@ func resourceSettings() *schema.Resource {
238238
Optional: true,
239239
Computed: true,
240240
Sensitive: true,
241-
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
242-
return old == "" || new == ""
243-
},
241+
// DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
242+
// return old == "" || new == ""
243+
// },
244244
},
245245
"reader_dn": {Type: schema.TypeString, Optional: true, Computed: true},
246246
"start_tls": {Type: schema.TypeBool, Optional: true, Computed: true},
@@ -561,11 +561,13 @@ func resourceSettingsRead(d *schema.ResourceData, meta interface{}) error {
561561
"kube_secret_key": result.OAuthSettings.KubeSecretKey,
562562
}
563563

564-
if currentOAuth, ok := d.GetOk("oauth_settings"); ok {
565-
if items := currentOAuth.([]interface{}); len(items) > 0 {
566-
if current := items[0].(map[string]interface{}); current != nil {
567-
if secret := current["client_secret"]; secret != "" {
568-
oauth["client_secret"] = secret
564+
if currentOAuthRaw, ok := d.GetOk("oauth_settings"); ok {
565+
if list, ok := currentOAuthRaw.([]interface{}); ok && len(list) > 0 && list[0] != nil {
566+
if currentMap, ok := list[0].(map[string]interface{}); ok {
567+
if secretRaw, ok := currentMap["client_secret"]; ok {
568+
if secretStr, ok := secretRaw.(string); ok && secretStr != "" {
569+
oauth["client_secret"] = secretStr
570+
}
569571
}
570572
}
571573
}

0 commit comments

Comments
 (0)