Skip to content

Commit

Permalink
Fix the issue for https host
Browse files Browse the repository at this point in the history
- Fixed an issue where if we would enable the https for a host and registered it with a orchestrator it would fail due to wrong port
- Added the new resource called vm_state to set a existing vm to a state
- Fixed an issue in the telemetry not being sent correctly
  • Loading branch information
cjlapao committed Nov 12, 2024
1 parent c8f17c4 commit 56e9f7a
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 57 deletions.
43 changes: 43 additions & 0 deletions docs/resources/vm_state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "parallels-desktop_vm_state Resource - terraform-provider-parallels-desktop"
subcategory: ""
description: |-
Parallels Virtual Machine State Resource
Use this to set a virtual machine to a desired state.
---

# parallels-desktop_vm_state (Resource)

Parallels Virtual Machine State Resource
Use this to set a virtual machine to a desired state.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `id` (String) Virtual Machine Id
- `operation` (String) Virtual Machine desired state

### Optional

- `authenticator` (Block, Optional) Authenticator block, this is used to authenticate with the Parallels Desktop API, if empty it will try to use the root password (see [below for nested schema](#nestedblock--authenticator))
- `ensure_state` (Boolean) Ensure the virtual machine is in the desired state
- `host` (String) Parallels Desktop DevOps Host
- `orchestrator` (String) Parallels Desktop DevOps Orchestrator

### Read-Only

- `current_state` (String) Virtual Machine current state

<a id="nestedblock--authenticator"></a>
### Nested Schema for `authenticator`

Optional:

- `api_key` (String, Sensitive) Parallels desktop API API Key
- `password` (String, Sensitive) Parallels desktop API Password
- `username` (String) Parallels desktop API Username
11 changes: 7 additions & 4 deletions internal/deploy/models/resource_models_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package models

import (
"strings"

"terraform-provider-parallels-desktop/internal/apiclient"
"terraform-provider-parallels-desktop/internal/models"
"terraform-provider-parallels-desktop/internal/schemas/authenticator"
Expand Down Expand Up @@ -127,15 +126,19 @@ func (o *DeployResourceModelV2) GenerateApiHostConfig(provider *models.Parallels

DisableTlsValidation: provider.DisableTlsValidation.ValueBool(),
}

api_port := strings.ReplaceAll(o.ApiConfig.Port.ValueString(), "\"", "")
api_schema := "http"

if api_port != "" {
hostConfig.Host = hostConfig.Host + ":" + api_port
}
if o.ApiConfig.EnableTLS.ValueBool() {
api_schema = "https"
api_port = strings.ReplaceAll(o.ApiConfig.TLSPort.ValueString(), "\"", "")
}

if api_port != "" {
hostConfig.Host = hostConfig.Host + ":" + api_port
}

hostConfig.Host = api_schema + "://" + hostConfig.Host

return hostConfig
Expand Down
6 changes: 3 additions & 3 deletions internal/deploy/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"errors"
"fmt"
"strings"

"terraform-provider-parallels-desktop/internal/common"
deploy_models "terraform-provider-parallels-desktop/internal/deploy/models"
"terraform-provider-parallels-desktop/internal/deploy/schemas"
"terraform-provider-parallels-desktop/internal/interfaces"
"terraform-provider-parallels-desktop/internal/localclient"
Expand All @@ -16,9 +14,10 @@ import (
"terraform-provider-parallels-desktop/internal/schemas/orchestrator"
"terraform-provider-parallels-desktop/internal/schemas/reverseproxy"
"terraform-provider-parallels-desktop/internal/ssh"

"terraform-provider-parallels-desktop/internal/telemetry"

deploy_models "terraform-provider-parallels-desktop/internal/deploy/models"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -1011,6 +1010,7 @@ func (r *DeployResource) registerWithOrchestrator(ctx context.Context, data, cur
password := strings.ReplaceAll(data.ApiConfig.RootPassword.String(), "\"", "")
if data.ApiConfig.EnableTLS.ValueBool() {
schema = "https"
port = strings.ReplaceAll(data.ApiConfig.TLSPort.String(), "\"", "")
}

if currentData != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"terraform-provider-parallels-desktop/internal/remoteimage"
"terraform-provider-parallels-desktop/internal/vagrantbox"
"terraform-provider-parallels-desktop/internal/virtualmachine"
"terraform-provider-parallels-desktop/internal/virtualmachinestate"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -122,7 +123,7 @@ func (p *ParallelsProvider) DataSources(_ context.Context) []func() datasource.D
// Resources defines the resources implemented in the provider.
func (p *ParallelsProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
// virtualmachinestate.NewVirtualMachineStateResource,
virtualmachinestate.NewVirtualMachineStateResource,
deploy.NewDeployResource,
// packertemplate.NewPackerTemplateVirtualMachineResource,
authorization.NewAuthorizationResource,
Expand Down
5 changes: 5 additions & 0 deletions internal/remoteimage/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ func (r *RemoteVmResource) Create(ctx context.Context, req resource.CreateReques
return
}

if createdVM == nil {
resp.Diagnostics.AddError("VM not found", "There was an issue creating the VM, we could not find it in the host")
return
}

hostConfig.HostId = createdVM.HostId

// stopping the machine as it might need some operations where the machine needs to be stopped
Expand Down
10 changes: 6 additions & 4 deletions internal/telemetry/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package telemetry
type TelemetryEvent string

const (
EventDeploy TelemetryEvent = "PD-TERRAFORM-PROVIDER::DEPLOY"
EventVagrant TelemetryEvent = "PD-TERRAFORM-PROVIDER::VAGRANT"
EventRemoteImage TelemetryEvent = "PD-TERRAFORM-PROVIDER::REMOTE_IMAGE"
EventCloneVm TelemetryEvent = "PD-TERRAFORM-PROVIDER::CLONE_VM"
EventDeploy TelemetryEvent = "PD-TERRAFORM-PROVIDER::DEPLOY"
EventVagrant TelemetryEvent = "PD-TERRAFORM-PROVIDER::VAGRANT"
EventRemoteImage TelemetryEvent = "PD-TERRAFORM-PROVIDER::REMOTE_IMAGE"
EventCloneVm TelemetryEvent = "PD-TERRAFORM-PROVIDER::CLONE_VM"
EventDataSourceVm TelemetryEvent = "PD-TERRAFORM-PROVIDER::DATA_SOURCE_VM"
EventVirtualMachineState TelemetryEvent = "PD-TERRAFORM-PROVIDER::VIRTUAL_MACHINE_STATE"
)

type TelemetryEventMode string
Expand Down
2 changes: 1 addition & 1 deletion internal/telemetry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func New(context context.Context) *TelemetryService {

config := amplitude.NewConfig(key)
config.FlushQueueSize = 100
config.FlushInterval = time.Second * 5
config.FlushInterval = time.Second * 3
// adding a callback to read what is the status
config.ExecuteCallback = func(result types.ExecuteResult) {
svc.Callback(result)
Expand Down
2 changes: 2 additions & 0 deletions internal/telemetry/telemetry_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ func NewTelemetryItem(ctx context.Context, userId string, eventType TelemetryEve
item.Properties["user_id"] = hashedUserId
}

item.UserID = hashedUserId

return item
}
1 change: 1 addition & 0 deletions internal/virtualmachinestate/models/resource_model_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type VirtualMachineStateResourceModelV1 struct {
ID types.String `tfsdk:"id"`
Operation types.String `tfsdk:"operation"`
CurrentState types.String `tfsdk:"current_state"`
EnsureState types.Bool `tfsdk:"ensure_state"`
}
Loading

0 comments on commit 56e9f7a

Please sign in to comment.