|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/chainguard-dev/terraform-provider-oci/pkg/validators" |
| 10 | + "github.com/google/go-containerregistry/pkg/name" |
| 11 | + "github.com/google/go-containerregistry/pkg/v1/remote" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 20 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 21 | +) |
| 22 | + |
| 23 | +var _ resource.Resource = &TagsResource{} |
| 24 | +var _ resource.ResourceWithImportState = &TagsResource{} |
| 25 | + |
| 26 | +func NewTagsResource() resource.Resource { |
| 27 | + return &TagsResource{} |
| 28 | +} |
| 29 | + |
| 30 | +// TagsResource defines the resource implementation. |
| 31 | +type TagsResource struct { |
| 32 | + popts ProviderOpts |
| 33 | +} |
| 34 | + |
| 35 | +// TagsResourceModel describes the resource data model. |
| 36 | +type TagsResourceModel struct { |
| 37 | + Id types.String `tfsdk:"id"` |
| 38 | + |
| 39 | + Repo string `tfsdk:"repo"` |
| 40 | + Tags map[string]string `tfsdk:"tags"` // tag -> digest |
| 41 | +} |
| 42 | + |
| 43 | +func (r *TagsResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 44 | + resp.TypeName = req.ProviderTypeName + "_tags" |
| 45 | +} |
| 46 | + |
| 47 | +func (r *TagsResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 48 | + resp.Schema = schema.Schema{ |
| 49 | + MarkdownDescription: "Tag many digests with many tags.", |
| 50 | + Attributes: map[string]schema.Attribute{ |
| 51 | + "repo": schema.StringAttribute{ |
| 52 | + MarkdownDescription: "Repository for the tags.", |
| 53 | + Required: true, |
| 54 | + Validators: []validator.String{validators.RepoValidator{}}, |
| 55 | + PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, |
| 56 | + }, |
| 57 | + "tags": schema.MapAttribute{ |
| 58 | + MarkdownDescription: "Map of tag -> digest to apply.", |
| 59 | + Required: true, |
| 60 | + ElementType: basetypes.StringType{}, |
| 61 | + // TODO: validator -- check that digests and tags are well formed. |
| 62 | + PlanModifiers: []planmodifier.Map{mapplanmodifier.RequiresReplace()}, |
| 63 | + }, |
| 64 | + |
| 65 | + // TODO: any outputs? |
| 66 | + |
| 67 | + "id": schema.StringAttribute{ |
| 68 | + Computed: true, |
| 69 | + MarkdownDescription: "The resulting fully-qualified image ref by digest (e.g. {repo}:tag@sha256:deadbeef).", |
| 70 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (r *TagsResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 77 | + // Prevent panic if the provider has not been configured. |
| 78 | + if req.ProviderData == nil { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + popts, ok := req.ProviderData.(*ProviderOpts) |
| 83 | + if !ok || popts == nil { |
| 84 | + resp.Diagnostics.AddError("Client Error", "invalid provider data") |
| 85 | + return |
| 86 | + } |
| 87 | + r.popts = *popts |
| 88 | +} |
| 89 | + |
| 90 | +func (r *TagsResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 91 | + var data *TagsResourceModel |
| 92 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 93 | + if resp.Diagnostics.HasError() { |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + digest, err := r.doTags(ctx, data) |
| 98 | + if err != nil { |
| 99 | + resp.Diagnostics.AddError("Tag Error", fmt.Sprintf("Error tagging image: %s", err.Error())) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + data.Id = types.StringValue(digest) |
| 104 | + |
| 105 | + // Save data into Terraform state |
| 106 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 107 | +} |
| 108 | + |
| 109 | +func (r *TagsResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 110 | + var data *TagsResourceModel |
| 111 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 112 | + if resp.Diagnostics.HasError() { |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + // Don't actually tag, but check whether the digests are already tagged with all requested tags, so we get a useful diff. |
| 117 | + // If the digests are already tagged with all requested tags, we'll set the ID to the correct output value. |
| 118 | + // Otherwise, we'll set them to empty strings so that the create will run when applied. |
| 119 | + // TODO: Can we get a better diff about what new updates will be applied? |
| 120 | + if id, err := r.checkTags(ctx, data); err != nil { |
| 121 | + data.Id = types.StringValue("") |
| 122 | + } else { |
| 123 | + data.Id = types.StringValue(id) |
| 124 | + } |
| 125 | + |
| 126 | + // Save updated data into Terraform state |
| 127 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 128 | +} |
| 129 | + |
| 130 | +func (r *TagsResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 131 | + var data *TagsResourceModel |
| 132 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 133 | + if resp.Diagnostics.HasError() { |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + id, err := r.doTags(ctx, data) |
| 138 | + if err != nil { |
| 139 | + resp.Diagnostics.AddError("Tag Error", fmt.Sprintf("Error tagging images: %s", err.Error())) |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + data.Id = types.StringValue(id) |
| 144 | + |
| 145 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 146 | +} |
| 147 | + |
| 148 | +func (r *TagsResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 149 | + resp.Diagnostics.Append(req.State.Get(ctx, &TagsResourceModel{})...) |
| 150 | +} |
| 151 | + |
| 152 | +func (r *TagsResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 153 | + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) |
| 154 | +} |
| 155 | + |
| 156 | +func (r *TagsResource) checkTags(ctx context.Context, data *TagsResourceModel) (string, error) { |
| 157 | + repo, err := name.NewRepository(data.Repo) |
| 158 | + if err != nil { |
| 159 | + return "", fmt.Errorf("error parsing repo ref: %w", err) |
| 160 | + } |
| 161 | + |
| 162 | + for tag, digest := range data.Tags { |
| 163 | + t := repo.Tag(tag) |
| 164 | + desc, err := remote.Head(t, r.popts.withContext(ctx)...) |
| 165 | + if err != nil { |
| 166 | + return "", fmt.Errorf("error getting tag %q: %w", t, err) |
| 167 | + } |
| 168 | + if desc.Digest.String() != digest { |
| 169 | + return "", fmt.Errorf("tag %q does not point to digest %q (got %q)", tag, digest, desc.Digest.String()) |
| 170 | + } |
| 171 | + } |
| 172 | + // ID is the SHA256 of the JSONified map. |
| 173 | + b, err := json.Marshal(data.Tags) |
| 174 | + if err != nil { |
| 175 | + return "", fmt.Errorf("error marshaling tags: %w", err) |
| 176 | + } |
| 177 | + return fmt.Sprintf("%x", sha256.Sum256(b)), nil |
| 178 | +} |
| 179 | + |
| 180 | +func (r *TagsResource) doTags(ctx context.Context, data *TagsResourceModel) (string, error) { |
| 181 | + repo, err := name.NewRepository(data.Repo) |
| 182 | + if err != nil { |
| 183 | + return "", fmt.Errorf("error parsing repo ref: %w", err) |
| 184 | + } |
| 185 | + |
| 186 | + for tag, digest := range data.Tags { |
| 187 | + t := repo.Tag(tag) |
| 188 | + d := repo.Digest(digest) |
| 189 | + desc, err := remote.Get(d, r.popts.withContext(ctx)...) |
| 190 | + if err != nil { |
| 191 | + return "", fmt.Errorf("error getting digest %q: %w", digest, err) |
| 192 | + } |
| 193 | + if err := remote.Tag(t, desc, r.popts.withContext(ctx)...); err != nil { |
| 194 | + return "", fmt.Errorf("error tagging %q with %q: %w", digest, tag, err) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + // ID is the SHA256 of the JSONified map. |
| 199 | + b, err := json.Marshal(data.Tags) |
| 200 | + if err != nil { |
| 201 | + return "", fmt.Errorf("error marshaling tags: %w", err) |
| 202 | + } |
| 203 | + return fmt.Sprintf("%x", sha256.Sum256(b)), nil |
| 204 | +} |
0 commit comments