Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
add new datasource (singular) virtual_machine
Browse files Browse the repository at this point in the history
  • Loading branch information
pokgak committed Sep 28, 2023
1 parent 880351c commit a077661
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
7 changes: 4 additions & 3 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ func (p *FakecloudProvider) Resources(ctx context.Context) []func() resource.Res
}

func (p *FakecloudProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource {
NewVirtualMachinesDataSource,
}
return []func() datasource.DataSource{
NewVirtualMachinesDataSource,
NewVirtualMachineDataSource,
}
}

func New(version string) func() provider.Provider {
Expand Down
99 changes: 99 additions & 0 deletions internal/provider/virtual_machine_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
fakecloud "github.com/pokgak/fakecloud/sdk"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &virtualMachineDataSource{}
_ datasource.DataSourceWithConfigure = &virtualMachineDataSource{}
)

func NewVirtualMachineDataSource() datasource.DataSource {
return &virtualMachineDataSource{}
}

type virtualMachineDataSource struct {
client *fakecloud.Client
}

// virtualMachineDataSourceModel maps the data source schema data.
type virtualMachineDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
InstanceType types.String `tfsdk:"instance_type"`
}

func (d *virtualMachineDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_virtual_machine"
}

// Configure adds the provider configured client to the data source.
func (d *virtualMachineDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*fakecloud.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *fakecloud.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
}

// Schema defines the schema for the data source.
func (d *virtualMachineDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Required: true,
},
"name": schema.StringAttribute{
Computed: true,
},
"instance_type": schema.StringAttribute{
Computed: true,
},
},
}
}

// Read refreshes the Terraform state with the latest data.
func (d *virtualMachineDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state virtualMachineDataSourceModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)

vm, err := d.client.GetVM(int(state.ID.ValueInt64()))
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Fakecloud VM",
err.Error(),
)
return
}

state.Name = types.StringValue(vm.Name)
state.InstanceType = types.StringValue(vm.InstanceType)

// Set state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

0 comments on commit a077661

Please sign in to comment.