File tree Expand file tree Collapse file tree 7 files changed +121
-53
lines changed Expand file tree Collapse file tree 7 files changed +121
-53
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ package provider
2
2
3
3
import (
4
4
"context"
5
- "net/url "
5
+ "github.com/coder/terraform-provider-coder/v2/provider/helpers "
6
6
"regexp"
7
7
8
8
"github.com/google/uuid"
@@ -93,15 +93,9 @@ func appResource() *schema.Resource {
93
93
Description : "A URL to an icon that will display in the dashboard. View built-in " +
94
94
"icons [here](https://github.com/coder/coder/tree/main/site/static/icon). Use a " +
95
95
"built-in icon with `\" ${data.coder_workspace.me.access_url}/icon/<path>\" `." ,
96
- ForceNew : true ,
97
- Optional : true ,
98
- ValidateFunc : func (i any , s string ) ([]string , []error ) {
99
- _ , err := url .Parse (s )
100
- if err != nil {
101
- return nil , []error {err }
102
- }
103
- return nil , nil
104
- },
96
+ ForceNew : true ,
97
+ Optional : true ,
98
+ ValidateFunc : helpers .ValidateURL ,
105
99
},
106
100
"slug" : {
107
101
Type : schema .TypeString ,
Original file line number Diff line number Diff line change @@ -478,4 +478,61 @@ func TestApp(t *testing.T) {
478
478
})
479
479
}
480
480
})
481
+
482
+ t .Run ("Icon" , func (t * testing.T ) {
483
+ t .Parallel ()
484
+
485
+ cases := []struct {
486
+ name string
487
+ icon string
488
+ expectError * regexp.Regexp
489
+ }{
490
+ {
491
+ name : "Empty" ,
492
+ icon : "" ,
493
+ },
494
+ {
495
+ name : "ValidURL" ,
496
+ icon : "/icon/region.svg" ,
497
+ },
498
+ {
499
+ name : "InvalidURL" ,
500
+ icon : "/icon%.svg" ,
501
+ expectError : regexp .MustCompile ("invalid URL escape" ),
502
+ },
503
+ }
504
+
505
+ for _ , c := range cases {
506
+ c := c
507
+
508
+ t .Run (c .name , func (t * testing.T ) {
509
+ t .Parallel ()
510
+
511
+ config := fmt .Sprintf (`
512
+ provider "coder" {}
513
+ resource "coder_agent" "dev" {
514
+ os = "linux"
515
+ arch = "amd64"
516
+ }
517
+ resource "coder_app" "code-server" {
518
+ agent_id = coder_agent.dev.id
519
+ slug = "code-server"
520
+ display_name = "Testing"
521
+ url = "http://localhost:13337"
522
+ open_in = "slim-window"
523
+ icon = "%s"
524
+ }
525
+ ` , c .icon )
526
+
527
+ resource .Test (t , resource.TestCase {
528
+ ProviderFactories : coderFactory (),
529
+ IsUnitTest : true ,
530
+ Steps : []resource.TestStep {{
531
+ Config : config ,
532
+ ExpectError : c .expectError ,
533
+ }},
534
+ })
535
+ })
536
+ }
537
+ })
481
538
}
Original file line number Diff line number Diff line change
1
+ package helpers
2
+
3
+ import (
4
+ "fmt"
5
+ "net/url"
6
+ )
7
+
8
+ // ValidateURL checks that the given value is a valid URL string.
9
+ // Example: for `icon = "/icon/region.svg"`, value is `/icon/region.svg` and label is `icon`.
10
+ func ValidateURL (value interface {}, label string ) ([]string , []error ) {
11
+ val , ok := value .(string )
12
+ if ! ok {
13
+ return nil , []error {fmt .Errorf ("expected %q to be a string" , label )}
14
+ }
15
+ if _ , err := url .Parse (val ); err != nil {
16
+ return nil , []error {err }
17
+ }
18
+ return nil , nil
19
+ }
Original file line number Diff line number Diff line change @@ -2,8 +2,7 @@ package provider
2
2
3
3
import (
4
4
"context"
5
- "net/url"
6
-
5
+ "github.com/coder/terraform-provider-coder/v2/provider/helpers"
7
6
"github.com/google/uuid"
8
7
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9
8
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -56,15 +55,9 @@ func metadataResource() *schema.Resource {
56
55
Description : "A URL to an icon that will display in the dashboard. View built-in " +
57
56
"icons [here](https://github.com/coder/coder/tree/main/site/static/icon). Use a " +
58
57
"built-in icon with `\" ${data.coder_workspace.me.access_url}/icon/<path>\" `." ,
59
- ForceNew : true ,
60
- Optional : true ,
61
- ValidateFunc : func (i interface {}, s string ) ([]string , []error ) {
62
- _ , err := url .Parse (s )
63
- if err != nil {
64
- return nil , []error {err }
65
- }
66
- return nil , nil
67
- },
58
+ ForceNew : true ,
59
+ Optional : true ,
60
+ ValidateFunc : helpers .ValidateURL ,
68
61
},
69
62
"daily_cost" : {
70
63
Type : schema .TypeInt ,
Original file line number Diff line number Diff line change 6
6
"encoding/hex"
7
7
"encoding/json"
8
8
"fmt"
9
- "net/url "
9
+ "github.com/coder/terraform-provider-coder/v2/provider/helpers "
10
10
"os"
11
11
"regexp"
12
12
"strconv"
@@ -223,15 +223,9 @@ func parameterDataSource() *schema.Resource {
223
223
Description : "A URL to an icon that will display in the dashboard. View built-in " +
224
224
"icons [here](https://github.com/coder/coder/tree/main/site/static/icon). Use a " +
225
225
"built-in icon with `\" ${data.coder_workspace.me.access_url}/icon/<path>\" `." ,
226
- ForceNew : true ,
227
- Optional : true ,
228
- ValidateFunc : func (i interface {}, s string ) ([]string , []error ) {
229
- _ , err := url .Parse (s )
230
- if err != nil {
231
- return nil , []error {err }
232
- }
233
- return nil , nil
234
- },
226
+ ForceNew : true ,
227
+ Optional : true ,
228
+ ValidateFunc : helpers .ValidateURL ,
235
229
},
236
230
"option" : {
237
231
Type : schema .TypeList ,
@@ -263,15 +257,9 @@ func parameterDataSource() *schema.Resource {
263
257
Description : "A URL to an icon that will display in the dashboard. View built-in " +
264
258
"icons [here](https://github.com/coder/coder/tree/main/site/static/icon). Use a " +
265
259
"built-in icon with `\" ${data.coder_workspace.me.access_url}/icon/<path>\" `." ,
266
- ForceNew : true ,
267
- Optional : true ,
268
- ValidateFunc : func (i interface {}, s string ) ([]string , []error ) {
269
- _ , err := url .Parse (s )
270
- if err != nil {
271
- return nil , []error {err }
272
- }
273
- return nil , nil
274
- },
260
+ ForceNew : true ,
261
+ Optional : true ,
262
+ ValidateFunc : helpers .ValidateURL ,
275
263
},
276
264
},
277
265
},
Original file line number Diff line number Diff line change @@ -234,10 +234,6 @@ func TestParameter(t *testing.T) {
234
234
icon = "/icon/code.svg"
235
235
description = "Something!"
236
236
}
237
- option {
238
- name = "2"
239
- value = "2"
240
- }
241
237
}
242
238
` ,
243
239
Check : func (state * terraform.ResourceState ) {
@@ -665,7 +661,33 @@ data "coder_parameter" "region" {
665
661
}
666
662
` ,
667
663
ExpectError : regexp .MustCompile ("ephemeral parameter requires the default property" ),
668
- }} {
664
+ }, {
665
+ Name : "InvalidIconURL" ,
666
+ Config : `
667
+ data "coder_parameter" "region" {
668
+ name = "Region"
669
+ type = "string"
670
+ icon = "/icon%.svg"
671
+ }
672
+ ` ,
673
+ ExpectError : regexp .MustCompile ("invalid URL escape" ),
674
+ }, {
675
+ Name : "OptionInvalidIconURL" ,
676
+ Config : `
677
+ data "coder_parameter" "region" {
678
+ name = "Region"
679
+ type = "string"
680
+ option {
681
+ name = "1"
682
+ value = "1"
683
+ icon = "/icon%.svg"
684
+ description = "Something!"
685
+ }
686
+ }
687
+ ` ,
688
+ ExpectError : regexp .MustCompile ("foo" ),
689
+ },
690
+ } {
669
691
tc := tc
670
692
t .Run (tc .Name , func (t * testing.T ) {
671
693
t .Parallel ()
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package provider
2
2
3
3
import (
4
4
"context"
5
+ "github.com/coder/terraform-provider-coder/v2/provider/helpers"
5
6
"net/url"
6
7
"reflect"
7
8
"strings"
@@ -26,14 +27,8 @@ func New() *schema.Provider {
26
27
Optional : true ,
27
28
// The "CODER_AGENT_URL" environment variable is used by default
28
29
// as the Access URL when generating scripts.
29
- DefaultFunc : schema .EnvDefaultFunc ("CODER_AGENT_URL" , "https://mydeployment.coder.com" ),
30
- ValidateFunc : func (i interface {}, s string ) ([]string , []error ) {
31
- _ , err := url .Parse (s )
32
- if err != nil {
33
- return nil , []error {err }
34
- }
35
- return nil , nil
36
- },
30
+ DefaultFunc : schema .EnvDefaultFunc ("CODER_AGENT_URL" , "https://mydeployment.coder.com" ),
31
+ ValidateFunc : helpers .ValidateURL ,
37
32
},
38
33
},
39
34
ConfigureContextFunc : func (c context.Context , resourceData * schema.ResourceData ) (interface {}, diag.Diagnostics ) {
You can’t perform that action at this time.
0 commit comments