Skip to content

Commit

Permalink
feat: Add user_data support (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bilby91 committed Jul 14, 2024
1 parent 53eac30 commit 3f844a2
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ description: |-
terraform {
required_providers {
crunchloop = {
source = "registry.terraform.io/crunchloop/crunchloop"
source = "bilby91/crunchloop"
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Vm resource
terraform {
required_providers {
crunchloop = {
source = "registry.terraform.io/crunchloop/crunchloop"
source = "bilby91/crunchloop"
}
}
}
Expand All @@ -40,6 +40,7 @@ resource "crunchloop_vm" "default" {
cores = 1
memory_megabytes = 1024
root_volume_size_gigabytes = 10
user_data = "echo 'Hello, World!'"
}
```

Expand All @@ -53,6 +54,7 @@ resource "crunchloop_vm" "default" {
- `memory_megabytes` (Number) Vm memory in megabytes
- `name` (String) Vm name
- `root_volume_size_gigabytes` (Number) Vm root volume size in gigabytes
- `user_data` (String) Vm cloud init user data
- `vmi_id` (Number) Vm Vmi id

### Read-Only
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/vm_state.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Vm state resource
terraform {
required_providers {
crunchloop = {
source = "registry.terraform.io/crunchloop/crunchloop"
source = "bilby91/crunchloop"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
crunchloop = {
source = "registry.terraform.io/crunchloop/crunchloop"
source = "bilby91/crunchloop"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/resources/crunchloop_vm/resource.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
crunchloop = {
source = "registry.terraform.io/crunchloop/crunchloop"
source = "bilby91/crunchloop"
}
}
}
Expand All @@ -25,4 +25,5 @@ resource "crunchloop_vm" "default" {
cores = 1
memory_megabytes = 1024
root_volume_size_gigabytes = 10
user_data = "echo 'Hello, World!'"
}
2 changes: 1 addition & 1 deletion examples/resources/crunchloop_vm_state/resource.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
crunchloop = {
source = "registry.terraform.io/crunchloop/crunchloop"
source = "bilby91/crunchloop"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/client/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/client/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ paths:
ssh_key:
type: string
example: ssh-rsa AAAAB3NzaC1y....
user_data:
type: string
required:
- name
- host_id
Expand Down
12 changes: 12 additions & 0 deletions internal/provider/vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type VmResourceModel struct {
VmiId types.Int64 `tfsdk:"vmi_id"`
HostId types.Int64 `tfsdk:"host_id"`
RootVolumeSizeGigabytes types.Int64 `tfsdk:"root_volume_size_gigabytes"`
UserData types.String `tfsdk:"user_data"`
}

func (r *VmResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -100,6 +101,13 @@ func (r *VmResource) Schema(ctx context.Context, req resource.SchemaRequest, res
int64planmodifier.RequiresReplace(),
},
},
"user_data": schema.StringAttribute{
Required: true,
MarkdownDescription: "Vm cloud init user data",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
Expand Down Expand Up @@ -143,6 +151,10 @@ func (r *VmResource) Create(ctx context.Context, req resource.CreateRequest, res
RootVolumeSizeGigabytes: int(data.RootVolumeSizeGigabytes.ValueInt64()),
}

if data.UserData.ValueString() != "" {
createOptions.UserData = data.UserData.ValueStringPointer()
}

vm, err := r.client.CreateVmWithResponse(ctx, createOptions)
if err != nil {
resp.Diagnostics.AddError(
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
flag.Parse()

opts := providerserver.ServeOpts{
Address: "registry.terraform.io/crunchloop/crunchloop",
Address: "registry.terraform.io/bilby91/crunchloop",
Debug: debug,
}

Expand Down

0 comments on commit 3f844a2

Please sign in to comment.