Skip to content

Commit 416c80f

Browse files
committed
starting
1 parent 03a98cd commit 416c80f

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
8+
"github.com/hashicorp/terraform-plugin-framework/resource"
9+
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
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+
"github.com/coder/coder/v2/codersdk"
16+
)
17+
18+
// Ensure provider defined types fully satisfy framework interfaces.
19+
var _ resource.Resource = &ProvisionerKeyResource{}
20+
21+
func NewProvisionerKeyResource() resource.Resource {
22+
return &ProvisionerKeyResource{}
23+
}
24+
25+
// ProvisionerKeyResource defines the resource implementation.
26+
type ProvisionerKeyResource struct {
27+
*CoderdProviderData
28+
}
29+
30+
// ProvisionerKeyResourceModel describes the resource data model.
31+
type ProvisionerKeyResourceModel struct {
32+
OrganizationID UUID `tfsdk:"organization_id"`
33+
Name types.String `tfsdk:"name"`
34+
Tags types.Map `tfsdk:"tags"`
35+
Key types.String `tfsdk:"key"`
36+
}
37+
38+
func (r *ProvisionerKeyResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
39+
resp.TypeName = req.ProviderTypeName + "_provisioner_key"
40+
}
41+
42+
func (r *ProvisionerKeyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
43+
resp.Schema = schema.Schema{
44+
MarkdownDescription: "A provisioner key for a Coder deployment.",
45+
46+
Attributes: map[string]schema.Attribute{
47+
"organization_id": schema.StringAttribute{
48+
CustomType: UUIDType,
49+
MarkdownDescription: "The organization that provisioners connected with this key will be connected to.",
50+
PlanModifiers: []planmodifier.String{
51+
stringplanmodifier.RequiresReplace(),
52+
},
53+
},
54+
"name": schema.StringAttribute{
55+
MarkdownDescription: "The name of the key.",
56+
PlanModifiers: []planmodifier.String{
57+
stringplanmodifier.RequiresReplace(),
58+
},
59+
},
60+
"tags": schema.MapAttribute{
61+
ElementType: types.StringType,
62+
MarkdownDescription: "The tags that the provisioner will accept jobs for.",
63+
PlanModifiers: []planmodifier.Map{
64+
mapplanmodifier.RequiresReplace(),
65+
},
66+
},
67+
"key": schema.StringAttribute{
68+
MarkdownDescription: "A provisionerkey key for Coder.",
69+
Computed: true,
70+
Sensitive: true,
71+
},
72+
},
73+
}
74+
}
75+
76+
func (r *ProvisionerKeyResource) 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+
data, ok := req.ProviderData.(*CoderdProviderData)
83+
84+
if !ok {
85+
resp.Diagnostics.AddError(
86+
"Unexpected Resource Configure Type",
87+
fmt.Sprintf("Expected *CoderdProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData),
88+
)
89+
90+
return
91+
}
92+
93+
r.CoderdProviderData = data
94+
}
95+
96+
func (r *ProvisionerKeyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
97+
// Read Terraform plan data into the model
98+
var data ProvisionerKeyResourceModel
99+
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
100+
if resp.Diagnostics.HasError() {
101+
return
102+
}
103+
104+
createKeyResult, err := r.Client.CreateProvisionerKey(ctx, data.OrganizationID.ValueUUID(), codersdk.CreateProvisionerKeyRequest{
105+
Name: data.Name.ValueString(),
106+
Tags: map[string]string{},
107+
})
108+
if err != nil {
109+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create provisioner_key, got error: %s", err))
110+
return
111+
}
112+
113+
data.Key = types.StringValue(createKeyResult.Key)
114+
// Save data into Terraform state
115+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
116+
}
117+
118+
func (r *ProvisionerKeyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
119+
// Read Terraform prior state data into the model
120+
var data ProvisionerKeyResourceModel
121+
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
122+
if resp.Diagnostics.HasError() {
123+
return
124+
}
125+
126+
// Provisioner keys are immutable, no reading necessary.
127+
128+
// Save updated data into Terraform state
129+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
130+
}
131+
132+
func (r *ProvisionerKeyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
133+
// Provisioner keys are immutable, updating is always invalid.
134+
resp.Diagnostics.Append(diag.NewErrorDiagnostic("invalid update", "terraform is attempting to update a resource which must be replaced"))
135+
}
136+
137+
func (r *ProvisionerKeyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
138+
// Read Terraform prior state data into the model
139+
var data ProvisionerKeyResourceModel
140+
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
141+
if resp.Diagnostics.HasError() {
142+
return
143+
}
144+
145+
err := r.Client.DeleteProvisionerKey(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
146+
if err != nil {
147+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete provisionerkey, got error: %s", err))
148+
return
149+
}
150+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"os"
6+
"strings"
7+
"testing"
8+
"text/template"
9+
10+
"github.com/coder/terraform-provider-coderd/integration"
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestAccLicenseResource(t *testing.T) {
16+
if os.Getenv("TF_ACC") == "" {
17+
t.Skip("Acceptance tests are disabled.")
18+
}
19+
ctx := context.Background()
20+
client := integration.StartCoder(ctx, t, "license_acc", false)
21+
22+
license := os.Getenv("CODER_ENTERPRISE_LICENSE")
23+
if license == "" {
24+
t.Skip("No license found for license resource tests, skipping")
25+
}
26+
27+
cfg1 := testAccLicenseResourceconfig{
28+
URL: client.URL.String(),
29+
Token: client.SessionToken(),
30+
License: license,
31+
}
32+
33+
resource.Test(t, resource.TestCase{
34+
IsUnitTest: true,
35+
PreCheck: func() { testAccPreCheck(t) },
36+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
37+
Steps: []resource.TestStep{
38+
{
39+
Config: cfg1.String(t),
40+
},
41+
},
42+
})
43+
}
44+
45+
type testAccLicenseResourceconfig struct {
46+
URL string
47+
Token string
48+
License string
49+
}
50+
51+
func (c testAccLicenseResourceconfig) String(t *testing.T) string {
52+
t.Helper()
53+
tpl := `
54+
provider coderd {
55+
url = "{{.URL}}"
56+
token = "{{.Token}}"
57+
}
58+
59+
resource "coderd_license" "test" {
60+
license = "{{.License}}"
61+
}
62+
`
63+
funcMap := template.FuncMap{
64+
"orNull": PrintOrNull,
65+
}
66+
67+
buf := strings.Builder{}
68+
tmpl, err := template.New("licenseResource").Funcs(funcMap).Parse(tpl)
69+
require.NoError(t, err)
70+
71+
err = tmpl.Execute(&buf, c)
72+
require.NoError(t, err)
73+
return buf.String()
74+
}

0 commit comments

Comments
 (0)