Skip to content

Commit

Permalink
feat: Add datasource cloudavenue_backup
Browse files Browse the repository at this point in the history
  • Loading branch information
David MICHENEAU committed Oct 10, 2023
1 parent ffa3c9a commit 7127c2a
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 84 deletions.
76 changes: 76 additions & 0 deletions internal/provider/backup/backup_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Package backup provides a Terraform datasource.
package backup

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
)

var (
_ datasource.DataSource = &backupDataSource{}
_ datasource.DataSourceWithConfigure = &backupDataSource{}
)

func NewBackupDataSource() datasource.DataSource {
return &backupDataSource{}
}

type backupDataSource struct {
client *client.CloudAvenue
}

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

func (d *backupDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = backupSchema(ctx).GetDataSource(ctx)
}

func (d *backupDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

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

func (d *backupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("cloudavenue_backup", d.client.GetOrgName(), metrics.Read)()

config := &backupModel{}

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}

s := &backupResource{
client: d.client,
}

// Read data from the API
data, _, diags := s.read(ctx, config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Set refreshed state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
15 changes: 9 additions & 6 deletions internal/provider/backup/backup_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,23 @@ func backupSchema(_ context.Context) superschema.Schema {
"type": superschema.SuperStringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "Scope of the backup.",
Required: true,
},
Resource: &schemaR.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf("vdc", "vapp", "vm"),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"target_id": superschema.SuperStringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "The ID of the target. A target can be a VDC, a VApp or a VM.",
Optional: true,
},
Resource: &schemaR.StringAttribute{
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("target_id"), path.MatchRoot("target_name")),
fstringvalidator.IsUUID(),
Expand All @@ -76,13 +73,16 @@ func backupSchema(_ context.Context) superschema.Schema {
stringplanmodifier.UseStateForUnknown(),
},
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"target_name": superschema.SuperStringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "The name of the target. A target can be a VDC, a VApp or a VM.",
Optional: true,
},
Resource: &schemaR.StringAttribute{
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("target_id"), path.MatchRoot("target_name")),
},
Expand All @@ -91,6 +91,9 @@ func backupSchema(_ context.Context) superschema.Schema {
stringplanmodifier.UseStateForUnknown(),
},
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"policies": superschema.SuperSetNestedAttribute{
Common: &schemaR.SetNestedAttribute{
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/provider_datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/alb"
netbackup "github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/backup"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/catalog"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/edgegw"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/iam"
Expand Down Expand Up @@ -79,5 +80,8 @@ func (p *cloudavenueProvider) DataSources(_ context.Context) []func() datasource
// * STORAGE
storage.NewProfileDataSource,
storage.NewProfilesDataSource,

// * BACKUP
netbackup.NewBackupDataSource,
}
}
61 changes: 61 additions & 0 deletions internal/testsacc/backup_datasource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package testsacc

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/helpers/testsacc"
)

var _ testsacc.TestACC = &BackupDataSource{}

const (
BackupDataSourceName = testsacc.ResourceName("data.cloudavenue_backup")
)

type BackupDataSource struct{}

func NewBackupDataSourceTest() testsacc.TestACC {
return &BackupDataSource{}
}

// GetResourceName returns the name of the resource.
func (r *BackupDataSource) GetResourceName() string {
return BackupDataSourceName.String()
}

func (r *BackupDataSource) DependenciesConfig() (configs testsacc.TFData) {
// Add dependencies config to the resource
configs.Append(GetResourceConfig()[BackupResourceName]().GetDefaultConfig())
configs.Append(GetResourceConfig()[VDCResourceName]().GetDefaultConfig())
return
}

func (r *BackupDataSource) Tests(ctx context.Context) map[testsacc.TestName]func(ctx context.Context, resourceName string) testsacc.Test {
return map[testsacc.TestName]func(ctx context.Context, resourceName string) testsacc.Test{
// * Test One (backup vdc example)
"example": func(_ context.Context, resourceName string) testsacc.Test {
return testsacc.Test{
// ! Create testing
Create: testsacc.TFConfig{
TFConfig: `
data "cloudavenue_backup" "example" {
type = "vdc"
target_name = cloudavenue_backup.example.target_name
}`,
Checks: NewBackupResourceTest().Tests(ctx)["example"](ctx, resourceName).GenerateCheckWithCommonChecks(),
},
}
},
}
}

func TestAccBackupDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { TestAccPreCheck(t) },
ProtoV6ProviderFactories: TestAccProtoV6ProviderFactories,
Steps: testsacc.GenerateTests(&BackupDataSource{}),
})
}
Loading

0 comments on commit 7127c2a

Please sign in to comment.