|
| 1 | +package sshkey |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | +) |
| 10 | + |
| 11 | +type resourceED25519KeyPairType struct{} |
| 12 | + |
| 13 | +func (r resourceED25519KeyPairType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { |
| 14 | + return tfsdk.Schema{ |
| 15 | + Attributes: map[string]tfsdk.Attribute{ |
| 16 | + "id": { |
| 17 | + Type: types.StringType, |
| 18 | + Computed: true, |
| 19 | + }, |
| 20 | + "private_key_pem": { |
| 21 | + Type: types.StringType, |
| 22 | + Computed: true, |
| 23 | + Sensitive: true, |
| 24 | + }, |
| 25 | + "public_key": { |
| 26 | + Type: types.StringType, |
| 27 | + Computed: true, |
| 28 | + }, |
| 29 | + "fingerprint_md5": { |
| 30 | + Type: types.StringType, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "fingerprint_sha256": { |
| 34 | + Type: types.StringType, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, nil |
| 39 | +} |
| 40 | + |
| 41 | +func (r resourceED25519KeyPairType) NewResource(ctx context.Context, p tfsdk.Provider) (tfsdk.Resource, diag.Diagnostics) { |
| 42 | + return resourceED25519KeyPair{}, nil |
| 43 | +} |
| 44 | + |
| 45 | +type resourceED25519KeyPair struct{} |
| 46 | + |
| 47 | +func (r resourceED25519KeyPair) Create(ctx context.Context, req tfsdk.CreateResourceRequest, resp *tfsdk.CreateResourceResponse) { |
| 48 | + var plan ED25519KeyPair |
| 49 | + diags := req.Plan.Get(ctx, &plan) |
| 50 | + resp.Diagnostics.Append(diags...) |
| 51 | + if resp.Diagnostics.HasError() { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + key, err := generateED25519KeyPair() |
| 56 | + if err != nil { |
| 57 | + resp.Diagnostics.AddError("Unable to generate a new ED25519 key", err.Error()) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + privateKeyPEM, err := key.PrivateKeyPEM() |
| 62 | + if err != nil { |
| 63 | + resp.Diagnostics.AddError("Unable to render private key to PEM format", err.Error()) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + result := ED25519KeyPair{ |
| 68 | + ID: types.String{Value: key.FingerprintSHA256()}, |
| 69 | + PrivateKeyPEM: types.String{Value: privateKeyPEM}, |
| 70 | + PublicKey: types.String{Value: key.PublicKey()}, |
| 71 | + FingerprintMD5: types.String{Value: key.FingerprintMD5()}, |
| 72 | + FingerprintSHA256: types.String{Value: key.FingerprintSHA256()}, |
| 73 | + } |
| 74 | + |
| 75 | + diags = resp.State.Set(ctx, result) |
| 76 | + resp.Diagnostics.Append(diags...) |
| 77 | + if resp.Diagnostics.HasError() { |
| 78 | + return |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func (r resourceED25519KeyPair) Read(ctx context.Context, req tfsdk.ReadResourceRequest, resp *tfsdk.ReadResourceResponse) { |
| 83 | + // no need to support Read at the moment since the resource is fully within state |
| 84 | + // NOTE: if we support sourcing from files on disk in the future, this will have to be implemented |
| 85 | +} |
| 86 | + |
| 87 | +func (r resourceED25519KeyPair) Update(ctx context.Context, req tfsdk.UpdateResourceRequest, resp *tfsdk.UpdateResourceResponse) { |
| 88 | + // no need to support Update at the moment, since the only possible changes require replacement |
| 89 | + // NOTE: if we need to support changes later for comments, etc, this will need to be implemented |
| 90 | +} |
| 91 | + |
| 92 | +func (r resourceED25519KeyPair) Delete(ctx context.Context, req tfsdk.DeleteResourceRequest, resp *tfsdk.DeleteResourceResponse) { |
| 93 | + var state ED25519KeyPair |
| 94 | + diags := req.State.Get(ctx, &state) |
| 95 | + resp.Diagnostics.Append(diags...) |
| 96 | + if resp.Diagnostics.HasError() { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + resp.State.RemoveResource(ctx) |
| 101 | +} |
| 102 | + |
| 103 | +func (r resourceED25519KeyPair) ImportState(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) { |
| 104 | + tfsdk.ResourceImportStateNotImplemented(ctx, "", resp) |
| 105 | +} |
0 commit comments