-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add datasource
cloudavenue_backup
- Loading branch information
David MICHENEAU
committed
Oct 10, 2023
1 parent
ffa3c9a
commit 7127c2a
Showing
5 changed files
with
248 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}), | ||
}) | ||
} |
Oops, something went wrong.