Skip to content

Commit 8b7a582

Browse files
committed
add stubs for other methods
1 parent 30313a8 commit 8b7a582

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

internal/provider/cached_image_resource.go

+81-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"os"
1313
"path/filepath"
14+
"strings"
1415

1516
kconfig "github.com/GoogleContainerTools/kaniko/pkg/config"
1617
"github.com/coder/envbuilder"
@@ -245,6 +246,42 @@ func (r *CachedImageResource) Read(ctx context.Context, req resource.ReadRequest
245246
return
246247
}
247248

249+
data.Image = data.BuilderImage
250+
if data.Image.IsUnknown() {
251+
return
252+
}
253+
254+
img, err := getRemoteImage(data.Image.ValueString())
255+
if err != nil {
256+
if !strings.Contains(err.Error(), "NAME_UNKNOWN") {
257+
resp.Diagnostics.AddError("Error checking remote image", err.Error())
258+
return
259+
}
260+
// Image does not exist
261+
return
262+
}
263+
264+
// Found image!
265+
digest, err := img.Digest()
266+
if err != nil {
267+
resp.Diagnostics.AddError("Error fetching image digest", err.Error())
268+
return
269+
}
270+
271+
data.ID = types.StringValue(digest.String())
272+
data.Image = types.StringValue(fmt.Sprintf("%s@%s", data.CacheRepo.ValueString(), digest))
273+
data.Exists = types.BoolValue(true)
274+
275+
for key, elem := range data.ExtraEnv.Elements() {
276+
data.Env = appendKnownEnvToList(data.Env, key, elem)
277+
}
278+
279+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_CACHE_REPO", data.CacheRepo)
280+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_CACHE_TTL_DAYS", data.CacheTTLDays)
281+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_URL", data.GitURL)
282+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_USERNAME", data.GitUsername)
283+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_PASSWORD", data.GitPassword)
284+
248285
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
249286
}
250287

@@ -283,19 +320,45 @@ func (r *CachedImageResource) Create(ctx context.Context, req resource.CreateReq
283320
}
284321

285322
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_CACHE_REPO", data.CacheRepo)
286-
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_CACHE_TTL_DAYS", data.CacheTTLDays)
287323
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_URL", data.GitURL)
288-
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_USERNAME", data.GitUsername)
289-
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_PASSWORD", data.GitPassword)
324+
if !data.CacheTTLDays.IsNull() {
325+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_CACHE_TTL_DAYS", data.CacheTTLDays)
326+
}
327+
if !data.GitUsername.IsNull() {
328+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_USERNAME", data.GitUsername)
329+
}
330+
if !data.GitPassword.IsNull() {
331+
data.Env = appendKnownEnvToList(data.Env, "ENVBUILDER_GIT_PASSWORD", data.GitPassword)
332+
}
290333

291334
// Save data into Terraform state
292335
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
293336
}
294337

295338
func (r *CachedImageResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
339+
// Updates are a no-op.
340+
var data CachedImageResourceModel
341+
342+
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
343+
344+
if resp.Diagnostics.HasError() {
345+
return
346+
}
347+
348+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
296349
}
297350

298351
func (r *CachedImageResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
352+
// Deletes are a no-op.
353+
var data CachedImageResourceModel
354+
355+
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
356+
357+
if resp.Diagnostics.HasError() {
358+
return
359+
}
360+
361+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
299362
}
300363

301364
// runCacheProbe performs a 'fake build' of the requested image and ensures that
@@ -395,16 +458,26 @@ func (r *CachedImageResource) runCacheProbe(ctx context.Context, data CachedImag
395458
return envbuilder.RunCacheProbe(ctx, opts)
396459
}
397460

398-
// extractEnvbuilderFromImage reads the image located at imgRef and extracts
399-
// MagicBinaryLocation to destPath.
400-
func extractEnvbuilderFromImage(ctx context.Context, imgRef, destPath string) error {
401-
needle := filepath.Clean(constants.MagicBinaryLocation)[1:] // skip leading '/'
461+
// getRemoteImage fetches the image manifest of the image.
462+
func getRemoteImage(imgRef string) (v1.Image, error) {
402463
ref, err := name.ParseReference(imgRef)
403464
if err != nil {
404-
return fmt.Errorf("parse reference: %w", err)
465+
return nil, fmt.Errorf("parse reference: %w", err)
405466
}
406467

407468
img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
469+
if err != nil {
470+
return nil, fmt.Errorf("check remote image: %w", err)
471+
}
472+
473+
return img, nil
474+
}
475+
476+
// extractEnvbuilderFromImage reads the image located at imgRef and extracts
477+
// MagicBinaryLocation to destPath.
478+
func extractEnvbuilderFromImage(ctx context.Context, imgRef, destPath string) error {
479+
needle := filepath.Clean(constants.MagicBinaryLocation)[1:] // skip leading '/'
480+
img, err := getRemoteImage(imgRef)
408481
if err != nil {
409482
return fmt.Errorf("check remote image: %w", err)
410483
}

internal/provider/cached_image_resource_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ func TestAccCachedImageDataSource(t *testing.T) {
3131
PreCheck: func() { testAccPreCheck(t) },
3232
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
3333
Steps: []resource.TestStep{
34+
// Plan create only
35+
{
36+
Config: deps.Config(t),
37+
PlanOnly: true,
38+
ExpectNonEmptyPlan: true, // TODO: check plan
39+
},
40+
// Create
3441
{
3542
Config: deps.Config(t),
3643
Check: resource.ComposeAggregateTestCheckFunc(
@@ -62,8 +69,15 @@ func TestAccCachedImageDataSource(t *testing.T) {
6269
return nil
6370
}),
6471
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "env.0", "FOO=\"bar\""),
72+
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "env.1", fmt.Sprintf("ENVBUILDER_CACHE_REPO=%q", deps.CacheRepo)),
73+
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "env.2", fmt.Sprintf("ENVBUILDER_GIT_URL=%q", deps.Repo.URL)),
6574
),
6675
},
76+
// Should produce an empty plan after apply
77+
{
78+
Config: deps.Config(t),
79+
PlanOnly: true,
80+
},
6781
},
6882
})
6983
})
@@ -83,6 +97,13 @@ func TestAccCachedImageDataSource(t *testing.T) {
8397
PreCheck: func() { testAccPreCheck(t) },
8498
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
8599
Steps: []resource.TestStep{
100+
// Should produce a non-empty plan.
101+
{
102+
Config: deps.Config(t),
103+
PlanOnly: true,
104+
ExpectNonEmptyPlan: true,
105+
},
106+
// Should detect that no cached image is present.
86107
{
87108
Config: deps.Config(t),
88109
Check: resource.ComposeAggregateTestCheckFunc(

0 commit comments

Comments
 (0)