@@ -64,17 +64,17 @@ func New() *schema.Provider {
64
64
// Default to start!
65
65
transition = "start"
66
66
}
67
- rd .Set ("transition" ,transition )
67
+ _ = rd .Set ("transition" ,transition )
68
68
count := 0
69
69
if transition == "start" {
70
70
count = 1
71
71
}
72
- rd .Set ("start_count" ,count )
72
+ _ = rd .Set ("start_count" ,count )
73
73
owner := os .Getenv ("CODER_WORKSPACE_OWNER" )
74
74
if owner == "" {
75
75
owner = "default"
76
76
}
77
- rd .Set ("owner" ,owner )
77
+ _ = rd .Set ("owner" ,owner )
78
78
name := os .Getenv ("CODER_WORKSPACE_NAME" )
79
79
if name == "" {
80
80
name = "default"
@@ -109,46 +109,17 @@ func New() *schema.Provider {
109
109
ResourcesMap :map [string ]* schema.Resource {
110
110
"coder_agent" : {
111
111
Description :"Use this resource to associate an agent." ,
112
- CreateContext :func (c context.Context ,rd * schema.ResourceData ,i interface {}) diag.Diagnostics {
112
+ CreateContext :func (c context.Context ,resourceData * schema.ResourceData ,i interface {}) diag.Diagnostics {
113
113
// This should be a real authentication token!
114
- rd .SetId (uuid .NewString ())
115
- err := rd .Set ("token" ,uuid .NewString ())
114
+ resourceData .SetId (uuid .NewString ())
115
+ err := resourceData .Set ("token" ,uuid .NewString ())
116
116
if err != nil {
117
117
return diag .FromErr (err )
118
118
}
119
- return nil
119
+ return updateInitScript ( resourceData , i )
120
120
},
121
121
ReadContext :func (c context.Context ,resourceData * schema.ResourceData ,i interface {}) diag.Diagnostics {
122
- config ,valid := i .(config )
123
- if ! valid {
124
- return diag .Errorf ("config was unexpected type %q" ,reflect .TypeOf (i ).String ())
125
- }
126
- auth ,valid := resourceData .Get ("auth" ).(string )
127
- if ! valid {
128
- return diag .Errorf ("auth was unexpected type %q" ,reflect .TypeOf (resourceData .Get ("auth" )))
129
- }
130
- operatingSystem ,valid := resourceData .Get ("os" ).(string )
131
- if ! valid {
132
- return diag .Errorf ("os was unexpected type %q" ,reflect .TypeOf (resourceData .Get ("os" )))
133
- }
134
- arch ,valid := resourceData .Get ("arch" ).(string )
135
- if ! valid {
136
- return diag .Errorf ("arch was unexpected type %q" ,reflect .TypeOf (resourceData .Get ("arch" )))
137
- }
138
- accessURL ,err := config .URL .Parse ("/" )
139
- if err != nil {
140
- return diag .Errorf ("parse access url: %s" ,err )
141
- }
142
- script := os .Getenv (fmt .Sprintf ("CODER_AGENT_SCRIPT_%s_%s" ,operatingSystem ,arch ))
143
- if script != "" {
144
- script = strings .ReplaceAll (script ,"${ACCESS_URL}" ,accessURL .String ())
145
- script = strings .ReplaceAll (script ,"${AUTH_TYPE}" ,auth )
146
- }
147
- err = resourceData .Set ("init_script" ,script )
148
- if err != nil {
149
- return diag .FromErr (err )
150
- }
151
- return nil
122
+ return updateInitScript (resourceData ,i )
152
123
},
153
124
DeleteContext :func (c context.Context ,rd * schema.ResourceData ,i interface {}) diag.Diagnostics {
154
125
return nil
@@ -234,3 +205,38 @@ func New() *schema.Provider {
234
205
},
235
206
}
236
207
}
208
+
209
+ // updateInitScript fetches parameters from a "coder_agent" to produce the
210
+ // agent script from environment variables.
211
+ func updateInitScript (resourceData * schema.ResourceData ,i interface {}) diag.Diagnostics {
212
+ config ,valid := i .(config )
213
+ if ! valid {
214
+ return diag .Errorf ("config was unexpected type %q" ,reflect .TypeOf (i ).String ())
215
+ }
216
+ auth ,valid := resourceData .Get ("auth" ).(string )
217
+ if ! valid {
218
+ return diag .Errorf ("auth was unexpected type %q" ,reflect .TypeOf (resourceData .Get ("auth" )))
219
+ }
220
+ operatingSystem ,valid := resourceData .Get ("os" ).(string )
221
+ if ! valid {
222
+ return diag .Errorf ("os was unexpected type %q" ,reflect .TypeOf (resourceData .Get ("os" )))
223
+ }
224
+ arch ,valid := resourceData .Get ("arch" ).(string )
225
+ if ! valid {
226
+ return diag .Errorf ("arch was unexpected type %q" ,reflect .TypeOf (resourceData .Get ("arch" )))
227
+ }
228
+ accessURL ,err := config .URL .Parse ("/" )
229
+ if err != nil {
230
+ return diag .Errorf ("parse access url: %s" ,err )
231
+ }
232
+ script := os .Getenv (fmt .Sprintf ("CODER_AGENT_SCRIPT_%s_%s" ,operatingSystem ,arch ))
233
+ if script != "" {
234
+ script = strings .ReplaceAll (script ,"${ACCESS_URL}" ,accessURL .String ())
235
+ script = strings .ReplaceAll (script ,"${AUTH_TYPE}" ,auth )
236
+ }
237
+ err = resourceData .Set ("init_script" ,script )
238
+ if err != nil {
239
+ return diag .FromErr (err )
240
+ }
241
+ return nil
242
+ }