This commit is contained in:
2023-10-22 00:52:33 -04:00
parent 5d5621266f
commit 50eeba7961
16 changed files with 489 additions and 256 deletions

View File

@@ -17,7 +17,7 @@ import (
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &ExampleDataSource{}
func NewExampleDataSource() datasource.DataSource {
func NewVirtfusionDataSource() datasource.DataSource {
return &ExampleDataSource{}
}

View File

@@ -18,7 +18,7 @@ func TestAccExampleDataSource(t *testing.T) {
{
Config: testAccExampleDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.scaffolding_example.test", "id", "example-id"),
resource.TestCheckResourceAttr("data.virtfusion_example.test", "id", "example-id"),
),
},
},
@@ -26,7 +26,7 @@ func TestAccExampleDataSource(t *testing.T) {
}
const testAccExampleDataSourceConfig = `
data "scaffolding_example" "test" {
data "virtfusion_example" "test" {
configurable_attribute = "example"
}
`

View File

@@ -1,187 +0,0 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"fmt"
"net/http"
"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/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &ExampleResource{}
var _ resource.ResourceWithImportState = &ExampleResource{}
func NewExampleResource() resource.Resource {
return &ExampleResource{}
}
// ExampleResource defines the resource implementation.
type ExampleResource struct {
client *http.Client
}
// ExampleResourceModel describes the resource data model.
type ExampleResourceModel struct {
ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
Defaulted types.String `tfsdk:"defaulted"`
Id types.String `tfsdk:"id"`
}
func (r *ExampleResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_example"
}
func (r *ExampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Example resource",
Attributes: map[string]schema.Attribute{
"configurable_attribute": schema.StringAttribute{
MarkdownDescription: "Example configurable attribute",
Optional: true,
},
"defaulted": schema.StringAttribute{
MarkdownDescription: "Example configurable attribute with default value",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("example value when not configured"),
},
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Example identifier",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
}
}
func (r *ExampleResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*http.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r *ExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data ExampleResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
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 create example, got error: %s", err))
// return
// }
// For the purposes of this example code, hardcoding a response value to
// save into the Terraform state.
data.Id = types.StringValue("example-id")
// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Trace(ctx, "created a resource")
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ExampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data ExampleResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
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
// }
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ExampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data ExampleResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
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 update example, got error: %s", err))
// return
// }
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ExampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data ExampleResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
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 delete example, got error: %s", err))
// return
// }
}
func (r *ExampleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

View File

@@ -5,13 +5,14 @@ package provider
import (
"context"
"net/http"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"net/http"
"net/url"
"os"
)
// Ensure ScaffoldingProvider satisfies various provider interfaces.
@@ -28,10 +29,11 @@ type ScaffoldingProvider struct {
// ScaffoldingProviderModel describes the provider data model.
type ScaffoldingProviderModel struct {
Endpoint types.String `tfsdk:"endpoint"`
ApiToken types.String `tfsdk:"api_token"`
}
func (p *ScaffoldingProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "scaffolding"
resp.TypeName = "virtfusion"
resp.Version = p.version
}
@@ -39,14 +41,22 @@ func (p *ScaffoldingProvider) Schema(ctx context.Context, req provider.SchemaReq
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"endpoint": schema.StringAttribute{
MarkdownDescription: "Example provider attribute",
Optional: true,
MarkdownDescription: "The endpoint to use for API requests.",
Required: true,
},
"api_token": schema.StringAttribute{
MarkdownDescription: "The API token to use for API requests.",
Required: true,
},
},
}
}
func (p *ScaffoldingProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
// Check environment variables
apiToken := os.Getenv("VIRTFUSION_API_TOKEN")
endpoint := os.Getenv("VIRTFUSION_ENDPOINT")
var data ScaffoldingProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
@@ -58,24 +68,76 @@ func (p *ScaffoldingProvider) Configure(ctx context.Context, req provider.Config
// Configuration values are now available.
// if data.Endpoint.IsNull() { /* ... */ }
if data.Endpoint.ValueString() != "" {
endpoint = data.Endpoint.ValueString()
}
if data.ApiToken.ValueString() != "" {
apiToken = data.ApiToken.ValueString()
}
if apiToken == "" {
resp.Diagnostics.AddError(
"Missing API Token Configuration",
"While configuring the provider, the API token was not found in "+
"the VIRTFUSION_API_TOKEN environment variable or provider "+
"configuration block api_token attribute.",
)
// Not returning early allows the logic to collect all errors.
}
if endpoint == "" {
resp.Diagnostics.AddError(
"Missing Endpoint Configuration",
"While configuring the provider, the endpoint was not found in "+
"the VIRTFUSION_ENDPOINT environment variable or provider "+
"configuration block endpoint attribute.",
)
// Not returning early allows the logic to collect all errors.
}
customTransport := &CustomTransport{
Transport: http.DefaultTransport,
BaseURL: &url.URL{Scheme: "https", Host: endpoint + "/api/v1"},
Token: apiToken,
}
// Example client configuration for data sources and resources
client := http.DefaultClient
client := &http.Client{
Transport: customTransport,
}
resp.DataSourceData = client
resp.ResourceData = client
}
func (p *ScaffoldingProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewExampleResource,
NewVirtfusionServerResource,
}
}
func (p *ScaffoldingProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewExampleDataSource,
NewVirtfusionDataSource,
}
}
type CustomTransport struct {
Transport http.RoundTripper
BaseURL *url.URL
Token string
}
func (c *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", "Bearer "+c.Token)
req.URL.Scheme = c.BaseURL.Scheme
req.URL.Host = c.BaseURL.Host
req.URL.Path = c.BaseURL.Path + req.URL.Path
return c.Transport.RoundTrip(req)
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &ScaffoldingProvider{

View File

@@ -0,0 +1,337 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"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"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &VirtfusionServerResource{}
var _ resource.ResourceWithImportState = &VirtfusionServerResource{}
func NewVirtfusionServerResource() resource.Resource {
return &VirtfusionServerResource{}
}
// VirtfusionServerResource defines the resource implementation.
type VirtfusionServerResource struct {
client *http.Client
}
// VirtfusionServerResourceModel describes the resource data model.
type VirtfusionServerResourceModel struct {
PackageId types.Int64 `tfsdk:"package_id"`
UserId types.Int64 `tfsdk:"user_id"`
HypervisorId types.Int64 `tfsdk:"hypervisor_id"`
Ipv4 types.Int64 `tfsdk:"ipv4"`
Storage types.Int64 `tfsdk:"storage"`
Memory types.Int64 `tfsdk:"memory"`
Cores types.Int64 `tfsdk:"cores"`
Traffic types.Int64 `tfsdk:"traffic"`
InboundNetworkSpeed types.Int64 `tfsdk:"inbound_network_speed"`
OutboundNetworkSpeed types.Int64 `tfsdk:"outbound_network_speed"`
StorageProfile types.Int64 `tfsdk:"storage_profile"`
NetworkProfile types.Int64 `tfsdk:"network_profile"`
Name types.String `tfsdk:"name"`
Id types.Int64 `tfsdk:"id"`
}
func (r *VirtfusionServerResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_server_example"
}
func GenerateRandomName() string {
// Generate a random name for the server if one is not provided
// It'll be in a format like "tf-<uuid>"
return "tf-" + uuid.New().String()[:8]
}
func (r *VirtfusionServerResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Virtfusion Server Resource",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "Server name. If omitted, a random UUID will be generated.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(GenerateRandomName()),
},
"package_id": schema.Int64Attribute{
MarkdownDescription: "Package ID",
Required: true,
},
"user_id": schema.Int64Attribute{
MarkdownDescription: "User ID",
Required: true,
},
"hypervisor_id": schema.Int64Attribute{
MarkdownDescription: "Hypervisor ID",
Required: true,
},
"ipv4": schema.Int64Attribute{
MarkdownDescription: "How many IPv4 addresses to allocate. Default is 1.",
Optional: true,
Computed: true,
Default: int64default.StaticInt64(1),
},
"storage": schema.Int64Attribute{
MarkdownDescription: "Primary storage size in GB. Omit to use the default storage size from the package.",
Optional: true,
},
"memory": schema.Int64Attribute{
MarkdownDescription: "How much memory to allocate in MB. Omit to use the default memory size from the package.",
Optional: true,
},
"cores": schema.Int64Attribute{
MarkdownDescription: "How many cores to allocate. Omit to use the default core count from the package.",
Optional: true,
},
"traffic": schema.Int64Attribute{
MarkdownDescription: "How much traffic to allocate in GB. Omit to use the default traffic size from the package. 0=Unlimited",
Optional: true,
},
"inbound_network_speed": schema.Int64Attribute{
MarkdownDescription: "Inbound network speed in kB/s. Omit to use the default inbound network speed from the package.",
Optional: true,
},
"outbound_network_speed": schema.Int64Attribute{
MarkdownDescription: "Outbound network speed in kB/s. Omit to use the default outbound network speed from the package.",
Optional: true,
},
"storage_profile": schema.Int64Attribute{
MarkdownDescription: "Storage profile ID. Omit to use the default storage profile from the package.",
Optional: true,
},
"network_profile": schema.Int64Attribute{
MarkdownDescription: "Network profile ID. Omit to use the default network profile from the package.",
Optional: true,
},
"id": schema.Int64Attribute{
MarkdownDescription: "Server ID",
Computed: true,
},
},
}
}
func (r *VirtfusionServerResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*http.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r *VirtfusionServerResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data VirtfusionServerResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
createReq := VirtfusionServerResourceModel{
PackageId: data.PackageId,
UserId: data.UserId,
HypervisorId: data.HypervisorId,
Ipv4: data.Ipv4,
Storage: data.Storage,
Traffic: data.Traffic,
Memory: data.Memory,
Cores: data.Cores,
InboundNetworkSpeed: data.InboundNetworkSpeed,
OutboundNetworkSpeed: data.OutboundNetworkSpeed,
StorageProfile: data.StorageProfile,
NetworkProfile: data.NetworkProfile,
}
httpReqBody, err := json.Marshal(createReq)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Resource",
"An unexpected error occurred while creating the resource create request. "+
"Please report this issue to the provider developers.\n\n"+
"JSON Error: "+err.Error(),
)
return
}
httpReq, err := http.NewRequest("POST", "/servers", bytes.NewBuffer(httpReqBody))
if err != nil {
resp.Diagnostics.AddError(
"Failed to Create Request",
fmt.Sprintf("Failed to create a new HTTP request: %s", err.Error()),
)
return
}
// Add any additional headers (Content-Type, etc.)
httpReq.Header.Set("Content-Type", "application/json")
httpResponse, err := r.client.Do(httpReq)
if err != nil {
resp.Diagnostics.AddError(
"Failed to Execute Request",
fmt.Sprintf("Failed to execute HTTP request: %s", err.Error()),
)
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
resp.Diagnostics.AddError(
"Failed to Close Request",
fmt.Sprintf("Failed to close HTTP request: %s", err.Error()),
)
return
}
}(httpResponse.Body)
if httpResponse.StatusCode != 201 {
resp.Diagnostics.AddError(
"Failed to Create Resource",
fmt.Sprintf("Failed to create resource: %s", httpResponse.Status),
)
return
}
responseBody, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
resp.Diagnostics.AddError(
"Failed to Read Response",
fmt.Sprintf("Failed to read HTTP response body: %s", err.Error()),
)
return
}
type ResponseData struct {
Data struct {
Id types.Int64 `json:"id"`
Uuid types.String `json:"uuid"`
} `json:"data"`
}
var responseData ResponseData
// Unmarshal the JSON response
err = json.Unmarshal(responseBody, &responseData)
if err != nil {
resp.Diagnostics.AddError(
"Failed to Parse Response",
fmt.Sprintf("Failed to parse HTTP response body: %s", err.Error()),
)
return
}
// Update the Terraform state with the server ID
data.Id = types.Int64(responseData.Data.Id)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
//tflog.Trace(ctx, "created a resource")
// Save data into Terraform state
//resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *VirtfusionServerResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data VirtfusionServerResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
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
// }
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *VirtfusionServerResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data VirtfusionServerResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
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 update example, got error: %s", err))
// return
// }
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *VirtfusionServerResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data VirtfusionServerResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
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 delete example, got error: %s", err))
// return
// }
}
func (r *VirtfusionServerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

View File

@@ -19,14 +19,14 @@ func TestAccExampleResource(t *testing.T) {
{
Config: testAccExampleResourceConfig("one"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "one"),
resource.TestCheckResourceAttr("scaffolding_example.test", "defaulted", "example value when not configured"),
resource.TestCheckResourceAttr("scaffolding_example.test", "id", "example-id"),
resource.TestCheckResourceAttr("virtfusion_example.test", "configurable_attribute", "one"),
resource.TestCheckResourceAttr("virtfusion_example.test", "defaulted", "example value when not configured"),
resource.TestCheckResourceAttr("virtfusion_example.test", "id", "example-id"),
),
},
// ImportState testing
{
ResourceName: "scaffolding_example.test",
ResourceName: "virtfusion_example.test",
ImportState: true,
ImportStateVerify: true,
// This is not normally necessary, but is here because this
@@ -39,7 +39,7 @@ func TestAccExampleResource(t *testing.T) {
{
Config: testAccExampleResourceConfig("two"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "two"),
resource.TestCheckResourceAttr("virtfusion_example.test", "configurable_attribute", "two"),
),
},
// Delete testing automatically occurs in TestCase
@@ -49,7 +49,7 @@ func TestAccExampleResource(t *testing.T) {
func testAccExampleResourceConfig(configurableAttribute string) string {
return fmt.Sprintf(`
resource "scaffolding_example" "test" {
resource "virtfusion_example" "test" {
configurable_attribute = %[1]q
}
`, configurableAttribute)