|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + _ resource.Resource = &deployKeyResource{} |
| 18 | + _ resource.ResourceWithConfigure = &deployKeyResource{} |
| 19 | + _ resource.ResourceWithImportState = &deployKeyResource{} |
| 20 | +) |
| 21 | + |
| 22 | +func NewDeployKeyResource() resource.Resource { |
| 23 | + return &deployKeyResource{} |
| 24 | +} |
| 25 | + |
| 26 | +type deployKeyResource struct { |
| 27 | + data NetlifyProviderData |
| 28 | +} |
| 29 | + |
| 30 | +type deployKeyResourceModel struct { |
| 31 | + ID types.String `tfsdk:"id"` |
| 32 | + LastUpdated types.String `tfsdk:"last_updated"` |
| 33 | + PublicKey types.String `tfsdk:"public_key"` |
| 34 | +} |
| 35 | + |
| 36 | +func (r *deployKeyResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 37 | + resp.TypeName = req.ProviderTypeName + "_deploy_key" |
| 38 | +} |
| 39 | + |
| 40 | +func (r *deployKeyResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 41 | + if req.ProviderData == nil { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + data, ok := req.ProviderData.(NetlifyProviderData) |
| 46 | + |
| 47 | + if !ok { |
| 48 | + resp.Diagnostics.AddError( |
| 49 | + "Unexpected Resource Configure Type", |
| 50 | + fmt.Sprintf("Expected NetlifyProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 51 | + ) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + r.data = data |
| 56 | +} |
| 57 | + |
| 58 | +func (r *deployKeyResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 59 | + resp.Schema = schema.Schema{ |
| 60 | + Description: "Deploy key for Git repositories. Avoid creating this resource directly if possible.", |
| 61 | + MarkdownDescription: "Deploy key for Git repositories. Avoid creating this resource directly if possible. [Read more](https://docs.netlify.com/git/repo-permissions-linking/#deploy-keys)", |
| 62 | + Attributes: map[string]schema.Attribute{ |
| 63 | + "id": schema.StringAttribute{ |
| 64 | + Computed: true, |
| 65 | + PlanModifiers: []planmodifier.String{ |
| 66 | + stringplanmodifier.UseStateForUnknown(), |
| 67 | + }, |
| 68 | + }, |
| 69 | + "last_updated": schema.StringAttribute{ |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + "public_key": schema.StringAttribute{ |
| 73 | + Computed: true, |
| 74 | + PlanModifiers: []planmodifier.String{ |
| 75 | + stringplanmodifier.UseStateForUnknown(), |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func (r *deployKeyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 83 | + var plan deployKeyResourceModel |
| 84 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) |
| 85 | + if resp.Diagnostics.HasError() { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + key, _, err := r.data.client.DeployKeysAPI.CreateDeployKey(ctx).Execute() |
| 90 | + if err != nil { |
| 91 | + resp.Diagnostics.AddError( |
| 92 | + "Error creating Netlify deploy key", |
| 93 | + fmt.Sprintf("Could not create Netlify deploy key: %q", err.Error()), |
| 94 | + ) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + plan.ID = types.StringValue(key.Id) |
| 99 | + plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC3339)) |
| 100 | + plan.PublicKey = types.StringValue(key.PublicKey) |
| 101 | + |
| 102 | + resp.Diagnostics.Append(resp.State.Set(ctx, plan)...) |
| 103 | + if resp.Diagnostics.HasError() { |
| 104 | + return |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func (r *deployKeyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 109 | + var state deployKeyResourceModel |
| 110 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 111 | + if resp.Diagnostics.HasError() { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + key, _, err := r.data.client.DeployKeysAPI.GetDeployKey(ctx, state.ID.ValueString()).Execute() |
| 116 | + if err != nil { |
| 117 | + resp.Diagnostics.AddError( |
| 118 | + "Error reading Netlify deploy key", |
| 119 | + fmt.Sprintf("Could not read Netlify deploy key %q: %q", state.ID.ValueString(), err.Error()), |
| 120 | + ) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + state.PublicKey = types.StringValue(key.PublicKey) |
| 125 | + |
| 126 | + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) |
| 127 | + if resp.Diagnostics.HasError() { |
| 128 | + return |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func (r *deployKeyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 133 | + resp.Diagnostics.AddError( |
| 134 | + "Update not supported for Netlify deploy keys", |
| 135 | + "Update is not supported for Netlify deploy keys at this time.", |
| 136 | + ) |
| 137 | +} |
| 138 | + |
| 139 | +func (r *deployKeyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 140 | + var state deployKeyResourceModel |
| 141 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 142 | + if resp.Diagnostics.HasError() { |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + _, err := r.data.client.DeployKeysAPI.DeleteDeployKey(ctx, state.ID.ValueString()).Execute() |
| 147 | + if err != nil { |
| 148 | + resp.Diagnostics.AddError( |
| 149 | + "Error deleting Netlify deploy key", |
| 150 | + fmt.Sprintf("Could not delete Netlify deploy key %q: %q", state.ID.ValueString(), err.Error()), |
| 151 | + ) |
| 152 | + return |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func (r *deployKeyResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 157 | + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) |
| 158 | +} |
0 commit comments