Changes, still WIP
This commit is contained in:
@@ -4,12 +4,12 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
// Ensure provider defined types fully satisfy framework interfaces.
|
||||
@@ -47,12 +47,12 @@ type VirtfusionServerDataSourceModel struct {
|
||||
Created types.String `tfsdk:"created" json:"created"`
|
||||
Updated types.String `tfsdk:"updated" json:"updated"`
|
||||
//Settings struct {
|
||||
// OsTemplateInstall types.Bool `tfsdk:"os_template_install" json:"osTemplateInstall"`
|
||||
// OsTemplateInstall *bool `tfsdk:"os_template_install" json:"osTemplateInstall"`
|
||||
// OsTemplateInstallId types.Int64 `tfsdk:"os_template_install_id" json:"osTemplateInstallId"`
|
||||
// EncryptedPassword types.String `tfsdk:"encrypted_password" json:"encryptedPassword"`
|
||||
// EncryptedPassword *string `tfsdk:"encrypted_password" json:"encryptedPassword"`
|
||||
// BackupPlan types.Int64 `tfsdk:"backup_plan" json:"backupPlan"`
|
||||
// Uefi types.Bool `tfsdk:"uefi" json:"uefi"`
|
||||
// CloudInit types.Bool `tfsdk:"cloud_init" json:"cloudInit"`
|
||||
// Uefi *bool `tfsdk:"uefi" json:"uefi"`
|
||||
// CloudInit *bool `tfsdk:"cloud_init" json:"cloudInit"`
|
||||
// CloudInitType types.Int64 `tfsdk:"cloud_init_type" json:"cloudInitType"`
|
||||
// Resources struct {
|
||||
// Memory types.Int64 `tfsdk:"memory" json:"memory"`
|
||||
@@ -61,40 +61,94 @@ type VirtfusionServerDataSourceModel struct {
|
||||
// Traffic types.Int64 `tfsdk:"traffic" json:"traffic"`
|
||||
// } `tfsdk:"resources" json:"resources"`
|
||||
//} `tfsdk:"settings" json:"settings"`
|
||||
Network struct {
|
||||
Interfaces []struct {
|
||||
Enabled types.Bool `tfsdk:"enabled" json:"enabled"`
|
||||
Name types.String `tfsdk:"name" json:"name"`
|
||||
Type types.String `tfsdk:"type" json:"type"`
|
||||
Mac types.String `tfsdk:"mac" json:"mac"`
|
||||
Ipv4 []struct {
|
||||
Id types.Int64 `tfsdk:"id" json:"id"`
|
||||
Enabled types.Bool `tfsdk:"enabled" json:"enabled"`
|
||||
Address types.String `tfsdk:"address" json:"address"`
|
||||
Netmask types.String `tfsdk:"netmask" json:"netmask"`
|
||||
Gateway types.String `tfsdk:"gateway" json:"gateway"`
|
||||
ResolverOne types.String `tfsdk:"resolver_one" json:"resolverOne"`
|
||||
ResolverTwo types.String `tfsdk:"resolver_two" json:"resolverTwo"`
|
||||
Rdns types.String `tfsdk:"rdns" json:"rdns"`
|
||||
Mac types.String `tfsdk:"mac" json:"mac"`
|
||||
} `tfsdk:"ipv4" json:"ipv4"`
|
||||
Ipv6 []struct {
|
||||
} `tfsdk:"ipv6" json:"ipv6"`
|
||||
} `tfsdk:"interfaces" json:"interfaces"`
|
||||
} `tfsdk:"network" json:"network"`
|
||||
Network *NetworkStruct `tfsdk:"network" json:"network"`
|
||||
//Owner struct {
|
||||
// Id types.Int64 `tfsdk:"id" json:"id"`
|
||||
// Name types.String `tfsdk:"name" json:"name"`
|
||||
// Email types.String `tfsdk:"email" json:"email"`
|
||||
// Name *string `tfsdk:"name" json:"name"`
|
||||
// Email *string `tfsdk:"email" json:"email"`
|
||||
// ExtId types.Int64 `tfsdk:"ext_id" json:"extRelationId"`
|
||||
// Timezone types.String `tfsdk:"timezone" json:"timezone"`
|
||||
// Suspended types.Bool `tfsdk:"suspended" json:"suspended"`
|
||||
// Created types.String `tfsdk:"created" json:"created"`
|
||||
// Updated types.String `tfsdk:"updated" json:"updated"`
|
||||
// TwoFactorAuth types.Bool `tfsdk:"two_factor_auth" json:"twoFactorAuth"`
|
||||
// Timezone *string `tfsdk:"timezone" json:"timezone"`
|
||||
// Suspended *bool `tfsdk:"suspended" json:"suspended"`
|
||||
// Created *string `tfsdk:"created" json:"created"`
|
||||
// Updated *string `tfsdk:"updated" json:"updated"`
|
||||
// TwoFactorAuth *bool `tfsdk:"two_factor_auth" json:"twoFactorAuth"`
|
||||
//} `tfsdk:"owner" json:"owner"`
|
||||
}
|
||||
|
||||
type NetworkStruct struct {
|
||||
Interfaces []struct {
|
||||
Enabled bool `tfsdk:"enabled" json:"enabled"`
|
||||
Name string `tfsdk:"name" json:"name"`
|
||||
Type string `tfsdk:"type" json:"type"`
|
||||
Mac string `tfsdk:"mac" json:"mac"`
|
||||
Ipv4 []struct {
|
||||
Id int64 `tfsdk:"id" json:"id"`
|
||||
Enabled bool `tfsdk:"enabled" json:"enabled"`
|
||||
Address string `tfsdk:"address" json:"address"`
|
||||
Netmask string `tfsdk:"netmask" json:"netmask"`
|
||||
Gateway string `tfsdk:"gateway" json:"gateway"`
|
||||
ResolverOne string `tfsdk:"resolver_one" json:"resolverOne"`
|
||||
ResolverTwo string `tfsdk:"resolver_two" json:"resolverTwo"`
|
||||
Rdns string `tfsdk:"rdns" json:"rdns"`
|
||||
Mac string `tfsdk:"mac" json:"mac"`
|
||||
} `tfsdk:"ipv4" json:"ipv4"`
|
||||
Ipv6 []struct {
|
||||
} `tfsdk:"ipv6" json:"ipv6"`
|
||||
} `tfsdk:"interfaces" json:"interfaces"`
|
||||
}
|
||||
|
||||
type VirtfusionServerDataResponseModel struct {
|
||||
Data struct {
|
||||
Id int64 `json:"id"`
|
||||
OwnerId int64 `json:"owner_id"`
|
||||
HypervisorId int64 `json:"hypervisor_id"`
|
||||
Name string `json:"name"`
|
||||
Hostname string `json:"hostname"`
|
||||
CommissionStatus int64 `json:"commission_status"`
|
||||
Uuid string `json:"uuid"`
|
||||
State string `json:"state"`
|
||||
MigrateLevel int64 `json:"migrate_level"`
|
||||
DeleteLevel int64 `json:"delete_level"`
|
||||
ConfigLevel int64 `json:"config_level"`
|
||||
Rebuild bool `json:"rebuild"`
|
||||
Suspended bool `json:"suspended"`
|
||||
Protected bool `json:"protected"`
|
||||
BuildFailed bool `json:"build_failed"`
|
||||
PrimaryNetworkDhcp4 bool `json:"primary_network_dhcp4"`
|
||||
PrimaryNetworkDhcp6 bool `json:"primary_network_dhcp6"`
|
||||
Built string `json:"built"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
//Settings struct {
|
||||
// OsTemplateInstall *bool `json:"os_template_install"`
|
||||
// OsTemplateInstallId *int64 `json:"os_template_install_id"`
|
||||
// EncryptedPassword *string `json:"encrypted_password"`
|
||||
// BackupPlan *int64 `json:"backup_plan"`
|
||||
// Uefi *bool `json:"uefi"`
|
||||
// CloudInit *bool `json:"cloud_init"`
|
||||
// CloudInitType *int64 `json:"cloud_init_type"`
|
||||
// Resources struct {
|
||||
// Memory *int64 `json:"memory"`
|
||||
// Cores *int64 `json:"cpu_cores"`
|
||||
// Storage *int64 `json:"storage"`
|
||||
// Traffic *int64 `json:"traffic"`
|
||||
// } `json:"resources"`
|
||||
//} `json:"settings"`
|
||||
Network *NetworkStruct `tfsdk:"network" json:"network"`
|
||||
//Owner struct {
|
||||
// Id *int64 `json:"id"`
|
||||
// Name *string `json:"name"`
|
||||
// Email *string `json:"email"`
|
||||
// ExtId *int64 `json:"ext_id"`
|
||||
// Timezone *string `json:"timezone"`
|
||||
// Suspended *bool `json:"suspended"`
|
||||
// Created *string `json:"created"`
|
||||
// Updated *string `json:"updated"`
|
||||
// TwoFactorAuth *bool `json:"two_factor_auth"`
|
||||
//} `json:"owner"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func (d *VirtfusionServerDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_server"
|
||||
}
|
||||
@@ -282,77 +336,7 @@ func (d *VirtfusionServerDataSource) Configure(ctx context.Context, req datasour
|
||||
|
||||
func (d *VirtfusionServerDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data VirtfusionServerDataSourceModel
|
||||
var responseData struct {
|
||||
Data struct {
|
||||
Id int64 `json:"id"`
|
||||
OwnerId int64 `json:"owner_id"`
|
||||
HypervisorId int64 `json:"hypervisor_id"`
|
||||
Name string `json:"name"`
|
||||
Hostname string `json:"hostname"`
|
||||
CommissionStatus int64 `json:"commission_status"`
|
||||
Uuid string `json:"uuid"`
|
||||
State string `json:"state"`
|
||||
MigrateLevel int64 `json:"migrate_level"`
|
||||
DeleteLevel int64 `json:"delete_level"`
|
||||
ConfigLevel int64 `json:"config_level"`
|
||||
Rebuild bool `json:"rebuild"`
|
||||
Suspended bool `json:"suspended"`
|
||||
Protected bool `json:"protected"`
|
||||
BuildFailed bool `json:"build_failed"`
|
||||
PrimaryNetworkDhcp4 bool `json:"primary_network_dhcp4"`
|
||||
PrimaryNetworkDhcp6 bool `json:"primary_network_dhcp6"`
|
||||
Built string `json:"built"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
//Settings struct {
|
||||
// OsTemplateInstall types.Bool `json:"os_template_install"`
|
||||
// OsTemplateInstallId types.Int64 `json:"os_template_install_id"`
|
||||
// EncryptedPassword types.String `json:"encrypted_password"`
|
||||
// BackupPlan types.Int64 `json:"backup_plan"`
|
||||
// Uefi types.Bool `json:"uefi"`
|
||||
// CloudInit types.Bool `json:"cloud_init"`
|
||||
// CloudInitType types.Int64 `json:"cloud_init_type"`
|
||||
// Resources struct {
|
||||
// Memory types.Int64 `json:"memory"`
|
||||
// Cores types.Int64 `json:"cpu_cores"`
|
||||
// Storage types.Int64 `json:"storage"`
|
||||
// Traffic types.Int64 `json:"traffic"`
|
||||
// } `json:"resources"`
|
||||
//} `json:"settings"`
|
||||
Network struct {
|
||||
Interfaces []struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Mac string `json:"mac"`
|
||||
Ipv4 []struct {
|
||||
Id int64 `json:"id"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Address string `json:"address"`
|
||||
Netmask string `json:"netmask"`
|
||||
Gateway string `json:"gateway"`
|
||||
ResolverOne string `json:"resolver_one"`
|
||||
ResolverTwo string `json:"resolver_two"`
|
||||
Rdns string `json:"rdns"`
|
||||
Mac string `json:"mac"`
|
||||
} `json:"ipv4"`
|
||||
Ipv6 []struct {
|
||||
} `json:"ipv6"`
|
||||
} `json:"interfaces"`
|
||||
} `json:"network"`
|
||||
//Owner struct {
|
||||
// Id types.Int64 `json:"id"`
|
||||
// Name types.String `json:"name"`
|
||||
// Email types.String `json:"email"`
|
||||
// ExtId types.Int64 `json:"ext_id"`
|
||||
// Timezone types.String `json:"timezone"`
|
||||
// Suspended types.Bool `json:"suspended"`
|
||||
// Created types.String `json:"created"`
|
||||
// Updated types.String `json:"updated"`
|
||||
// TwoFactorAuth types.Bool `json:"two_factor_auth"`
|
||||
//} `json:"owner"`
|
||||
} `json:"data"`
|
||||
}
|
||||
var responseData VirtfusionServerDataResponseModel
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
@@ -383,6 +367,10 @@ func (d *VirtfusionServerDataSource) Read(ctx context.Context, req datasource.Re
|
||||
)
|
||||
}
|
||||
}(httpResponse.Body)
|
||||
resp.Diagnostics.AddWarning(
|
||||
"Terraform is not able to read the server data",
|
||||
fmt.Sprintf("Terraform is not able to read the server data: %s", httpResponse.Body),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
@@ -428,6 +416,8 @@ func (d *VirtfusionServerDataSource) Read(ctx context.Context, req datasource.Re
|
||||
data.Created = types.StringValue(responseData.Data.Created)
|
||||
data.Updated = types.StringValue(responseData.Data.Updated)
|
||||
|
||||
data.Network = responseData.Data.Network
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
@@ -8,15 +8,15 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
)
|
||||
|
||||
// Ensure provider defined types fully satisfy framework interfaces.
|
||||
@@ -34,19 +34,36 @@ type VirtfusionServerResource struct {
|
||||
|
||||
// VirtfusionServerResourceModel describes the resource data model.
|
||||
type VirtfusionServerResourceModel struct {
|
||||
PackageId *int64 `tfsdk:"package_id" json:"packageId,omitempty"`
|
||||
UserId *int64 `tfsdk:"user_id" json:"userId,omitempty"`
|
||||
HypervisorId *int64 `tfsdk:"hypervisor_id" json:"hypervisorId,omitempty"`
|
||||
Ipv4 *int64 `tfsdk:"ipv4" json:"ipv4,omitempty"`
|
||||
Storage *int64 `tfsdk:"storage" json:"storage,omitempty"`
|
||||
Memory *int64 `tfsdk:"memory" json:"memory,omitempty"`
|
||||
Cores *int64 `tfsdk:"cores" json:"cpuCores,omitempty"`
|
||||
Traffic *int64 `tfsdk:"traffic" json:"traffic,omitempty"`
|
||||
InboundNetworkSpeed *int64 `tfsdk:"inbound_network_speed" json:"networkSpeedInbound,omitempty"`
|
||||
OutboundNetworkSpeed *int64 `tfsdk:"outbound_network_speed" json:"networkSpeedOutbound,omitempty"`
|
||||
StorageProfile *int64 `tfsdk:"storage_profile" json:"storageProfile,omitempty"`
|
||||
NetworkProfile *int64 `tfsdk:"network_profile" json:"networkProfile,omitempty"`
|
||||
Id types.Int64 `tfsdk:"id" json:"id"`
|
||||
PackageId *int64 `tfsdk:"package_id" json:"packageId,omitempty"`
|
||||
UserId *int64 `tfsdk:"user_id" json:"userId,omitempty"`
|
||||
HypervisorId *int64 `tfsdk:"hypervisor_id" json:"hypervisorId,omitempty"`
|
||||
Ipv4 *int64 `tfsdk:"ipv4" json:"ipv4,omitempty"`
|
||||
Storage *int64 `tfsdk:"storage" json:"storage,omitempty"`
|
||||
Memory *int64 `tfsdk:"memory" json:"memory,omitempty"`
|
||||
Cores *int64 `tfsdk:"cores" json:"cpuCores,omitempty"`
|
||||
Traffic *int64 `tfsdk:"traffic" json:"traffic,omitempty"`
|
||||
InboundNetworkSpeed *int64 `tfsdk:"inbound_network_speed" json:"networkSpeedInbound,omitempty"`
|
||||
OutboundNetworkSpeed *int64 `tfsdk:"outbound_network_speed" json:"networkSpeedOutbound,omitempty"`
|
||||
StorageProfile *int64 `tfsdk:"storage_profile" json:"storageProfile,omitempty"`
|
||||
NetworkProfile *int64 `tfsdk:"network_profile" json:"networkProfile,omitempty"`
|
||||
Id types.Int64 `tfsdk:"id" json:"id"`
|
||||
Name types.String `tfsdk:"name" json:"name"`
|
||||
Hostname types.String `tfsdk:"hostname" json:"hostname"`
|
||||
Network struct {
|
||||
Interfaces []struct {
|
||||
Ipv4 []struct {
|
||||
Address string `tfsdk:"address" json:"address"`
|
||||
Gateway string `tfsdk:"gateway" json:"gateway"`
|
||||
Netmask string `tfsdk:"netmask" json:"netmask"`
|
||||
} `tfsdk:"ipv4" json:"ipv4"`
|
||||
Ipv6 []struct {
|
||||
Addresses []string `tfsdk:"addresses" json:"addresses"`
|
||||
Gateway string `tfsdk:"gateway" json:"gateway"`
|
||||
Cidr string `tfsdk:"cidr" json:"cidr"`
|
||||
Subnet string `tfsdk:"subnet" json:"subnet"`
|
||||
} `tfsdk:"ipv6" json:"ipv6"`
|
||||
} `tfsdk:"interfaces" json:"interfaces"`
|
||||
} `tfsdk:"network" json:"network"`
|
||||
}
|
||||
|
||||
func (r *VirtfusionServerResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
@@ -113,6 +130,16 @@ func (r *VirtfusionServerResource) Schema(ctx context.Context, req resource.Sche
|
||||
MarkdownDescription: "Server ID",
|
||||
Computed: true,
|
||||
},
|
||||
"name": schema.StringAttribute{
|
||||
MarkdownDescription: "Server Name",
|
||||
Computed: true,
|
||||
Default: stringdefault.StaticString(""),
|
||||
},
|
||||
"hostname": schema.StringAttribute{
|
||||
MarkdownDescription: "Server Hostname",
|
||||
Computed: true,
|
||||
Default: stringdefault.StaticString(""),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -293,13 +320,78 @@ func (r *VirtfusionServerResource) Read(ctx context.Context, req resource.ReadRe
|
||||
return
|
||||
}
|
||||
|
||||
// If applicable, this is a great opportunity to initialize any necessary
|
||||
// provider client data and make a call using it.
|
||||
// httpResp, err := r.client.Do(httpReq)
|
||||
// if err != nil {
|
||||
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
|
||||
// return
|
||||
// }
|
||||
type ResponseData struct {
|
||||
Data struct {
|
||||
Name string `json:"name"`
|
||||
Hostname string `json:"hostname"`
|
||||
Network struct {
|
||||
Interfaces []struct {
|
||||
Ipv4 []struct {
|
||||
Address string `tfsdk:"address" json:"address"`
|
||||
Gateway string `tfsdk:"gateway" json:"gateway"`
|
||||
Netmask string `tfsdk:"netmask" json:"netmask"`
|
||||
} `tfsdk:"ipv4" json:"ipv4"`
|
||||
Ipv6 []struct {
|
||||
Addresses []string `tfsdk:"addresses" json:"addresses"`
|
||||
Gateway string `tfsdk:"gateway" json:"gateway"`
|
||||
Cidr string `tfsdk:"cidr" json:"cidr"`
|
||||
Subnet string `tfsdk:"subnet" json:"subnet"`
|
||||
} `tfsdk:"ipv6" json:"ipv6"`
|
||||
} `tfsdk:"interfaces" json:"interfaces"`
|
||||
} `tfsdk:"network" json:"network"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
var responseData ResponseData
|
||||
|
||||
httpReq, err := http.NewRequest("GET", fmt.Sprintf("/servers/%d", data.Id.ValueInt64()), nil)
|
||||
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Failed to Create Request",
|
||||
fmt.Sprintf("Failed to create a new HTTP request: %s", err.Error()),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// If the resource returns a 404, then the resource has been deleted. Return an empty state.
|
||||
httpResponse, err := r.client.Do(httpReq)
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Failed to close response body",
|
||||
fmt.Sprintf("Failed to close response body: %s", err.Error()),
|
||||
)
|
||||
}
|
||||
}(httpResponse.Body)
|
||||
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Failed to Execute Request",
|
||||
fmt.Sprintf("Failed to execute HTTP request: %s", err.Error()),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if httpResponse.StatusCode == 404 {
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewDecoder(httpResponse.Body).Decode(&responseData)
|
||||
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Failed to decode response body",
|
||||
fmt.Sprintf("Failed to decode response body: %s", err.Error()),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
data.Name = types.StringValue(responseData.Data.Name)
|
||||
data.Hostname = types.StringValue(responseData.Data.Hostname)
|
||||
data.Network = responseData.Data.Network
|
||||
|
||||
// Save updated data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
|
||||
Reference in New Issue
Block a user