Skip to content

Commit 7e3d827

Browse files
committed
fix: Update agent script on create and read
The prior behavior led to create not having an agent script. This ensures it's set on create and update, leaving no room for an empty state!
1 parent 578c648 commit 7e3d827

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

Diff for: internal/provider/provider.go

+43-37
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ func New() *schema.Provider {
6464
// Default to start!
6565
transition = "start"
6666
}
67-
rd.Set("transition", transition)
67+
_ = rd.Set("transition", transition)
6868
count := 0
6969
if transition == "start" {
7070
count = 1
7171
}
72-
rd.Set("start_count", count)
72+
_ = rd.Set("start_count", count)
7373
owner := os.Getenv("CODER_WORKSPACE_OWNER")
7474
if owner == "" {
7575
owner = "default"
7676
}
77-
rd.Set("owner", owner)
77+
_ = rd.Set("owner", owner)
7878
name := os.Getenv("CODER_WORKSPACE_NAME")
7979
if name == "" {
8080
name = "default"
@@ -109,46 +109,17 @@ func New() *schema.Provider {
109109
ResourcesMap: map[string]*schema.Resource{
110110
"coder_agent": {
111111
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 {
113113
// 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())
116116
if err != nil {
117117
return diag.FromErr(err)
118118
}
119-
return nil
119+
return updateInitScript(resourceData, i)
120120
},
121121
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)
152123
},
153124
DeleteContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
154125
return nil
@@ -234,3 +205,38 @@ func New() *schema.Provider {
234205
},
235206
}
236207
}
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+
}

0 commit comments

Comments
 (0)