Skip to content

Commit aa71551

Browse files
committed
netlify_deploy_key
1 parent bcea61e commit aa71551

File tree

7 files changed

+255
-1
lines changed

7 files changed

+255
-1
lines changed

docs/resources/deploy_key.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "netlify_deploy_key Resource - netlify"
4+
subcategory: ""
5+
description: |-
6+
Deploy key for Git repositories. Avoid creating this resource directly if possible. Read more https://docs.netlify.com/git/repo-permissions-linking/#deploy-keys
7+
---
8+
9+
# netlify_deploy_key (Resource)
10+
11+
Deploy key for Git repositories. Avoid creating this resource directly if possible. [Read more](https://docs.netlify.com/git/repo-permissions-linking/#deploy-keys)
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "netlify_deploy_key" "common" {}
17+
```
18+
19+
<!-- schema generated by tfplugindocs -->
20+
## Schema
21+
22+
### Read-Only
23+
24+
- `id` (String) The ID of this resource.
25+
- `last_updated` (String)
26+
- `public_key` (String)
27+
28+
## Import
29+
30+
Import is supported using the following syntax:
31+
32+
```shell
33+
# Import a deploy key by its ID
34+
terraform import netlify_deploy_key.common 6600abcdef1234567890abcd
35+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Import a deploy key by its ID
2+
terraform import netlify_deploy_key.common 6600abcdef1234567890abcd
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
resource "netlify_deploy_key" "common" {}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/terraform"
10+
)
11+
12+
func TestAccDeployKey(t *testing.T) {
13+
accTest(t, []resource.TestStep{
14+
{
15+
Config: `resource "netlify_deploy_key" "example" {}`,
16+
Check: resource.ComposeAggregateTestCheckFunc(
17+
resource.TestCheckResourceAttrSet("netlify_deploy_key.example", "id"),
18+
resource.TestCheckResourceAttrSet("netlify_deploy_key.example", "last_updated"),
19+
resource.TestCheckResourceAttrSet("netlify_deploy_key.example", "public_key"),
20+
),
21+
},
22+
{
23+
ResourceName: "netlify_deploy_key.example",
24+
ImportState: true,
25+
ImportStateVerify: true,
26+
ImportStateVerifyIgnore: []string{"last_updated"},
27+
},
28+
}, testAccDeployKeyDestroy)
29+
}
30+
31+
func testAccDeployKeyDestroy(s *terraform.State) error {
32+
for _, m := range s.Modules {
33+
if v, ok := m.Resources["netlify_deploy_key.example"]; ok {
34+
key, _, err := testAccProvider.client.DeployKeysAPI.GetDeployKey(context.Background(), v.Primary.Attributes["id"]).Execute()
35+
if err != nil {
36+
//lint:ignore nilerr we expect an error to know it was not found
37+
return nil
38+
}
39+
return fmt.Errorf("Deploy key still exists: %s", key.Id)
40+
}
41+
}
42+
return fmt.Errorf("not found in testAccDeployKeyDestroy check destroy")
43+
}

internal/provider/dns_record_resource_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,28 @@ func TestAccDnsRecordA(t *testing.T) {
2727
resource.TestCheckResourceAttr("netlify_dns_record.example", "value", "10.0.0.0"),
2828
),
2929
},
30+
{
31+
ResourceName: "netlify_dns_record.example",
32+
ImportState: true,
33+
ImportStateIdFunc: func(s *terraform.State) (string, error) {
34+
for _, m := range s.Modules {
35+
if v, ok := m.Resources["netlify_dns_record.example"]; ok {
36+
return fmt.Sprintf("%s:%s", v.Primary.Attributes["zone_id"], v.Primary.Attributes["id"]), nil
37+
}
38+
}
39+
return "", fmt.Errorf("not found in TestAccDnsRecordA import test step")
40+
},
41+
ImportStateVerify: true,
42+
ImportStateVerifyIgnore: []string{"last_updated"},
43+
},
3044
{
3145
Config: fmt.Sprintf(`resource "netlify_dns_record" "example" {
3246
type = "A"
3347
zone_id = "%s"
3448
hostname = "testacc.examplepetstore.com"
3549
value = "10.0.0.1"
3650
}`, zoneId),
37-
Check: resource.ComposeTestCheckFunc(
51+
Check: resource.ComposeAggregateTestCheckFunc(
3852
resource.TestCheckResourceAttr("netlify_dns_record.example", "type", "A"),
3953
resource.TestCheckResourceAttr("netlify_dns_record.example", "zone_id", zoneId),
4054
resource.TestCheckResourceAttr("netlify_dns_record.example", "hostname", "testacc.examplepetstore.com"),

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func (p *NetlifyProvider) Configure(ctx context.Context, req provider.ConfigureR
205205

206206
func (p *NetlifyProvider) Resources(ctx context.Context) []func() resource.Resource {
207207
return []func() resource.Resource{
208+
NewDeployKeyResource,
208209
NewDnsRecordResource,
209210
NewDnsZoneResource,
210211
NewEnvironmentVariableResource,

0 commit comments

Comments
 (0)