parent
460c18c202
commit
53a3697850
124 changed files with 8342 additions and 6042 deletions
|
|
@ -30,6 +30,7 @@ var (
|
|||
type DataSourceModel struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
ImageId types.String `tfsdk:"image_id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
DiskFormat types.String `tfsdk:"disk_format"`
|
||||
|
|
@ -49,7 +50,8 @@ func NewImageDataSource() datasource.DataSource {
|
|||
|
||||
// imageDataSource is the data source implementation.
|
||||
type imageDataSource struct {
|
||||
client *iaas.APIClient
|
||||
client *iaas.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the data source type name.
|
||||
|
|
@ -58,12 +60,13 @@ func (d *imageDataSource) Metadata(_ context.Context, req datasource.MetadataReq
|
|||
}
|
||||
|
||||
func (d *imageDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
var ok bool
|
||||
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
|
@ -72,14 +75,14 @@ func (d *imageDataSource) Configure(ctx context.Context, req datasource.Configur
|
|||
}
|
||||
|
||||
// Schema defines the schema for the datasource.
|
||||
func (r *imageDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
func (d *imageDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
description := "Image datasource schema. Must have a `region` specified in the provider configuration."
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: description,
|
||||
Description: description,
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`image_id`\".",
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`image_id`\".",
|
||||
Computed: true,
|
||||
},
|
||||
"project_id": schema.StringAttribute{
|
||||
|
|
@ -90,6 +93,11 @@ func (r *imageDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
|
|||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"region": schema.StringAttribute{
|
||||
Description: "The resource region. If not defined, the provider region is used.",
|
||||
// the region cannot be found, so it has to be passed
|
||||
Optional: true,
|
||||
},
|
||||
"image_id": schema.StringAttribute{
|
||||
Description: "The image ID.",
|
||||
Required: true,
|
||||
|
|
@ -203,23 +211,26 @@ func (r *imageDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
|
|||
}
|
||||
}
|
||||
|
||||
// // Read refreshes the Terraform state with the latest data.
|
||||
func (r *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (d *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
var model DataSourceModel
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := d.providerData.GetRegionWithOverride(model.Region)
|
||||
imageId := model.ImageId.ValueString()
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "image_id", imageId)
|
||||
|
||||
imageResp, err := r.client.GetImage(ctx, projectId, imageId).Execute()
|
||||
imageResp, err := d.client.GetImage(ctx, projectId, region, imageId).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
|
|
@ -238,7 +249,7 @@ func (r *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest,
|
|||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapDataSourceFields(ctx, imageResp, &model)
|
||||
err = mapDataSourceFields(ctx, imageResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading image", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -252,7 +263,7 @@ func (r *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest,
|
|||
tflog.Info(ctx, "image read")
|
||||
}
|
||||
|
||||
func mapDataSourceFields(ctx context.Context, imageResp *iaas.Image, model *DataSourceModel) error {
|
||||
func mapDataSourceFields(ctx context.Context, imageResp *iaas.Image, model *DataSourceModel, region string) error {
|
||||
if imageResp == nil {
|
||||
return fmt.Errorf("response input is nil")
|
||||
}
|
||||
|
|
@ -269,7 +280,8 @@ func mapDataSourceFields(ctx context.Context, imageResp *iaas.Image, model *Data
|
|||
return fmt.Errorf("image id not present")
|
||||
}
|
||||
|
||||
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), imageId)
|
||||
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, imageId)
|
||||
model.Region = types.StringValue(region)
|
||||
|
||||
// Map config
|
||||
var configModel = &configModel{}
|
||||
|
|
|
|||
|
|
@ -12,69 +12,81 @@ import (
|
|||
)
|
||||
|
||||
func TestMapDataSourceFields(t *testing.T) {
|
||||
type args struct {
|
||||
state DataSourceModel
|
||||
input *iaas.Image
|
||||
region string
|
||||
}
|
||||
tests := []struct {
|
||||
description string
|
||||
state DataSourceModel
|
||||
input *iaas.Image
|
||||
args args
|
||||
expected DataSourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
description: "default_values",
|
||||
args: args{
|
||||
state: DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
},
|
||||
input: &iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
},
|
||||
region: "eu01",
|
||||
},
|
||||
&iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
},
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,iid"),
|
||||
expected: DataSourceModel{
|
||||
Id: types.StringValue("pid,eu01,iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Region: types.StringValue("eu01"),
|
||||
},
|
||||
true,
|
||||
isValid: true,
|
||||
},
|
||||
{
|
||||
"simple_values",
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
description: "simple_values",
|
||||
args: args{
|
||||
state: DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Region: types.StringValue("eu01"),
|
||||
},
|
||||
input: &iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
Name: utils.Ptr("name"),
|
||||
DiskFormat: utils.Ptr("format"),
|
||||
MinDiskSize: utils.Ptr(int64(1)),
|
||||
MinRam: utils.Ptr(int64(1)),
|
||||
Protected: utils.Ptr(true),
|
||||
Scope: utils.Ptr("scope"),
|
||||
Config: &iaas.ImageConfig{
|
||||
BootMenu: utils.Ptr(true),
|
||||
CdromBus: iaas.NewNullableString(utils.Ptr("cdrom_bus")),
|
||||
DiskBus: iaas.NewNullableString(utils.Ptr("disk_bus")),
|
||||
NicModel: iaas.NewNullableString(utils.Ptr("model")),
|
||||
OperatingSystem: utils.Ptr("os"),
|
||||
OperatingSystemDistro: iaas.NewNullableString(utils.Ptr("os_distro")),
|
||||
OperatingSystemVersion: iaas.NewNullableString(utils.Ptr("os_version")),
|
||||
RescueBus: iaas.NewNullableString(utils.Ptr("rescue_bus")),
|
||||
RescueDevice: iaas.NewNullableString(utils.Ptr("rescue_device")),
|
||||
SecureBoot: utils.Ptr(true),
|
||||
Uefi: utils.Ptr(true),
|
||||
VideoModel: iaas.NewNullableString(utils.Ptr("model")),
|
||||
VirtioScsi: utils.Ptr(true),
|
||||
},
|
||||
Checksum: &iaas.ImageChecksum{
|
||||
Algorithm: utils.Ptr("algorithm"),
|
||||
Digest: utils.Ptr("digest"),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
region: "eu02",
|
||||
},
|
||||
&iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
Name: utils.Ptr("name"),
|
||||
DiskFormat: utils.Ptr("format"),
|
||||
MinDiskSize: utils.Ptr(int64(1)),
|
||||
MinRam: utils.Ptr(int64(1)),
|
||||
Protected: utils.Ptr(true),
|
||||
Scope: utils.Ptr("scope"),
|
||||
Config: &iaas.ImageConfig{
|
||||
BootMenu: utils.Ptr(true),
|
||||
CdromBus: iaas.NewNullableString(utils.Ptr("cdrom_bus")),
|
||||
DiskBus: iaas.NewNullableString(utils.Ptr("disk_bus")),
|
||||
NicModel: iaas.NewNullableString(utils.Ptr("model")),
|
||||
OperatingSystem: utils.Ptr("os"),
|
||||
OperatingSystemDistro: iaas.NewNullableString(utils.Ptr("os_distro")),
|
||||
OperatingSystemVersion: iaas.NewNullableString(utils.Ptr("os_version")),
|
||||
RescueBus: iaas.NewNullableString(utils.Ptr("rescue_bus")),
|
||||
RescueDevice: iaas.NewNullableString(utils.Ptr("rescue_device")),
|
||||
SecureBoot: utils.Ptr(true),
|
||||
Uefi: utils.Ptr(true),
|
||||
VideoModel: iaas.NewNullableString(utils.Ptr("model")),
|
||||
VirtioScsi: utils.Ptr(true),
|
||||
},
|
||||
Checksum: &iaas.ImageChecksum{
|
||||
Algorithm: utils.Ptr("algorithm"),
|
||||
Digest: utils.Ptr("digest"),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,iid"),
|
||||
expected: DataSourceModel{
|
||||
Id: types.StringValue("pid,eu02,iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Name: types.StringValue("name"),
|
||||
|
|
@ -105,47 +117,50 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Region: types.StringValue("eu02"),
|
||||
},
|
||||
true,
|
||||
isValid: true,
|
||||
},
|
||||
{
|
||||
"empty_labels",
|
||||
DataSourceModel{
|
||||
description: "empty_labels",
|
||||
args: args{
|
||||
state: DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
|
||||
},
|
||||
input: &iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
},
|
||||
region: "eu01",
|
||||
},
|
||||
expected: DataSourceModel{
|
||||
Id: types.StringValue("pid,eu01,iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
|
||||
Region: types.StringValue("eu01"),
|
||||
},
|
||||
&iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
},
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
|
||||
},
|
||||
true,
|
||||
isValid: true,
|
||||
},
|
||||
{
|
||||
"response_nil_fail",
|
||||
DataSourceModel{},
|
||||
nil,
|
||||
DataSourceModel{},
|
||||
false,
|
||||
description: "response_nil_fail",
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
description: "no_resource_id",
|
||||
args: args{
|
||||
state: DataSourceModel{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
input: &iaas.Image{},
|
||||
},
|
||||
&iaas.Image{},
|
||||
DataSourceModel{},
|
||||
false,
|
||||
expected: DataSourceModel{},
|
||||
isValid: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
err := mapDataSourceFields(context.Background(), tt.input, &tt.state)
|
||||
err := mapDataSourceFields(context.Background(), tt.args.input, &tt.args.state, tt.args.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
@ -153,7 +168,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(tt.state, tt.expected)
|
||||
diff := cmp.Diff(tt.args.state, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ import (
|
|||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"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/boolplanmodifier"
|
||||
|
|
@ -40,11 +39,13 @@ var (
|
|||
_ resource.Resource = &imageResource{}
|
||||
_ resource.ResourceWithConfigure = &imageResource{}
|
||||
_ resource.ResourceWithImportState = &imageResource{}
|
||||
_ resource.ResourceWithModifyPlan = &imageResource{}
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
ImageId types.String `tfsdk:"image_id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
DiskFormat types.String `tfsdk:"disk_format"`
|
||||
|
|
@ -111,7 +112,8 @@ func NewImageResource() resource.Resource {
|
|||
|
||||
// imageResource is the resource implementation.
|
||||
type imageResource struct {
|
||||
client *iaas.APIClient
|
||||
client *iaas.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the resource type name.
|
||||
|
|
@ -119,14 +121,45 @@ func (r *imageResource) Metadata(_ context.Context, req resource.MetadataRequest
|
|||
resp.TypeName = req.ProviderTypeName + "_image"
|
||||
}
|
||||
|
||||
// ModifyPlan implements resource.ResourceWithModifyPlan.
|
||||
// Use the modifier to set the effective region in the current plan.
|
||||
func (r *imageResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel Model
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
}
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
var planModel Model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the resource.
|
||||
func (r *imageResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
var ok bool
|
||||
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
|
@ -140,7 +173,7 @@ func (r *imageResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
|
|||
Description: "Image resource schema. Must have a `region` specified in the provider configuration.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`image_id`\".",
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`image_id`\".",
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.UseStateForUnknown(),
|
||||
|
|
@ -157,6 +190,15 @@ func (r *imageResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
|
|||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"region": schema.StringAttribute{
|
||||
Description: "The resource region. If not defined, the provider region is used.",
|
||||
Optional: true,
|
||||
// must be computed to allow for storing the override value from the provider
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"image_id": schema.StringAttribute{
|
||||
Description: "The image ID.",
|
||||
Computed: true,
|
||||
|
|
@ -378,11 +420,12 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
|
|||
}
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toCreatePayload(ctx, &model)
|
||||
if err != nil {
|
||||
|
|
@ -391,7 +434,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
|
|||
}
|
||||
|
||||
// Create new image
|
||||
imageCreateResp, err := r.client.CreateImage(ctx, projectId).CreateImagePayload(*payload).Execute()
|
||||
imageCreateResp, err := r.client.CreateImage(ctx, projectId, region).CreateImagePayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
|
|
@ -401,15 +444,15 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
|
|||
|
||||
ctx = tflog.SetField(ctx, "image_id", *imageCreateResp.Id)
|
||||
|
||||
// Get the image object, as the create response does not contain all fields
|
||||
image, err := r.client.GetImage(ctx, projectId, *imageCreateResp.Id).Execute()
|
||||
// Get the image object, as the creation response does not contain all fields
|
||||
image, err := r.client.GetImage(ctx, projectId, region, *imageCreateResp.Id).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, image, &model)
|
||||
err = mapFields(ctx, image, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -430,7 +473,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
|
|||
}
|
||||
|
||||
// Wait for image to become available
|
||||
waiter := wait.UploadImageWaitHandler(ctx, r.client, projectId, *imageCreateResp.Id)
|
||||
waiter := wait.UploadImageWaitHandler(ctx, r.client, projectId, region, *imageCreateResp.Id)
|
||||
waiter = waiter.SetTimeout(7 * 24 * time.Hour) // Set timeout to one week, to make the timeout useless
|
||||
waitResp, err := waiter.WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
|
|
@ -439,7 +482,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
|
|||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, waitResp, &model)
|
||||
err = mapFields(ctx, waitResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -454,7 +497,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
|
|||
tflog.Info(ctx, "Image created")
|
||||
}
|
||||
|
||||
// // Read refreshes the Terraform state with the latest data.
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (r *imageResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
diags := req.State.Get(ctx, &model)
|
||||
|
|
@ -462,15 +505,18 @@ func (r *imageResource) Read(ctx context.Context, req resource.ReadRequest, resp
|
|||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
imageId := model.ImageId.ValueString()
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "image_id", imageId)
|
||||
|
||||
imageResp, err := r.client.GetImage(ctx, projectId, imageId).Execute()
|
||||
imageResp, err := r.client.GetImage(ctx, projectId, region, imageId).Execute()
|
||||
if err != nil {
|
||||
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
|
|
@ -484,7 +530,7 @@ func (r *imageResource) Read(ctx context.Context, req resource.ReadRequest, resp
|
|||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, imageResp, &model)
|
||||
err = mapFields(ctx, imageResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading image", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -507,12 +553,15 @@ func (r *imageResource) Update(ctx context.Context, req resource.UpdateRequest,
|
|||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
imageId := model.ImageId.ValueString()
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "image_id", imageId)
|
||||
|
||||
// Retrieve values from state
|
||||
|
|
@ -530,7 +579,7 @@ func (r *imageResource) Update(ctx context.Context, req resource.UpdateRequest,
|
|||
return
|
||||
}
|
||||
// Update existing image
|
||||
updatedImage, err := r.client.UpdateImage(ctx, projectId, imageId).UpdateImagePayload(*payload).Execute()
|
||||
updatedImage, err := r.client.UpdateImage(ctx, projectId, region, imageId).UpdateImagePayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating image", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
|
|
@ -538,7 +587,7 @@ func (r *imageResource) Update(ctx context.Context, req resource.UpdateRequest,
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
err = mapFields(ctx, updatedImage, &model)
|
||||
err = mapFields(ctx, updatedImage, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating image", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -563,14 +612,15 @@ func (r *imageResource) Delete(ctx context.Context, req resource.DeleteRequest,
|
|||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
imageId := model.ImageId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "image_id", imageId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "image_id", imageId)
|
||||
|
||||
// Delete existing image
|
||||
err := r.client.DeleteImage(ctx, projectId, imageId).Execute()
|
||||
err := r.client.DeleteImage(ctx, projectId, region, imageId).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting image", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
|
|
@ -578,7 +628,7 @@ func (r *imageResource) Delete(ctx context.Context, req resource.DeleteRequest,
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
_, err = wait.DeleteImageWaitHandler(ctx, r.client, projectId, imageId).WaitWithContext(ctx)
|
||||
_, err = wait.DeleteImageWaitHandler(ctx, r.client, projectId, region, imageId).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting image", fmt.Sprintf("image deletion waiting: %v", err))
|
||||
return
|
||||
|
|
@ -588,29 +638,28 @@ func (r *imageResource) Delete(ctx context.Context, req resource.DeleteRequest,
|
|||
}
|
||||
|
||||
// ImportState imports a resource into the Terraform state on success.
|
||||
// The expected format of the resource import identifier is: project_id,image_id
|
||||
// The expected format of the resource import identifier is: project_id,region,image_id
|
||||
func (r *imageResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
idParts := strings.Split(req.ID, core.Separator)
|
||||
|
||||
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
|
||||
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics,
|
||||
"Error importing image",
|
||||
fmt.Sprintf("Expected import identifier with format: [project_id],[image_id] Got: %q", req.ID),
|
||||
fmt.Sprintf("Expected import identifier with format: [project_id],[region],[image_id] Got: %q", req.ID),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
projectId := idParts[0]
|
||||
imageId := idParts[1]
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "image_id", imageId)
|
||||
utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
|
||||
"project_id": idParts[0],
|
||||
"region": idParts[1],
|
||||
"image_id": idParts[2],
|
||||
})
|
||||
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("image_id"), imageId)...)
|
||||
tflog.Info(ctx, "Image state imported")
|
||||
}
|
||||
|
||||
func mapFields(ctx context.Context, imageResp *iaas.Image, model *Model) error {
|
||||
func mapFields(ctx context.Context, imageResp *iaas.Image, model *Model, region string) error {
|
||||
if imageResp == nil {
|
||||
return fmt.Errorf("response input is nil")
|
||||
}
|
||||
|
|
@ -627,7 +676,8 @@ func mapFields(ctx context.Context, imageResp *iaas.Image, model *Model) error {
|
|||
return fmt.Errorf("image id not present")
|
||||
}
|
||||
|
||||
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), imageId)
|
||||
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, imageId)
|
||||
model.Region = types.StringValue(region)
|
||||
|
||||
// Map config
|
||||
var configModel = &configModel{}
|
||||
|
|
|
|||
|
|
@ -17,69 +17,81 @@ import (
|
|||
)
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
type args struct {
|
||||
state Model
|
||||
input *iaas.Image
|
||||
region string
|
||||
}
|
||||
tests := []struct {
|
||||
description string
|
||||
state Model
|
||||
input *iaas.Image
|
||||
args args
|
||||
expected Model
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
description: "default_values",
|
||||
args: args{
|
||||
state: Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
},
|
||||
input: &iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
},
|
||||
region: "eu01",
|
||||
},
|
||||
&iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
},
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid"),
|
||||
expected: Model{
|
||||
Id: types.StringValue("pid,eu01,iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Region: types.StringValue("eu01"),
|
||||
},
|
||||
true,
|
||||
isValid: true,
|
||||
},
|
||||
{
|
||||
"simple_values",
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
description: "simple_values",
|
||||
args: args{
|
||||
state: Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Region: types.StringValue("eu01"),
|
||||
},
|
||||
input: &iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
Name: utils.Ptr("name"),
|
||||
DiskFormat: utils.Ptr("format"),
|
||||
MinDiskSize: utils.Ptr(int64(1)),
|
||||
MinRam: utils.Ptr(int64(1)),
|
||||
Protected: utils.Ptr(true),
|
||||
Scope: utils.Ptr("scope"),
|
||||
Config: &iaas.ImageConfig{
|
||||
BootMenu: utils.Ptr(true),
|
||||
CdromBus: iaas.NewNullableString(utils.Ptr("cdrom_bus")),
|
||||
DiskBus: iaas.NewNullableString(utils.Ptr("disk_bus")),
|
||||
NicModel: iaas.NewNullableString(utils.Ptr("model")),
|
||||
OperatingSystem: utils.Ptr("os"),
|
||||
OperatingSystemDistro: iaas.NewNullableString(utils.Ptr("os_distro")),
|
||||
OperatingSystemVersion: iaas.NewNullableString(utils.Ptr("os_version")),
|
||||
RescueBus: iaas.NewNullableString(utils.Ptr("rescue_bus")),
|
||||
RescueDevice: iaas.NewNullableString(utils.Ptr("rescue_device")),
|
||||
SecureBoot: utils.Ptr(true),
|
||||
Uefi: utils.Ptr(true),
|
||||
VideoModel: iaas.NewNullableString(utils.Ptr("model")),
|
||||
VirtioScsi: utils.Ptr(true),
|
||||
},
|
||||
Checksum: &iaas.ImageChecksum{
|
||||
Algorithm: utils.Ptr("algorithm"),
|
||||
Digest: utils.Ptr("digest"),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
region: "eu02",
|
||||
},
|
||||
&iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
Name: utils.Ptr("name"),
|
||||
DiskFormat: utils.Ptr("format"),
|
||||
MinDiskSize: utils.Ptr(int64(1)),
|
||||
MinRam: utils.Ptr(int64(1)),
|
||||
Protected: utils.Ptr(true),
|
||||
Scope: utils.Ptr("scope"),
|
||||
Config: &iaas.ImageConfig{
|
||||
BootMenu: utils.Ptr(true),
|
||||
CdromBus: iaas.NewNullableString(utils.Ptr("cdrom_bus")),
|
||||
DiskBus: iaas.NewNullableString(utils.Ptr("disk_bus")),
|
||||
NicModel: iaas.NewNullableString(utils.Ptr("model")),
|
||||
OperatingSystem: utils.Ptr("os"),
|
||||
OperatingSystemDistro: iaas.NewNullableString(utils.Ptr("os_distro")),
|
||||
OperatingSystemVersion: iaas.NewNullableString(utils.Ptr("os_version")),
|
||||
RescueBus: iaas.NewNullableString(utils.Ptr("rescue_bus")),
|
||||
RescueDevice: iaas.NewNullableString(utils.Ptr("rescue_device")),
|
||||
SecureBoot: utils.Ptr(true),
|
||||
Uefi: utils.Ptr(true),
|
||||
VideoModel: iaas.NewNullableString(utils.Ptr("model")),
|
||||
VirtioScsi: utils.Ptr(true),
|
||||
},
|
||||
Checksum: &iaas.ImageChecksum{
|
||||
Algorithm: utils.Ptr("algorithm"),
|
||||
Digest: utils.Ptr("digest"),
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid"),
|
||||
expected: Model{
|
||||
Id: types.StringValue("pid,eu02,iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Name: types.StringValue("name"),
|
||||
|
|
@ -110,47 +122,48 @@ func TestMapFields(t *testing.T) {
|
|||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Region: types.StringValue("eu02"),
|
||||
},
|
||||
true,
|
||||
isValid: true,
|
||||
},
|
||||
{
|
||||
"empty_labels",
|
||||
Model{
|
||||
description: "empty_labels",
|
||||
args: args{
|
||||
state: Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
|
||||
},
|
||||
input: &iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
},
|
||||
region: "eu01",
|
||||
},
|
||||
expected: Model{
|
||||
Id: types.StringValue("pid,eu01,iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
|
||||
Region: types.StringValue("eu01"),
|
||||
},
|
||||
&iaas.Image{
|
||||
Id: utils.Ptr("iid"),
|
||||
},
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
ImageId: types.StringValue("iid"),
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
|
||||
},
|
||||
true,
|
||||
isValid: true,
|
||||
},
|
||||
{
|
||||
"response_nil_fail",
|
||||
Model{},
|
||||
nil,
|
||||
Model{},
|
||||
false,
|
||||
description: "response_nil_fail",
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
description: "no_resource_id",
|
||||
args: args{
|
||||
state: Model{
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
input: &iaas.Image{},
|
||||
},
|
||||
&iaas.Image{},
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
err := mapFields(context.Background(), tt.input, &tt.state)
|
||||
err := mapFields(context.Background(), tt.args.input, &tt.args.state, tt.args.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
@ -158,7 +171,7 @@ func TestMapFields(t *testing.T) {
|
|||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(tt.state, tt.expected)
|
||||
diff := cmp.Diff(tt.args.state, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue